No project description provided
Project description
Grafane
A very opinionated influxdb client that uses the official python client and is very much inspired in grafana's query builder.
Setup
pip install grafane
In order to query influxdb this library expects the following environment variables to be set:
INFLUXDB_HOST: Defaults to 0.0.0.0INFLUXDB_PORT: Defaults to 8086INFLUXDB_DB: Defaults to metricsINFLUXDB_USER: Defaults to adminINFLUXDB_USER_PASSWORD: Defaults to admin123
Drop measurement
c = Grafane(metric='test')
c.drop_measurement() # Drops test from influxdb
Write
With:
points = [
{
'fields': {
'value': 1.2,
'value2': 1.3,
},
'tags': {
'tag1': 'value1',
'tag2': 'value2'
}
},
{
'fields': {
'value': 1.86,
'value2': 2.3,
},
'tags': {
'tag1': 'value2',
'tag2': 'value1'
}
},
{
'fields': {
'value': 1.4,
'value2': 1.1,
},
'tags': {
'tag1': 'value3',
'tag2': 'value2'
}
},
{
'fields': {
'value': 1.8,
'value2': 1.95,
},
'tags': {
'tag1': 'value1',
'tag2': 'value2'
}
},
]
You can do either do multiple single queries:
from grafane import Grafane
c = Grafane(metric='generic') # Metric defaults to generic
for p in points:
c.report(p['fields'], p['tags'])
Or a single query with multiple points:
c.report_points(points)
if you don't provide time for a point it defaults to:
>>> datetime.utcnow().replace(tzinfo=pytz.utc)
datetime.datetime(2019, 2, 8, 19, 32, 38, 788003, tzinfo=<UTC>)
Read
Select
In [6]: c.select(fields='value')
In [7]: c.execute_query()
Out[7]:
[{'time': '2019-02-10T20:37:13.786477056Z', 'value': 1.2},
{'time': '2019-02-10T20:37:13.786508032Z', 'value': 1.86},
{'time': '2019-02-10T20:37:13.786518016Z', 'value': 1.4},
{'time': '2019-02-10T20:37:13.786535936Z', 'value': 1.8}]
Select multiple fields
In [16]: c.select(fields=['value', 'value2'])
In [17]: c.execute_query()
Out[17]:
[{'time': '2019-02-10T20:42:37.22864512Z', 'value': 1.2, 'value2': 1.3},
{'time': '2019-02-10T20:42:37.228871936Z', 'value': 1.86, 'value2': 2.3},
{'time': '2019-02-10T20:42:37.228883968Z', 'value': 1.4, 'value2': 1.1},
{'time': '2019-02-10T20:42:37.22889216Z', 'value': 1.8, 'value2': 1.95}]
Select w/ aggregation
In [18]: c.select(fields='value', aggregation='sum')
In [19]: c.execute_query()
Out[19]: [{'time': '1970-01-01T00:00:00Z', 'sum': 6.26}]
Select multiple fields w/ aggregation
In [20]: c.select(fields=['value', 'value2'], aggregation=['sum', 'mean'])
In [21]: c.execute_query()
Out[21]: [{'time': '1970-01-01T00:00:00Z', 'sum': 6.26, 'mean': 1.6625}]
Group aggregated results in time blocks
In [22]: c.select(fields=['value', 'value2'], aggregation=['sum', 'mean'])
In [23]: c.time_block('1m')
In [24]: c.execute_query()
Out[24]:
[{'time': '2019-02-10T20:42:00Z', 'sum': 6.26, 'mean': 1.6625},
{'time': '2019-02-10T20:43:00Z', 'sum': None, 'mean': None},
{'time': '2019-02-10T20:44:00Z', 'sum': None, 'mean': None},
{'time': '2019-02-10T20:45:00Z', 'sum': None, 'mean': None},
{'time': '2019-02-10T20:46:00Z', 'sum': None, 'mean': None},
{'time': '2019-02-10T20:47:00Z', 'sum': None, 'mean': None}]
When grouping time blocks, in order to avoid empty rows you need to fill results with None
In [29]: c.select(fields=['value', 'value2'], aggregation=['sum', 'mean'])
In [30]: c.time_block('1m')
In [31]: c.fill_with('none')
In [32]: c.execute_query()
Out[32]: [{'time': '2019-02-10T20:42:00Z', 'sum': 6.26, 'mean': 1.6625}]
Group aggregated results by tag values
In [34]: c.select(fields=['value', 'value2'], aggregation=['sum', 'mean'])
In [35]: c.group_by('tag1')
In [36]: c.execute_query()
Out[36]:
[{'tags': {'tag1': 'value1'},
'time': '1970-01-01T00:00:00Z',
'sum': 3,
'mean': 1.625},
{'tags': {'tag1': 'value2'},
'time': '1970-01-01T00:00:00Z',
'sum': 1.86,
'mean': 2.3},
{'tags': {'tag1': 'value3'},
'time': '1970-01-01T00:00:00Z',
'sum': 1.4,
'mean': 1.1}]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file Grafane-0.6.tar.gz.
File metadata
- Download URL: Grafane-0.6.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0539dce5ff4bd4756a312f2e918eff86c677c8f2d09ef599639840cf9e6a818
|
|
| MD5 |
020783a7b0b9ccf59ba98f0237163a1b
|
|
| BLAKE2b-256 |
7575b1ab04960ec75ac8e38659b7f1790e4302b4d9fc33d29e0238103db0c0c7
|
File details
Details for the file Grafane-0.6-py3-none-any.whl.
File metadata
- Download URL: Grafane-0.6-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.0.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e5483c40f720fe82688132c6c35864d2f526be105a44f6d2f6b7f888a03c7ef
|
|
| MD5 |
47e7f3a2a8ae53f8602e9a881fe9b397
|
|
| BLAKE2b-256 |
39a81aa78c3248620d5d5038a2c203882d7b73d9b5f7735ddbb4b5e181bdb972
|