feat(api): alerts-custom-metrics transform payload and response to support UI

This commit is contained in:
Taha Yassine Kraiem 2022-02-08 18:51:49 +01:00
parent 38b18b11b0
commit d7a0b7cda1
3 changed files with 21 additions and 6 deletions

View file

@ -18,7 +18,7 @@ def get(id):
{"id": id})
)
a = helper.dict_to_camel_case(cur.fetchone())
return __process_circular(a)
return helper.custom_alert_to_front(__process_circular(a))
def get_all(project_id):
@ -31,8 +31,8 @@ def get_all(project_id):
{"project_id": project_id})
cur.execute(query=query)
all = helper.list_to_camel_case(cur.fetchall())
for a in all:
a = __process_circular(a)
for i in range(len(all)):
all[i] = helper.custom_alert_to_front(__process_circular(all[i]))
return all
@ -58,7 +58,7 @@ def create(project_id, data: schemas.AlertSchema):
{"project_id": project_id, **data})
)
a = helper.dict_to_camel_case(cur.fetchone())
return {"data": helper.dict_to_camel_case(__process_circular(a))}
return {"data": helper.custom_alert_to_front(helper.dict_to_camel_case(__process_circular(a)))}
def update(id, data: schemas.AlertSchema):
@ -81,7 +81,7 @@ def update(id, data: schemas.AlertSchema):
{"id": id, **data})
cur.execute(query=query)
a = helper.dict_to_camel_case(cur.fetchone())
return {"data": __process_circular(a)}
return {"data": helper.custom_alert_to_front(__process_circular(a))}
def process_notifications(data):

View file

@ -377,3 +377,10 @@ def old_search_payload_to_flat(values):
v["isEvent"] = False
values["filters"] = values.pop("events") + values.get("filters", [])
return values
def custom_alert_to_front(values):
# to support frontend format for payload
if values.get("seriesId") is not None and values["query"]["left"] == schemas.AlertColumn.custom:
values["query"]["left"] = values["seriesId"]
return values

View file

@ -316,7 +316,7 @@ class MathOperator(str, Enum):
class _AlertQuerySchema(BaseModel):
left: AlertColumn = Field(...)
left: Union[AlertColumn, int] = Field(...)
right: float = Field(...)
# operator: Literal["<", ">", "<=", ">="] = Field(...)
operator: MathOperator = Field(...)
@ -335,6 +335,14 @@ class AlertSchema(BaseModel):
query: _AlertQuerySchema = Field(...)
series_id: Optional[int] = Field(None)
@root_validator(pre=True)
def transform_alert(cls, values):
if values.get("seriesId") is None and isinstance(values["query"]["left"], int):
values["seriesId"] = values["query"]["left"]
values["query"]["left"] = AlertColumn.custom
return values
@root_validator
def alert_validator(cls, values):
if values.get("query") is not None and values["query"].left == AlertColumn.custom: