Skip to content

Commit ccb71b7

Browse files
committed
2 parents 293397f + 839b06c commit ccb71b7

File tree

4 files changed

+77
-34
lines changed

4 files changed

+77
-34
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ from databox import Client
3434
client = Client('<access token>')
3535
client.push('sales.total', 1447.0)
3636
client.push('orders.total', 32, date='2015-01-01 09:00:00')
37+
client.push('orders.total', 34, attributes={
38+
'something': 1447,
39+
})
40+
3741
```
3842

3943
Inserting multiple matrices with one `insert_all`:

databox test/test_push.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,26 @@ def test_push(self):
1919
assert self.client.push("templj", 10.0) is True
2020
assert self.client.push("templj", 12.0, date="2015-01-01 09:00:00") is True
2121

22+
def test_push_with_attributes(self):
23+
self.client._push_json = lambda data=None, path='/': dict({
24+
'status': 'ok',
25+
}.items() + data.items())
26+
27+
push = self.client.push("meta", 100, attributes={
28+
'n': 100
29+
})
30+
31+
assert self.client.last_push_content['data'][0]['$meta'] == 100
32+
assert self.client.last_push_content['data'][0]['n'] == 100
33+
34+
2235
def test_push_validation(self):
2336
self.assertRaises(
2437
Client.KPIValidationException,
2538
lambda: self.client.push(None, None)
2639
)
2740

41+
2842
def test_insert_all(self):
2943
assert self.client.insert_all([
3044
{'key': 'templj', 'value': 83.3},
@@ -41,6 +55,7 @@ def test_insert_all(self):
4155
])
4256
)
4357

58+
4459
def test_last_push(self):
4560
self.client._push_json = lambda data=None, path='/': {
4661
'err': [],
@@ -49,17 +64,23 @@ def test_last_push(self):
4964

5065
assert self.client.last_push()['err'] == []
5166

67+
68+
def test_last_push_with_number(self):
69+
self.client._push_json = lambda data=None, path='/': path
70+
assert self.client.last_push(3) == '/lastpushes/3'
71+
72+
5273
def test_short(self):
5374
Client._push_json = mock_push_json
5475

5576
assert push("templj", 22, token=self.databox_push_token) is True
5677

5778
assert insert_all([
58-
{
59-
'key': 'templj',
60-
'value': 83.3
61-
},
62-
], token=self.databox_push_token) is True
79+
{
80+
'key': 'templj',
81+
'value': 83.3
82+
},
83+
], token=self.databox_push_token) is True
6384

6485
Client._push_json = lambda data=None, path='/': {
6586
'err': [],

databox/__init__.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class Client(object):
88
push_token = None
99
push_host = 'https://push2new.databox.com'
10+
last_push_content = None
1011

1112
class MissingToken(Exception):
1213
pass
@@ -39,6 +40,10 @@ def process_kpi(self, **args):
3940
if date is not None:
4041
item['date'] = date
4142

43+
attributes = args.get('attributes', None)
44+
if attributes is not None:
45+
item = dict(item.items() + attributes.items())
46+
4247
return item
4348

4449
def _push_json(self, data=None, path="/"):
@@ -54,18 +59,31 @@ def _push_json(self, data=None, path="/"):
5459

5560
return response.json()
5661

57-
def push(self, key, value, date=None):
58-
return self._push_json({
59-
'data': [self.process_kpi(key=key, value=value, date=date)]
60-
})['status'] == 'ok'
62+
63+
def push(self, key, value, date=None, attributes=None):
64+
self.last_push_content = self._push_json({
65+
'data': [self.process_kpi(
66+
key=key,
67+
value=value,
68+
date=date,
69+
attributes=attributes
70+
)]
71+
})
72+
73+
return self.last_push_content['status'] == 'ok'
74+
75+
6176

6277
def insert_all(self, rows):
63-
return self._push_json({
78+
self.last_push_content = self._push_json({
6479
'data': [self.process_kpi(**row) for row in rows]
65-
})['status'] == 'ok'
80+
})
81+
82+
return self.last_push_content['status'] == 'ok'
83+
6684

67-
def last_push(self):
68-
return self._push_json(path='/lastpushes/1')
85+
def last_push(self, number=1):
86+
return self._push_json(path='/lastpushes/{n}'.format(**{'n': number}))
6987

7088

7189
def push(key, value, date=None, token=None):

example.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,33 @@
99

1010
TOKEN = "adxg1kq5a4g04k0wk0s4wkssow8osw84"
1111

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."
21-
22-
2312
from databox import Client
2413

25-
client = Client('<access token>')
26-
client.push('sales.total', 1447.0)
27-
client.push('orders.total', 32, date='2015-01-01 09:00:00')
14+
client = Client(TOKEN)
15+
# client.push('sales.total', 1447.0)
16+
#client.push('orders.total', 32, date='2015-01-01 09:00:00')
17+
18+
# key = 'temp.ljx'
19+
# rows = []
20+
#
21+
# for i, day in enumerate([date.today()-timedelta(days=d) for d in range(0, 14)]):
22+
# n = 20 + randint(0, 15)
23+
# rows.append({'key': key, 'value': n, 'date': day.strftime("%Y-%m-%d")})
24+
#
25+
# if insert_all(rows, token=TOKEN):
26+
# print "Inserted", len(rows), "rows."
27+
#
28+
#
2829

29-
print client.last_push()
3030

31-
client.insert_all([
31+
print client.insert_all([
3232
{'key': 'temp.boston', 'value': 51},
3333
{'key': 'temp.boston', 'value': 49, 'date': '2015-01-01 17:00:00'},
34-
{'key': 'sales.total', 'value': 3000},
34+
{'key': 'sales.total', 'value': 3000, 'attributes': {
35+
'name': "Oto",
36+
'price': 199
37+
}},
3538
])
3639

40+
print client.last_push(3)
3741

38-
39-
from databox import push, insert_all, last_push
40-
push('sales.total', 1448.9, token=TOKEN)
41-
print last_push(TOKEN)

0 commit comments

Comments
 (0)