Skip to content

Commit 318d7dd

Browse files
committed
Adding Client#last_push
1 parent b3c7bdb commit 318d7dd

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

databox test/test_push.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
from pprint import pprint as pp
44

55

6-
def mock_push_json(data={}):
7-
return True
6+
def mock_push_json(data=None, path='/'):
7+
return {'status': 'ok'}
88

99

1010
class TestPush(unittest.TestCase):
1111
def setUp(self):
1212
self.databox_push_token = "adxg1kq5a4g04k0wk0s4wkssow8osw84"
1313
self.client = Client(self.databox_push_token)
14+
15+
self.original_push_json = self.client._push_json
1416
self.client._push_json = mock_push_json
1517

1618
def test_push(self):
@@ -39,9 +41,29 @@ def test_insert_all(self):
3941
])
4042
)
4143

44+
def test_last_push(self):
45+
self.client._push_json = lambda data=None, path='/': {
46+
'err': [],
47+
'no_err': 0
48+
}
49+
50+
assert self.client.last_push()['err'] == []
51+
4252
def test_short(self):
53+
Client._push_json = mock_push_json
54+
4355
assert push("templj", 22, token=self.databox_push_token) is True
4456

4557
assert insert_all([
46-
{'key': 'templj', 'value': 83.3},
47-
], token=self.databox_push_token) is True
58+
{
59+
'key': 'templj',
60+
'value': 83.3
61+
},
62+
], token=self.databox_push_token) is True
63+
64+
Client._push_json = lambda data=None, path='/': {
65+
'err': [],
66+
'no_err': 0
67+
}
68+
69+
assert last_push(token=self.databox_push_token)['err'] == []

databox/__init__.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,40 @@ def process_kpi(self, **args):
4141

4242
return item
4343

44-
def _push_json(self, data={}):
44+
def _push_json(self, data=None, path="/"):
45+
if data is not None:
46+
data = json_dumps(data)
47+
4548
response = requests.post(
46-
self.push_host,
49+
self.push_host + path,
4750
auth=HTTPBasicAuth(self.push_token, ''),
4851
headers={'Content-Type': 'application/json'},
49-
data=json_dumps(data)
52+
data=data
5053
)
5154

52-
return response.json()['status'] == 'ok'
55+
return response.json()
5356

5457
def push(self, key, value, date=None):
5558
return self._push_json({
5659
'data': [self.process_kpi(key=key, value=value, date=date)]
57-
})
60+
})['status'] == 'ok'
5861

5962
def insert_all(self, rows):
6063
return self._push_json({
6164
'data': [self.process_kpi(**row) for row in rows]
62-
})
65+
})['status'] == 'ok'
66+
67+
def last_push(self):
68+
return self._push_json(path='/lastpushes/1')
6369

6470

6571
def push(key, value, date=None, token=None):
6672
return Client(token).push(key, value, date)
6773

6874

6975
def insert_all(rows=[], token=None):
70-
return Client(token).insert_all(rows)
76+
return Client(token).insert_all(rows)
77+
78+
79+
def last_push(token=None):
80+
return Client(token).last_push()

example.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
#!/usr/bin/env python
2+
from datetime import date, timedelta
3+
from random import randint
4+
from databox import insert_all
25

3-
from databox import Client, push
6+
"""
7+
This example generates fake temperature KPIs for last 14 days and inserts them.
8+
"""
49

5-
Client("adxg1kq5a4g04k0wk0s4wkssow8osw84")\
6-
.push("templj", 92.2)
10+
TOKEN = "adxg1kq5a4g04k0wk0s4wkssow8osw84"
711

8-
push("templj", 102.3, token="adxg1kq5a4g04k0wk0s4wkssow8osw84")
12+
key = 'temp.ljx'
13+
rows = []
14+
15+
for i, day in enumerate([date.today()-timedelta(days=d) for d in range(0, 14)]):
16+
n = 20 + randint(0, 15)
17+
rows.append({'key': key, 'value': n, 'date': day.strftime("%Y-%m-%d")})
18+
19+
if insert_all(rows, token=TOKEN):
20+
print "Inserted", len(rows), "rows."

0 commit comments

Comments
 (0)