influxer 0.2.4 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +12 -0
  4. data/.travis.yml +1 -17
  5. data/Changelog.md +7 -0
  6. data/Gemfile +7 -0
  7. data/README.md +6 -20
  8. data/Rakefile +4 -0
  9. data/gemfiles/rails42.gemfile +4 -1
  10. data/influxer.gemspec +6 -6
  11. data/lib/influxer/client.rb +4 -44
  12. data/lib/influxer/config.rb +23 -14
  13. data/lib/influxer/engine.rb +0 -1
  14. data/lib/influxer/metrics/metrics.rb +40 -7
  15. data/lib/influxer/metrics/relation/calculations.rb +13 -7
  16. data/lib/influxer/metrics/relation/time_query.rb +14 -8
  17. data/lib/influxer/metrics/relation/where_clause.rb +62 -0
  18. data/lib/influxer/metrics/relation.rb +67 -76
  19. data/lib/influxer/metrics/scoping.rb +4 -4
  20. data/lib/influxer/model.rb +4 -0
  21. data/lib/influxer/rails/client.rb +47 -0
  22. data/lib/influxer/version.rb +1 -1
  23. data/lib/influxer.rb +4 -2
  24. data/spec/cases/points_spec.rb +34 -0
  25. data/spec/client_spec.rb +21 -24
  26. data/spec/fixtures/empty_result.json +21 -0
  27. data/spec/fixtures/single_series.json +29 -0
  28. data/spec/metrics/metrics_spec.rb +139 -113
  29. data/spec/metrics/relation_spec.rb +160 -105
  30. data/spec/metrics/scoping_spec.rb +11 -17
  31. data/spec/model/user_spec.rb +44 -0
  32. data/spec/spec_helper.rb +38 -8
  33. data/spec/support/metrics/action_metrics.rb +3 -0
  34. data/spec/support/metrics/custom_metrics.rb +4 -0
  35. data/spec/support/{dummy_metrics.rb → metrics/dummy_metrics.rb} +2 -1
  36. data/spec/support/metrics/user_metrics.rb +4 -0
  37. data/spec/support/metrics/visits_metrics.rb +4 -0
  38. data/spec/support/shared_contexts/shared_query.rb +16 -0
  39. data/spec/support/user.rb +14 -0
  40. metadata +42 -71
  41. data/gemfiles/rails40.gemfile +0 -7
  42. data/gemfiles/rails41.gemfile +0 -7
  43. data/lib/influxer/metrics/fanout.rb +0 -65
  44. data/lib/influxer/metrics/relation/fanout_query.rb +0 -49
  45. data/lib/tasks/influxer_tasks.rake +0 -4
  46. data/spec/config_spec.rb +0 -17
  47. data/spec/dummy/README.rdoc +0 -28
  48. data/spec/dummy/Rakefile +0 -6
  49. data/spec/dummy/app/assets/images/.keep +0 -0
  50. data/spec/dummy/app/assets/javascripts/application.js +0 -13
  51. data/spec/dummy/app/assets/stylesheets/application.css +0 -15
  52. data/spec/dummy/app/controllers/application_controller.rb +0 -5
  53. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  54. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  55. data/spec/dummy/app/mailers/.keep +0 -0
  56. data/spec/dummy/app/metrics/testo_metrics.rb +0 -3
  57. data/spec/dummy/app/models/.keep +0 -0
  58. data/spec/dummy/app/models/concerns/.keep +0 -0
  59. data/spec/dummy/app/models/testo.rb +0 -6
  60. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  61. data/spec/dummy/bin/bundle +0 -3
  62. data/spec/dummy/bin/rails +0 -4
  63. data/spec/dummy/bin/rake +0 -4
  64. data/spec/dummy/config/application.rb +0 -23
  65. data/spec/dummy/config/boot.rb +0 -5
  66. data/spec/dummy/config/database.yml +0 -25
  67. data/spec/dummy/config/environment.rb +0 -5
  68. data/spec/dummy/config/environments/development.rb +0 -37
  69. data/spec/dummy/config/environments/production.rb +0 -83
  70. data/spec/dummy/config/environments/test.rb +0 -38
  71. data/spec/dummy/config/influxdb.yml +0 -5
  72. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  73. data/spec/dummy/config/initializers/cookies_serializer.rb +0 -3
  74. data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  75. data/spec/dummy/config/initializers/inflections.rb +0 -16
  76. data/spec/dummy/config/initializers/mime_types.rb +0 -4
  77. data/spec/dummy/config/initializers/session_store.rb +0 -3
  78. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  79. data/spec/dummy/config/locales/en.yml +0 -23
  80. data/spec/dummy/config/routes.rb +0 -56
  81. data/spec/dummy/config/secrets.yml +0 -25
  82. data/spec/dummy/config.ru +0 -4
  83. data/spec/dummy/db/migrate/20140730133818_add_testos.rb +0 -11
  84. data/spec/dummy/db/migrate/20140731162044_add_column_to_testos.rb +0 -5
  85. data/spec/dummy/db/schema.rb +0 -22
  86. data/spec/dummy/db/test.sqlite3 +0 -0
  87. data/spec/dummy/lib/assets/.keep +0 -0
  88. data/spec/dummy/log/.keep +0 -0
  89. data/spec/dummy/public/404.html +0 -67
  90. data/spec/dummy/public/422.html +0 -67
  91. data/spec/dummy/public/500.html +0 -66
  92. data/spec/dummy/public/favicon.ico +0 -0
  93. data/spec/fixtures/fanout_series.json +0 -23
  94. data/spec/metrics/fanout_spec.rb +0 -61
  95. data/spec/model/testo_spec.rb +0 -27
