class Nexus::Analytics
Attributes
a_file[RW]
data[R]
db[R]
db_file[R]
log[RW]
Public Class Methods
new(database_dir='./',db_filename='cache-analytics.db', logger=nil)
click to toggle source
new method
# File lib/nexus_client/analytics.rb, line 9 def initialize(database_dir='./',db_filename='cache-analytics.db', logger=nil) @log = logger filename ||= db_filename || 'cache-analytics.db' log.warn "Filename is nil" if filename.nil? @db_file = File.join(database_dir,filename) begin require 'sqlite3' @db = SQLite3::Database.new( @db_file) init_tables total_view rescue LoadError => e log.error 'The sqlite3 gem must be installed before using the analytics class' raise(e.message) end end
Public Instance Methods
add_item(gav, file_path)
click to toggle source
# File lib/nexus_client/analytics.rb, line 34 def add_item(gav, file_path) begin db.execute("insert into artifacts (sha1,artifact_gav,filesize,request_time, modified, file_location) "+ "values ('#{gav.sha1}','#{gav.to_s}', #{gav.attributes[:size]}, #{gav.attributes[:total_time]},"+ "'#{Time.now.to_i}', '#{file_path}')") rescue log.warn("Ignoring Duplicate entry #{file_path}") end end
gavs()
click to toggle source
# File lib/nexus_client/analytics.rb, line 45 def gavs db.execute("select artifact_gav from artifacts").flatten end
hit_count(gav)
click to toggle source
# File lib/nexus_client/analytics.rb, line 70 def hit_count(gav) row = db.execute("select hit_count from totals where sha1 = '#{gav.sha1}'").first if row.nil? 0 else row.first end end
old_items(mtime=(Time.now.to_i)-(172800))
click to toggle source
get items older than mtime, defaults to 2 days ago
# File lib/nexus_client/analytics.rb, line 133 def old_items(mtime=(Time.now.to_i)-(172800)) data = db.execute <<SQL SELECT * from artifacts where modified < #{mtime} SQL data || [] end
remove_old_items(mtime)
click to toggle source
removes old items from the database that are older than mtime
# File lib/nexus_client/analytics.rb, line 126 def remove_old_items(mtime) db.execute <<SQL DELETE from artifacts where modified < #{mtime} SQL end
to_json(pretty=true)
click to toggle source
returns the totals view as json the results as hash returns extra key/values we don't want so we had to create our own hash there are better ways of doing this but this was simple to create
# File lib/nexus_client/analytics.rb, line 107 def to_json(pretty=true) db.results_as_hash = false totals = db.execute("select * from totals") hash_total = [] totals.each do |row| h = {} (0...row.length).each do |col| h[total_columns[col]] = row[col] end hash_total << h end if pretty JSON.pretty_generate(hash_total) else hash_total.to_json end end
top_x(amount=10)
click to toggle source
returns the top X most utilized caches
# File lib/nexus_client/analytics.rb, line 141 def top_x(amount=10) db.execute <<SQL SELECT * FROM totals ORDER BY hit_count desc LIMIT #{amount} SQL end
total(gav)
click to toggle source
# File lib/nexus_client/analytics.rb, line 61 def total(gav) # TODO fix NoMethodError: undefined method `sha1' for #<String:0x7f1a0f387720> # when type is not a gav or sha1 is not available data = db.execute("select * from totals where sha1 = '#{gav.sha1}'") db.results_as_hash = false data end
total_bytes(gav, pretty=false)
click to toggle source
# File lib/nexus_client/analytics.rb, line 89 def total_bytes(gav, pretty=false) row = db.execute("select total_bytes_saved from totals where sha1 = '#{gav.sha1}'").first if row.nil? 0 else if pretty Filesize.from("#{row.first} B").pretty else row.first end end end
total_time(gav)
click to toggle source
# File lib/nexus_client/analytics.rb, line 79 def total_time(gav) row = db.execute("select total_time_saved from totals where sha1 = '#{gav.sha1}'").first if row.nil? 0 else row.first end end
totals()
click to toggle source
# File lib/nexus_client/analytics.rb, line 57 def totals db.execute("select * from totals") end
update_item(gav)
click to toggle source
# File lib/nexus_client/analytics.rb, line 49 def update_item(gav) count = hit_count(gav) db.execute <<SQL UPDATE artifacts SET hit_count=#{count + 1}, modified=#{Time.now.to_i} WHERE sha1='#{gav.sha1}' SQL end
Private Instance Methods
artifact_columns()
click to toggle source
# File lib/nexus_client/analytics.rb, line 155 def artifact_columns %w(sha1 artifact_gav filesize, request_time hit_count modified file_location ) end
init_tables()
click to toggle source
# File lib/nexus_client/analytics.rb, line 160 def init_tables # id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, db.execute <<SQL CREATE TABLE IF NOT EXISTS artifacts ( sha1 VARCHAR(40) PRIMARY KEY NOT NULL, artifact_gav VARCHAR(255), filesize BIGINT default 0, request_time FLOAT default 0, hit_count INTEGER default 0, modified BIGINT default '#{Time.now.to_i}', file_location VARCHAR(255) ); SQL end
total_columns()
click to toggle source
# File lib/nexus_client/analytics.rb, line 151 def total_columns %w(sha1 artifact_gav filesize request_time modified file_location hit_count total_bytes_saved total_time_saved) end
total_view()
click to toggle source
# File lib/nexus_client/analytics.rb, line 175 def total_view db.execute <<SQL CREATE VIEW IF NOT EXISTS totals AS SELECT sha1, artifact_gav, filesize, request_time, modified, file_location, hit_count, sum(hit_count * filesize) AS 'total_bytes_saved', sum(hit_count * request_time) as 'total_time_saved' FROM artifacts SQL end