class GaTrackable::BaseFetcher

Constants

GA_DATE_FORMAT

Attributes

end_date[R]
sometime_processed_counters[RW]
start_date[R]

Public Class Methods

new( client: GaTrackable.client, analytics: GaTrackable.analytics, config: GaTrackable.config ) click to toggle source
# File lib/ga_trackable/base_fetcher.rb, line 10
def initialize(
      client: GaTrackable.client,
      analytics: GaTrackable.analytics,
      config: GaTrackable.config
    )
  @client = client
  @analytics = analytics
  @config = config
  @sometime_processed_counters = []
end

Public Instance Methods

fetch_for_current_day!() click to toggle source

Вытащить просмотры за текущий день. Метод может вызываться много раз, количество просмотров за текущий день будет обновляться.

# File lib/ga_trackable/base_fetcher.rb, line 23
def fetch_for_current_day!
  @start_date = DateTime.current.beginning_of_day
  @end_date = DateTime.current

  self.sometime_processed_counters = []

  start_index = 1
  max_results = 1000

  begin
    rows = get_data(start_date, end_date, start_index, max_results).rows
    rows.each do |row|
      begin
        process_row(row, create_in_past: true)
      rescue => ex
        handle_exception(ex)
      end
    end
    start_index += max_results
  end while rows.size == max_results

  self.sometime_processed_counters = []
  true
end

Private Instance Methods

handle_exception(ex) click to toggle source
# File lib/ga_trackable/base_fetcher.rb, line 82
def handle_exception(ex)
  @config.out << "Exception occured:\n"
  @config.out << ex.message
  @config.out << "\n"
  @config.out << ex.backtrace.join("\n")

  @config.exceptions_handler.error(ex)
end
initial_fetch!(start_date: 1.year.ago) click to toggle source

Инициализировать кеши просмотров. Метод приватный, потому что его вызов приведет к тому, что все каунтеры будут удалены и перестроены. Стартовой датой считаем 1 год назад. Вытаскиваем просмотры за все время, исключая сегодняшний день. Просмотры за сегодня и далее должны вытаскиваться с помощью fetch_for_current_day! и запоминаться в виде отдельного счетчика.

# File lib/ga_trackable/base_fetcher.rb, line 56
def initial_fetch!(start_date: 1.year.ago)
  @start_date = start_date
  @end_date = 1.day.ago.end_of_day

  self.sometime_processed_counters = []
  counter_class.delete_all

  start_index = 1
  max_results = 1000

  begin
    rows = get_data(start_date, end_date, start_index, max_results).rows
    rows.each do |row|
      begin
        process_row(row, create_in_past: true)
      rescue => ex
        handle_exception(ex)
      end
    end
    start_index += max_results
  end while rows.size == max_results

  self.sometime_processed_counters = []
  true
end