Last active
July 21, 2019 14:19
-
-
Save shantanoo-desai/cefbe3155630aa2d22b6391928cd98ef to your computer and use it in GitHub Desktop.
Data Query Function Snippet for the Uploader Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_points(conf): | |
# We use the configuration in the TOML file | |
# to get the data points using `influxdb-python` | |
# Refer to Repository for complete code | |
# create the query string | |
dict_str = ''.join('"{}"=\'{}\''.format(key,value) for key, value in conf['tags'].items()) | |
QUERY = 'SELECT "{}" FROM "{}" WHERE "status"=0 AND {} LIMIT {}'.format( | |
'","'.join(conf['fields']), | |
conf['measurement'], | |
dict_str, | |
conf['limit'] | |
) | |
try: | |
# query the db | |
query_results = LOCAL_DB.query(QUERY, epoch='ms') | |
except InfluxDBClientError as e: | |
LOCAL_DB.close() | |
CLOUD_DB.close() | |
raise(e) | |
if len(list(query_results)) == 0: | |
# no data for the query | |
# either everything uploaded or something wrong with query | |
else: | |
# Re-format the data for the cloud upload | |
# according to `influxdb-python` library | |
batch = [] | |
for point in list(query_results)[0]: | |
# standard dict as needed by `influxdb-python` | |
# module to write to InfluxDB via HTTP | |
upload_json_body = { | |
'measurement': conf['measurement'], | |
'fields': {} | |
} | |
if 'tags' in conf: | |
# if tags exist add them | |
upload_json_body['tags'] = conf['tags'] | |
upload_json_body['time'] = point['time'] | |
# remove the `time` key to get all the `fields` | |
del point['time'] | |
upload_json_body['fields'] = point | |
# add the point to the batch array | |
batch.append(upload_json_body) | |
upload_data(batch) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment