simple_metric 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e62345627aa093f653d23f6176846c9ca9fd1422
4
- data.tar.gz: 39758f3e78dc1b576ffee430ac15e829a92836f0
3
+ metadata.gz: 94f7b39d79bdf0a94d696a0b14f4593dbc29c42d
4
+ data.tar.gz: bf8c428bf48f7c5fcd243dd94680815625fbe02b
5
5
  SHA512:
6
- metadata.gz: 2c2b6899e9413f53a386536f11122d09c62dd8a127d7c357a1fda5fdf6244899048c4c5cc60d7c8a0a2447795de6dbb2cd9686206eb18bd94fc74ac9c5b3b481
7
- data.tar.gz: f08871d0a55002806076f457cb0edabb5b2de0c40ea8a06ec0bf67f360ee8c5a7f482da2ee1db0acffd380c32c13bdcf00c918ecd8018c9c17fdb04bca8b33e1
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
+ ![example 1](http://sergio-fry.github.io/simple-metric/images/example.jpeg)
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 )
@@ -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
- @spliner[x.to_f]
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
 
@@ -1,5 +1,5 @@
1
1
  module Simple
2
2
  module Metric
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_metric
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergei O. Udalov