Class: Nexus::Analytics
- Inherits:
-
Object
- Object
- Nexus::Analytics
- Defined in:
- lib/nexus_client/analytics.rb
Instance Attribute Summary collapse
-
#a_file ⇒ Object
Returns the value of attribute a_file.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#db_file ⇒ Object
readonly
Returns the value of attribute db_file.
-
#log ⇒ Object
Returns the value of attribute log.
Instance Method Summary collapse
- #add_item(gav, file_path) ⇒ Object
- #gavs ⇒ Object
- #hit_count(gav) ⇒ Object
-
#initialize(database_dir = './', db_filename = 'cache-analytics.db', logger = nil) ⇒ Analytics
constructor
new method.
-
#old_items(mtime = (Time.now.to_i)-(172800)) ⇒ Object
get items older than mtime, defaults to 2 days ago.
-
#remove_old_items(mtime) ⇒ Object
removes old items from the database that are older than mtime.
-
#to_json(pretty = true) ⇒ Object
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.
-
#top_x(amount = 10) ⇒ Object
returns the top X most utilized caches.
- #total(gav) ⇒ Object
- #total_bytes(gav, pretty = false) ⇒ Object
- #total_time(gav) ⇒ Object
- #totals ⇒ Object
- #update_item(gav) ⇒ Object
Constructor Details
#initialize(database_dir = './', db_filename = 'cache-analytics.db', logger = nil) ⇒ Analytics
new method
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# 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.) end end |
Instance Attribute Details
#a_file ⇒ Object
Returns the value of attribute a_file.
6 7 8 |
# File 'lib/nexus_client/analytics.rb', line 6 def a_file @a_file end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/nexus_client/analytics.rb', line 5 def data @data end |
#db ⇒ Object (readonly)
Returns the value of attribute db.
5 6 7 |
# File 'lib/nexus_client/analytics.rb', line 5 def db @db end |
#db_file ⇒ Object (readonly)
Returns the value of attribute db_file.
5 6 7 |
# File 'lib/nexus_client/analytics.rb', line 5 def db_file @db_file end |
#log ⇒ Object
Returns the value of attribute log.
6 7 8 |
# File 'lib/nexus_client/analytics.rb', line 6 def log @log end |
Instance Method Details
#add_item(gav, file_path) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# 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 ⇒ Object
45 46 47 |
# File 'lib/nexus_client/analytics.rb', line 45 def gavs db.execute("select artifact_gav from artifacts").flatten end |
#hit_count(gav) ⇒ Object
70 71 72 73 74 75 76 77 |
# 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)) ⇒ Object
get items older than mtime, defaults to 2 days ago
133 134 135 136 137 138 |
# 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) ⇒ Object
removes old items from the database that are older than mtime
126 127 128 129 130 |
# 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) ⇒ Object
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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# 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) ⇒ Object
returns the top X most utilized caches
141 142 143 144 145 146 147 |
# 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) ⇒ Object
61 62 63 64 65 66 67 |
# 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) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# 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 ⇒ Object
57 58 59 |
# File 'lib/nexus_client/analytics.rb', line 57 def totals db.execute("select * from totals") end |
#update_item(gav) ⇒ Object
49 50 51 52 53 54 55 |
# 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 |