simple_metric 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +29 -21
- data/lib/simple_metric.rb +41 -2
- data/lib/simple_metric/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94f7b39d79bdf0a94d696a0b14f4593dbc29c42d
|
4
|
+
data.tar.gz: bf8c428bf48f7c5fcd243dd94680815625fbe02b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 09d4914879fabc586e3e1a5a37f60b273838a77fda473c9236078bcfcc6240c3f43877661487c4a623f5bbdfd6feef4e8e724bb36a3b806182d8a337f8550a31
|
7
|
+
data.tar.gz: f205565bde2018e77835432105e981bf6e3818a36e7893b1f8d16861ed47853d631349718a323ea1a024f8d62cc1024a99f03a0f54f1a676cbe37cd3cd6fad8e
|
data/README.md
CHANGED
@@ -2,6 +2,35 @@
|
|
2
2
|
|
3
3
|
Rails metric solution. Store data-points, display graphs. Based on dygraphs js lib.
|
4
4
|
|
5
|
+
## Example
|
6
|
+
|
7
|
+
|
8
|
+

|
9
|
+
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Add some data points:
|
14
|
+
|
15
|
+
SimpleMetric::Metric.add_data_point "Users count", 30.days.ago, 10
|
16
|
+
SimpleMetric::Metric.add_data_point "Users count", 20.days.ago, 15
|
17
|
+
SimpleMetric::Metric.add_data_point "Users count", 10.days.ago, 25
|
18
|
+
|
19
|
+
Display graph into your erb template:
|
20
|
+
|
21
|
+
<%= simple_metric_graph "Users count" %>
|
22
|
+
|
23
|
+
Plot multiple metrics:
|
24
|
+
|
25
|
+
<%= simple_metric_graph "metric_1", "metric_2" %>
|
26
|
+
|
27
|
+
Add custom titles:
|
28
|
+
|
29
|
+
<%= simple_metric_graph ["metric_1", "Title for metric 1"], ["metric_2", "Title 2"] %>
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
|
5
34
|
## Installation
|
6
35
|
|
7
36
|
Add this line to your application's Gemfile:
|
@@ -46,27 +75,6 @@ Include dygraph js lib into your application.js:
|
|
46
75
|
|
47
76
|
//= require dygraph-combined
|
48
77
|
|
49
|
-
## Usage
|
50
|
-
|
51
|
-
Add some data points:
|
52
|
-
|
53
|
-
SimpleMetric::Metric.add_data_point "Users count", 30.days.ago, 10
|
54
|
-
SimpleMetric::Metric.add_data_point "Users count", 20.days.ago, 15
|
55
|
-
SimpleMetric::Metric.add_data_point "Users count", 10.days.ago, 25
|
56
|
-
|
57
|
-
Display graph into your erb template:
|
58
|
-
|
59
|
-
<%= simple_metric_graph "Users count" %>
|
60
|
-
|
61
|
-
Plot multiple metrics:
|
62
|
-
|
63
|
-
<%= simple_metric_graph "metric_1", "metric_2" %>
|
64
|
-
|
65
|
-
Add custom titles:
|
66
|
-
|
67
|
-
<%= simple_metric_graph ["metric_1", "Title for metric 1"], ["metric_2", "Title 2"] %>
|
68
|
-
|
69
|
-
|
70
78
|
## Contributing
|
71
79
|
|
72
80
|
1. Fork it ( https://github.com/sergio-fry/simple-metric/fork )
|
data/lib/simple_metric.rb
CHANGED
@@ -62,11 +62,46 @@ module SimpleMetric
|
|
62
62
|
|
63
63
|
def initialize(data_points)
|
64
64
|
@data_points = data_points.sort_by { |point| point.x }
|
65
|
-
@spliner = Spliner::Spliner.new(@data_points.map(&:x), @data_points.map(&:y))
|
66
65
|
end
|
67
66
|
|
68
67
|
def get_value(x)
|
69
|
-
|
68
|
+
x = x.to_f
|
69
|
+
value = @data_points.find { |point| point.x == x }.try(:y)
|
70
|
+
|
71
|
+
if value.blank?
|
72
|
+
points = [points_before(x, 2), points_after(x, 2)].flatten
|
73
|
+
|
74
|
+
spliner = Spliner::Spliner.new(points.map(&:x), points.map(&:y))
|
75
|
+
value = spliner[x]
|
76
|
+
end
|
77
|
+
|
78
|
+
value
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def points_before(x, count)
|
84
|
+
points = []
|
85
|
+
|
86
|
+
@data_points.reverse.each do |point|
|
87
|
+
break if points.size >= count
|
88
|
+
|
89
|
+
points << point if point.x <= x
|
90
|
+
end
|
91
|
+
|
92
|
+
points.sort_by{ |p| p.x }
|
93
|
+
end
|
94
|
+
|
95
|
+
def points_after(x, count)
|
96
|
+
points = []
|
97
|
+
|
98
|
+
@data_points.each do |point|
|
99
|
+
break if points.size >= count
|
100
|
+
|
101
|
+
points << point if point.x >= x
|
102
|
+
end
|
103
|
+
|
104
|
+
points.sort_by{ |p| p.x }
|
70
105
|
end
|
71
106
|
end
|
72
107
|
|
@@ -79,6 +114,8 @@ module SimpleMetric
|
|
79
114
|
end
|
80
115
|
|
81
116
|
class Metric < ActiveRecord::Base
|
117
|
+
MAX_SIZE = 1000
|
118
|
+
|
82
119
|
serialize :data_set
|
83
120
|
validates :key, :presence => true, :uniqueness => true
|
84
121
|
|
@@ -93,6 +130,8 @@ module SimpleMetric
|
|
93
130
|
|
94
131
|
data_set << [date.to_time, value]
|
95
132
|
|
133
|
+
data_set.shift(data_set.size - MAX_SIZE) if data_set.size > MAX_SIZE
|
134
|
+
|
96
135
|
save!
|
97
136
|
end
|
98
137
|
|