Class: Litemetric::Collector

Inherits:
Object
  • Object
show all
Includes:
Litesupport::Liteconnection
Defined in:
lib/litestack/litemetric.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  path: ":memory:",
  sync: 1,
  flush_interval: 3, # flush data every 1 minute
  summarize_interval: 10, # summarize data every 1 minute
  snapshot_interval: 1 # snapshot every 10 minutes
}
RESOLUTIONS =
{
  minute: 300, # 5 minutes (highest resolution)
  hour: 3600, # 1 hour
  day: 24 * 3600, # 1 day
  week: 7 * 24 * 3600 # 1 week (lowest resolution)
}

Instance Method Summary collapse

Methods included from Litesupport::Liteconnection

#close, #journal_mode, #options, #path, #size, #synchronous

Methods included from Litesupport::Forkable

#_fork

Constructor Details

#initialize(options = {}) ⇒ Collector

Returns a new instance of Collector.



264
265
266
# File 'lib/litestack/litemetric.rb', line 264

def initialize(options = {})
  init(options)
end

Instance Method Details

#capture(topic, event, key, value = nil, time = nil) ⇒ Object



268
269
270
271
272
273
274
# File 'lib/litestack/litemetric.rb', line 268

def capture(topic, event, key, value = nil, time = nil)
  if key.is_a? Array
    key.each { |k| capture_single_key(topic, event, k, value, time) }
  else
    capture_single_key(topic, event, key, value, time)
  end
end

#capture_single_key(topic, event, key, value, time = nil) ⇒ Object



276
277
278
# File 'lib/litestack/litemetric.rb', line 276

def capture_single_key(topic, event, key, value, time = nil)
  run_stmt(:capture_event, topic.to_s, event.to_s, key.to_s, time, 1, value)
end

#countObject



280
281
282
# File 'lib/litestack/litemetric.rb', line 280

def count
  run_stmt(:event_count)[0][0]
end

#create_connectionObject



299
300
301
302
303
304
# File 'lib/litestack/litemetric.rb', line 299

def create_connection
  super("#{__dir__}/litemetric_collector.sql.yml") do |conn|
    conn.execute("ATTACH ? as m", @options[:dbpath].to_s)
    conn.wal_autocheckpoint = 10000
  end
end

#flushObject



284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/litestack/litemetric.rb', line 284

def flush
  limit = 1000 # migrate 1000 records at a time
  count = run_stmt(:event_count)[0][0]
  while count > 0
    @conn.acquire do |conn|
      conn.transaction(:immediate) do
        conn.stmts[:migrate_events].execute!(limit)
        conn.stmts[:delete_migrated_events].execute!(limit)
        count = conn.stmts[:event_count].execute![0][0]
      end
    end
    sleep 0.005 # give other threads a chance to run
  end
end