@@ -1,177 +1,203 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Influxer::Metrics do
4
- before do
5
- allow_any_instance_of(Influxer::Client).to receive(:query) do |_, sql|
6
- sql
7
- end
3
+ describe Influxer::Metrics, :query do
4
+ let(:metrics) { described_class.new }
5
+ let(:dummy_metrics) { DummyMetrics.new dummy_id: 1, user_id: 1 }
6
+
7
+ subject { metrics }
8
+
9
+ context "class methods" do
10
+ subject { described_class }
11
+
12
+ specify { is_expected.to respond_to :attributes }
13
+ specify { is_expected.to respond_to :set_series }
14
+ specify { is_expected.to respond_to :series }
15
+ specify { is_expected.to respond_to :write }
16
+ specify { is_expected.to respond_to :write! }
17
+
18
+ specify { is_expected.to respond_to :all }
19
+ specify { is_expected.to respond_to :where }
20
+ specify { is_expected.to respond_to :offset }
21
+ specify { is_expected.to respond_to :time }
22
+ specify { is_expected.to respond_to :past }
23
+ specify { is_expected.to respond_to :since }
24
+ specify { is_expected.to respond_to :limit }
25
+ specify { is_expected.to respond_to :select }
26
+ specify { is_expected.to respond_to :delete_all }
8
27
  end
9
28
 
10
- let(:metrics) { Influxer::Metrics.new }
11
- let(:metrics_class) { Influxer::Metrics }
12
-
13
- # class methods
14
-
15
- specify { expect(metrics_class).to respond_to :attributes }
16
- specify { expect(metrics_class).to respond_to :set_series }
17
- specify { expect(metrics_class).to respond_to :series }
18
-
19
- # query methods
20
-
21
- specify { expect(metrics_class).to respond_to :all }
22
- specify { expect(metrics_class).to respond_to :where }
23
- specify { expect(metrics_class).to respond_to :merge }
24
- specify { expect(metrics_class).to respond_to :time }
25
- specify { expect(metrics_class).to respond_to :past }
26
- specify { expect(metrics_class).to respond_to :since }
27
- specify { expect(metrics_class).to respond_to :limit }
28
- specify { expect(metrics_class).to respond_to :select }
29
- specify { expect(metrics_class).to respond_to :delete_all }
30
-
31
- # instance methods
32
-
33
- specify { expect(metrics).to respond_to :write }
34
- specify { expect(metrics).to respond_to :write! }
35
- specify { expect(metrics).to respond_to :persisted? }
36
- specify { expect(metrics).to respond_to :series }
37
-
38
- # ActiveModel::Validations
39
-
40
- specify { expect(metrics).to respond_to :valid? }
41
- specify { expect(metrics).to respond_to :invalid? }
42
- specify { expect(metrics).to respond_to :errors }
43
-
44
- describe "instance methods" do
45
- let(:dummy_metrics) { DummyMetrics.new dummy_id: 1, user_id: 1 }
29
+ context "instance methods" do
30
+ specify { is_expected.to respond_to :write }
31
+ specify { is_expected.to respond_to :write! }
32
+ specify { is_expected.to respond_to :persisted? }
33
+ specify { is_expected.to respond_to :series }
34
+ specify { is_expected.to be_a ActiveModel::Model }
35
+ end
46
36
 
47
- describe "#write" do
48
- context "Validations" do
49
- it "should not write if required attribute is missing" do
50
- m = DummyMetrics.new(dummy_id: 1)
51
- expect(m.write).to be false
52
- expect(m.errors.size).to eq(1)
53
- end
37
+ describe "#write" do
38
+ it "doesn't write if required attribute is missing" do
39
+ m = DummyMetrics.new(dummy_id: 1)
40
+ expect(client).not_to receive(:write_point)
41
+ expect(m.write).to be false
42
+ expect(m.errors.size).to eq(1)
43
+ end
54
44
 
