Skip to content

Commit 88aeedb

Browse files
committed
Add hour and minute time_precision support.
1 parent b65e4a4 commit 88aeedb

File tree

4 files changed

+65
-17
lines changed

4 files changed

+65
-17
lines changed

influxdb/_dataframe_client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ def _convert_dataframe_to_json(self, dataframe, measurement, tags=None,
140140
"n": 1,
141141
"u": 1e3,
142142
"ms": 1e6,
143-
"s": 1e9
143+
"s": 1e9,
144+
"m": 1e9 * 60,
145+
"h": 1e9 * 3600,
144146
}.get(time_precision, 1)
145147

146148
points = [
@@ -154,7 +156,11 @@ def _convert_dataframe_to_json(self, dataframe, measurement, tags=None,
154156

155157
def _datetime_to_epoch(self, datetime, time_precision='s'):
156158
seconds = (datetime - self.EPOCH).total_seconds()
157-
if time_precision == 's':
159+
if time_precision == 'h':
160+
return seconds / 3600
161+
elif time_precision == 'm':
162+
return seconds / 60
163+
elif time_precision == 's':
158164
return seconds
159165
elif time_precision == 'ms':
160166
return seconds * 1e3

influxdb/line_protocol.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def _convert_timestamp(timestamp, precision=None):
2727
return ns / 1e6
2828
elif precision == 's':
2929
return ns / 1e9
30+
elif precision == 'm':
31+
return ns / 1e9 / 60
32+
elif precision == 'h':
33+
return ns / 1e9 / 3600
3034

3135
raise ValueError(timestamp)
3236

influxdb/tests/client_test.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def setUp(self):
8888
"host": "server01",
8989
"region": "us-west"
9090
},
91-
"time": "2009-11-10T23:00:00Z",
91+
"time": "2009-11-10T23:00:00.123456Z",
9292
"fields": {
9393
"value": 0.64
9494
}
@@ -172,8 +172,8 @@ def test_write_points(self):
172172
self.dummy_points,
173173
)
174174
self.assertEqual(
175-
"cpu_load_short,host=server01,region=us-west "
176-
"value=0.64 1257894000000000000\n",
175+
'cpu_load_short,host=server01,region=us-west '
176+
'value=0.64 1257894000123456000\n',
177177
m.last_request.body.decode('utf-8'),
178178
)
179179

@@ -193,8 +193,8 @@ def test_write_points_toplevel_attributes(self):
193193
retention_policy="somepolicy"
194194
)
195195
self.assertEqual(
196-
"cpu_load_short,host=server01,region=us-west,tag=hello "
197-
"value=0.64 1257894000000000000\n",
196+
'cpu_load_short,host=server01,region=us-west,tag=hello '
197+
'value=0.64 1257894000123456000\n',
198198
m.last_request.body.decode('utf-8'),
199199
)
200200

@@ -240,8 +240,8 @@ def test_write_points_udp(self):
240240
received_data, addr = s.recvfrom(1024)
241241

242242
self.assertEqual(
243-
"cpu_load_short,host=server01,region=us-west "
244-
"value=0.64 1257894000000000000\n",
243+
'cpu_load_short,host=server01,region=us-west '
244+
'value=0.64 1257894000123456000\n',
245245
received_data.decode()
246246
)
247247

@@ -278,22 +278,22 @@ def test_write_points_with_precision(self):
278278

279279
cli.write_points(self.dummy_points, time_precision='n')
280280
self.assertEqual(
281-
b"cpu_load_short,host=server01,region=us-west "
282-
b"value=0.64 1257894000000000000\n",
281+
b'cpu_load_short,host=server01,region=us-west '
282+
b'value=0.64 1257894000123456000\n',
283283
m.last_request.body,
284284
)
285285

286286
cli.write_points(self.dummy_points, time_precision='u')
287287
self.assertEqual(
288-
b"cpu_load_short,host=server01,region=us-west "
289-
b"value=0.64 1257894000000000\n",
288+
b'cpu_load_short,host=server01,region=us-west '
289+
b'value=0.64 1257894000123456\n',
290290
m.last_request.body,
291291
)
292292

293293
cli.write_points(self.dummy_points, time_precision='ms')
294294
self.assertEqual(
295-
b"cpu_load_short,host=server01,region=us-west "
296-
b"value=0.64 1257894000000\n",
295+
b'cpu_load_short,host=server01,region=us-west '
296+
b'value=0.64 1257894000123\n',
297297
m.last_request.body,
298298
)
299299

@@ -304,6 +304,20 @@ def test_write_points_with_precision(self):
304304
m.last_request.body,
305305
)
306306

307+
cli.write_points(self.dummy_points, time_precision='m')
308+
self.assertEqual(
309+
b'cpu_load_short,host=server01,region=us-west '
310+
b'value=0.64 20964900\n',
311+
m.last_request.body,
312+
)
313+
314+
cli.write_points(self.dummy_points, time_precision='h')
315+
self.assertEqual(
316+
b'cpu_load_short,host=server01,region=us-west '
317+
b'value=0.64 349415\n',
318+
m.last_request.body,
319+
)
320+
307321
def test_write_points_bad_precision(self):
308322
cli = InfluxDBClient()
309323
with self.assertRaisesRegexp(

influxdb/tests/dataframe_client_test.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ def test_write_points_from_dataframe_with_numeric_column_names(self):
7171
index=[now, now + timedelta(hours=1)])
7272

7373
expected = (
74-
b"foo,hello=there 0=\"1\",1=1,2=1.0 0\n"
75-
b"foo,hello=there 0=\"2\",1=2,2=2.0 3600000000000\n"
74+
b'foo,hello=there 0=\"1\",1=1,2=1.0 0\n'
75+
b'foo,hello=there 0=\"2\",1=2,2=2.0 3600000000000\n'
7676
)
7777

7878
with requests_mock.Mocker() as m:
@@ -122,6 +122,22 @@ def test_write_points_from_dataframe_with_time_precision(self):
122122
cli = DataFrameClient(database='db')
123123
measurement = "foo"
124124

125+
cli.write_points(dataframe, measurement, time_precision='h')
126+
self.assertEqual(m.last_request.qs['precision'], ['h'])
127+
self.assertEqual(
128+
b'foo column_one="1",column_three=1.0,column_two=1 0\nfoo '
129+
b'column_one="2",column_three=2.0,column_two=2 1\n',
130+
m.last_request.body,
131+
)
132+
133+
cli.write_points(dataframe, measurement, time_precision='m')
134+
self.assertEqual(m.last_request.qs['precision'], ['m'])
135+
self.assertEqual(
136+
b'foo column_one="1",column_three=1.0,column_two=1 0\nfoo '
137+
b'column_one="2",column_three=2.0,column_two=2 60\n',
138+
m.last_request.body,
139+
)
140+
125141
cli.write_points(dataframe, measurement, time_precision='s')
126142
self.assertEqual(m.last_request.qs['precision'], ['s'])
127143
self.assertEqual(
@@ -298,6 +314,14 @@ def test_datetime_to_epoch(self):
298314
cli._datetime_to_epoch(timestamp),
299315
1356998400.0
300316
)
317+
self.assertEqual(
318+
cli._datetime_to_epoch(timestamp, time_precision='h'),
319+
1356998400.0 / 3600
320+
)
321+
self.assertEqual(
322+
cli._datetime_to_epoch(timestamp, time_precision='m'),
323+
1356998400.0 / 60
324+
)
301325
self.assertEqual(
302326
cli._datetime_to_epoch(timestamp, time_precision='s'),
303327
1356998400.0

0 commit comments

Comments
 (0)