influxer 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/{Changelog.md → CHANGELOG.md} +9 -50
  3. data/{MIT-LICENSE → LICENSE.txt} +1 -1
  4. data/README.md +83 -24
  5. data/lib/influxer/metrics/metrics.rb +1 -1
  6. data/lib/influxer/metrics/relation.rb +15 -3
  7. data/lib/influxer/metrics/relation/where_clause.rb +6 -0
  8. data/lib/influxer/rails/client.rb +1 -1
  9. data/lib/influxer/version.rb +1 -1
  10. metadata +8 -38
  11. data/.gitignore +0 -37
  12. data/.rspec +0 -2
  13. data/.rubocop.yml +0 -57
  14. data/.travis.yml +0 -15
  15. data/Gemfile +0 -13
  16. data/Rakefile +0 -15
  17. data/bin/console +0 -8
  18. data/gemfiles/rails32.gemfile +0 -7
  19. data/gemfiles/rails42.gemfile +0 -7
  20. data/gemfiles/rails5.gemfile +0 -7
  21. data/gemfiles/rails6.gemfile +0 -7
  22. data/influxer.gemspec +0 -40
  23. data/spec/cases/points_spec.rb +0 -36
  24. data/spec/cases/write_points_spec.rb +0 -85
  25. data/spec/client_spec.rb +0 -46
  26. data/spec/fixtures/empty_result.json +0 -21
  27. data/spec/fixtures/single_series.json +0 -29
  28. data/spec/metrics/metrics_spec.rb +0 -329
  29. data/spec/metrics/relation_spec.rb +0 -467
  30. data/spec/metrics/scoping_spec.rb +0 -66
  31. data/spec/model/user_spec.rb +0 -46
  32. data/spec/spec_helper.rb +0 -63
  33. data/spec/support/metrics/action_metrics.rb +0 -5
  34. data/spec/support/metrics/custom_metrics.rb +0 -6
  35. data/spec/support/metrics/dummy_metrics.rb +0 -12
  36. data/spec/support/metrics/user_metrics.rb +0 -6
  37. data/spec/support/metrics/visits_metrics.rb +0 -8
  38. data/spec/support/shared_contexts/shared_precision.rb +0 -19
  39. data/spec/support/shared_contexts/shared_query.rb +0 -16
  40. data/spec/support/user.rb +0 -16
