module Resque::Reports::Common::BatchedReport

Constants

BATCH_SIZE

Public Instance Methods

connection() click to toggle source

Internal: Подключение используемое для выполнения запросов

Returns connection adapter

# File lib/resque/reports/common/batched_report.rb, line 15
def connection
  ActiveRecord::Base.connection
end
data_each(force = false) { |element| ... } click to toggle source

Internal: Выполняет запрос отчета пачками и выполняет block для каждой пачки

Переопредленный метод из Resque::Reports

Returns Nothing

# File lib/resque/reports/common/batched_report.rb, line 23
def data_each(force = false)
  0.step(data_size, batch_size) do |batch_offset|
    connection.execute(batched_query(batch_offset)).each do |element|
      yield element
    end
  end
end
data_size() click to toggle source

Internal: Возвращает общее кол-во строк в отчете

Переопредленный метод из Resque::Reports

Returns Fixnum

# File lib/resque/reports/common/batched_report.rb, line 35
def data_size
  @data_size ||= connection.execute(count_query)[0]['count'].to_i
end

Protected Instance Methods

base_query() click to toggle source

Internal: Основной запрос отчета (Arel)

Returns Arel::SelectManager

# File lib/resque/reports/common/batched_report.rb, line 83
def base_query
  fail NotImplementedError
end
batch_size() click to toggle source

Internal: Размер пачки отчета

Returns Fixnum

# File lib/resque/reports/common/batched_report.rb, line 69
def batch_size
  BATCH_SIZE
end
batched_query(offset) click to toggle source

Internal: Запрос пачки строк отчета

offset - Numeric, число строк на которое сдвигается запрос

Returns String (SQL)

# File lib/resque/reports/common/batched_report.rb, line 120
def batched_query(offset)
  query.project(Arel.sql(select))
       .take(batch_size)
       .skip(offset)
       .order(order_by)
       .to_sql
end
count_query() click to toggle source

Internal: Запрос количества строк в отчете

Returns String (SQL)

# File lib/resque/reports/common/batched_report.rb, line 111
def count_query
  query.project(Arel.sql('COUNT(*) as count')).to_sql
end
filter(query) click to toggle source

Internal: Фильтры отчета

Returns Arel::SelectManager

# File lib/resque/reports/common/batched_report.rb, line 104
def filter(query)
  query
end
join_tables(source_table, *joins) click to toggle source

Internal: Полезный метод для join’а необходимых таблиц через Arel

Returns Arel

# File lib/resque/reports/common/batched_report.rb, line 62
def join_tables(source_table, *joins)
  joins.inject(source_table) { |query, joined| query.join(joined[:table]).on(joined[:on]) }
end
models() click to toggle source

Internal: Модели используемые в отчете

Returns Array of Arel::Table

# File lib/resque/reports/common/batched_report.rb, line 76
def models
  fail NotImplementedError
end
order_by() click to toggle source

Internal: Порядок строк отчета

Returns String (SQL)

# File lib/resque/reports/common/batched_report.rb, line 97
def order_by
  nil
end
query() click to toggle source

Internal: Возвращает отфильтрованный запрос отчета

Returns Arel::SelectManager

# File lib/resque/reports/common/batched_report.rb, line 44
def query
  filter base_query
end
select() click to toggle source

Internal: Поля запрашиваемые отчетом

Returns String (SQL)

# File lib/resque/reports/common/batched_report.rb, line 90
def select
  fail NotImplementedError
end
tables() click to toggle source

Internal: Полезный метод для хранения Arel::Table объектов для запроса отчета

Returns Hash, {:table_name => #<Arel::Table @name=“table_name”>, …}

# File lib/resque/reports/common/batched_report.rb, line 51
def tables
  return @tables if defined? @tables

  tables = models.map(&:arel_table)

  @tables = tables.reduce({}) { |a, e| a.store(e.name, e) && a }.with_indifferent_access
end