Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Move influxdb to influx lib
Browse files Browse the repository at this point in the history
  • Loading branch information
myoung34 committed Feb 14, 2020
1 parent eedddc1 commit 833ec42
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 37 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ payload_template = {"color": "{{ color }}", "gravity": {{ gravity }}, "temp": {{
method = GET
[influxdb]
url = http://grafana.corp.com
url = influxdb.corp.com
port = 80
database = tilty
gravity_payload_template = gravity,color={{ color }} value={{ gravity }} {{timestamp}}
temperature_payload_template = temperature,scale=fahrenheit,color={{ color }} value={{ temp }} {{timestamp}}
gravity_payload_template = {"measurement": "gravity", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ gravity }}}}
temperature_payload_template = {"measurement": "temperature", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ temp }}}}
EOF
$ tilty
```
Expand Down
52 changes: 50 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ click = "^7.0"
pybluez = "^0.22.0"
requests = "^2.22"
jinja2 = "^2.11.1"
influxdb = "^5.2.3"

[tool.poetry.dev-dependencies]
flake8 = "^3.7"
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ filelock==3.0.12
flake8==3.7.9
idna==2.8
importlib-metadata==1.3.0
influxdb==5.2.3
isort==4.3.21
Jinja2==2.11.1
lazy-object-proxy==1.4.3
Expand All @@ -25,6 +26,8 @@ pylint==2.4.4
pyparsing==2.4.5
pytest==5.3.2
pytest-cov==2.8.1
python-dateutil==2.8.1
pytz==2019.3
requests==2.22.0
six==1.13.0
toml==0.10.0
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
packages=find_packages(exclude=['tests*']),
install_requires=[
'Click',
'influxdb',
'Jinja2',
'pybluez',
'requests',
Expand Down
14 changes: 14 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
from tilty import common


def test_safe_get_key_no_fallback():
assert common.safe_get_key({}, 'foo') is None


def test_safe_get_key_fallback():
assert common.safe_get_key({}, 'foo', 'wut') == 'wut'


def test_safe_get_key_valid():
assert common.safe_get_key({'foo': 'asdf'}, 'foo', 'wut') == 'asdf'
39 changes: 25 additions & 14 deletions tests/test_influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,37 @@
from tilty.emitters import influxdb


@mock.patch('tilty.emitters.influxdb.requests')
@mock.patch('tilty.emitters.influxdb.InfluxDBClient')
def test_influxdb(
mock_requests,
mock_influx_client,
):
config = {
'url': 'http://www.google.com',
'database': 'foo',
'temperature_payload': 'temperature,color=Black value=85 1422568543702900257', # noqa
'gravity_payload': 'gravity,color=Black value=1.045 1422568543702900257', # noqa
'gravity_payload': '{"measurement": "gravity", "tags": {"color": "Black"}, "fields": {"value": 1.054}}', # noqa
'temperature_payload': '{"measurement": "temperature", "tags": {"color": "Black", "scale": "fahrenheight"}, "fields": {"value": 32}}', # noqa
}
influxdb.InfluxDB(config=config).emit()
assert mock_requests.mock_calls == [
mock.call.post(
data='temperature,color=Black value=85 1422568543702900257',
params=('db', 'mydb'),
url='http://www.google.com'
assert mock_influx_client.mock_calls == [
mock.call(
'http://www.google.com',
80,
None,
None,
'foo'
),
mock.call.post(
data='gravity,color=Black value=1.045 1422568543702900257',
params=('db', 'mydb'),
url='http://www.google.com'
)
mock.call().write_points([
{
'measurement': 'temperature',
'tags': {'color': 'Black', 'scale': 'fahrenheight'},
'fields': {'value': 32}
}
]),
mock.call().write_points([
{
'measurement': 'gravity',
'tags': {'color': 'Black'},
'fields': {'value': 1.054}
}
])
]
2 changes: 1 addition & 1 deletion tests/test_tilty.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_scan_for_tilt_data_parse_influxdb(
'url': 'http://www.google.com',
'database': 'foo',
'temperature_payload': 'temperature,scale=fahrenheit,color=black value=32 155558888', # noqa
'gravity_payload': 'temperature,scale=fahrenheit,color=black value=32 155558888' # noqa
'gravity_payload': 'gravity,color=black value=1 155558888'
}
),
mock.call().emit()
Expand Down
11 changes: 11 additions & 0 deletions tilty/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
""" Common methods """


def safe_get_key(config, key, fallback=None):
""" Class to safely pull key from config or a fallback value """
try:
return config[key]
except KeyError:
pass
return fallback
32 changes: 19 additions & 13 deletions tilty/emitters/influxdb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# -*- coding: utf-8 -*-
""" InfluxDB emitter """
import requests
import json
import logging

from influxdb import InfluxDBClient

from tilty.common import safe_get_key

LOGGER = logging.getLogger()


class InfluxDB: # pylint: disable=too-few-public-methods
Expand All @@ -11,23 +18,22 @@ def __init__(self, config):
Args:
config: (dict) represents the configuration for the emitter
"""
self.url = config['url']
self.database = config['database']
self.temperature_payload = config['temperature_payload']
self.gravity_payload = config['gravity_payload']
self.client = InfluxDBClient(
config['url'],
safe_get_key(config, 'port', 80),
safe_get_key(config, 'user'),
safe_get_key(config, 'password'),
config['database']
)

def emit(self, **kwargs): # pylint: disable=no-self-use,unused-argument
""" Initializer
Args:
"""
requests.post(
url=self.url,
params=(('db', 'mydb')),
data=self.temperature_payload,
)
requests.post(
url=self.url,
params=(('db', 'mydb')),
data=self.gravity_payload,
)
LOGGER.info('[influxdb] posting temperature data')
self.client.write_points([json.loads(self.temperature_payload)])
LOGGER.info('[influxdb] posting gravity data')
self.client.write_points([json.loads(self.gravity_payload)])
9 changes: 5 additions & 4 deletions tilty/tilty.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ def emit(config, tilt_data):

# <start config sample>
# [influxdb]
# url = http://www.foo.com
# url = www.foo.com
# port = 80
# database = tilty
# gravity_payload_template = 'gravity,color={{ color }} value={{ gravity }} {{timestamp}} # noqa
# temperature_payload_template = 'temperature,scale=fahrenheit,....
# gravity_payload_template = {"measurement": "gravity", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ gravity }}}} # noqa # pylint: disable=line-too-long
# temperature_payload_template = {"measurement": "temperature", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ temp }}}} # noqa # pylint: disable=line-too-long
if config.has_section('influxdb'):
_gravity_template = Template(config['influxdb']['gravity_payload_template']) # noqa
_gravity_template = Template(config['influxdb']['gravity_payload_template']) # noqa
_temperature_template = Template(config['influxdb']['temperature_payload_template']) # noqa
_config = {
'url': config['influxdb']['url'],
Expand Down

0 comments on commit 833ec42

Please sign in to comment.