Module: TabsTabs

Extended by:
TabsTabs, Storage
Included in:
TabsTabs
Defined in:
lib/tabs_tabs/config.rb,
lib/tabs_tabs/helpers.rb,
lib/tabs_tabs/storage.rb,
lib/tabs_tabs/version.rb,
lib/tabs_tabs/tabs_tabs.rb,
lib/tabs_tabs/resolution.rb,
lib/tabs_tabs/metrics/task.rb,
lib/tabs_tabs/metrics/value.rb,
lib/tabs_tabs/resolutionable.rb,
lib/tabs_tabs/metrics/counter.rb,
lib/tabs_tabs/resolutions/day.rb,
lib/tabs_tabs/resolutions/hour.rb,
lib/tabs_tabs/resolutions/week.rb,
lib/tabs_tabs/resolutions/year.rb,
lib/tabs_tabs/resolutions/month.rb,
lib/tabs_tabs/metrics/task/token.rb,
lib/tabs_tabs/resolutions/minute.rb,
lib/tabs_tabs/metrics/value/stats.rb,
lib/tabs_tabs/metrics/counter/stats.rb

Defined Under Namespace

Modules: Config, Helpers, Metrics, Resolution, Resolutionable, Resolutions, Storage Classes: DuplicateMetricError, MetricTypeMismatchError, ResolutionMissingError, UnknownMetricError, UnknownTypeError

Constant Summary collapse

VERSION =
"2.0.0"
METRIC_TYPES =
["counter", "value", "task"]

Instance Method Summary collapse

Methods included from Storage

del, del_by_prefix, exists, expireat, get, hdel, hget, hgetall, hincrby, hkeys, hset, incr, mget, rpush, sadd, set, sismember, smembers, smembers_all, tabs_key, ttl

Instance Method Details

#complete_task(key, token, timestamp = Time.now) ⇒ Object



43
44
45
46
# File 'lib/tabs_tabs/tabs_tabs.rb', line 43

def complete_task(key, token, timestamp=Time.now)
  raise MetricTypeMismatchError.new("Only task metrics can complete a task") unless metric_type(key) == "task"
  get_metric(key).complete(token, timestamp)
end

#configObject



21
22
23
# File 'lib/tabs_tabs/tabs_tabs.rb', line 21

def config
  Config
end

#configure {|Config| ... } ⇒ Object

Yields:



13
14
15
# File 'lib/tabs_tabs/tabs_tabs.rb', line 13

def configure
  yield(Config)
end

#counter_total(key) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tabs_tabs/tabs_tabs.rb', line 61

def counter_total(key)
  unless metric_exists?(key)
    if block_given?
      return yield
    else
      raise UnknownMetricError.new("Unknown metric: #{key}")
    end
  end
  raise MetricTypeMismatchError.new("Only counter metrics can be incremented") unless metric_type(key) == "counter"
  get_metric(key).total
end

#create_metric(key, type) ⇒ Object

Raises:



48
49
50
51
52
53
# File 'lib/tabs_tabs/tabs_tabs.rb', line 48

def create_metric(key, type)
  raise UnknownTypeError.new("Unknown metric type: #{type}") unless METRIC_TYPES.include?(type)
  raise DuplicateMetricError.new("Metric already exists: #{key}") if metric_exists?(key)
  hset "metrics", key, type
  metric_klass(type).new(key)
end

#drop_all_metrics!Object



99
100
101
102
# File 'lib/tabs_tabs/tabs_tabs.rb', line 99

def drop_all_metrics!
  metrics = self.list_metrics
  metrics.each { |key| self.drop_metric! key }
end

#drop_metric!(key) ⇒ Object

Raises:



92
93
94
95
96
97
# File 'lib/tabs_tabs/tabs_tabs.rb', line 92

def drop_metric!(key)
  raise UnknownMetricError.new("Unknown metric: #{key}") unless metric_exists?(key)
  metric = get_metric(key)
  metric.drop!
  hdel "metrics", key
end

#drop_resolution_for_metric!(key, resolution) ⇒ Object

Raises:



104
105
106
107
108
109
# File 'lib/tabs_tabs/tabs_tabs.rb', line 104

def drop_resolution_for_metric!(key, resolution)
  raise UnknownMetricError.new("Unknown metric: #{key}") unless metric_exists?(key)
  raise ResolutionMissingError.new(resolution) unless TabsTabs::Resolution.all.include? resolution
  metric = get_metric(key)
  metric.drop_by_resolution!(resolution) unless metric_type(key) == "task"
end

#get_metric(key) ⇒ Object

Raises:



55
56
57
58
59
# File 'lib/tabs_tabs/tabs_tabs.rb', line 55

def get_metric(key)
  raise UnknownMetricError.new("Unknown metric: #{key}") unless metric_exists?(key)
  type = hget("metrics", key)
  metric_klass(type).new(key)
end

#get_stats(key, period, resolution) ⇒ Object

Raises:



73
74
75
76
77
# File 'lib/tabs_tabs/tabs_tabs.rb', line 73

def get_stats(key, period, resolution)
  raise UnknownMetricError.new("Unknown metric: #{key}") unless metric_exists?(key)
  metric = get_metric(key)
  metric.stats(period, resolution)
end

#increment_counter(key, timestamp = Time.now) ⇒ Object



25
26
27
28
29
# File 'lib/tabs_tabs/tabs_tabs.rb', line 25

def increment_counter(key, timestamp=Time.now)
  create_metric(key, "counter") unless metric_exists?(key)
  raise MetricTypeMismatchError.new("Only counter metrics can be incremented") unless metric_type(key) == "counter"
  get_metric(key).increment(timestamp)
end

#list_metricsObject



84
85
86
# File 'lib/tabs_tabs/tabs_tabs.rb', line 84

def list_metrics
  hkeys "metrics"
end

#metric_exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/tabs_tabs/tabs_tabs.rb', line 88

def metric_exists?(key)
  list_metrics.include? key
end

#metric_type(key) ⇒ Object

Raises:



79
80
81
82
# File 'lib/tabs_tabs/tabs_tabs.rb', line 79

def metric_type(key)
  raise UnknownMetricError.new("Unknown metric: #{key}") unless metric_exists?(key)
  hget "metrics", key
end

#record_value(key, value, timestamp = Time.now) ⇒ Object



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

def record_value(key, value, timestamp=Time.now)
  create_metric(key, "value") unless metric_exists?(key)
  raise MetricTypeMismatchError.new("Only value metrics can record a value") unless metric_type(key) == "value"
  get_metric(key).record(value, timestamp)
end

#redisObject



17
18
19
# File 'lib/tabs_tabs/tabs_tabs.rb', line 17

def redis
  Config.redis
end

#start_task(key, token, timestamp = Time.now) ⇒ Object



37
38
39
40
41
# File 'lib/tabs_tabs/tabs_tabs.rb', line 37

def start_task(key, token, timestamp=Time.now)
  create_metric(key, "task")
  raise MetricTypeMismatchError.new("Only task metrics can start a task") unless metric_type(key) == "task"
  get_metric(key).start(token, timestamp)
end