influxer 1.1.3 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +92 -0
  3. data/{MIT-LICENSE → LICENSE.txt} +1 -1
  4. data/README.md +106 -47
  5. data/lib/influxer.rb +10 -9
  6. data/lib/influxer/client.rb +1 -1
  7. data/lib/influxer/config.rb +5 -4
  8. data/lib/influxer/engine.rb +1 -1
  9. data/lib/influxer/metrics/active_model3/model.rb +2 -4
  10. data/lib/influxer/metrics/metrics.rb +10 -13
  11. data/lib/influxer/metrics/quoting/timestamp.rb +52 -9
  12. data/lib/influxer/metrics/relation.rb +33 -19
  13. data/lib/influxer/metrics/relation/calculations.rb +1 -1
  14. data/lib/influxer/metrics/relation/time_query.rb +15 -13
  15. data/lib/influxer/metrics/relation/where_clause.rb +19 -11
  16. data/lib/influxer/metrics/scoping.rb +4 -4
  17. data/lib/influxer/metrics/scoping/current_scope.rb +2 -1
  18. data/lib/influxer/metrics/scoping/default.rb +1 -1
  19. data/lib/influxer/metrics/scoping/named.rb +2 -1
  20. data/lib/influxer/model.rb +4 -9
  21. data/lib/influxer/rails/client.rb +2 -2
  22. data/lib/influxer/version.rb +1 -1
  23. metadata +30 -95
  24. data/.gitignore +0 -37
  25. data/.rspec +0 -2
  26. data/.rubocop.yml +0 -77
  27. data/.travis.yml +0 -10
  28. data/Changelog.md +0 -103
  29. data/Gemfile +0 -10
  30. data/Rakefile +0 -13
  31. data/gemfiles/rails32.gemfile +0 -7
  32. data/gemfiles/rails42.gemfile +0 -7
  33. data/gemfiles/rails5.gemfile +0 -7
  34. data/influxer.gemspec +0 -33
  35. data/spec/cases/points_spec.rb +0 -36
  36. data/spec/cases/write_points_spec.rb +0 -85
  37. data/spec/client_spec.rb +0 -46
  38. data/spec/fixtures/empty_result.json +0 -21
  39. data/spec/fixtures/single_series.json +0 -29
  40. data/spec/metrics/metrics_spec.rb +0 -283
  41. data/spec/metrics/relation_spec.rb +0 -425
  42. data/spec/metrics/scoping_spec.rb +0 -66
  43. data/spec/model/user_spec.rb +0 -46
  44. data/spec/spec_helper.rb +0 -64
  45. data/spec/support/metrics/action_metrics.rb +0 -5
  46. data/spec/support/metrics/custom_metrics.rb +0 -6
  47. data/spec/support/metrics/dummy_metrics.rb +0 -12
  48. data/spec/support/metrics/user_metrics.rb +0 -6
  49. data/spec/support/metrics/visits_metrics.rb +0 -8
  50. data/spec/support/shared_contexts/shared_query.rb +0 -16
  51. data/spec/support/user.rb +0 -16
