Class: Rangefinder::Bigquery

Inherits:
Object
  • Object
show all
Defined in:
lib/rangefinder/bigquery.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Bigquery

Returns a new instance of Bigquery.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rangefinder/bigquery.rb', line 4

def initialize(options)
  $logger.info "Starting Bigquery connection"
  gcloud = options[:gcloud]

  raise "Required gCloud configuration missing" unless gcloud

  gcloud[:keyfile] = File.expand_path(gcloud[:keyfile])
  @bigquery = Google::Cloud::Bigquery.new(
    :project_id  => gcloud[:project],
    :credentials => Google::Cloud::Bigquery::Credentials.new(gcloud[:keyfile]),
  )
  @dataset = @bigquery.dataset(gcloud[:dataset])
  raise "\nThere is a problem with the gCloud configuration: \n #{JSON.pretty_generate(options)}" if @dataset.nil?
end

Instance Method Details

#find(namespace, kind, name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rangefinder/bigquery.rb', line 19

def find(namespace, kind, name)
  sql = "SELECT DISTINCT module, i.source, m.source AS repo
         FROM `bto-dataops-datalake-prod.community.forge_itemized` AS i
         JOIN `bto-dataops-datalake-prod.community.forge_modules` AS m
           ON m.slug = i.module
         WHERE kind = @kind AND element = @name"

  data = @dataset.query(sql, params: {kind: kind.to_s, name: name})

  exact, near = data.partition {|row| row[:source] == namespace and not namespace.nil?}

  {
    :kind  => kind,
    :name  => name,
    :exact => exact,
    :near  => near,
  }
end