<commit_before>require 'google/api_client'

class ArticleTotalStatsWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def self.get_authorized_client
    private_key = ENV['GOOGLE_PRIVATE_KEY']
    passphrase = ENV['GOOGLE_PRIVATE_KEY_PASSPHRASE']
    key = OpenSSL::PKey::RSA.new private_key, passphrase

    client = Google::APIClient.new
    client.authorization = Signet::OAuth2::Client.new(
        :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
        :audience => 'https://accounts.google.com/o/oauth2/token',
        :scope => 'https://www.googleapis.com/auth/analytics.readonly',
        :issuer => ENV['GOOGLE_SERVICE_ACCOUNT'],
        :signing_key => key)
    client.authorization.fetch_access_token!
    client
  end

  # Autherize client
  @@client = ArticleTotalStatsWorker.get_authorized_client
  @@analytics = @@client.discovered_api('analytics', 'v3')

  def perform(start_index=1)
    puts "Articles stats worker starting at index #{start_index}..."

    # Query Analytics API  to retrieve Unique Page Views and Time on Page
    query_data = @@client.execute(:api_method => @@analytics.data.ga.get, :parameters => {
      'ids' => ENV['GOOGLE_ANALYTICS_PROFILE_ID'],
      'start-date' => '2014-01-01',<commit_msg>Catch errors in the article stats worker to make sure the app does not just crash everything.<commit_after>require 'google/api_client'

class ArticleTotalStatsWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def self.get_authorized_client
    private_key = ENV['GOOGLE_PRIVATE_KEY']
    passphrase = ENV['GOOGLE_PRIVATE_KEY_PASSPHRASE']
    key = OpenSSL::PKey::RSA.new private_key, passphrase

    client = Google::APIClient.new
    client.authorization = Signet::OAuth2::Client.new(
        :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
        :audience => 'https://accounts.google.com/o/oauth2/token',
        :scope => 'https://www.googleapis.com/auth/analytics.readonly',
        :issuer => ENV['GOOGLE_SERVICE_ACCOUNT'],
        :signing_key => key)
    client.authorization.fetch_access_token!
    client
  end

  begin
    # Autherize client
    @@client = ArticleTotalStatsWorker.get_authorized_client
    @@analytics = @@client.discovered_api('analytics', 'v3')
  rescue Exception => e
    @@client = nil
    @@analytics = nil
    puts e.message
    puts e.backtrace.inspect
  end

  def perform(start_index=1)

    if @@client.nil? or @@analytics.nil?
      puts 'Not calculating total articles stats due to client authorization or service discovery issue.'
      return
    end

    puts "Articles stats worker starting at index #{start_index}..."

    # Query Analytics API  to retrieve Unique Page Views and Time on Page
    query_data = @@client.execute(:api_method => @@analytics.data.ga.get, :parameters => {
      'ids' => ENV['GOOGLE_ANALYTICS_PROFILE_ID'],
      'start-date' => '2014-01-01',
{'fraction_non_alphanumeric': 0.11915329463980881, 'fraction_numerical': 0.008876749743939911, 'mean_word_length': 3.834983498349835, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1881582', 'n_tokens_mistral': 974, 'n_tokens_neox': 931, 'n_words': 194}