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
# 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

  @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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rangefinder/bigquery.rb', line 18

def find(namespace, kind, name)
  sql = "SELECT DISTINCT module, i.source, m.source AS repo
         FROM `#{@dataset.project_id}.#{@dataset.dataset_id}.forge_itemized` AS i
         JOIN `#{@dataset.project_id}.#{@dataset.dataset_id}.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})

  if namespace
    exact, near = data.partition {|row| row[:source] == namespace}
    puppetfile  = "#{puppetfile_count(namespace)} of #{puppetfile_count}"
  else
    exact      = nil
    puppetfile = nil
    near       = data
  end

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

#puppetfile_count(modname = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rangefinder/bigquery.rb', line 45

def puppetfile_count(modname=nil)
  if modname
    sql = "SELECT COUNT(DISTINCT repo_name) AS count
           FROM `#{@dataset.project_id}.#{@dataset.dataset_id}.github_puppetfile_usage`
           WHERE module = @name"

    data = @dataset.query(sql, params: {name: modname})
  else
    sql = "SELECT COUNT(DISTINCT repo_name) AS count
           FROM `#{@dataset.project_id}.#{@dataset.dataset_id}.github_puppetfile_usage`"

    data = @dataset.query(sql)
  end
  data.first[:count]
end