Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Add hour and minute time_precision support. #226

Merged
merged 1 commit into from
Aug 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions influxdb/_dataframe_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def _convert_dataframe_to_json(self, dataframe, measurement, tags=None,
"n": 1,
"u": 1e3,
"ms": 1e6,
"s": 1e9
"s": 1e9,
"m": 1e9 * 60,
"h": 1e9 * 3600,
}.get(time_precision, 1)

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

def _datetime_to_epoch(self, datetime, time_precision='s'):
seconds = (datetime - self.EPOCH).total_seconds()
if time_precision == 's':
if time_precision == 'h':
return seconds / 3600
elif time_precision == 'm':
return seconds / 60
elif time_precision == 's':
return seconds
elif time_precision == 'ms':
return seconds * 1e3
Expand Down
4 changes: 4 additions & 0 deletions influxdb/line_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def _convert_timestamp(timestamp, precision=None):
return ns / 1e6
elif precision == 's':
return ns / 1e9
elif precision == 'm':
return ns / 1e9 / 60
elif precision == 'h':
return ns / 1e9 / 3600

raise ValueError(timestamp)

Expand Down
40 changes: 27 additions & 13 deletions influxdb/tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def setUp(self):
"host": "server01",
"region": "us-west"
},
"time": "2009-11-10T23:00:00Z",
"time": "2009-11-10T23:00:00.123456Z",
"fields": {
"value": 0.64
}
Expand Down Expand Up @@ -172,8 +172,8 @@ def test_write_points(self):
self.dummy_points,
)
self.assertEqual(
"cpu_load_short,host=server01,region=us-west "
"value=0.64 1257894000000000000\n",
'cpu_load_short,host=server01,region=us-west '
'value=0.64 1257894000123456000\n',
m.last_request.body.decode('utf-8'),
)

Expand All @@ -193,8 +193,8 @@ def test_write_points_toplevel_attributes(self):
retention_policy="somepolicy"
)
self.assertEqual(
"cpu_load_short,host=server01,region=us-west,tag=hello "
"value=0.64 1257894000000000000\n",
'cpu_load_short,host=server01,region=us-west,tag=hello '
'value=0.64 1257894000123456000\n',
m.last_request.body.decode('utf-8'),
)

Expand Down Expand Up @@ -240,8 +240,8 @@ def test_write_points_udp(self):
received_data, addr = s.recvfrom(1024)

self.assertEqual(
"cpu_load_short,host=server01,region=us-west "
"value=0.64 1257894000000000000\n",
'cpu_load_short,host=server01,region=us-west '
'value=0.64 1257894000123456000\n',
received_data.decode()
)

Expand Down Expand Up @@ -278,22 +278,22 @@ def test_write_points_with_precision(self):

cli.write_points(self.dummy_points, time_precision='n')
self.assertEqual(
b"cpu_load_short,host=server01,region=us-west "
b"value=0.64 1257894000000000000\n",
b'cpu_load_short,host=server01,region=us-west '
b'value=0.64 1257894000123456000\n',
m.last_request.body,
)

cli.write_points(self.dummy_points, time_precision='u')
self.assertEqual(
b"cpu_load_short,host=server01,region=us-west "
b"value=0.64 1257894000000000\n",
b'cpu_load_short,host=server01,region=us-west '
b'value=0.64 1257894000123456\n',
m.last_request.body,
)

cli.write_points(self.dummy_points, time_precision='ms')
self.assertEqual(
b"cpu_load_short,host=server01,region=us-west "
b"value=0.64 1257894000000\n",
b'cpu_load_short,host=server01,region=us-west '
b'value=0.64 1257894000123\n',
m.last_request.body,
)

Expand All @@ -304,6 +304,20 @@ def test_write_points_with_precision(self):
m.last_request.body,
)

cli.write_points(self.dummy_points, time_precision='m')
self.assertEqual(
b'cpu_load_short,host=server01,region=us-west '
b'value=0.64 20964900\n',
m.last_request.body,
)

cli.write_points(self.dummy_points, time_precision='h')
self.assertEqual(
b'cpu_load_short,host=server01,region=us-west '
b'value=0.64 349415\n',
m.last_request.body,
)

def test_write_points_bad_precision(self):
cli = InfluxDBClient()
with self.assertRaisesRegexp(
Expand Down
28 changes: 26 additions & 2 deletions influxdb/tests/dataframe_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def test_write_points_from_dataframe_with_numeric_column_names(self):
index=[now, now + timedelta(hours=1)])

expected = (
b"foo,hello=there 0=\"1\",1=1,2=1.0 0\n"
b"foo,hello=there 0=\"2\",1=2,2=2.0 3600000000000\n"
b'foo,hello=there 0=\"1\",1=1,2=1.0 0\n'
b'foo,hello=there 0=\"2\",1=2,2=2.0 3600000000000\n'
)

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

cli.write_points(dataframe, measurement, time_precision='h')
self.assertEqual(m.last_request.qs['precision'], ['h'])
self.assertEqual(
b'foo column_one="1",column_three=1.0,column_two=1 0\nfoo '
b'column_one="2",column_three=2.0,column_two=2 1\n',
m.last_request.body,
)

cli.write_points(dataframe, measurement, time_precision='m')
self.assertEqual(m.last_request.qs['precision'], ['m'])
self.assertEqual(
b'foo column_one="1",column_three=1.0,column_two=1 0\nfoo '
b'column_one="2",column_three=2.0,column_two=2 60\n',
m.last_request.body,
)

cli.write_points(dataframe, measurement, time_precision='s')
self.assertEqual(m.last_request.qs['precision'], ['s'])
self.assertEqual(
Expand Down Expand Up @@ -298,6 +314,14 @@ def test_datetime_to_epoch(self):
cli._datetime_to_epoch(timestamp),
1356998400.0
)
self.assertEqual(
cli._datetime_to_epoch(timestamp, time_precision='h'),
1356998400.0 / 3600
)
self.assertEqual(
cli._datetime_to_epoch(timestamp, time_precision='m'),
1356998400.0 / 60
)
self.assertEqual(
cli._datetime_to_epoch(timestamp, time_precision='s'),
1356998400.0
Expand Down