influxer 1.1.4 → 1.2.2

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