Skip to main content

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.0
  • INFLUXDB_PORT: Defaults to 8086
  • INFLUXDB_DB: Defaults to metrics
  • INFLUXDB_USER: Defaults to admin
  • INFLUXDB_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Grafane-0.5.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Grafane-0.5-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file Grafane-0.5.tar.gz.

File metadata

  • Download URL: Grafane-0.5.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

Hashes for Grafane-0.5.tar.gz
Algorithm Hash digest
SHA256 3d590ad16a619ec4a5c5bcb2915bdba2d11b2342ef26a3949ab3ee67076fa503
MD5 0bc10fdb4745c85bdc9f8a99ca2e0b0f
BLAKE2b-256 e56b152e570e17de96d5536626b991c26da124379c6eb729f32af3ba0498c4e6

See more details on using hashes here.

File details

Details for the file Grafane-0.5-py3-none-any.whl.

File metadata

  • Download URL: Grafane-0.5-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

Hashes for Grafane-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 5a81b3072cf9b685f09760a620483bf98fbc4aed6a53802f821be46b4c123550
MD5 beeaacb291abaa85c2c36c37bba5c118
BLAKE2b-256 2038a58beca43f0aa6818409c3686b2c80db1345cb54e3eea75300f67bc28053

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page