Verified Commit b99bfdf6 authored by Andrey Vertiprahov's avatar Andrey Vertiprahov
Browse files

Fix default format result on grafanads services query.

parent 79377b7d
...@@ -231,12 +231,8 @@ class JsonDSAPI(object): ...@@ -231,12 +231,8 @@ class JsonDSAPI(object):
except ClickhouseError as e: except ClickhouseError as e:
self.logger.error("Clickhouse query error: %s", e) self.logger.error("Clickhouse query error: %s", e)
raise HTTPException(status_code=500, detail=e) raise HTTPException(status_code=500, detail=e)
r += self.format_result( r += [(query_configs, orjson.loads(result))]
orjson.loads(result), return self.format_result(r, result_type=req.result_type)
request_metrics={qc.alias or qc.metric_type for qc in query_configs},
return r
def get_query( def get_query(
self, self,
...@@ -301,28 +297,35 @@ class JsonDSAPI(object): ...@@ -301,28 +297,35 @@ class JsonDSAPI(object):
) )
@classmethod @classmethod
def format_result( def format_time_series(cls, results: List[Tuple[List["QueryConfig"], Dict[str, Any]]]):
cls, result, result_type: str = "timeseries", request_metrics: Set["str"] = None result = []
): for query_configs, data in results:
""" request_metrics = {qc.alias or qc.metric_type for qc in query_configs}
Formatting output for row in data["data"]:
:param result:
:param result_type:
:param request_metrics: Set requested metric
r = []
for row in result["data"]:
for field in row: for field in row:
if field == "target": if field == "target":
continue continue
r.append({"target": f"{field} | {row['target']}", "datapoints": row[field]}) result.append(
{"target": f"{field} | {row['target']}", "datapoints": row[field]}
if field in request_metrics: if field in request_metrics:
request_metrics.remove(field) request_metrics.remove(field)
# Add metrics without data # Add metrics without data
for rm_name in request_metrics: for rm_name in request_metrics:
r.append({"target": f"{rm_name}", "datapoints": []}) result.append({"target": f"{rm_name}", "datapoints": []})
return r return result
def format_result(
results: List[Tuple[List["QueryConfig"], Dict[str, Any]]],
result_type: str = "time_series",
if not hasattr(cls, f"format_{result_type}"):
raise HTTPException(
status_code=404, detail=f"Requested format '{result_type}' not supported"
return getattr(cls, f"format_{result_type}")(results)
@staticmethod @staticmethod
def get_target_expression(table_name: str = None) -> Tuple[str, Optional[str]]: def get_target_expression(table_name: str = None) -> Tuple[str, Optional[str]]:
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment