Class: Rangefinder::Bigquery
- Inherits:
-
Object
- Object
- Rangefinder::Bigquery
- Defined in:
- lib/rangefinder/bigquery.rb
Instance Method Summary collapse
- #find(namespace, kind, name) ⇒ Object
-
#initialize(options) ⇒ Bigquery
constructor
A new instance of Bigquery.
- #puppetfile_count(modname = nil) ⇒ Object
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() $logger.info "Starting Bigquery connection" gcloud = [: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()}" 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 |