Skip to content

Commit a2869b8

Browse files
committed
Merge pull request #1 from databox/patch-patchage-01
Patch Patchage 01
2 parents cae40c5 + 01ef2c1 commit a2869b8

File tree

14 files changed

+328
-123
lines changed

14 files changed

+328
-123
lines changed

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
workon databox-python
2+

.gitignore

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Source: https://github.com/github/gitignore/blob/master/Python.gitignore
2+
*.xml
3+
*.pid
4+
.idea/
5+
6+
# Byte-compiled / optimized / DLL files
7+
__pycache__/
8+
*.py[cod]
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
.Python
15+
env/
16+
build/
17+
develop-eggs/
18+
dist/
19+
downloads/
20+
eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
*.egg-info/
27+
.installed.cfg
28+
*.egg
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.coverage
44+
.cache
45+
nosetests.xml
46+
coverage.xml
47+
48+
# Translations
49+
*.mo
50+
*.pot
51+
52+
# Django stuff:
53+
*.log
54+
55+
# Sphinx documentation
56+
docs/_build/
57+
58+
# PyBuilder
59+
target/

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: python
2+
python:
3+
- "2.7"
4+
install:
5+
- "pip install ."
6+
- "pip install -r requirements.txt"
7+
script:
8+
- python -m unittest discover -v

README.md

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,71 @@
1-
Databox bindings for Python
2-
===========================
1+
# Databox bindings for Python
32

4-
Setup
5-
-----
3+
## Setup
64

75
You can install this package by cloning this directory and running:
86

97
```$ python setup.py install```
108

11-
Getting Databox access tokens
12-
-----------------------------
9+
## Getting Databox access tokens
1310

1411
When in Databox Designer go to Account > Access tokens, then either create a new token or get already existing one.
1512

13+
## Using the Databox Python library
14+
15+
To use databox-python libary you have to pass access token to client. You can do this when initialising `Client` or by setting `DATABOX_PUSH_TOKEN` environment variable.
16+
17+
Using `Client` and `push` to push data directly:
1618

17-
Using the Databox Python library
18-
--------------------------------
1919
```python
20+
from databox import Client
2021

21-
from databox import PushClient
22+
client = Client('<access token>')
23+
client.push('sales.total', 1447.0)
24+
client.push('orders.total', 32, date='2015-01-01 09:00:00')
25+
```
2226

23-
# instantiate PushClient
24-
client = PushClient('<access token>')
27+
Inserting multiple matrices with one `insert_all`:
2528

26-
# push metric "answer1" with value 42 for today
27-
client.add(42, "answer1")
29+
```python
30+
client.insert_all([
31+
{'key': 'temp.boston', 'value': 51},
32+
{'key': 'temp.boston', 'value': 49, 'date': '2015-01-01 17:00:00'},
33+
{'key': 'sales.total', 'value': 3000},
34+
])
35+
```
2836

29-
# push metric "answer1" with value 42 for any other day
30-
client.add(42, "answer1", "2015-05-01 10:10:10")
37+
Retriving information from last push with `last_push`:
38+
```python
39+
print client.last_push()
40+
```
3141

32-
# send to Databox
33-
print client.send()
42+
Libary can be used with shorthand methods:
3443

35-
# get last push
36-
print client.lastPush()
44+
45+
```python
46+
from databox import push, insert_all, last_push
47+
48+
push('sales.total', 1448.9)
49+
print last_push(token)
3750
```
3851

