Influx related functionality finished
This commit is contained in:
34
influx_related.py
Normal file
34
influx_related.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from fastapi import FastAPI
|
||||
from influxdb_client.client.influxdb_client_async import InfluxDBClientAsync
|
||||
from influxdb_client import Point
|
||||
from config import *
|
||||
|
||||
influxdb_client: InfluxDBClientAsync
|
||||
|
||||
def init_influx(app: FastAPI):
|
||||
global influxdb_client
|
||||
influxdb_client = InfluxDBClientAsync(
|
||||
url=INFLUX_URL,
|
||||
token=INFLUX_TOKEN,
|
||||
org=INFLUX_ORG,
|
||||
)
|
||||
return influxdb_client
|
||||
|
||||
async def get_influxdb_client() -> InfluxDBClientAsync:
|
||||
global influxdb_client
|
||||
return influxdb_client
|
||||
|
||||
|
||||
async def get_influx_data(client: InfluxDBClientAsync) -> list[dict]:
|
||||
query_api = client.query_api()
|
||||
query = f'from(bucket: "{INFLUX_BUCKET}") |> range(start: -1h)'
|
||||
tables = await query_api.query(query=query, org=INFLUX_ORG)
|
||||
records = [
|
||||
{"time": record.get_time(), "value": record.get_value(), "field": record.get_field()}
|
||||
for table in tables for record in table.records
|
||||
]
|
||||
return records
|
||||
|
||||
async def write_influx_data(client: InfluxDBClientAsync, temp:float):
|
||||
point = Point("measurement").field("temperature", temp)
|
||||
return await client.write_api().write(bucket=INFLUX_BUCKET, record=point)
|
||||
Reference in New Issue
Block a user