Selecting data

Once your data is written to InfluxDB, you are ready to select it for analysis.

This package provides the DBQuery class to connect to your InfluxDB and read data from it. Here is an example of it’s usage.

[1]:
from chronobiology.chronobiology import DBQuery
[2]:
# Create a client to access a local database.
client = DBQuery('my_db', 'admin', 'pass')

Getting measurements

[3]:
# Get all measurements found in database.
client.get_measurements()
[3]:
['test_series']

Getting tags

[4]:
# Get tags of a series.
client.get_tags('test_series')
[4]:
['sensor_id']
[5]:
# Try to get tags of a nonexistent series.
client.get_tags('foo')
[5]:
[]

Getting fields

[6]:
# Get fields of a series.
client.get_fields('test_series')
[6]:
['is_night', 'value']
[7]:
# Get fields of a series with their types.
client.get_fields('test_series', return_types=True)
[7]:
(['is_night', 'value'], ['boolean', 'float'])
[8]:
# Try to get fields of a nonexistent series.
client.get_fields('foo')
[8]:
[]

Getting keys

Keys is a set of all possible values of a tag.

[9]:
# Get keys of a series tag.
client.get_keys('test_series', 'sensor_id')
[9]:
['1']
[10]:
# Try to get keys of a nonexistent series.
client.get_keys('foo', 'sensor_id')
[10]:
[]
[11]:
# Try to get keys of a nonexistent tag.
client.get_keys('test_series', 'foo')
[11]:
[]

Getting data

Data includes both fields and tags.

[12]:
# Get all data from a series.
client.get_data('test_series', '*')
[12]:
{'time': array(['2021-01-01T00:00:00.000000000', '2021-01-01T01:00:00.000000000',
        '2021-01-01T02:00:00.000000000', '2021-01-01T03:00:00.000000000',
        '2021-01-01T04:00:00.000000000'], dtype='datetime64[ns]'),
 'is_night': array([ True,  True,  True,  True,  True]),
 'value': array([2.6367757 , 9.63496467, 5.48810458, 2.30511222, 5.83427087]),
 'sensor_id': array(['1', '1', '1', '1', '1'], dtype='<U1')}
[13]:
# Get values of a single field of a series.
client.get_data('test_series', 'value')
[13]:
{'time': array(['2021-01-01T00:00:00.000000000', '2021-01-01T01:00:00.000000000',
        '2021-01-01T02:00:00.000000000', '2021-01-01T03:00:00.000000000',
        '2021-01-01T04:00:00.000000000'], dtype='datetime64[ns]'),
 'value': array([2.6367757 , 9.63496467, 5.48810458, 2.30511222, 5.83427087])}
[14]:
# Get values of multiple fields/tags of a series.
client.get_data('test_series', ['value', 'sensor_id'])
[14]:
{'time': array(['2021-01-01T00:00:00.000000000', '2021-01-01T01:00:00.000000000',
        '2021-01-01T02:00:00.000000000', '2021-01-01T03:00:00.000000000',
        '2021-01-01T04:00:00.000000000'], dtype='datetime64[ns]'),
 'value': array([2.6367757 , 9.63496467, 5.48810458, 2.30511222, 5.83427087]),
 'sensor_id': array(['1', '1', '1', '1', '1'], dtype='<U1')}
[15]:
# Get data filtered by a single key value.
client.get_data('test_series', '*', keys={'sensor_id': '1'})
[15]:
{'time': array(['2021-01-01T00:00:00.000000000', '2021-01-01T01:00:00.000000000',
        '2021-01-01T02:00:00.000000000', '2021-01-01T03:00:00.000000000',
        '2021-01-01T04:00:00.000000000'], dtype='datetime64[ns]'),
 'is_night': array([ True,  True,  True,  True,  True]),
 'value': array([2.6367757 , 9.63496467, 5.48810458, 2.30511222, 5.83427087]),
 'sensor_id': array(['1', '1', '1', '1', '1'], dtype='<U1')}
[16]:
# Get data filtered by multiple key values.
client.get_data('test_series', '*', keys={'sensor_id': ['1', '2']})
[16]:
{'time': array(['2021-01-01T00:00:00.000000000', '2021-01-01T01:00:00.000000000',
        '2021-01-01T02:00:00.000000000', '2021-01-01T03:00:00.000000000',
        '2021-01-01T04:00:00.000000000'], dtype='datetime64[ns]'),
 'is_night': array([ True,  True,  True,  True,  True]),
 'value': array([2.6367757 , 9.63496467, 5.48810458, 2.30511222, 5.83427087]),
 'sensor_id': array(['1', '1', '1', '1', '1'], dtype='<U1')}
[17]:
# Get data newer than a given timestamp (inclusive).
client.get_data('test_series', '*', keys={'sensor_id': '1'}, start='2021-01-01 02:00:00')
[17]:
{'time': array(['2021-01-01T02:00:00.000000000', '2021-01-01T03:00:00.000000000',
        '2021-01-01T04:00:00.000000000'], dtype='datetime64[ns]'),
 'is_night': array([ True,  True,  True]),
 'value': array([5.48810458, 2.30511222, 5.83427087]),
 'sensor_id': array(['1', '1', '1'], dtype='<U1')}
[18]:
# Get data older than a given timestamp (exclusive).
client.get_data('test_series', '*', keys={'sensor_id': '1'}, stop='2021-01-01 03:00:00')
[18]:
{'time': array(['2021-01-01T00:00:00.000000000', '2021-01-01T01:00:00.000000000',
        '2021-01-01T02:00:00.000000000'], dtype='datetime64[ns]'),
 'is_night': array([ True,  True,  True]),
 'value': array([2.6367757 , 9.63496467, 5.48810458]),
 'sensor_id': array(['1', '1', '1'], dtype='<U1')}