39-
Check working sample in [sample1.py](/sample1.py) file
52+
## Development and testing
53+
54+
Using virtualenv:
55+
56+
mkvirtualenv --no-site-packages databox-python
57+
workon databox-python
58+
pip install --upgrade -r requirements.txt
59+
60+
Running test suite with unittest:
61+
62+
python -munittest discover -p -t . 'test*' -v
63+
64+
## Authors and contributions
65+
66+
- [Vlada Petrović](https://github.com/VladaPetrovic)
67+
- [Oto Brglez](https://github.com/otobrglez)
68+
69+
## Licence
70+
71+
- [MIT](LICENSE.txt)

databox test/__init__.py

Whitespace-only changes.

databox test/test_push.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import unittest
2+
from databox import *
3+
from pprint import pprint as pp
4+
5+
6+
def mock_push_json(data=None, path='/'):
7+
return {'status': 'ok'}
8+
9+
10+
class TestPush(unittest.TestCase):
11+
def setUp(self):
12+
self.databox_push_token = "adxg1kq5a4g04k0wk0s4wkssow8osw84"
13+
self.client = Client(self.databox_push_token)
14+
15+
self.original_push_json = self.client._push_json
16+
self.client._push_json = mock_push_json
17+
18+
def test_push(self):
19+
assert self.client.push("templj", 10.0) is True
20+
assert self.client.push("templj", 12.0, date="2015-01-01 09:00:00") is True
21+
22+
def test_push_validation(self):
23+
self.assertRaises(
24+
Client.KPIValidationException,
25+
lambda: self.client.push(None, None)
26+
)
27+
28+
def test_insert_all(self):
29+
assert self.client.insert_all([
30+
{'key': 'templj', 'value': 83.3},
31+
{'key': 'templj', 'value': 83.3, 'date': "2015-01-01 09:00:00"},
32+
{'key': 'templj', 'value': 12.3},
33+
]) is True
34+
35+
self.assertRaises(
36+
Client.KPIValidationException,
37+
lambda: self.client.insert_all([
38+
{'value': 83.3},
39+
{'key': 'templj', 'value': 83.3, 'date': "2015-01-01 09:00:00"},
40+
{'key': 'templj', 'value': 12.3},
41+
])
42+
)
43+
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+
52+
def test_short(self):
53+
Client._push_json = mock_push_json
54+
55+
assert push("templj", 22, token=self.databox_push_token) is True
56+
57+
assert insert_all([
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: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,80 @@
1-
# Databox Python bindings
2-
# Authors:
3-
# Vlada Petrovic <[email protected]>
4-
5-
__package_name__ = 'databox'
6-
__version__ = '0.1'
7-
__author__ = 'Vlada Petrovic'
8-
__author_email__ = '[email protected]'
9-
__description__ = (
10-
'''Push metrics to Databox. ''')
11-
__url__ = 'https://github.com/databox/databox-python'
1+
import requests
2+
from requests.auth import HTTPBasicAuth
3+
from os import getenv
4+
from json import dumps as json_dumps
5+
6+
7+
class Client(object):
8+
push_token = None
9+
push_host = 'https://push2new.databox.com'
10+
11+
class MissingToken(Exception):
12+
pass
13+
14+
class KPIValidationException(Exception):
15+
pass
16+
17+
def __init__(self, token=None):
18+
self.push_token = token
19+
if self.push_token is None:
20+
self.push_token = getenv('DATABOX_PUSH_TOKEN', None)
21+
22+
if self.push_token is None:
23+
raise self.MissingToken('Push token is missing!')
24+
25+
self.push_host = getenv('DATABOX_PUSH_HOST', self.push_host)
26+
27+
def process_kpi(self, **args):
28+
key = args.get('key', None)
29+
if key is None:
30+
raise self.KPIValidationException('Missing "key"')
31+
32+
value = args.get('value', None)
33+
if value is None:
34+
raise self.KPIValidationException('Missing "value"')
35+
36+
item = {("$%s" % args['key']): args['value']}
37+
38+
date = args.get('date', None)
39+
if date is not None:
40+
item['date'] = date
41+
42+
return item
43+
44+
def _push_json(self, data=None, path="/"):
45+
if data is not None:
46+
data = json_dumps(data)
47+
48+
response = requests.post(
49+
self.push_host + path,
50+
auth=HTTPBasicAuth(self.push_token, ''),
51+
headers={'Content-Type': 'application/json'},
52+
data=data
53+
)
54+
55+
return response.json()
56+
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'
61+
62+
def insert_all(self, rows):
63+
return self._push_json({
64+
'data': [self.process_kpi(**row) for row in rows]
65+
})['status'] == 'ok'
66+
67+
def last_push(self):
68+
return self._push_json(path='/lastpushes/1')
69+
70+
71+
def push(key, value, date=None, token=None):
72+
return Client(token).push(key, value, date)
73+
74+
75+
def insert_all(rows=[], token=None):
76+
return Client(token).insert_all(rows)
77+
78+
79+
def last_push(token=None):
80+
return Client(token).last_push()

databox/__init__.pyc

-437 Bytes
Binary file not shown.

databox/client.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

databox/client.pyc

-2.09 KB
Binary file not shown.

0 commit comments

Comments
 (0)