tabs 0.5.0 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Tabs
2
2
 
3
- Tabs is a redis-backed metrics tracker that supports counts, sums,
4
- averages, and min/max stats sliceable by the minute, hour, day, week,
5
- month, and year.
3
+ Tabs is a redis-backed metrics tracker that supports counts, sums, averages, and min/max stats sliceable by the minute, hour, day, week, month, and year.
6
4
 
7
5
  ## Installation
8
6
 
@@ -20,16 +18,94 @@ Or install it yourself as:
20
18
 
21
19
  ## Usage
22
20
 
23
- To count events, simply call the `increment` or `record` methods to
24
- write an event to the store.
21
+ Metrics come in two flavors: counters and values.
25
22
 
26
- ### Increment a counter
23
+ ### Counter Metrics
27
24
 
28
- Tabs.increment(key)
25
+ A counter metric simply records the number of events that occur within a given timeframe. To create a counter metric called ‘website-visits’, simply call:
29
26
 
30
- ### Record a value
27
+ Tabs.create_metric(“website-visits”, “counter”)
28
+
29
+ This will set up the metric in the database.
31
30
 
32
- Tabs.record(key, 37)
31
+ To increment a metric counter, simply call:
32
+
33
+ Tabs.increment_counter(“website-visits”)
34
+
35
+ To retrieve the counts for a given time period just call `Tabs#get_stats` with the name of the metric, a range of times defining the period for which you want stats, and the resolution at which the data should be aggregated.
36
+
37
+ Tabs.get_stats(“website-visits”, (Time.now - 10.days)..Time.now, :hour)
38
+
39
+ This will return stats for the last 10 days by hour as an array of hashes in which the keys are an instance of `Time` and the value is the count for that time.
40
+
41
+ [
42
+ { 2000-01-01 00:00:00 UTC => 1 },
43
+ { 2000-01-01 01:00:00 UTC => 0 },
44
+ { 2000-01-01 02:00:00 UTC => 10 },
45
+ { 2000-01-01 03:00:00 UTC => 1 },
46
+ { 2000-01-01 04:00:00 UTC => 0 },
47
+ { 2000-01-01 05:00:00 UTC => 0 },
48
+ { 2000-01-01 06:00:00 UTC => 3 },
49
+ { 2000-01-01 07:00:00 UTC => 0 },
50
+ ...
51
+ ]
52
+
53
+ Times for the given period in which no events occurred will be “filled in” with a zero value to make visualizations easier.
54
+
55
+ The `Time` keys are also normalized. For example, in hour resolution, the minutes and seconds of the `Time` object are set to 00:00. Likewise for the week resolution, the day is set to the first day of the week.
56
+
57
+ ### Value Metrics
58
+
59
+ Value metrics take a value and record the min, max, avg, and sum for a given time resolution. Creating a value metric is easy:
60
+
61
+ Tabs.create_metric(“new-user-age”, “value”)
62
+
63
+ To record a value, simply call `Tabs#record_value`.
64
+
65
+ Tabs.record_value(“new-user-age”, 32)
66
+
67
+ Retrieving the stats for a value metric is just like retrieving a counter metric.
68
+
69
+ Tabs.get_stats(“new-user-age”, (Time.now - 6.months)..Time.now, :month)
70
+
71
+ This will return a familiar value, but with an expanded set of values.
72
+
73
+ [
74
+ { 2000-01-01 00:00:00 UTC => { min: 19, max: 54, sum: 226, avg: 38 } },
75
+ { 2000-02-01 01:00:00 UTC => { min: 0, max: 0, sum: 0, avg: 0 } },
76
+ { 2000-03-01 02:00:00 UTC => { min: 22, max: 34, sum: 180, avg: 26 } },
77
+ ...
78
+ ]
79
+
80
+ ### Resolutions
81
+
82
+ When tabs increments a counter or records a value it does so for each of the following “resolutions”. You may supply any of these as the last argument to the `Tabs#get_stats` method.
83
+
84
+ :minute, :hour, :day, :week, :month, :year
85
+
86
+ It automatically aggregates multiple events for the same period. For instance when you increment a counter metric, 1 will be added for each of the resolutions for the current time. Repeating the event 5 minutes later will increment a different minute slot, but the same hour, date, week, etc. When you retrieve metrics, all timestamps will be in UTC.
87
+
88
+ ### Drop a Metric
89
+
90
+ To drop a metric, just call `Tabs#drop_metric`
91
+
92
+ Tabs.drop_metric(“website-visits”)
93
+
94
+ This will drop all recorded values for the metric so it may not be un-done...be careful.
95
+
96
+ ### Configuration
97
+
98
+ There really isn’t much to configure with Tabs, it just works out of the box. You can use the following configure block to set the Redis connection instance that Tabs will use.
99
+
100
+ Tabs.configure do |config|
101
+
102
+ # set it to an existing connection
103
+ config.redis = Redis.current
104
+
105
+ # pass a config hash that will be passed to Redis.new
106
+ config.redis = { :host => 'localhost', :port => 6379 }
107
+
108
+ end
33
109
 
34
110
  ## Contributing
35
111
 
@@ -23,6 +23,11 @@ module Tabs
23
23
  redis.set("tabs:#{key}", value)
24
24
  end
25
25
 
26
+ def del(*keys)
27
+ prefixed_keys = keys.map { |k| "tabs:#{k}" }
28
+ redis.del(*prefixed_keys)
29
+ end
30
+
26
31
  def incr(key)
27
32
  redis.incr("tabs:#{key}")
28
33
  end
@@ -44,13 +44,23 @@ module Tabs
44
44
  metric_klass(type).new(key)
45
45
  end
46
46
 
47
+ def get_stats(key, period, resolution)
48
+ metric = get_metric(key)
49
+ metric.stats(period, resolution)
50
+ end
51
+
47
52
  def metric_type(key)
48
53
  hget "metrics", key
49
54
  end
50
55
 
51
56
  def drop_metric(key)
52
57
  hdel "metrics", key
53
- # TODO: Need to finish this
58
+ Tabs::RESOLUTIONS.each do |resolution|
59
+ stat_key = "stat:keys:#{key}:#{resolution}"
60
+ keys = smembers(stat_key)
61
+ del(keys)
62
+ del stat_key
63
+ end
54
64
  end
55
65
 
56
66
  def list_metrics
@@ -1,3 +1,3 @@
1
1
  module Tabs
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Tabs do
4
+ include Tabs::Storage
4
5
 
5
6
  describe "#create_metric" do
6
7
 
@@ -74,7 +75,13 @@ describe Tabs do
74
75
  expect(Tabs.metric_exists?("foo")).to be_false
75
76
  end
76
77
 
77
- it "removes the metrics values from redis"
78
+ it "removes the metrics values from redis" do
79
+ Tabs.increment_counter("foo")
80
+ keys = smembers("tabs:stat:keys:foo:hour")
81
+ expect(redis.keys).to include("tabs:stat:keys:foo:hour")
82
+ Tabs.drop_metric("foo")
83
+ expect(redis.keys).to_not include(keys[0])
84
+ end
78
85
 
79
86
  end
80
87
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-22 00:00:00.000000000 Z
12
+ date: 2013-02-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis