tabs 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -52,7 +52,7 @@ To retrieve the counts for a given time period just call `Tabs#get_stats` with t
52
52
  ```ruby
53
53
  Tabs.get_stats("website-visits", 10.days.ago..Time.now, :hour)
54
54
  ```
55
-
55
+
56
56
  This will return stats for the last 10 days by hour in the form of a `Tabs::Metrics::Counter::Stats` object. This object is enumerable so you can iterate through the results like so:
57
57
 
58
58
  ```ruby
@@ -81,7 +81,7 @@ results.avg #=> The avg count for timestamps in the period
81
81
  results.period #=> The timestamp range that was requested
82
82
  results.resolution #=> The resolution requested
83
83
  ```
84
-
84
+
85
85
  Timestamps for the given period in which no events occurred will be "filled in" with a count value to make visualizations easier.
86
86
 
87
87
  The timestamps are also normalized. For example, in hour resolution, the minutes and seconds of the timestamps are set to 00:00. Likewise for the week resolution, the day is set to the first day of the week.
@@ -116,13 +116,13 @@ a metric as well:
116
116
  ```ruby
117
117
  Tabs.create_metric("new-user-age", "value")
118
118
  ```
119
-
119
+
120
120
  Retrieving the stats for a value metric is just like retrieving a counter metric.
121
121
 
122
122
  ```ruby
123
123
  Tabs.get_stats("new-user-age", 6.months.ago..Time.now, :month)
124
124
  ```
125
-
125
+
126
126
  This will return a `Tabs::Metrics::Value::Stats` object. Again, this
127
127
  object is enumerable and encapsulates all the timestamps within the
128
128
  given period.
@@ -229,7 +229,7 @@ To drop a metric, just call `Tabs#drop_metric`
229
229
  ```ruby
230
230
  Tabs.drop_metric!("website-visits")
231
231
  ```
232
-
232
+
233
233
  This will drop all recorded values for the metric so it may not be un-done...be careful.
234
234
 
235
235
  Even more dangerous, you can drop all metrics...be very careful.
@@ -240,17 +240,21 @@ Tabs.drop_all_metrics!
240
240
 
241
241
  ### Configuration
242
242
 
243
- 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.
243
+ Tabs just works out of the box. However, if you want to override the default Redis connection or decimal precision, this is how:
244
244
 
245
245
  ```ruby
246
246
  Tabs.configure do |config|
247
247
 
248
248
  # set it to an existing connection
249
249
  config.redis = Redis.current
250
-
250
+
251
251
  # pass a config hash that will be passed to Redis.new
252
252
  config.redis = { :host => 'localhost', :port => 6379 }
253
-
253
+
254
+ # override default decimal precision (5)
255
+ # affects stat averages and task completion rate
256
+ config.decimal_precision = 2
257
+
254
258
  end
255
259
  ```
256
260
 
@@ -2,6 +2,14 @@ module Tabs
2
2
  module Config
3
3
  extend self
4
4
 
5
+ def decimal_precision
6
+ @decimal_precision ||= 5
7
+ end
8
+
9
+ def decimal_precision=(precision)
10
+ @decimal_precision = precision
11
+ end
12
+
5
13
  def redis=(arg)
6
14
  if arg.is_a?(Redis)
7
15
  @redis = arg
@@ -18,10 +18,5 @@ module Tabs
18
18
  period_end = Tabs::Resolution.normalize(resolution, period.last.utc)
19
19
  (period_start..period_end)
20
20
  end
21
-
22
- def round_float(f)
23
- (f*100).round / 100.0
24
- end
25
-
26
21
  end
27
22
  end
@@ -33,7 +33,8 @@ module Tabs
33
33
  end
34
34
 
35
35
  def avg
36
- round_float(self.total.to_f / values.size.to_f)
36
+ return 0 if values.size.zero?
37
+ (self.total.to_f / values.size.to_f).round(Config.decimal_precision)
37
38
  end
38
39
 
39
40
  def each(&block)
@@ -35,7 +35,7 @@ module Tabs
35
35
  started_tokens = tokens_for_period(range, resolution, "started")
36
36
  completed_tokens = tokens_for_period(range, resolution, "completed")
37
37
  matching_tokens = started_tokens & completed_tokens
38
- completion_rate = round_float(matching_tokens.size.to_f / range.size)
38
+ completion_rate = (matching_tokens.size.to_f / range.size).round(Config.decimal_precision)
39
39
  elapsed_times = matching_tokens.map { |t| t.time_elapsed(resolution) }
40
40
  average_completion_time = (elapsed_times.inject(&:+)) / matching_tokens.size
41
41
  Stats.new(
@@ -37,7 +37,8 @@ module Tabs
37
37
  end
38
38
 
39
39
  def avg
40
- round_float(self.sum.to_f / self.count.to_f)
40
+ return 0 if count.zero?
41
+ (self.sum.to_f / self.count.to_f).round(Config.decimal_precision)
41
42
  end
42
43
 
43
44
  def each(&block)
@@ -1,3 +1,3 @@
1
1
  module Tabs
2
- VERSION = "0.8.0"
2
+ VERSION = "0.8.1"
3
3
  end
@@ -31,7 +31,12 @@ describe Tabs::Metrics::Counter::Stats do
31
31
  end
32
32
 
33
33
  it "avg returns the average for the entire set" do
34
- expect(stats.avg).to eq 86.33
34
+ expect(stats.avg).to eq 86.33333
35
+ end
36
+
37
+ it "avg returns 0 if values empty" do
38
+ stats = Tabs::Metrics::Counter::Stats.new(period, resolution, [])
39
+ expect(stats.avg).to be_zero
35
40
  end
36
41
 
37
42
  end
@@ -38,4 +38,24 @@ describe Tabs::Metrics::Value::Stats do
38
38
  expect(stats.avg).to eq 16.76
39
39
  end
40
40
 
41
+ it "avg returns 0 if set is empty" do
42
+ stats = Tabs::Metrics::Value::Stats.new(period, resolution, [])
43
+ expect(stats.avg).to be_zero
44
+ end
45
+
46
+ context "override decimal precision" do
47
+ before do
48
+ @precision = Tabs.config.decimal_precision
49
+ Tabs.config.decimal_precision = 1
50
+ end
51
+
52
+ after do
53
+ Tabs.config.decimal_precision = @precision
54
+ end
55
+
56
+ it "allows you to override decimal precision" do
57
+ expect(stats.avg).to eq 16.8
58
+ end
59
+ end
60
+
41
61
  end
@@ -95,7 +95,6 @@ describe Tabs::Metrics::Value do
95
95
  expect(stats).to include({ "timestamp" => (now + 3.years), "count"=>1, "min"=>10, "max"=>10, "sum"=>10, "avg"=>10})
96
96
  expect(stats).to include({ "timestamp" => (now + 6.years), "count"=>2, "min"=>15, "max"=>20, "sum"=>35, "avg"=>17})
97
97
  end
98
-
99
98
  end
100
99
 
101
100
  describe ".drop!" do
@@ -66,7 +66,7 @@ describe Tabs::Metrics::Task do
66
66
  expect(stats.started_within_period).to eq 3
67
67
  expect(stats.completed_within_period).to eq 2
68
68
  expect(stats.started_and_completed_within_period).to eq 2
69
- expect(stats.completion_rate).to eq 0.18
69
+ expect(stats.completion_rate).to eq 0.18182
70
70
  expect(stats.average_completion_time).to eq 1.5
71
71
  end
72
72
 
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.8.0
4
+ version: 0.8.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: