Class: MetricSystem::Database
- Extended by:
- Forwardable
- Defined in:
- lib/metric_system/database.rb
Constant Summary collapse
- PERIODS =
[ [ :year, 31536000, "strftime('%Y-01-01', starts_at, 'unixepoch')" ], [ :month, 2592000, "strftime('%Y-%m-01', starts_at, 'unixepoch')" ], [ :week, 604800, "strftime('%Y-%m-%d', starts_at, 'unixepoch', 'weekday 1', '-7 days')" ], [ :day, 86400, "strftime('%Y-%m-%d', starts_at, 'unixepoch')" ], [ :hour, 3600, "strftime('%Y-%m-%d %H:00:00', starts_at, 'unixepoch')" ], [ :minute, 60, "strftime('%Y-%m-%d %H:%M:00', starts_at, 'unixepoch')" ], # [ :second, 1, "strftime('%Y-%m-%d %H:%M:%S', starts_at, 'unixepoch')" ], ]
- PERIODS_BY_KEY =
PERIODS.by(&:first)
Instance Method Summary collapse
- #add_event(table, name, value, starts_at) ⇒ Object
- #aggregate(*keys) ⇒ Object
-
#initialize(path, readonly = false) ⇒ Database
constructor
A new instance of Database.
Constructor Details
#initialize(path, readonly = false) ⇒ Database
Returns a new instance of Database.
2 3 4 5 6 7 8 |
# File 'lib/metric_system/database.rb', line 2 def initialize(path, readonly = false) require_relative "./sqlite3_extensions" open path exec "PRAGMA query_only = 1" if readonly # This might or might not work. @db.readonly = !!readonly end |
Instance Method Details
#add_event(table, name, value, starts_at) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/metric_system/database.rb', line 72 def add_event(table, name, value, starts_at) # get names of all related events. An event "a.b.c" is actually # 3 events: "a", "a.b", "a.b.c" names = begin parts = name.split(".") parts.length.downto(1).map do |cnt| parts[0,cnt].join(".") end end if starts_at starts_at = Time.parse(starts_at) if starts_at.is_a?(String) names.each do |name| run "INSERT INTO #{table}(name, value, starts_at) VALUES(?, ?, ?)", name, value, starts_at.to_i end else names.each do |name| run "INSERT INTO #{table}(name, value) VALUES(?, ?)", name, value end end rescue STDERR.puts "#{$!}: #{table} #{name.inspect}, #{value.inspect}" end |
#aggregate(*keys) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/metric_system/database.rb', line 99 def aggregate(*keys) if keys.empty? keys = PERIODS.map(&:first) end transaction do keys.each do |period| aggregate_for_period :period => period, :source => :counters, :dest => :aggregated_counters, :aggregate => "sum" end exec "DELETE FROM counters" end transaction do keys.each do |period| aggregate_for_period :period => period, :source => :gauges, :dest => :aggregated_gauges, :aggregate => "CAST(sum AS FLOAT) / count" end exec "DELETE FROM gauges" end end |