influxer 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb2769b10dc827cd4fbf01f688ab5a51dc2c20c2
4
- data.tar.gz: 6000427c5b89ce0e298d0eeef6c5d2a98d59976b
3
+ metadata.gz: c7fc8c72105e35542e5c818fa97fc55b49c6c068
4
+ data.tar.gz: eb461ecfc644cdf6a34789d940398ceda66c418e
5
5
  SHA512:
6
- metadata.gz: 5029154e957eaf73936e4891181cc542b18b0982b76cdcb81be9b3a4f9f9b6699c63dcddd6f5dda3541e5c36ac39b965cdbc9fbc47ad0a8599a3435ede15ea1b
7
- data.tar.gz: a5f4e65fa1e7a723251437980335b29d8383c079c39a5ea55405325db3fc7b6c27408ac2c1136bafff25cc81792ee8ceb6f0ad209637e94d765777423f2f498f
6
+ metadata.gz: d470618c9e5f329ba66ba8f1c9da6ba330075c6ee5be825f527c0feab3c3995827b44a5a92fd3f133456896df4b25417b42f2abc2197702117930572d36d8c64
7
+ data.tar.gz: 425b73368fafb1ff3e82dc6bab1ab005ab14ce9a2e1aac623359feb8eac763660aa13697a7dd3e8e9c018bfb6565b10109c66f7272fd34069ea4e38fdc35a830
data/Changelog.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.5.0
2
+ - Update `timestamp` support
3
+ - Add `epoch` method
4
+
1
5
  ## 0.4.0
2
6
  - Rename default `time` attribute to `timestamp`
3
7
 
data/influxer.gemspec CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.require_paths = ["lib"]
17
17
 
18
18
  s.add_dependency "activemodel", '>= 4.0'
19
- s.add_dependency "influxdb", "~> 0.2.2"
19
+ s.add_dependency "influxdb", "~> 0.2.3"
20
20
  s.add_dependency "anyway_config", "~>0.3.0"
21
21
 
22
22
  s.add_development_dependency "timecop"
@@ -7,9 +7,8 @@ module Influxer
7
7
  super Influxer.config.as_json.symbolize_keys!
8
8
  end
9
9
 
10
- # Public #execute
11
- def exec(sql)
12
- execute(sql)
10
+ def time_precision
11
+ @config.time_precision
13
12
  end
14
13
  end
15
14
  end
@@ -27,6 +27,7 @@ module Influxer
27
27
  :udp,
28
28
  :async,
29
29
  database: 'db',
30
+ time_precision: 'ns',
30
31
  cache: false
31
32
 
32
33
  def load
@@ -9,6 +9,7 @@ module Influxer
9
9
  # Base class for InfluxDB querying and writing
10
10
  # rubocop:disable Metrics/ClassLength
11
11
  class Metrics
12
+ TIME_FACTOR = 1_000_000_000
12
13
  include ActiveModel::Model
13
14
  include ActiveModel::Validations
14
15
  extend ActiveModel::Callbacks
@@ -24,7 +25,7 @@ module Influxer
24
25
  [
25
26
  :write, :write!, :select, :where,
26
27
  :group, :time, :past, :since,
27
- :limit, :offset, :fill, :delete_all
28
+ :limit, :offset, :fill, :delete_all, :epoch
28
29
  ] + Influxer::Calculations::CALCULATION_METHODS
29
30
  ),
30
31
  to: :all
@@ -108,6 +109,7 @@ module Influxer
108
109
  end
109
110
 
110
111
  delegate :tag_names, to: :class
112
+ attr_accessor :timestamp
111
113
 
112
114
  def initialize(attributes = {})
113
115
  @attributes = {}
@@ -132,7 +134,7 @@ module Influxer
132
134
  end
133
135
 
134
136
  def write_point
135
- client.write_point unquote(series), values: values, tags: tags
137
+ client.write_point unquote(series), values: values, tags: tags, timestamp: parsed_timestamp
136
138
  @persisted = true
137
139
  end
138
140
 
@@ -162,10 +164,23 @@ module Influxer
162
164
  @attributes.select { |k, _| tag_names.include?(k.to_s) }
163
165
  end
164
166
 
165
- attributes :timestamp
166
-
167
167
  private
168
168
 
