influxer 1.2.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{Changelog.md → CHANGELOG.md} +26 -50
- data/{MIT-LICENSE → LICENSE.txt} +1 -1
- data/README.md +83 -24
- data/lib/influxer/client.rb +1 -1
- data/lib/influxer/config.rb +16 -3
- data/lib/influxer/metrics/metrics.rb +1 -1
- data/lib/influxer/metrics/relation/where_clause.rb +6 -0
- data/lib/influxer/metrics/relation.rb +16 -4
- data/lib/influxer/metrics/scoping/current_scope.rb +16 -2
- data/lib/influxer/rails/client.rb +5 -5
- data/lib/influxer/version.rb +1 -1
- data/lib/influxer.rb +4 -0
- metadata +13 -43
- data/.gitignore +0 -37
- data/.rspec +0 -2
- data/.rubocop.yml +0 -57
- data/.travis.yml +0 -15
- data/Gemfile +0 -13
- data/Rakefile +0 -15
- data/bin/console +0 -8
- data/gemfiles/rails32.gemfile +0 -7
- data/gemfiles/rails42.gemfile +0 -7
- data/gemfiles/rails5.gemfile +0 -7
- data/gemfiles/rails6.gemfile +0 -7
- data/influxer.gemspec +0 -40
- data/spec/cases/points_spec.rb +0 -36
- data/spec/cases/write_points_spec.rb +0 -85
- data/spec/client_spec.rb +0 -46
- data/spec/fixtures/empty_result.json +0 -21
- data/spec/fixtures/single_series.json +0 -29
- data/spec/metrics/metrics_spec.rb +0 -329
- data/spec/metrics/relation_spec.rb +0 -467
- data/spec/metrics/scoping_spec.rb +0 -66
- data/spec/model/user_spec.rb +0 -46
- data/spec/spec_helper.rb +0 -63
- data/spec/support/metrics/action_metrics.rb +0 -5
- data/spec/support/metrics/custom_metrics.rb +0 -6
- data/spec/support/metrics/dummy_metrics.rb +0 -12
- data/spec/support/metrics/user_metrics.rb +0 -6
- data/spec/support/metrics/visits_metrics.rb +0 -8
- data/spec/support/shared_contexts/shared_precision.rb +0 -19
- data/spec/support/shared_contexts/shared_query.rb +0 -16
- 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
|
@@ -1,66 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
describe Influxer::Metrics, :query do
|
6
|
-
let(:klass) do
|
7
|
-
Class.new(Influxer::Metrics) do
|
8
|
-
set_series "dummy"
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
let(:dummy) do
|
13
|
-
Class.new(klass) do
|
14
|
-
default_scope -> { time(:hour) }
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
let(:dappy) do
|
19
|
-
Class.new(dummy) do
|
20
|
-
default_scope -> { limit(100) }
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
let(:doomy) do
|
25
|
-
Class.new(dappy) do
|
26
|
-
scope :by_user, ->(id) { where(user_id: id) if id.present? }
|
27
|
-
scope :hourly, -> { time(:hour) }
|
28
|
-
scope :daily, -> { time(:day) }
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe "default scope" do
|
33
|
-
it "works without default scope" do
|
34
|
-
expect(klass.all.to_sql).to eq "select * from \"dummy\""
|
35
|
-
end
|
36
|
-
|
37
|
-
it "works with default scope" do
|
38
|
-
expect(dummy.all.to_sql).to eq "select * from \"dummy\" group by time(1h)"
|
39
|
-
end
|
40
|
-
|
41
|
-
it "works with unscoped" do
|
42
|
-
expect(dummy.unscoped.to_sql).to eq "select * from \"dummy\""
|
43
|
-
end
|
44
|
-
|
45
|
-
it "works with several defaults" do
|
46
|
-
expect(dappy.where(user_id: 1).to_sql)
|
47
|
-
.to eq "select * from \"dummy\" where (user_id = 1) group by time(1h) limit 100"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
describe "named scope" do
|
52
|
-
it "works with named scope" do
|
53
|
-
expect(doomy.by_user(1).to_sql)
|
54
|
-
.to eq "select * from \"dummy\" where (user_id = 1) group by time(1h) limit 100"
|
55
|
-
end
|
56
|
-
|
57
|
-
it "works with named scope with empty relation" do
|
58
|
-
expect(doomy.by_user(nil).to_sql).to eq "select * from \"dummy\" group by time(1h) limit 100"
|
59
|
-
end
|
60
|
-
|
61
|
-
it "works with several scopes" do
|
62
|
-
expect(doomy.where(dummy_id: 100).by_user([1, 2, 3]).daily.to_sql)
|
63
|
-
.to eq "select * from \"dummy\" where (dummy_id = 100) and (user_id = 1 or user_id = 2 or user_id = 3) group by time(1d) limit 100"
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
data/spec/model/user_spec.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
describe User do
|
6
|
-
let(:user) { described_class.create age: 20, gender: 1, email: "user@example.com" }
|
7
|
-
subject { user }
|
8
|
-
|
9
|
-
specify { is_expected.to respond_to :metrics }
|
10
|
-
specify { is_expected.to respond_to :visits_metrics }
|
11
|
-
specify { is_expected.to respond_to :action_metrics }
|
12
|
-
specify { is_expected.to respond_to :custom_metrics }
|
13
|
-
|
14
|
-
describe "#metrics" do
|
15
|
-
subject { user.metrics.new }
|
16
|
-
|
17
|
-
it "add foreign key" do
|
18
|
-
expect(subject.user_id).to eq user.id
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe "#visits_metrics" do
|
23
|
-
subject { user.visits_metrics.new }
|
24
|
-
|
25
|
-
it "adds inherited attributes" do
|
26
|
-
expect(subject.age).to eq 20
|
27
|
-
expect(subject.gender).to eq 1
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
describe "#action_metrics" do
|
32
|
-
subject { user.action_metrics.new }
|
33
|
-
|
34
|
-
it "adds custom foreign key" do
|
35
|
-
expect(subject.user).to eq user.id
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
describe "#custom_metrics" do
|
40
|
-
subject { user.custom_metrics.new }
|
41
|
-
|
42
|
-
it "doesn't add foreign key" do
|
43
|
-
expect(subject.user_id).to be_nil
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
4
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
-
|
6
|
-
ENV["RAILS_ENV"] ||= "test"
|
7
|
-
|
8
|
-
require "rspec"
|
9
|
-
require "webmock/rspec"
|
10
|
-
|
11
|
-
begin
|
12
|
-
require "pry-byebug"
|
13
|
-
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
14
|
-
end
|
15
|
-
|
16
|
-
require "timecop"
|
17
|
-
|
18
|
-
require "active_record"
|
19
|
-
require "sqlite3"
|
20
|
-
|
21
|
-
require "influxer"
|
22
|
-
|
23
|
-
# Rails stub
|
24
|
-
class Rails
|
25
|
-
class << self
|
26
|
-
def cache
|
27
|
-
@cache ||= ActiveSupport::Cache::MemoryStore.new
|
28
|
-
end
|
29
|
-
|
30
|
-
def logger
|
31
|
-
@logger ||= Logger.new(nil)
|
32
|
-
end
|
33
|
-
|
34
|
-
def env
|
35
|
-
"test"
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
require "influxer/rails/client"
|
41
|
-
|
42
|
-
ActiveRecord::Base.send :include, Influxer::Model
|
43
|
-
|
44
|
-
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
45
|
-
|
46
|
-
Dir["#{File.dirname(__FILE__)}/support/metrics/*.rb"].each { |f| require f }
|
47
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
48
|
-
|
49
|
-
WebMock.disable_net_connect!
|
50
|
-
|
51
|
-
RSpec.configure do |config|
|
52
|
-
config.mock_with :rspec
|
53
|
-
|
54
|
-
config.example_status_persistence_file_path = "tmp/rspec_examples.txt"
|
55
|
-
config.filter_run :focus
|
56
|
-
config.run_all_when_everything_filtered = true
|
57
|
-
|
58
|
-
config.order = :random
|
59
|
-
Kernel.srand config.seed
|
60
|
-
|
61
|
-
config.after(:each) { Influxer.reset! }
|
62
|
-
config.after(:each) { Timecop.return }
|
63
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class DummyMetrics < Influxer::Metrics # :nodoc:
|
4
|
-
tags :dummy_id, :host
|
5
|
-
attributes :user_id
|
6
|
-
|
7
|
-
validates_presence_of :dummy_id, :user_id
|
8
|
-
|
9
|
-
before_write -> { self.timestamp = Time.now }
|
10
|
-
|
11
|
-
scope :calc, ->(method, *args) { send(method, *args) }
|
12
|
-
end
|