influxparser 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/influxparser.rb +8 -3
- data/test/test_parse_point_from_docs.rb +13 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f52776eebd46f588f036de0d523f3a8877c122e1
|
4
|
+
data.tar.gz: 9a798c8a95952675cb11961e52cd91a3e6b03d12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf6ec6f5b0376b4f2902d7c11b749b9d0ff9b62f3bb4d66a0b6d54164ee48a15a6cb7b78097604a9626e4226a310c77bea445a60b86770436e35900a6ac99484
|
7
|
+
data.tar.gz: d78c4e1005e41546958888b29a12b9bc004672b87edf03715d11ac88b9a670806dac093327fecf5f4d858aba3db926858f3c777f315a21cd1808aa2ffc54974e
|
data/lib/influxparser.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
# TODO: the tags hash should be absent when there are no tags
|
2
|
-
# TODO: optional timestamp parsing
|
3
2
|
# TODO: time key shouldn't exist if there is no time
|
4
3
|
# TODO: deal with improper line protocol
|
5
4
|
class InfluxParser
|
6
5
|
class << self
|
7
6
|
def parse_point(s, options = {})
|
8
|
-
default_options = {:parse_types => true}
|
7
|
+
default_options = {:parse_types => true, :time_format => nil}
|
9
8
|
options = default_options.merge(options)
|
10
9
|
|
11
10
|
point = {}
|
@@ -39,9 +38,10 @@ class InfluxParser
|
|
39
38
|
point['values'][last_key] = unescape_point(value[1],options)
|
40
39
|
end
|
41
40
|
# puts "-----\n#{point['values'].to_yaml}\n"
|
41
|
+
|
42
42
|
# check for a timestamp in the last value
|
43
43
|
# TODO: I hate this, but it's late and I just want to move past it for now
|
44
|
-
# TODO:
|
44
|
+
# TODO: this level of nesting would fill rubocop with rage
|
45
45
|
has_space = last_value_raw.rindex(/ /)
|
46
46
|
if has_space
|
47
47
|
time_stamp = last_value_raw[has_space+1..-1] # take everything from the space to the end
|
@@ -51,6 +51,11 @@ class InfluxParser
|
|
51
51
|
# it was a timestamp, strip it from the last value and set the timestamp
|
52
52
|
point['values'][last_key] = unescape_point(last_value_raw[0..has_space-1],options)
|
53
53
|
point['time'] = time_stamp
|
54
|
+
if options[:time_format]
|
55
|
+
n_time = time_stamp.to_f / 1000000000
|
56
|
+
t = Time.at(n_time).utc
|
57
|
+
point['time'] = t.strftime(options[:time_format])
|
58
|
+
end
|
54
59
|
end
|
55
60
|
else
|
56
61
|
point['time'] = nil
|
@@ -82,10 +82,22 @@ class TestParsePointFromDocs < Test::Unit::TestCase # def setup
|
|
82
82
|
|
83
83
|
end
|
84
84
|
def test_timestamp
|
85
|
+
# no timestamp
|
85
86
|
point = InfluxParser.parse_point('weather,location=us-midwest temperature=82')
|
86
87
|
assert_not_equal(false,point) # a straight up parse error will false
|
87
88
|
assert_nil(point['time'])
|
88
|
-
|
89
|
+
|
90
|
+
# unformatted time
|
91
|
+
point = InfluxParser.parse_point('weather,location=us-midwest temperature=82 1465839830100400200')
|
92
|
+
assert_not_equal(false,point) # a straight up parse error will false
|
93
|
+
assert_equal('1465839830100400200',point['time'])
|
94
|
+
|
95
|
+
# time
|
96
|
+
point = InfluxParser.parse_point('weather,location=us-midwest temperature=82 1465839830100400200',{:time_format => "%Y-%d-%mT%H:%M:%S.%NZ"})
|
97
|
+
assert_not_equal(false,point) # a straight up parse error will false
|
98
|
+
assert_equal('2016-13-06T17:43:50.100400209Z',point['time'])
|
99
|
+
|
100
|
+
end
|
89
101
|
|
90
102
|
def test_float
|
91
103
|
point = InfluxParser.parse_point('weather,location=us-midwest temperature=82 1465839830100400200')
|