169
+ def parsed_timestamp
170
+ return @timestamp unless client.time_precision == 'ns'
171
+
172
+ case @timestamp
173
+ when Numeric
174
+ @timestamp.to_i.to_s.ljust(19, '0').to_i
175
+ when String
176
+ (Time.parse(@timestamp).to_r * TIME_FACTOR).to_i
177
+ when Date
178
+ (@timestamp.to_time.to_r * TIME_FACTOR).to_i
179
+ when Time
180
+ (@timestamp.to_r * TIME_FACTOR).to_i
181
+ end
182
+ end
183
+
169
184
  def unquote(name)
170
185
  name.gsub(/(\A['"]|['"]\z)/, '')
171
186
  end
@@ -12,6 +12,8 @@ module Influxer
12
12
 
13
13
  attr_reader :values
14
14
 
15
+ SUPPORTED_EPOCH_FORMAT = [:h, :m, :s, :ms, :u, :ns]
16
+
15
17
  MULTI_VALUE_METHODS = [:select, :where, :group, :order]
16
18
 
17
19
  MULTI_KEY_METHODS = [:fanout]
@@ -109,6 +111,13 @@ module Influxer
109
111
  @values[:normalized] == true
110
112
  end
111
113
 
114
+ def epoch(val)
115
+ return self unless SUPPORTED_EPOCH_FORMAT.include? val
116
+
117
+ @values[:epoch] = val
118
+ self
119
+ end
120
+
112
121
  def order(val)
113
122
  case val
114
123
  when Hash
@@ -180,7 +189,7 @@ module Influxer
180
189
  end
181
190
 
182
191
  def load
183
- @records = get_points(@instance.client.query(to_sql, denormalize: !normalized?))
192
+ @records = get_points(@instance.client.query(to_sql, denormalize: !normalized?, epoch: @values[:epoch]))
184
193
  @loaded = true
185
194
  @records
186
195
  end
@@ -194,7 +203,7 @@ module Influxer
194
203
 
195
204
  sql = sql.join " "
196
205
 
197
- @instance.client.exec sql
206
+ @instance.client.query sql
198
207
  end
199
208
 
200
209
  def scoping
@@ -1,3 +1,3 @@
1
1
  module Influxer # :nodoc:
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -4,7 +4,7 @@ describe DummyMetrics do
4
4
  before do
5
5
  stub_request(:get, "http://localhost:8086/query")
6
6
  .with(
7
- query: { q: 'select * from "dummy"', u: "root", p: "root", precision: 's', db: 'db' }
7
+ query: { q: 'select * from "dummy"', u: "root", p: "root", precision: 'ns', db: 'db' }
8
8
  )
9
9
  .to_return(body: fixture_file)
10
10
  end
@@ -61,7 +61,7 @@ describe Influxer::Metrics, :query do
61
61
  it "sets current time" do
62
62
  Timecop.freeze(Time.local(2015))
63
63
  dummy_metrics.write!
64
- expect(dummy_metrics.timestamp).to eq Time.local(2015).to_i
64
+ expect(dummy_metrics.timestamp).to eq Time.local(2015)
65
65
  end
66
66
  end
67
67
  end
@@ -181,7 +181,7 @@ describe Influxer::Metrics, :query do
181
181
 
182
182
  it "write data and return point" do
183
183
  expect(client)
184
- .to receive(:write_point).with("dummies", tags: { dummy_id: 2, host: 'test' }, values: { user_id: 1 })
184
+ .to receive(:write_point).with("dummies", tags: { dummy_id: 2, host: 'test' }, values: { user_id: 1 }, timestamp: nil)
185
185
 
186
186
  point = dummy_metrics.write(user_id: 1, dummy_id: 2, host: 'test')
187
187
  expect(point.persisted?).to be_truthy
@@ -189,6 +189,34 @@ describe Influxer::Metrics, :query do
189
189
  expect(point.dummy_id).to eq 2
190
190
  end
191
191
 
192
+ it "test write data with time and return point" do
193
+ timestamp_test = Time.now
194
+ expected_time = (timestamp_test.to_r * 1_000_000_000).to_i
195
+
196
+ expect(client).
197
+ to receive(:write_point).with("dummies", tags: { dummy_id: 2, host: 'test' }, values: { user_id: 1 }, timestamp: expected_time)
198
+
199
+ point = dummy_metrics.write(user_id: 1, dummy_id: 2, host: 'test', timestamp: timestamp_test)
200
+ expect(point.persisted?).to be_truthy
201
+ expect(point.user_id).to eq 1
202
+ expect(point.dummy_id).to eq 2
203
+ expect(point.timestamp).to eq timestamp_test
204
+ end
205
+
206
+ it "test write data with string time" do
207
+ base_time = Time.now
208
+ timestamp_test = "#{base_time.to_s}"
209
+
210
+ expect(client).
211
+ to receive(:write_point).with("dummies", tags: { dummy_id: 2, host: 'test' }, values: { user_id: 1 }, timestamp: (base_time.to_i * 1_000_000_000).to_i)
212
+
213
+ point = dummy_metrics.write(user_id: 1, dummy_id: 2, host: 'test', timestamp: timestamp_test)
214
+ expect(point.persisted?).to be_truthy
215
+ expect(point.user_id).to eq 1
216
+ expect(point.dummy_id).to eq 2
217
+ expect(point.timestamp).to eq(timestamp_test)
218
+ end
219
+
192
220
  it "doesn't write data and return false if invalid" do
193
221
  expect(client).not_to receive(:write_point)
194
222
  expect(DummyMetrics.write(dummy_id: 2)).to be false
@@ -294,6 +294,15 @@ describe Influxer::Relation, :query do
294
294
  end
295
295
 
296
296
  describe "#delete_all" do
297
+ it "client expects to execute query method" do
298
+ expected_query = "drop series from \"dummy\""
299
+
300
+ expect(client)
301
+ .to receive(:query).with(expected_query)
302
+
303
+ rel.delete_all
304
+ end
305
+
297
306
  it "without tags" do
298
307
  expect(rel.delete_all)
299
308
  .to eq "drop series from \"dummy\""
@@ -321,4 +330,41 @@ describe Influxer::Relation, :query do
321
330
  expect(rel.inspect).to eq "#<Influxer::Relation [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]>"
322
331
  end
323
332
  end
333
+
334
+ describe "#epoch" do
335
+ it "format :h" do
336
+ expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :h).and_return []
337
+ DummyMetrics.epoch(:h).all.to_a
338
+ end
339
+
340
+ it "format :m" do
341
+ expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :m).and_return []
342
+ DummyMetrics.epoch(:m).all.to_a
343
+ end
344
+
345
+ it "format :s" do
346
+ expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :s).and_return []
347
+ DummyMetrics.epoch(:s).all.to_a
348
+ end
349
+
350
+ it "format :ms" do
351
+ expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :ms).and_return []
352
+ DummyMetrics.epoch(:ms).all.to_a
353
+ end
354
+
355
+ it "format :u" do
356
+ expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :u).and_return []
357
+ DummyMetrics.epoch(:u).all.to_a
358
+ end
359
+
360
+ it "format :ns" do
361
+ expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: :ns).and_return []
362
+ DummyMetrics.epoch(:ns).all.to_a
363
+ end
364
+
365
+ it "invalid epoch format" do
366
+ expect(client).to receive(:query).with('select * from "dummy"', denormalize: true, epoch: nil).and_return []
367
+ DummyMetrics.epoch(:invalid).all.to_a
368
+ end
369
+ end
324
370
  end
@@ -4,7 +4,7 @@ class DummyMetrics < Influxer::Metrics # :nodoc:
4
4
 
5
5
  validates_presence_of :dummy_id, :user_id
6
6
 
7
- before_write -> { self.timestamp = Time.now.to_i }
7
+ before_write -> { self.timestamp = Time.now }
8
8
 
9
9
  scope :calc, ->(method, *args) { send(method, *args) }
10
10
  end
@@ -7,9 +7,7 @@ shared_context "stub_query", :query do
7
7
  sql
8
8
  end
9
9
 
10
- allow_any_instance_of(Influxer::Client).to receive(:exec) do |_, sql|
11
- sql
12
- end
10
+ allow_any_instance_of(InfluxDB::Client).to receive(:time_precision)
13
11
 
14
12
  allow_any_instance_of(InfluxDB::Client).to receive(:post)
15
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: influxer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vlad Dem
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-21 00:00:00.000000000 Z
11
+ date: 2016-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.2.2
33
+ version: 0.2.3
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.2.2
40
+ version: 0.2.3
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: anyway_config
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -238,3 +238,4 @@ signing_key:
238
238
  specification_version: 4
239
239
  summary: InfluxDB for Rails
240
240
  test_files: []
241
+ has_rdoc: