Skip to content

Instantly share code, notes, and snippets.

@zgrge
Created October 12, 2022 06:56
Show Gist options
  • Save zgrge/4cd09d0415b94f864a6f22206f8aa503 to your computer and use it in GitHub Desktop.
Save zgrge/4cd09d0415b94f864a6f22206f8aa503 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import random
import time
from influxdb_client import InfluxDBClient, Point, WriteOptions
#from influxdb_client.client.write_api import SYNCHRONOUS
mydata = {'a':'value1', 'b':'value2', 'c':'value3'}
rand_vals = list()
client = InfluxDBClient(url="http://172.16.40.14:8086", \
token="PY7Mfq7KwnoM7pnLfvXoD4uv8mk5DZzqDRtZ-6IZFFWtkCvKqL1oUeTLDCUsQhNLmP833JvHroZRslPmhhhSWw==", \
org="gc", bucket='onehour', debug=False)
write_api = client.write_api(write_options=WriteOptions(batch_size=50_000, flush_interval=10_000))
for i in range(5):
newdata = {'fields': mydata.copy(), \
'measurement':'trx',
'tags': {'1':'x', '2':'y'}}
rand_val = str(random.randint(100_000_000_000,999_999_999_999))
rand_vals.append(rand_val)
newdata['fields']['rand_val'] = rand_val
write_api.write(bucket="onehour", record=newdata)
time.sleep(1)
print("*** ", rand_val, end="\n")
write_api.close()
client.close()
client = InfluxDBClient(url="http://172.16.40.14:8086", \
token="PY7Mfq7KwnoM7pnLfvXoD4uv8mk5DZzqDRtZ-6IZFFWtkCvKqL1oUeTLDCUsQhNLmP833JvHroZRslPmhhhSWw==", \
org="gc", bucket='onehour', debug=False)
for i in rand_vals:
query = 'from(bucket: "onehour") |> range(start: -1d)' \
' |> filter(fn: (r) => r["_field"] == "rand_val" and r["_value"] == "{}")'.format(i)
result = client.query_api().query(query=query)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment