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)
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,
...@@ -300,29 +296,36 @@ class JsonDSAPI(object): ...@@ -300,29 +296,36 @@ class JsonDSAPI(object):
group_by_expr, group_by_expr,
) )
@classmethod
def format_time_series(cls, results: List[Tuple[List["QueryConfig"], Dict[str, Any]]]):
result = []
for query_configs, data in results:
request_metrics = {qc.alias or qc.metric_type for qc in query_configs}
for row in data["data"]:
for field in row:
if field == "target":
continue
result.append(
{"target": f"{field} | {row['target']}", "datapoints": row[field]}
)
if field in request_metrics:
request_metrics.remove(field)
# Add metrics without data
for rm_name in request_metrics:
result.append({"target": f"{rm_name}", "datapoints": []})
return result
@classmethod @classmethod
def format_result( def format_result(
cls, result, result_type: str = "timeseries", request_metrics: Set["str"] = None cls,
results: List[Tuple[List["QueryConfig"], Dict[str, Any]]],
result_type: str = "time_series",
): ):
""" if not hasattr(cls, f"format_{result_type}"):
Formatting output raise HTTPException(
:param result: status_code=404, detail=f"Requested format '{result_type}' not supported"
:param result_type: )
:param request_metrics: Set requested metric return getattr(cls, f"format_{result_type}")(results)
:return:
"""
r = []
for row in result["data"]:
for field in row:
if field == "target":
continue
r.append({"target": f"{field} | {row['target']}", "datapoints": row[field]})
if field in request_metrics:
request_metrics.remove(field)
# Add metrics without data
for rm_name in request_metrics:
r.append({"target": f"{rm_name}", "datapoints": []})
return r
@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