diff --git a/influxdb_api.py b/influxdb_api.py index 36947eb..21f891a 100644 --- a/influxdb_api.py +++ b/influxdb_api.py @@ -1342,14 +1342,27 @@ def query_buckets(client: InfluxDBClient=client) -> list[str]: return buckets_list -def query_tags(bucket: str, client: InfluxDBClient=client) -> list[str]: +def query_measurements(bucket: str, client: InfluxDBClient=client) -> list[str]: + query = f''' + import "influxdata/influxdb/schema" + schema.measurements(bucket: "{bucket}") + ''' + + result = client.query_api().query(query) + + # 提取测量名称 + measurements = [row.values["_value"] for table in result for row in table.records] + return measurements + + +def query_tags(bucket: str, measurement: str, client: InfluxDBClient=client) -> list[str]: query_api = client.query_api() # 定义 Flux 查询 query = f''' - from(bucket: "your-bucket") + from(bucket: "{bucket}") |> range(start: -1y) # 时间范围(可根据需要调整) - |> filter(fn: (r) => r._measurement == "{bucket}") + |> filter(fn: (r) => r._measurement == "{measurement}") |> tagKeys() # 直接获取所有 Tag 键名 ''' diff --git a/main.py b/main.py index b8770d5..11cb51e 100644 --- a/main.py +++ b/main.py @@ -2134,9 +2134,13 @@ async def query_scada_data_by_device_id_and_time(ids: str, querytime: str): async def fastapi_query_influxdb_buckets(): return influxdb_api.query_buckets() +@app.get("/queryinfluxdbbucketmeasurements/") +async def fastapi_query_influxdb_bucket_measurements(bucket: str): + return influxdb_api.query_measurements(bucket=bucket) + @app.get("/queryinfluxdbbuckettags/") -async def fastapi_query_influxdb_bucket_tags(bucket: str): - return influxdb_api.query_tags(bucket=bucket) +async def fastapi_query_influxdb_bucket_tags(bucket: str, measurement: str): + return influxdb_api.query_tags(bucket=bucket, measurement=measurement) # DingZQ, 2024-12-31, generate openapi.json