<commit_before>require 'ostruct'
require 'json'

require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'

class GoogleBQQuery

  def client
    @client ||= Google::APIClient.new(
      :application_name => 'Repo Health Application',
      :application_version => '0.0.1'
    )
  end

  def bq_api
    @bq_api ||= client.discovered_api('bigquery','v2')
  end

  def signed_key
    @signed_key ||= OpenSSL::PKey::RSA.new ENV["GOOGLE_API_PRIVATE_KEY"], 'notasecret'
  end

  def auth
    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/bigquery',
      :issuer => ENV["GOOGLE_API_ISSUER"],
      :signing_key => signed_key)
    client.authorization.fetch_access_token!
  end

  # TODO: Rename or split out query/response
  def query(query)
    auth

    puts query

    resp = client.execute(
              :api_method =>  bq_api.jobs.query,
              :body_object => { "query" => query },
              :parameters => { "projectId" => "jovial-opus-656",
                              "format" => "json" })

    data_normalized_to_events(resp.body)
  end

  # NOTE: Data requires that fields are kept in order
  # See test/data/big_query_response.json for an example
  def data_normalized_to_events(data)
    json = JSON.parse(data)

    fields = fields_from_json(json)

    events = attrs_from_json(json).map{ |a| OpenStruct.new(a) }
  end
<commit_msg>Remove straggling display of query<commit_after>require 'ostruct'
require 'json'

require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'

class GoogleBQQuery

  def client
    @client ||= Google::APIClient.new(
      :application_name => 'Repo Health Application',
      :application_version => '0.0.1'
    )
  end

  def bq_api
    @bq_api ||= client.discovered_api('bigquery','v2')
  end

  def signed_key
    @signed_key ||= OpenSSL::PKey::RSA.new ENV["GOOGLE_API_PRIVATE_KEY"], 'notasecret'
  end

  def auth
    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/bigquery',
      :issuer => ENV["GOOGLE_API_ISSUER"],
      :signing_key => signed_key)
    client.authorization.fetch_access_token!
  end

  # TODO: Rename or split out query/response
  def query(query)
    auth

    resp = client.execute(
              :api_method =>  bq_api.jobs.query,
              :body_object => { "query" => query },
              :parameters => { "projectId" => "jovial-opus-656",
                              "format" => "json" })

    data_normalized_to_events(resp.body)
  end

  # NOTE: Data requires that fields are kept in order
  # See test/data/big_query_response.json for an example
  def data_normalized_to_events(data)
    json = JSON.parse(data)

    fields = fields_from_json(json)

    events = attrs_from_json(json).map{ |a| OpenStruct.new(a) }
  end

{'fraction_non_alphanumeric': 0.1283720930232558, 'fraction_numerical': 0.006201550387596899, 'mean_word_length': 3.1572164948453607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '1984389', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1087, 'n_words': 204}