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

Commit

Permalink
Merge pull request #35 from myoung34/influxdb_v2
Browse files Browse the repository at this point in the history
Add support for influx cloud via new influxdb client
  • Loading branch information
myoung34 authored Nov 24, 2020
2 parents 2b34b4b + 75302b4 commit 36ab4d1
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 134 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The Tilt supports writing to a google doc which you could use with something lik
* Generic (Send to any endpoint with any type)
* Brewstat.us (Example below)
* BrewersFriend (Example below)
* InfluxDB
* InfluxDB (1.8+)
* Datadog (dogstatsd)
* SQLite
* Google Sheets (experimental/advanced)
Expand Down Expand Up @@ -87,15 +87,14 @@ payload_template = {"name": "Tilt {{ color }}", "gravity": {{ gravity }}, "gravi
method = POST
[influxdb]
url = influxdb.corp.com
port = 80
database = tilty
password = foo # optional
ssl = True # default is False
verify_ssl = True # default is False
gravity_payload_template = {"measurement": "gravity", "tags": {"color": "{{ color }}", "mac": "{{ mac }}"}, "fields": {"value": {{ gravity }}}}
temperature_payload_template = {"measurement": "temperature", "tags": {"color": "{{ color }}", "mac": "{{ mac }}"}, "fields": {"value": {{ temp }}}}
# `% curl -s -G 'http://influxdb.service.consul:8086/query?pretty=true' --data-urlencode "db=tilty" --data-urlencode 'q=SELECT "value", "mac", "color" FROM "autogen"."gravity"' | jq '.results[].series[].values[0]'`
url = http://localhost:8086
verify_ssl = True # defaults to False, only used if url is https://
bucket = tilty
org = Mine
token = mytoken # if using influx cloud
token = myuser:password # if using self hosted
gravity_payload_template = gravity,color={{ color }},mac={{ mac }} sg={{ gravity }}
temperature_payload_template = temperature,color={{ color }},mac={{ mac }} temp={{ temp }}
[datadog]
# Note: make sure that the dd agent has DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true
Expand Down
133 changes: 60 additions & 73 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ click = "^7.0"
pybluez = "^0.22.0"
requests = "^2.22"
jinja2 = "^2.11.1"
influxdb = "^5.2.3"
influxdb-client = "^1.12.0"
datadog = "^0.34.1"
google-api-core = "1.20.1"
google-api-python-client = "^1.10.0"
Expand Down
12 changes: 6 additions & 6 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ google-auth-oauthlib==0.4.2
googleapis-common-protos==1.52.0
httplib2==0.18.1
idna==2.10
importlib-metadata==2.0.0; python_version < "3.8"
importlib-metadata==2.1.0; python_version < "3.8"
importlib-resources==3.3.0; python_version < "3.7"
influxdb==5.3.0
influxdb-client==1.12.0
isort==4.3.21
jinja2==2.11.2
lazy-object-proxy==1.4.3
markupsafe==1.1.1
mccabe==0.6.1
more-itertools==8.6.0
msgpack==0.6.1
mypy==0.782
mypy-extensions==0.4.3
oauth2client==4.1.3
Expand All @@ -44,7 +43,7 @@ packaging==20.4
pbr==5.5.1
pluggy==0.13.1
prometheus-client==0.8.0
protobuf==3.13.0
protobuf==3.14.0
py==1.9.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
Expand All @@ -58,9 +57,10 @@ pytest-cov==2.10.1
python-dateutil==2.8.1
pytz==2020.4
pyyaml==5.3.1
requests==2.24.0
requests==2.25.0
requests-oauthlib==1.3.0
rsa==4.6
rx==3.1.1
six==1.15.0
smmap==3.0.4
stevedore==3.2.2
Expand All @@ -70,7 +70,7 @@ typed-ast==1.4.1
typing-extensions==3.7.4.3
uritemplate==3.0.1
urllib3==1.25.11
virtualenv==20.1.0
virtualenv==20.2.1
wcwidth==0.2.5
wrapt==1.12.1
zipp==3.4.0; python_version < "3.8"
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ google-auth-oauthlib==0.4.2
googleapis-common-protos==1.52.0
httplib2==0.18.1
idna==2.10
influxdb==5.3.0
influxdb-client==1.12.0
jinja2==2.11.2
markupsafe==1.1.1
msgpack==0.6.1
oauth2client==4.1.3
oauthlib==3.1.0
prometheus-client==0.8.0
protobuf==3.13.0
protobuf==3.14.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pybluez==0.22
python-dateutil==2.8.1
pytz==2020.4
requests==2.24.0
requests==2.25.0
requests-oauthlib==1.3.0
rsa==4.6
rx==3.1.1
six==1.15.0
uritemplate==3.0.1
urllib3==1.25.11
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
install_requires=[
'Click',
'datadog',
'influxdb',
'influxdb-client==1.12.0',
'Jinja2',
'prometheus_client==0.8.0',
'pybluez',
Expand Down
40 changes: 18 additions & 22 deletions tests/test_influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def test_influxdb(
):
config = {
'url': 'http://www.google.com',
'database': 'foo',
'org': 'foo',
'bucket': 'wat',
'token': 'somelongtoken',
'gravity_payload_template': '{"measurement": "gravity", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ gravity }}}}', # noqa
'temperature_payload_template': '{"measurement": "temperature", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ temp }}}}', # noqa
}
Expand All @@ -28,26 +30,20 @@ def test_influxdb(
})
assert mock_influx_client.mock_calls == [
mock.call(
database='foo',
host='http://www.google.com',
password=None,
port=80,
ssl=False,
username=None,
verify_ssl=False,
org='foo',
token='somelongtoken',
url='http://www.google.com',
verify_ssl=False
),
mock.call().write_api(write_options=mock.ANY),
mock.call().write_api().write(
bucket='wat',
org='foo',
record='{"measurement": "temperature", "tags": {"color": "black"}, "fields": {"value": 80}}' # noqa
),
mock.call().write_api().write(
bucket='wat',
org='foo',
record='{"measurement": "gravity", "tags": {"color": "black"}, "fields": {"value": 1.054}}' # noqa
),
mock.call().write_points([
{
'measurement': 'temperature',
'tags': {'color': 'black'},
'fields': {'value': 80}
}
]),
mock.call().write_points([
{
'measurement': 'gravity',
'tags': {'color': 'black'},
'fields': {'value': 1.054}
}
])
]
38 changes: 21 additions & 17 deletions tilty/emitters/influxdb.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
""" InfluxDB emitter """
import json
import logging
from distutils import util as distutil

from influxdb import InfluxDBClient
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
from jinja2 import Template

from tilty.common import safe_get_key
Expand Down Expand Up @@ -32,21 +32,19 @@ def __init__(self, config: dict) -> None:
# temperature_payload_template = {"measurement": "temperature", "tags": {"color": "{{ color }}"}, "fields": {"value": {{ temp }}}} # noqa # pylint: disable=line-too-long
self.gravity_template = Template(config['gravity_payload_template']) # noqa
self.temperature_template = Template(config['temperature_payload_template']) # noqa
ssl = bool(distutil.strtobool(
safe_get_key(config, 'ssl', 'False')
))
self.bucket = safe_get_key(config, 'bucket')

verify_ssl = bool(distutil.strtobool(
safe_get_key(config, 'verify_ssl', 'False')
))
self.client = InfluxDBClient(
host=config['url'],
port=safe_get_key(config, 'port', 80),
username=safe_get_key(config, 'user'),
password=safe_get_key(config, 'password'),
ssl=ssl,
verify_ssl=verify_ssl,
database=config['database']
self.org = safe_get_key(config, 'org')
client = InfluxDBClient(
url=config['url'],
org=self.org,
token=safe_get_key(config, 'token'),
verify_ssl=verify_ssl
)
self.write_api = client.write_api(write_options=SYNCHRONOUS)

def emit(self, tilt_data: dict) -> None:
""" Initializer
Expand All @@ -59,16 +57,22 @@ def emit(self, tilt_data: dict) -> None:
gravity=tilt_data['gravity'],
mac=tilt_data['mac'],
temp=tilt_data['temp'],
timestamp=tilt_data['timestamp'],
)
gravity_payload = self.gravity_template.render(
color=tilt_data['color'],
gravity=tilt_data['gravity'],
mac=tilt_data['mac'],
temp=tilt_data['temp'],
timestamp=tilt_data['timestamp'],
)
LOGGER.info('[influxdb] posting temperature data')
self.client.write_points([json.loads(temperature_payload)])
self.write_api.write(
bucket=self.bucket,
org=self.org,
record=temperature_payload
)
LOGGER.info('[influxdb] posting gravity data')
self.client.write_points([json.loads(gravity_payload)])
self.write_api.write(
bucket=self.bucket,
org=self.org,
record=gravity_payload
)

0 comments on commit 36ab4d1

Please sign in to comment.