55
- it "should raise error if required attribute is missing" do
56
- expect { DummyMetrics.new(user_id: 1).write! }.to raise_error(StandardError)
57
- end
45
+ it "raises error if required attribute is missing" do
46
+ expect { DummyMetrics.new(user_id: 1).write! }.to raise_error(Influxer::MetricsInvalid)
47
+ end
58
48
 
59
- it "should raise error if you want to write twice" do
60
- expect(dummy_metrics.write).to be_truthy
61
- expect { dummy_metrics.write! }.to raise_error(StandardError)
62
- end
63
- end
49
+ it "raises error if you want to write twice" do
50
+ expect(dummy_metrics.write).to be_truthy
51
+ expect { dummy_metrics.write! }.to raise_error(Influxer::MetricsError)
52
+ end
64
53
 
65
- it "should write successfully when all required attributes are set" do
66
- expect(Influxer.client).to receive(:write_point).with("dummy", anything)
67
- expect(dummy_metrics.write).to be_truthy
68
- expect(dummy_metrics.persisted?).to be_truthy
69
- end
54
+ it "writes successfully" do
55
+ expect(client).to receive(:write_point).with("dummy", anything)
56
+ expect(dummy_metrics.write).to be_truthy
57
+ expect(dummy_metrics.persisted?).to be_truthy
70
58
  end
71
59
 
72
- context "active_model callbacks" do
73
- it "should set current time" do
74
- Timecop.freeze(Time.local(2014, 10, 10, 10, 10, 10)) do
75
- dummy_metrics.write!
76
- end
77
- expect(dummy_metrics.time).to eq Time.local(2014, 10, 10, 10, 10, 10)
60
+ context "after_write callback" do
61
+ it "sets current time" do
62
+ Timecop.freeze(Time.local(2015))
63
+ dummy_metrics.write!
64
+ expect(dummy_metrics.time).to eq Time.local(2015)
78
65
  end
79
66
  end
80
67
  end
81
68
 
82
- describe "class methods" do
69
+ describe "#series" do
83
70
  let(:dummy_metrics) do
84
- Class.new(Influxer::Metrics) do
71
+ Class.new(described_class) do
85
72
  set_series :dummies
86
73
  attributes :user_id, :dummy_id
87
74
  end
88
75
  end
89
76
 
90
77
  let(:dummy_metrics_2) do
91
- Class.new(Influxer::Metrics) do
78
+ Class.new(described_class) do
92
79
  set_series "dummy \"A\""
93
80
  end
94
81
  end
95
82
 
96
83
  let(:dummy_metrics_3) do
97
- Class.new(Influxer::Metrics) do
84
+ Class.new(described_class) do
98
85
  set_series /^.*$/
99
86
  end
100
87
  end
101
88
 
102
89
  let(:dummy_with_2_series) do
103
- Class.new(Influxer::Metrics) do
90
+ Class.new(described_class) do
104
91
  set_series :events, :errors
105
92
  end
106
93
  end
107
94
 
108
95
  let(:dummy_with_2_series_quoted) do
109
- Class.new(Influxer::Metrics) do
96
+ Class.new(described_class) do
110
97
  set_series "dummy \"A\"", "dummy \"B\""
111
98
  end
112
99
  end
113
100
 
114
101
  let(:dummy_with_proc_series) do
115
- Class.new(Influxer::Metrics) do
102
+ Class.new(described_class) do
116
103
  attributes :user_id, :test_id
117
104
  set_series ->(metrics) { "test/#{metrics.test_id}/user/#{metrics.user_id}" }
118
105
  end
119
106
  end
120
107
 
121
- describe "set_series" do
122
- it "should set series name from class name by default" do
123
- expect(DummyMetrics.new.series).to eq "\"dummy\""
124
- end
108
+ it "sets series name from class name by default" do
109
+ expect(DummyMetrics.new.series).to eq "\"dummy\""
110
+ end
125
111
 
126
- it "should set series from subclass" do
127
- expect(dummy_metrics.new.series).to eq "\"dummies\""
128
- end
112
+ it "sets series from subclass" do
113
+ expect(dummy_metrics.new.series).to eq "\"dummies\""
114
+ end
129
115
 
130
- it "should set series as regexp" do
131
- expect(dummy_metrics_3.new.series).to eq '/^.*$/'
132
- end
116
+ it "sets series as regexp" do
117
+ expect(dummy_metrics_3.new.series).to eq '/^.*$/'
118
+ end
133
119
 