@@ -1,21 +0,0 @@
1
- {
2
- "results": [
3
- {
4
- "series": [
5
- {
6
- "name": "cpu_load_short",
7
- "tags": {
8
- "host": "server01",
9
- "region": "us-west"
10
- },
11
- "columns": [
12
- "time",
13
- "value"
14
- ],
15
- "values": [
16
- ]
17
- }
18
- ]
19
- }
20
- ]
21
- }
@@ -1,29 +0,0 @@
1
- {
2
- "results": [
3
- {
4
- "series": [
5
- {
6
- "name": "cpu_load_short",
7
- "tags": {
8
- "host": "server01",
9
- "region": "us-west"
10
- },
11
- "columns": [
12
- "time",
13
- "value"
14
- ],
15
- "values": [
16
- [
17
- "2015-01-29T21:51:28.968422294Z",
18
- 0.64
19
- ],
20
- [
21
- "2015-01-29T21:51:38.968422294Z",
22
- 0.93
23
- ]
24
- ]
25
- }
26
- ]
27
- }
28
- ]
29
- }
@@ -1,283 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Influxer::Metrics, :query do
6
- let(:metrics) { described_class.new }
7
- let(:dummy_metrics) { DummyMetrics.new dummy_id: 1, user_id: 1 }
8
-
9
- subject { metrics }
10
-
11
- context "class methods" do
12
- subject { described_class }
13
-
14
- specify { is_expected.to respond_to :attributes }
15
- specify { is_expected.to respond_to :set_series }
16
- specify { is_expected.to respond_to :set_retention_policy }
17
- specify { is_expected.to respond_to :series }
18
- specify { is_expected.to respond_to :write }
19
- specify { is_expected.to respond_to :write! }
20
-
21
- specify { is_expected.to respond_to :all }
22
- specify { is_expected.to respond_to :where }
23
- specify { is_expected.to respond_to :offset }
24
- specify { is_expected.to respond_to :time }
25
- specify { is_expected.to respond_to :past }
26
- specify { is_expected.to respond_to :since }
27
- specify { is_expected.to respond_to :limit }
28
- specify { is_expected.to respond_to :select }
29
- specify { is_expected.to respond_to :delete_all }
30
- end
31
-
32
- context "instance methods" do
33
- specify { is_expected.to respond_to :write }
34
- specify { is_expected.to respond_to :write! }
35
- specify { is_expected.to respond_to :persisted? }
36
- specify { is_expected.to respond_to :series }
37
- if Influxer.active_model3?
38
- specify { is_expected.to be_a Influxer::ActiveModel3::Model }
39
- else
40
- specify { is_expected.to be_a ActiveModel::Model }
41
- end
42
- end
43
-
44
- describe "#initialize" do
45
- it "assigns initial values in constructor" do
46
- m = DummyMetrics.new(dummy_id: 1)
47
- expect(m.dummy_id).to eq 1
48
- end
49
- end
50
-
51
- describe "#write" do
52
- it "doesn't write if required attribute is missing" do
53
- m = DummyMetrics.new(dummy_id: 1)
54
- expect(client).not_to receive(:write_point)
55
- expect(m.write).to be false
56
- expect(m.errors.size).to eq(1)
57
- end
58
-
59
- it "raises error if required attribute is missing" do
60
- expect { DummyMetrics.new(user_id: 1).write! }.to raise_error(Influxer::MetricsInvalid)
61
- end
62
-
63
- it "raises error if you want to write twice" do
64
- expect(dummy_metrics.write).to be_truthy
65
- expect { dummy_metrics.write! }.to raise_error(Influxer::MetricsError)
66
- end
67
-
68
- it "writes successfully" do
69
- expect(client).to receive(:write_point).with("dummy", anything, nil, nil, nil)
70
- expect(dummy_metrics.write).to be_truthy
71
- expect(dummy_metrics.persisted?).to be_truthy
72
- end
73
-
74
- context "after_write callback" do
75
- it "sets current time" do
76
- Timecop.freeze(Time.local(2015))
77
- dummy_metrics.write!
78
- expect(dummy_metrics.timestamp).to eq Time.local(2015)
79
- end
80
- end
81
- end
82
-
83
- describe "#series" do
84
- let(:dummy_metrics) do
85
- Class.new(described_class) do
86
- set_series :dummies
87
- attributes :user_id, :dummy_id
88
- end
89
- end
90
-
91
- let(:dummy_metrics_2) do
92
- Class.new(described_class) do
93
- set_series "dummy \"A\""
94
- end
95
- end
96
-
97
- let(:dummy_metrics_3) do
98
- Class.new(described_class) do
99
- set_series /^.*$/
100
- end
101
- end
102
-
103
- let(:dummy_with_2_series) do
104
- Class.new(described_class) do
105
- set_series :events, :errors
106
- end
107
- end
108
-
109
- let(:dummy_with_2_series_quoted) do
110
- Class.new(described_class) do
111
- set_series "dummy \"A\"", "dummy \"B\""
112
- end
113
- end
114
-
115
- let(:dummy_with_proc_series) do
116
- Class.new(described_class) do
117
- attributes :user_id, :test_id
118
- set_series ->(metrics) { "test/#{metrics.test_id}/user/#{metrics.user_id}" }
119
- end
120
- end
121
-
122
- it "sets series name from class name by default" do
123
- expect(DummyMetrics.new.series).to eq "\"dummy\""
124
- end
125
-
126
- it "sets series from subclass" do
127
- expect(dummy_metrics.new.series).to eq "\"dummies\""
128
- end
129
-
130
- it "sets series as regexp" do
131
- expect(dummy_metrics_3.new.series).to eq '/^.*$/'
132
- end
133
-
134
- it "quotes series" do
135
- expect(dummy_metrics_2.new.series).to eq "\"dummy \\\"A\\\"\""
136
- end
137
-
138
- it "set several series" do
139
- expect(dummy_with_2_series.new.series).to eq "merge(\"events\",\"errors\")"
140
- end
141
-
142
- it "quotes several series" do
143
- expect(dummy_with_2_series_quoted.new.series)
144
- .to eq "merge(\"dummy \\\"A\\\"\",\"dummy \\\"B\\\"\")"
145
- end
146
-
147
- it "sets series from proc" do
148
- expect(dummy_with_proc_series.series).to be_an_instance_of Proc
149
-
150
- m = dummy_with_proc_series.new user_id: 2, test_id: 123
151
- expect(m.series).to eq "\"test/123/user/2\""
152
- end
153
- end
154
-
155
- describe "#quoted_series" do
156
- context "with retention policy" do
157
- let(:dummy_with_retention_policy) do
158
- Class.new(described_class) do
159
- attributes :user_id, :test_id
160
- set_series :dummies
161
- set_retention_policy :week
162
- end
163
- end
164
-
165
- it "sets retention policy" do
166
- expect(dummy_with_retention_policy.retention_policy).to eq :week
167
- end
168
-
169
- it "sets quoted series with retention policy" do
170
- expect(dummy_with_retention_policy.quoted_series).to eq "\"week\".\"dummies\""
171
- end
172
- end
173
- end
174
-
175
- describe ".tags" do
176
- let(:dummy1) { Class.new(DummyMetrics) }
177
- let!(:dummy2) do
178
- Class.new(dummy1) do
179
- tags :zone
180
- end
181
- end
182
-
183
- it "inherits tags" do
184
- expect(dummy2.tag_names).to include('dummy_id', 'host', 'zone')
185
- end
186
-
187
- it "clones tags" do
188
- dummy1.tags :status
189
- expect(dummy1.tag_names).to include('status')
190
- expect(dummy2.tag_names).not_to include('status')
191
- end
192
- end
193
-
194
- describe "#dup" do
195
- let(:point) { DummyMetrics.new(user_id: 1, dummy_id: 2) }
196
- subject { point.dup }
197
-
198
- specify { expect(subject.user_id).to eq 1 }
199
- specify { expect(subject.dummy_id).to eq 2 }
200
-
201
- context "dup is not persisted" do
202
- before { point.write }
203
- specify { expect(subject.persisted?).to be_falsey }
204
- end
205
- end
206
-
207
- describe ".write" do
208
- let(:dummy_metrics) do
209
- Class.new(described_class) do
210
- set_series :dummies
211
- tags :dummy_id, :host
212
- attributes :user_id
213
- end
214
- end
215
-
216
- it "write data and return point" do
217
- expect(client)
218
- .to receive(:write_point).with(
219
- "dummies",
220
- { tags: { dummy_id: 2, host: 'test' }, values: { user_id: 1 }, timestamp: nil },
221
- nil,
222
- nil,
223
- nil
224
- )
225
-
226
- point = dummy_metrics.write(user_id: 1, dummy_id: 2, host: 'test')
227
- expect(point.persisted?).to be_truthy
228
- expect(point.user_id).to eq 1
229
- expect(point.dummy_id).to eq 2
230
- end
231
-
232
- it "test write data with time and return point" do
233
- timestamp_test = Time.now
234
- expected_time = (timestamp_test.to_r * 1_000_000_000).to_i
235
-
236
- expect(client)
237
- .to receive(:write_point).with(
238
- "dummies",
239
- { tags: { dummy_id: 2, host: 'test' }, values: { user_id: 1 }, timestamp: expected_time },
240
- nil,
241
- nil,
242
- nil
243
- )
244
-
245
- point = dummy_metrics.write(user_id: 1, dummy_id: 2, host: 'test', timestamp: timestamp_test)
246
- expect(point.persisted?).to be_truthy
247
- expect(point.user_id).to eq 1
248
- expect(point.dummy_id).to eq 2
249
- expect(point.timestamp).to eq timestamp_test
250
- end
251
-
252
- it "test write data with string time" do
253
- base_time = Time.now
254
- timestamp_test = base_time.to_s
255
-
256
- expect(client)
257
- .to receive(:write_point).with(
258
- "dummies",
259
- { tags: { dummy_id: 2, host: 'test' }, values: { user_id: 1 }, timestamp: (base_time.to_i * 1_000_000_000).to_i },
260
- nil,
261
- nil,
262
- nil
263
- )
264
-
265
- point = dummy_metrics.write(user_id: 1, dummy_id: 2, host: 'test', timestamp: timestamp_test)
266
- expect(point.persisted?).to be_truthy
267
- expect(point.user_id).to eq 1
268
- expect(point.dummy_id).to eq 2
269
- expect(point.timestamp).to eq(timestamp_test)
270
- end
271
-
272
- it "doesn't write data and return false if invalid" do
273
- expect(client).not_to receive(:write_point)
274
- expect(DummyMetrics.write(dummy_id: 2)).to be false
275
- end
276
- end
277
-
278
- describe ".all" do
279
- it "responds with relation" do
280
- expect(described_class.all).to be_a Influxer::Relation
281
- end
282
- end
283
- end
@@ -1,425 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Influxer::Relation, :query do
6
- let(:rel) { Influxer::Relation.new DummyMetrics }
7
- let(:rel2) { Influxer::Relation.new DummyComplexMetrics }
8
-
9
- context "instance methods" do
10
- subject { rel }
11
-
12
- specify { is_expected.to respond_to :write }
13
- specify { is_expected.to respond_to :select }
14
- specify { is_expected.to respond_to :where }
15
- specify { is_expected.to respond_to :limit }
16
- specify { is_expected.to respond_to :group }
17
- specify { is_expected.to respond_to :delete_all }
18
- specify { is_expected.to respond_to :to_sql }
19
- end
20
-
21
- describe "#build" do
22
- specify { expect(rel.build).to be_a DummyMetrics }
23
- specify { expect(rel.new).to be_a DummyMetrics }
24
- end
25
-
26
- describe "#merge!" do
27
- it "merge multi values" do
28
- r1 = rel.where(id: [1, 2], dummy: 'qwe').time(:hour)
29
- r2 = Influxer::Relation.new(DummyMetrics).where.not(user_id: 0).group(:user_id).order(user_id: :asc)
30
- r1.merge!(r2)
31
- expect(r1.to_sql)
32
- .to eq "select * from \"dummy\" where (id = 1 or id = 2) and (dummy = 'qwe') and (user_id <> 0) " \
33
- "group by time(1h), user_id order by user_id asc"
34
- end
35
-
36
- it "merge single values" do
37
- r1 = rel.time(:hour, fill: 0).slimit(10)
38
- r2 = Influxer::Relation.new(DummyMetrics).group(:dummy_id).offset(10).slimit(5)
39
- r1.merge!(r2)
40
- expect(r1.to_sql).to eq "select * from \"dummy\" group by time(1h), dummy_id fill(0) offset 10 slimit 5"
41
- end
42
- end
43
-
44
- context "sql generation" do
45
- describe "#from" do
46
- it "generates valid from if no conditions" do
47
- expect(rel.to_sql).to eq "select * from \"dummy\""
48
- end
49
-
50
- it "generates sql using custom from clause" do
51
- expect(rel.from(:doomy).to_sql).to eq "select * from \"doomy\""
52
- end
53
- end
54
-
55
- describe "#select" do
56
- it "select array of symbols" do
57
- expect(rel.select(:user_id, :dummy_id).to_sql).to eq "select user_id, dummy_id from \"dummy\""
58
- end
59
-
60
- it "select string" do
61
- expect(rel.select("count(user_id)").to_sql).to eq "select count(user_id) from \"dummy\""
62
- end
63
-
64
- it "select expression" do
65
- expect(rel.select("(value + 6) / 10").to_sql).to eq "select (value + 6) / 10 from \"dummy\""
66
- end
67
- end
68
-
69
- describe "#where" do
70
- it "generate valid conditions from hash" do
71
- Timecop.freeze(Time.now)
72
- expect(rel.where(user_id: 1, dummy: 'q', timer: Time.now).to_sql).to eq "select * from \"dummy\" where (user_id = 1) and (dummy = 'q') and (timer = #{(Time.now.to_r * 1_000_000_000).to_i})"
73
- end
74
-
75
- it "generate valid conditions from strings" do
76
- expect(rel.where("time > now() - 1d").to_sql).to eq "select * from \"dummy\" where (time > now() - 1d)"
77
- end
78
-
79
- it "handle regexps" do
80
- expect(rel.where(user_id: 1, dummy: /^du.*/).to_sql).to eq "select * from \"dummy\" where (user_id = 1) and (dummy =~ /^du.*/)"
81
- end
82
-
83
- it "handle dates" do
84
- expect(rel.where(timer: Date.new(2015)).to_sql).to eq "select * from \"dummy\" where (timer = #{(Date.new(2015).to_time.to_r * 1_000_000_000).to_i})"
85
- end
86
-
87
- it "handle date times" do
88
- expect(rel.where(timer: DateTime.new(2015)).to_sql).to eq "select * from \"dummy\" where (timer = #{(DateTime.new(2015).to_time.to_r * 1_000_000_000).to_i})"
89
- end
90
-
91
- it "handle inclusive ranges" do
92
- expect(rel.where(user_id: 1..4).to_sql).to eq "select * from \"dummy\" where (user_id >= 1 and user_id <= 4)"
93
- end
94
-
95
- it "handle exclusive range" do
96
- expect(rel.where(user_id: 1...4).to_sql).to eq "select * from \"dummy\" where (user_id >= 1 and user_id < 4)"
97
- end
98
-
99
- it "handle arrays" do
100
- expect(rel.where(user_id: [1, 2, 3]).to_sql).to eq "select * from \"dummy\" where (user_id = 1 or user_id = 2 or user_id = 3)"
101
- end
102
-
103
- it "handle empty arrays", :aggregate_failures do
104
- expect(rel.where(user_id: []).to_sql).to eq "select * from \"dummy\" where (1 = 0)"
105
- expect(rel.to_a).to eq []
106
- end
107
-
108
- context "with tags" do
109
- it "integer tag values" do
110
- expect(rel.where(dummy_id: 10).to_sql).to eq "select * from \"dummy\" where (dummy_id = '10')"
111
- end
112
-
113
- it "array tag values" do
114
- expect(rel.where(dummy_id: [10, 'some']).to_sql).to eq "select * from \"dummy\" where (dummy_id = '10' or dummy_id = 'some')"
115
- end
116
-
117
- it "nil value" do
118
- expect(rel.where(dummy_id: nil).to_sql).to eq "select * from \"dummy\" where (dummy_id !~ /.*/)"
119
- end
120
- end
121
- end
122
-
123
- describe "#not" do
124
- it "negate simple values" do
125
- expect(rel.where.not(user_id: 1, dummy: :a).to_sql).to eq "select * from \"dummy\" where (user_id <> 1) and (dummy <> 'a')"
126
- end
127
-
128
- it "handle regexp" do
129
- expect(rel.where.not(user_id: 1, dummy: /^du.*/).to_sql).to eq "select * from \"dummy\" where (user_id <> 1) and (dummy !~ /^du.*/)"
130
- end
131
-
132
- it "handle inclusive ranges" do
133
- expect(rel.where.not(user_id: 1..4).to_sql).to eq "select * from \"dummy\" where (user_id < 1 or user_id > 4)"
134
- end
135
-
136
- it "handle exclusive ranges" do
137
- expect(rel.where.not(user_id: 1...4).to_sql).to eq "select * from \"dummy\" where (user_id < 1 or user_id >= 4)"
138
- end
139
-
140
- it "handle arrays" do
141
- expect(rel.where.not(user_id: [1, 2, 3]).to_sql).to eq "select * from \"dummy\" where (user_id <> 1 and user_id <> 2 and user_id <> 3)"
142
- end
143
-
144
- it "handle empty arrays", :aggregate_failures do
145
- expect(rel.where.not(user_id: []).to_sql).to eq "select * from \"dummy\" where (1 = 0)"
146
- expect(rel.to_a).to eq []
147
- end
148
-
149
- context "with tags" do
150
- it "nil value" do
151
- expect(rel.not(dummy_id: nil).to_sql).to eq "select * from \"dummy\" where (dummy_id =~ /.*/)"
152
- end
153
- end
154
- end
155
-
156
- describe "#none" do
157
- it "returns empty array", :aggregate_failures do
158
- expect(rel.none.to_sql).to eq "select * from \"dummy\" where (1 = 0)"
159
- expect(rel.to_a).to eq []
160
- end
161
-
162
- it "works with chaining", :aggregate_failures do
163
- expect(rel.none.where.not(user_id: 1, dummy: :a).to_sql)
164
- .to eq "select * from \"dummy\" where (1 = 0) and (user_id <> 1) and (dummy <> 'a')"
165
- expect(rel.to_a).to eq []
166
- end
167
- end
168
-
169
- describe "#past" do
170
- it "work with predefined symbols" do
171
- expect(rel.past(:hour).to_sql).to eq "select * from \"dummy\" where (time > now() - 1h)"
172
- end
173
-
174
- it "work with any symbols" do
175
- expect(rel.past(:s).to_sql).to eq "select * from \"dummy\" where (time > now() - 1s)"
176
- end
177
-
178
- it "work with strings" do
179
- expect(rel.past("3d").to_sql).to eq "select * from \"dummy\" where (time > now() - 3d)"
180
- end
181
-
182
- it "work with numbers" do
183
- expect(rel.past(1.day).to_sql).to eq "select * from \"dummy\" where (time > now() - 86400s)"
184
- end
185
- end
186
-
187
- describe "#since" do
188
- it "work with datetime" do
189
- expect(rel.since(Time.utc(2014, 12, 31)).to_sql).to eq "select * from \"dummy\" where (time > 1419984000s)"
190
- end
191
- end
192
-
193
- describe "#group" do
194
- it "generate valid groups" do
195
- expect(rel.group(:user_id, "time(1m) fill(0)").to_sql).to eq "select * from \"dummy\" group by user_id, time(1m) fill(0)"
196
- end
197
-
198
- context "group by time predefined values" do
199
- it "group by hour" do
200
- expect(rel.time(:hour).to_sql).to eq "select * from \"dummy\" group by time(1h)"
201
- end
202
-
203
- it "group by minute" do
204
- expect(rel.time(:minute).to_sql).to eq "select * from \"dummy\" group by time(1m)"
205
- end
206
-
207
- it "group by second" do
208
- expect(rel.time(:second).to_sql).to eq "select * from \"dummy\" group by time(1s)"
209
- end
210
-
211
- it "group by millisecond" do
212
- expect(rel.time(:ms).to_sql).to eq "select * from \"dummy\" group by time(1ms)"
213
- end
214
-
215
- it "group by microsecond" do
216
- expect(rel.time(:u).to_sql).to eq "select * from \"dummy\" group by time(1u)"
217
- end
218
-
219
- it "group by day" do
220
- expect(rel.time(:day).to_sql).to eq "select * from \"dummy\" group by time(1d)"
221
- end
222
-
223
- it "group by week" do
224
- expect(rel.time(:week).to_sql).to eq "select * from \"dummy\" group by time(1w)"
225
- end
226
-
227
- it "group by month" do
228
- expect(rel.time(:month).to_sql).to eq "select * from \"dummy\" group by time(30d)"
229
- end
230
-
231
- it "group by hour and fill" do
232
- expect(rel.time(:month, fill: 0).to_sql).to eq "select * from \"dummy\" group by time(30d) fill(0)"
233
- end
234
- end
235
-
236
- it "group by time with string value" do
237
- expect(rel.time("4d").to_sql).to eq "select * from \"dummy\" group by time(4d)"
238
- end
239
-
240
- %w[null previous none].each do |val|
241
- it "group by time with string value and fill #{val}" do
242
- expect(rel.time("4d", fill: val.to_sym).to_sql).to eq "select * from \"dummy\" group by time(4d) fill(#{val})"
243
- end
244
- end
245
-
246
- it "group by time and other fields with fill zero" do
247
- expect(rel.time("4d", fill: 0).group(:dummy_id).to_sql).to eq "select * from \"dummy\" group by time(4d), dummy_id fill(0)"
248
- end
249
-
250
- it "group by time and other fields with fill negative" do
251
- expect(rel.time("4d", fill: -1).group(:dummy_id).to_sql).to eq "select * from \"dummy\" group by time(4d), dummy_id fill(-1)"
252
- end
253
- end
254
-
255
- describe "#order" do
256
- it "generate valid order" do
257
- expect(rel.order(time_spent: :asc).to_sql).to eq "select * from \"dummy\" order by time_spent asc"
258
- end
259
-
260
- it "generate order from string" do
261
- expect(rel.order('cpu desc, val asc').to_sql).to eq "select * from \"dummy\" order by cpu desc, val asc"
262
- end
263
- end
264
-
265
- describe "#limit" do
266
- it "generate valid limit" do
267
- expect(rel.limit(100).to_sql).to eq "select * from \"dummy\" limit 100"
268
- end
269
- end
270
-
271
- describe "#slimit" do
272
- it "generate valid slimit" do
273
- expect(rel.slimit(100).to_sql).to eq "select * from \"dummy\" slimit 100"
274
- end
275
- end
276
-
277
- describe "#offset" do
278
- it "generate valid offset" do
279
- expect(rel.limit(100).offset(10).to_sql).to eq "select * from \"dummy\" limit 100 offset 10"
280
- end
281
- end
282
-
283
- describe "#soffset" do
284
- it "generate valid soffset" do
285
- expect(rel.soffset(10).to_sql).to eq "select * from \"dummy\" soffset 10"
286
- end
287
- end
288
-
289
- context "calculations" do
290
- context "one arg calculation methods" do
291
- [
292
- :count, :min, :max, :mean,
293
- :mode, :median, :distinct, :derivative,
294
- :stddev, :sum, :first, :last
295
- ].each do |method|
296
- describe "##{method}" do
297
- specify do
298
- expect(rel.where(user_id: 1).calc(method, :column_name).to_sql)
299
- .to eq "select #{method}(column_name) from \"dummy\" where (user_id = 1)"
300
- end
301
- end
302
- end
303
- end
304
-
305
- context "with aliases" do
306
- it "select count as alias" do
307
- expect(rel.count(:val, 'total').to_sql).to eq "select count(val) as total from \"dummy\""
308
- end
309
-
310
- it "select percentile as alias" do
311
- expect(rel.percentile(:val, 90, 'p1').to_sql).to eq "select percentile(val, 90) as p1 from \"dummy\""
312
- end
313
- end
314
- end
315
-
316
- context "complex queries" do
317
- it "group + where" do
318
- expect(rel.count('user_id').group(:traffic_source).fill(0).where(user_id: 123).past('28d').to_sql)
319
- .to eq "select count(user_id) from \"dummy\" where (user_id = 123) and (time > now() - 28d) " \
320
- "group by traffic_source fill(0)"
321
- end
322
-
323
- it "where + group + order + limit" do
324
- expect(rel.group(:user_id).where(account_id: 123).order(account_id: :desc).limit(10).offset(10).to_sql)
325
- .to eq "select * from \"dummy\" where (account_id = 123) group by user_id " \
326
- "order by account_id desc limit 10 offset 10"
327
- end
328
-
329
- it "offset + slimit" do
330
- expect(rel.where(account_id: 123).slimit(10).offset(10).to_sql)
331
- .to eq "select * from \"dummy\" where (account_id = 123) " \
332
- "offset 10 slimit 10"
333
- end
334
- end
335
- end
336
-
337
- describe "#empty?" do
338
- it "return false if has points" do
339
- allow(client).to receive(:query) { [{ "values" => [{ time: 1, id: 2 }] }] }
340
- expect(rel.empty?).to be_falsey
341
- expect(rel.present?).to be_truthy
342
- end
343
-
344
- it "return true if no points" do
345
- allow(client).to receive(:query) { [] }
346
- expect(rel.empty?).to be_truthy
347
- expect(rel.present?).to be_falsey
348
- end
349
- end
350
-
351
- describe "#delete_all" do
352
- it "client expects to execute query method" do
353
- expected_query = "drop series from \"dummy\""
354
-
355
- expect(client)
356
- .to receive(:query).with(expected_query)
357
-
358
- rel.delete_all
359
- end
360
-
361
- it "without tags" do
362
- expect(rel.delete_all)
363
- .to eq "drop series from \"dummy\""
364
- end
365
-
366
- it "with tags" do
367
- expect(rel.where(dummy_id: 1, host: 'eu').delete_all)
368
- .to eq "drop series from \"dummy\" where (dummy_id = '1') and (host = 'eu')"
369
- end
370
- end
371
-
372
- describe "#inspect" do
373
- it "return correct String represantation of empty relation" do
374
- allow(rel).to receive(:to_a) { [] }
375
- expect(rel.inspect).to eq "#<Influxer::Relation []>"
376
- end
377
-
378
- it "return correct String represantation of non-empty relation" do
379
- allow(rel).to receive(:to_a) { [1, 2, 3] }
380
- expect(rel.inspect).to eq "#<Influxer::Relation [1, 2, 3]>"
381
- end
382
-
383
- it "return correct String represantation of non-empty large (>11) relation" do
384
- allow(rel).to receive(:to_a) { [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] }
385
- expect(rel.inspect).to eq "#<Influxer::Relation [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]>"
386
- end
387
- end
388
-
389
- describe "#epoch" do
390
- it "format :h" do
391
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :h).and_return []
392
- DummyMetrics.epoch(:h).all.to_a
393
- end
394
-
395
- it "format :m" do
396
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :m).and_return []
397
- DummyMetrics.epoch(:m).all.to_a
398
- end
399
-
400
- it "format :s" do
401
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :s).and_return []
402
- DummyMetrics.epoch(:s).all.to_a
403
- end
404
-
405
- it "format :ms" do
406
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :ms).and_return []
407
- DummyMetrics.epoch(:ms).all.to_a
408
- end
409
-
410
- it "format :u" do
411
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :u).and_return []
412
- DummyMetrics.epoch(:u).all.to_a
413
- end
414
-
415
- it "format :ns" do
416
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :ns).and_return []
417
- DummyMetrics.epoch(:ns).all.to_a
418
- end
419
-
420
- it "invalid epoch format" do
421
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: nil).and_return []
422
- DummyMetrics.epoch(:invalid).all.to_a
423
- end
424
- end
425
- end