pyFTSex/service/api/influx.py

47 lines
1.8 KiB
Python
Raw Permalink Normal View History

2024-08-15 12:15:32 +04:00
import influxdb_client
import requests, os
token = "dILwu7gJsK7spzhXPTj1LqlFi_LqdJdSS_3G_VvLZ27fi4nEpSiAyuM-gp8Qe6wS5fxbX_DXoW6dHaUyS5YREA=="
#token = os.environ.get('INFLUXDB_TOKEN')
org = "UlSTU"
bucket="Prometheus"
url = "http://influx.athene.tech"
client = influxdb_client.InfluxDBClient(url=url, token=token, org=org)
def get_field(interval='10m', field='pve_cpu_usage_ratio'):
query_api = client.query_api()
query = f"""
from(bucket: "{bucket}")
|> range(start: -{interval})
|> filter(fn: (r) => r["_field"] == "{field}")
|> pivot(rowKey:["_time"], columnKey: ["id"], valueColumn: "_value")
"""
return query_api.query_data_frame(query, org=org)
def get_field_at_container(interval='10m', field="pve_cpu_usage_ratio", container="lxc/116"):
query_api = client.query_api()
query = f"""
from(bucket: "{bucket}")
|> range(start: -{interval})
|> filter(fn: (r) => r["_field"] == "{field}")
|> filter(fn: (r) => r["id"] == "{container}")
|> pivot(rowKey:["_time"], columnKey: ["id"], valueColumn: "_value")
"""
return query_api.query_data_frame(query, org=org)
#TODO: Удалить лишние объекты
def request_delete_measurement(bucket, measurement):
response = requests.post(f'{url}/api/v2/delete?org={org}&bucket={bucket}',
headers={'Authorization': f'Token {token}', 'Content-Type': 'application/json' },
data={'start': "2020-03-01",
'stop': "2025-03-01",
'predicate': f'_measurement={measurement}'})
print(response.content)
if __name__ == "__main__":
print("influx import test")
## request_delete_measurement(bucket="Prometheus", measurement="prometheus")
data = get_field_at_container()
print("Ok")