Class: Litemetric

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

Overview

Measurable Module

Defined Under Namespace

Modules: Measurable Classes: Collector

Constant Summary collapse

DEFAULT_OPTIONS =
{
  config_path: "./litemetric.yml",
  path: Litesupport.root.join("metrics.sqlite3"),
  sync: 1,
  mmap_size: 128 * 1024 * 1024, # 16MB of memory to easily process 1 year worth of data
  flush_interval: 10, # flush data every 10 seconds
  summarize_interval: 30, # summarize data every 1/2 minute
  snapshot_interval: 10 * 60 # 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)
}

Class Method Summary collapse

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 = {}) ⇒ Litemetric

:nodoc:



42
43
44
45
# File 'lib/litestack/litemetric.rb', line 42

def initialize(options = {})
  options = options.merge(Litemetric.options) if Litemetric.options
  init(options)
end

Class Method Details

.optionsObject



37
38
39
# File 'lib/litestack/litemetric.rb', line 37

def self.options
  @options
end

.options=(options) ⇒ Object

:nodoc:



31
32
33
34
35
# File 'lib/litestack/litemetric.rb', line 31

def self.options=(options)
  # an ugly hack to pass options to a singleton
  # need to rethink the whole singleton thing
  @options = options
end

Instance Method Details

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



61
62
63
# File 'lib/litestack/litemetric.rb', line 61

def capture(topic, event, key = event, value = nil)
  @collector.capture(topic, event, key, value, current_time_slot)
end

#capture_snapshot(topic, state) ⇒ Object



65
66
67
# File 'lib/litestack/litemetric.rb', line 65

def capture_snapshot(topic, state)
  run_stmt(:capture_state, topic, Oj.dump(state))
end

#current_time_slotObject

event capturing



57
58
59
# File 'lib/litestack/litemetric.rb', line 57

def current_time_slot
  (Time.now.to_i / 300) * 300
end

#event_data_points(step, count, resolution, topic, event) ⇒ Object



107
108
109
# File 'lib/litestack/litemetric.rb', line 107

def event_data_points(step, count, resolution, topic, event)
  run_stmt_hash(:event_data_points, step, count, resolution, topic, event).to_a
end

#events_summaries(topic, resolution, order, dir, search, count) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/litestack/litemetric.rb', line 85

def events_summaries(topic, resolution, order, dir, search, count)
  search = "%#{search}%" if search
  if dir.downcase == "desc"
    run_stmt_hash(:events_summaries, topic, resolution, order, search, count)
  else
    run_stmt_hash(:events_summaries_asc, topic, resolution, order, search, count)
  end
end

#key_data_points(step, count, resolution, topic, event, key) ⇒ Object



111
112
113
# File 'lib/litestack/litemetric.rb', line 111

def key_data_points(step, count, resolution, topic, event, key)
  run_stmt_hash(:key_data_points, step, count, resolution, topic, event, key).to_a
end

#keys_summaries(topic, event, resolution, order, dir, search, count) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/litestack/litemetric.rb', line 94

def keys_summaries(topic, event, resolution, order, dir, search, count)
  search = "%#{search}%" if search
  if dir.downcase == "desc"
    run_stmt_hash(:keys_summaries, topic, event, resolution, order, search, count).to_a
  else
    run_stmt_hash(:keys_summaries_asc, topic, event, resolution, order, search, count).to_a
  end
end

#register(identifier) ⇒ Object

registers a class for metrics to be collected



48
49
50
51
52
# File 'lib/litestack/litemetric.rb', line 48

def register(identifier)
  @registered[identifier] = true
  @metrics[identifier] = {} unless @metrics[identifier]
  run_stmt(:register_topic, identifier) # it is safe to call register topic multiple times with the same identifier
end

#snapshot(topic) ⇒ Object



115
116
117
# File 'lib/litestack/litemetric.rb', line 115

def snapshot(topic)
  run_stmt(:snapshot, topic)[0].to_a
end

#summarizeObject

summarize data



122
123
124
125
126
127
128
129
# File 'lib/litestack/litemetric.rb', line 122

def summarize
  run_stmt(:summarize_events, RESOLUTIONS[:hour], "hour", "minute")
  run_stmt(:summarize_events, RESOLUTIONS[:day], "day", "hour")
  run_stmt(:summarize_events, RESOLUTIONS[:week], "week", "day")
  run_stmt(:delete_events, "minute", RESOLUTIONS[:hour] * 1)
  run_stmt(:delete_events, "hour", RESOLUTIONS[:day] * 1)
  run_stmt(:delete_events, "day", RESOLUTIONS[:week] * 1)
end

#topic_data_points(step, count, resolution, topic) ⇒ Object



103
104
105
# File 'lib/litestack/litemetric.rb', line 103

def topic_data_points(step, count, resolution, topic)
  run_stmt(:topic_data_points, step, count, resolution, topic).to_a
end

#topic_summaries(resolution, count, order, dir, search) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/litestack/litemetric.rb', line 76

def topic_summaries(resolution, count, order, dir, search)
  search = "%#{search}%" if search
  if dir.downcase == "desc"
    run_stmt(:topics_summaries, resolution, count, order, search).to_a
  else
    run_stmt(:topics_summaries_asc, resolution, count, order, search).to_a
  end
end

#topicsObject

event reporting



72
73
74
# File 'lib/litestack/litemetric.rb', line 72

def topics
  run_stmt(:list_topics).to_a
end