134
- it "should set series with quotes" do
135
- expect(dummy_metrics_2.new.series).to eq "\"dummy \\\"A\\\"\""
136
- end
120
+ it "quotes series" do
121
+ expect(dummy_metrics_2.new.series).to eq "\"dummy \\\"A\\\"\""
122
+ end
137
123
 
138
- it "should set several series" do
139
- expect(dummy_with_2_series.new.series).to eq "merge(\"events\",\"errors\")"
140
- end
124
+ it "set several series" do
125
+ expect(dummy_with_2_series.new.series).to eq "merge(\"events\",\"errors\")"
126
+ end
141
127
 
142
- it "should set several series with quotes" do
143
- expect(dummy_with_2_series_quoted.new.series)
144
- .to eq "merge(\"dummy \\\"A\\\"\",\"dummy \\\"B\\\"\")"
145
- end
128
+ it "quotes several series" do
129
+ expect(dummy_with_2_series_quoted.new.series)
130
+ .to eq "merge(\"dummy \\\"A\\\"\",\"dummy \\\"B\\\"\")"
131
+ end
146
132
 
147
- it "should set series from proc" do
148
- expect(dummy_with_proc_series.series).to be_an_instance_of Proc
133
+ it "sets series from proc" do
134
+ expect(dummy_with_proc_series.series).to be_an_instance_of Proc
135
+
136
+ m = dummy_with_proc_series.new user_id: 2, test_id: 123
137
+ expect(m.series).to eq "\"test/123/user/2\""
138
+ end
139
+ end
149
140
 
150
- m = dummy_with_proc_series.new user_id: 2, test_id: 123
151
- expect(m.series).to eq "\"test/123/user/2\""
141
+ describe ".tags" do
142
+ let(:dummy1) { Class.new(DummyMetrics) }
143
+ let!(:dummy2) do
144
+ Class.new(dummy1) do
145
+ tags :zone
152
146
  end
153
147
  end
154
148
 
155
- describe "write method" do
156
- it "should write data and return point" do
157
- expect(Influxer.client)
158
- .to receive(:write_point).with("dummies", user_id: 1, dummy_id: 2)
149
+ it "inherits tags" do
150
+ expect(dummy2.tag_names).to include('dummy_id', 'host', 'zone')
151
+ end
159
152
 
160
- point = dummy_metrics.write(user_id: 1, dummy_id: 2)
161
- expect(point.persisted?).to be_truthy
162
- expect(point.user_id).to eq 1
163
- expect(point.dummy_id).to eq 2
164
- end
153
+ it "clones tags" do
154
+ dummy1.tags :status
155
+ expect(dummy1.tag_names).to include('status')
156
+ expect(dummy2.tag_names).not_to include('status')
157
+ end
158
+ end
165
159
 
166
- it "should not write data and return false" do
167
- expect(DummyMetrics.write(dummy_id: 2)).to be false
168
- end
160
+ describe "#dup" do
161
+ let(:point) { DummyMetrics.new(user_id: 1, dummy_id: 2) }
162
+ subject { point.dup }
163
+
164
+ specify { expect(subject.user_id).to eq 1 }
165
+ specify { expect(subject.dummy_id).to eq 2 }
166
+
167
+ context "dup is not persisted" do
168
+ before { point.write }
169
+ specify { expect(subject.persisted?).to be_falsey }
169
170
  end
171
+ end
170
172
 
171
- describe "all method" do
172
- it "should respond with relation" do
173
- expect(metrics_class.all).to be_an_instance_of Influxer::Relation
173
+ describe ".write" do
174
+ let(:dummy_metrics) do
175
+ Class.new(described_class) do
176
+ set_series :dummies
177
+ tags :dummy_id, :host
178
+ attributes :user_id
174
179
  end
175
180
  end
181
+
182
+ it "write data and return point" do
183
+ expect(client)
184
+ .to receive(:write_point).with("dummies", tags: { dummy_id: 2, host: 'test' }, values: { user_id: 1 })
185
+
186
+ point = dummy_metrics.write(user_id: 1, dummy_id: 2, host: 'test')
187
+ expect(point.persisted?).to be_truthy
188
+ expect(point.user_id).to eq 1
189
+ expect(point.dummy_id).to eq 2
190
+ end
191
+
192
+ it "doesn't write data and return false if invalid" do
193
+ expect(client).not_to receive(:write_point)
194
+ expect(DummyMetrics.write(dummy_id: 2)).to be false
195
+ end
196
+ end
197
+
198
+ describe ".all" do
199
+ it "responds with relation" do
200
+ expect(described_class.all).to be_a Influxer::Relation
201
+ end
176
202
  end
177
203
  end