@@ -1,467 +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", time: Time.now).to_sql).to eq "select * from \"dummy\" where (user_id = 1) and (dummy = 'q') and (time = #{(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(time: Date.new(2015)).to_sql).to eq "select * from \"dummy\" where (time = #{(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(time: DateTime.new(2015)).to_sql).to eq "select * from \"dummy\" where (time = #{(DateTime.new(2015).to_time.to_r * 1_000_000_000).to_i})"
89
- end
90
-
91
- it "handle date ranges" do
92
- expect(rel.where(time: Date.new(2015)..Date.new(2016)).to_sql).to eq "select * from \"dummy\" where (time >= #{(Date.new(2015).to_time.to_r * 1_000_000_000).to_i} and time <= #{(Date.new(2016).to_time.to_r * 1_000_000_000).to_i})"
93
- end
94
-
95
- it "handle date time ranges" do
96
- expect(rel.where(time: DateTime.new(2015)..DateTime.new(2016)).to_sql).to eq "select * from \"dummy\" where (time >= #{(DateTime.new(2015).to_time.to_r * 1_000_000_000).to_i} and time <= #{(DateTime.new(2016).to_time.to_r * 1_000_000_000).to_i})"
97
- end
98
-
99
- it "handle inclusive ranges" do
100
- expect(rel.where(user_id: 1..4).to_sql).to eq "select * from \"dummy\" where (user_id >= 1 and user_id <= 4)"
101
- end
102
-
103
- it "handle exclusive range" do
104
- expect(rel.where(user_id: 1...4).to_sql).to eq "select * from \"dummy\" where (user_id >= 1 and user_id < 4)"
105
- end
106
-
107
- it "handle arrays" do
108
- 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)"
109
- end
110
-
111
- it "handle empty arrays", :aggregate_failures do
112
- expect(rel.where(user_id: []).to_sql).to eq "select * from \"dummy\" where (time < 0)"
113
- expect(rel.to_a).to eq []
114
- end
115
-
116
- context "with timestamp duration", :duration_suffix do
117
- it "adds ns suffix to times" do
118
- expect(rel.where(time: DateTime.new(2015)).to_sql).to eq "select * from \"dummy\" where (time = #{(DateTime.new(2015).to_time.to_r * 1_000_000_000).to_i}ns)"
119
- end
120
-
121
- context "with different time_precision", precision: :s do
122
- it "adds s suffix to times" do
123
- expect(rel.where(time: DateTime.new(2015)).to_sql).to eq "select * from \"dummy\" where (time = #{DateTime.new(2015).to_time.to_i}s)"
124
- end
125
- end
126
-
127
- context "with unsupported time_precision" do
128
- around do |ex|
129
- old_precision = Influxer.config.time_precision
130
- Influxer.config.time_precision = "h"
131
- ex.run
132
- Influxer.config.time_precision = old_precision
133
- end
134
-
135
- it "casts to ns with suffix" do
136
- expect(rel.where(time: DateTime.new(2015)).to_sql).to eq "select * from \"dummy\" where (time = #{(DateTime.new(2015).to_time.to_r * 1_000_000_000).to_i}ns)"
137
- end
138
- end
139
- end
140
-
141
- context "with different time_precision", precision: :s do
142
- it "casts to correct numeric representation" do
143
- expect(rel.where(time: DateTime.new(2015)).to_sql).to eq "select * from \"dummy\" where (time = #{DateTime.new(2015).to_time.to_i})"
144
- end
145
- end
146
-
147
- context "with tags" do
148
- it "integer tag values" do
149
- expect(rel.where(dummy_id: 10).to_sql).to eq "select * from \"dummy\" where (dummy_id = '10')"
150
- end
151
-
152
- it "array tag values" do
153
- expect(rel.where(dummy_id: [10, "some"]).to_sql).to eq "select * from \"dummy\" where (dummy_id = '10' or dummy_id = 'some')"
154
- end
155
-
156
- it "nil value" do
157
- expect(rel.where(dummy_id: nil).to_sql).to eq "select * from \"dummy\" where (dummy_id !~ /.*/)"
158
- end
159
- end
160
- end
161
-
162
- describe "#not" do
163
- it "negate simple values" do
164
- expect(rel.where.not(user_id: 1, dummy: :a).to_sql).to eq "select * from \"dummy\" where (user_id <> 1) and (dummy <> 'a')"
165
- end
166
-
167
- it "handle regexp" do
168
- expect(rel.where.not(user_id: 1, dummy: /^du.*/).to_sql).to eq "select * from \"dummy\" where (user_id <> 1) and (dummy !~ /^du.*/)"
169
- end
170
-
171
- it "handle inclusive ranges" do
172
- expect(rel.where.not(user_id: 1..4).to_sql).to eq "select * from \"dummy\" where (user_id < 1 or user_id > 4)"
173
- end
174
-
175
- it "handle exclusive ranges" do
176
- expect(rel.where.not(user_id: 1...4).to_sql).to eq "select * from \"dummy\" where (user_id < 1 or user_id >= 4)"
177
- end
178
-
179
- it "handle arrays" do
180
- 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)"
181
- end
182
-
183
- it "handle empty arrays", :aggregate_failures do
184
- expect(rel.where.not(user_id: []).to_sql).to eq "select * from \"dummy\" where (time >= 0)"
185
- end
186
-
187
- context "with tags" do
188
- it "nil value" do
189
- expect(rel.not(dummy_id: nil).to_sql).to eq "select * from \"dummy\" where (dummy_id =~ /.*/)"
190
- end
191
- end
192
- end
193
-
194
- describe "#none" do
195
- it "returns empty array", :aggregate_failures do
196
- expect(rel.none.to_sql).to eq "select * from \"dummy\" where (time < 0)"
197
- expect(rel.to_a).to eq []
198
- end
199
-
200
- it "works with chaining", :aggregate_failures do
201
- expect(rel.none.where.not(user_id: 1, dummy: :a).to_sql)
202
- .to eq "select * from \"dummy\" where (time < 0) and (user_id <> 1) and (dummy <> 'a')"
203
- expect(rel.to_a).to eq []
204
- end
205
- end
206
-
207
- describe "#past" do
208
- it "work with predefined symbols" do
209
- expect(rel.past(:hour).to_sql).to eq "select * from \"dummy\" where (time > now() - 1h)"
210
- end
211
-
212
- it "work with any symbols" do
213
- expect(rel.past(:s).to_sql).to eq "select * from \"dummy\" where (time > now() - 1s)"
214
- end
215
-
216
- it "work with strings" do
217
- expect(rel.past("3d").to_sql).to eq "select * from \"dummy\" where (time > now() - 3d)"
218
- end
219
-
220
- it "work with numbers" do
221
- expect(rel.past(1.day).to_sql).to eq "select * from \"dummy\" where (time > now() - 86400s)"
222
- end
223
- end
224
-
225
- describe "#since" do
226
- it "work with datetime" do
227
- expect(rel.since(Time.utc(2014, 12, 31)).to_sql).to eq "select * from \"dummy\" where (time > 1419984000s)"
228
- end
229
- end
230
-
231
- describe "#group" do
232
- it "generate valid groups" do
233
- expect(rel.group(:user_id, "time(1m) fill(0)").to_sql).to eq "select * from \"dummy\" group by user_id, time(1m) fill(0)"
234
- end
235
-
236
- context "group by time predefined values" do
237
- it "group by hour" do
238
- expect(rel.time(:hour).to_sql).to eq "select * from \"dummy\" group by time(1h)"
239
- end
240
-
241
- it "group by minute" do
242
- expect(rel.time(:minute).to_sql).to eq "select * from \"dummy\" group by time(1m)"
243
- end
244
-
245
- it "group by second" do
246
- expect(rel.time(:second).to_sql).to eq "select * from \"dummy\" group by time(1s)"
247
- end
248
-
249
- it "group by millisecond" do
250
- expect(rel.time(:ms).to_sql).to eq "select * from \"dummy\" group by time(1ms)"
251
- end
252
-
253
- it "group by microsecond" do
254
- expect(rel.time(:u).to_sql).to eq "select * from \"dummy\" group by time(1u)"
255
- end
256
-
257
- it "group by day" do
258
- expect(rel.time(:day).to_sql).to eq "select * from \"dummy\" group by time(1d)"
259
- end
260
-
261
- it "group by week" do
262
- expect(rel.time(:week).to_sql).to eq "select * from \"dummy\" group by time(1w)"
263
- end
264
-
265
- it "group by month" do
266
- expect(rel.time(:month).to_sql).to eq "select * from \"dummy\" group by time(30d)"
267
- end
268
-
269
- it "group by year" do
270
- expect(rel.time(:year).to_sql).to eq "select * from \"dummy\" group by time(365d)"
271
- end
272
-
273
- it "group by hour and fill" do
274
- expect(rel.time(:month, fill: 0).to_sql).to eq "select * from \"dummy\" group by time(30d) fill(0)"
275
- end
276
- end
277
-
278
- it "group by time with string value" do
279
- expect(rel.time("4d").to_sql).to eq "select * from \"dummy\" group by time(4d)"
280
- end
281
-
282
- %w[null previous none].each do |val|
283
- it "group by time with string value and fill #{val}" do
284
- expect(rel.time("4d", fill: val.to_sym).to_sql).to eq "select * from \"dummy\" group by time(4d) fill(#{val})"
285
- end
286
- end
287
-
288
- it "group by time and other fields with fill zero" do
289
- expect(rel.time("4d", fill: 0).group(:dummy_id).to_sql).to eq "select * from \"dummy\" group by time(4d), dummy_id fill(0)"
290
- end
291
-
292
- it "group by time and other fields with fill negative" do
293
- expect(rel.time("4d", fill: -1).group(:dummy_id).to_sql).to eq "select * from \"dummy\" group by time(4d), dummy_id fill(-1)"
294
- end
295
- end
296
-
297
- describe "#order" do
298
- it "generate valid order" do
299
- expect(rel.order(time_spent: :asc).to_sql).to eq "select * from \"dummy\" order by time_spent asc"
300
- end
301
-
302
- it "generate order from string" do
303
- expect(rel.order("cpu desc, val asc").to_sql).to eq "select * from \"dummy\" order by cpu desc, val asc"
304
- end
305
- end
306
-
307
- describe "#limit" do
308
- it "generate valid limit" do
309
- expect(rel.limit(100).to_sql).to eq "select * from \"dummy\" limit 100"
310
- end
311
- end
312
-
313
- describe "#slimit" do
314
- it "generate valid slimit" do
315
- expect(rel.slimit(100).to_sql).to eq "select * from \"dummy\" slimit 100"
316
- end
317
- end
318
-
319
- describe "#offset" do
320
- it "generate valid offset" do
321
- expect(rel.limit(100).offset(10).to_sql).to eq "select * from \"dummy\" limit 100 offset 10"
322
- end
323
- end
324
-
325
- describe "#soffset" do
326
- it "generate valid soffset" do
327
- expect(rel.soffset(10).to_sql).to eq "select * from \"dummy\" soffset 10"
328
- end
329
- end
330
-
331
- context "calculations" do
332
- context "one arg calculation methods" do
333
- [
334
- :count, :min, :max, :mean,
335
- :mode, :median, :distinct, :derivative,
336
- :stddev, :sum, :first, :last
337
- ].each do |method|
338
- describe "##{method}" do
339
- specify do
340
- expect(rel.where(user_id: 1).calc(method, :column_name).to_sql)
341
- .to eq "select #{method}(column_name) from \"dummy\" where (user_id = 1)"
342
- end
343
- end
344
- end
345
- end
346
-
347
- context "with aliases" do
348
- it "select count as alias" do
349
- expect(rel.count(:val, "total").to_sql).to eq "select count(val) as total from \"dummy\""
350
- end
351
-
352
- it "select percentile as alias" do
353
- expect(rel.percentile(:val, 90, "p1").to_sql).to eq "select percentile(val, 90) as p1 from \"dummy\""
354
- end
355
- end
356
- end
357
-
358
- context "complex queries" do
359
- it "group + where" do
360
- expect(rel.count("user_id").group(:traffic_source).fill(0).where(user_id: 123).past("28d").to_sql)
361
- .to eq "select count(user_id) from \"dummy\" where (user_id = 123) and (time > now() - 28d) " \
362
- "group by traffic_source fill(0)"
363
- end
364
-
365
- it "where + group + order + limit" do
366
- expect(rel.group(:user_id).where(account_id: 123).order(account_id: :desc).limit(10).offset(10).to_sql)
367
- .to eq "select * from \"dummy\" where (account_id = 123) group by user_id " \
368
- "order by account_id desc limit 10 offset 10"
369
- end
370
-
371
- it "offset + slimit" do
372
- expect(rel.where(account_id: 123).slimit(10).offset(10).to_sql)
373
- .to eq "select * from \"dummy\" where (account_id = 123) " \
374
- "offset 10 slimit 10"
375
- end
376
- end
377
- end
378
-
379
- describe "#empty?" do
380
- it "return false if has points" do
381
- allow(client).to receive(:query) { [{"values" => [{time: 1, id: 2}]}] }
382
- expect(rel.empty?).to be_falsey
383
- expect(rel.present?).to be_truthy
384
- end
385
-
386
- it "return true if no points" do
387
- allow(client).to receive(:query) { [] }
388
- expect(rel.empty?).to be_truthy
389
- expect(rel.present?).to be_falsey
390
- end
391
- end
392
-
393
- describe "#delete_all" do
394
- it "client expects to execute query method" do
395
- expected_query = "drop series from \"dummy\""
396
-
397
- expect(client)
398
- .to receive(:query).with(expected_query)
399
-
400
- rel.delete_all
401
- end
402
-
403
- it "without tags" do
404
- expect(rel.delete_all)
405
- .to eq "drop series from \"dummy\""
406
- end
407
-
408
- it "with tags" do
409
- expect(rel.where(dummy_id: 1, host: "eu").delete_all)
410
- .to eq "drop series from \"dummy\" where (dummy_id = '1') and (host = 'eu')"
411
- end
412
- end
413
-
414
- describe "#inspect" do
415
- it "return correct String represantation of empty relation" do
416
- allow(rel).to receive(:to_a) { [] }
417
- expect(rel.inspect).to eq "#<Influxer::Relation []>"
418
- end
419
-
420
- it "return correct String represantation of non-empty relation" do
421
- allow(rel).to receive(:to_a) { [1, 2, 3] }
422
- expect(rel.inspect).to eq "#<Influxer::Relation [1, 2, 3]>"
423
- end
424
-
425
- it "return correct String represantation of non-empty large (>11) relation" do
426
- allow(rel).to receive(:to_a) { [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] }
427
- expect(rel.inspect).to eq "#<Influxer::Relation [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]>"
428
- end
429
- end
430
-
431
- describe "#epoch" do
432
- it "format :h" do
433
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :h).and_return []
434
- DummyMetrics.epoch(:h).all.to_a
435
- end
436
-
437
- it "format :m" do
438
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :m).and_return []
439
- DummyMetrics.epoch(:m).all.to_a
440
- end
441
-
442
- it "format :s" do
443
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :s).and_return []
444
- DummyMetrics.epoch(:s).all.to_a
445
- end
446
-
447
- it "format :ms" do
448
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :ms).and_return []
449
- DummyMetrics.epoch(:ms).all.to_a
450
- end
451
-
452
- it "format :u" do
453
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :u).and_return []
454
- DummyMetrics.epoch(:u).all.to_a
455
- end
456
-
457
- it "format :ns" do
458
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :ns).and_return []
459
- DummyMetrics.epoch(:ns).all.to_a
460
- end
461
-
462
- it "invalid epoch format" do
463
- expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: nil).and_return []
464
- DummyMetrics.epoch(:invalid).all.to_a
465
- end
466
- end
467
- end