feat(api): changed update widget

feat(api): FOSS&EE optimized /projects
This commit is contained in:
Taha Yassine Kraiem 2022-04-06 14:17:24 +02:00
parent 584de8d09c
commit 3c30ebf76d
6 changed files with 34 additions and 28 deletions

View file

@ -167,7 +167,7 @@ def add_widget(project_id, user_id, dashboard_id, data: schemas.AddWidgetToDashb
return helper.dict_to_camel_case(row)
def update_widget(project_id, user_id, dashboard_id, widget_id, data: schemas.AddWidgetToDashboardPayloadSchema):
def update_widget(project_id, user_id, dashboard_id, widget_id, data: schemas.UpdateWidgetPayloadSchema):
with pg_client.PostgresClient() as cur:
pg_query = """UPDATE dashboard_widgets
SET config= %(config)s

View file

@ -65,27 +65,26 @@ def get_projects(tenant_id, recording_state=False, gdpr=None, recorded=False, st
FROM public.projects AS s
{'LEFT JOIN LATERAL (SELECT COUNT(*) AS count FROM public.integrations WHERE s.project_id = integrations.project_id LIMIT 1) AS stack_integrations ON TRUE' if stack_integrations else ''}
WHERE s.deleted_at IS NULL
ORDER BY s.project_id;"""
)
ORDER BY s.project_id;""")
rows = cur.fetchall()
if recording_state:
project_ids = [f'({r["project_id"]})' for r in rows]
query = f"""SELECT projects.project_id, COALESCE(MAX(start_ts), 0) AS last
FROM (VALUES {",".join(project_ids)}) AS projects(project_id)
LEFT JOIN sessions USING (project_id)
GROUP BY project_id;"""
cur.execute(
query=query
)
query = cur.mogrify(f"""SELECT projects.project_id, COALESCE(MAX(start_ts), 0) AS last
FROM (VALUES {",".join(project_ids)}) AS projects(project_id)
LEFT JOIN sessions USING (project_id)
WHERE sessions.start_ts >= %(startDate)s AND sessions.start_ts <= %(endDate)s
GROUP BY project_id;""",
{"startDate": TimeUTC.now(delta_days=-3), "endDate": TimeUTC.now(delta_days=1)})
cur.execute(query=query)
status = cur.fetchall()
for r in rows:
r["status"] = "red"
for s in status:
if s["project_id"] == r["project_id"]:
if s["last"] < TimeUTC.now(-2):
r["status"] = "red"
elif s["last"] < TimeUTC.now(-1):
if TimeUTC.now(-2) <= s["last"] < TimeUTC.now(-1):
r["status"] = "yellow"
else:
elif s["last"] >= TimeUTC.now(-1):
r["status"] = "green"
break

View file

@ -68,7 +68,7 @@ def create_metric_and_add_to_dashboard(projectId: int, dashboardId: int,
@app.post('/{projectId}/dashboards/{dashboardId}/widgets/{widgetId}', tags=["dashboard"])
@app.put('/{projectId}/dashboards/{dashboardId}/widgets/{widgetId}', tags=["dashboard"])
def update_widget_in_dashboard(projectId: int, dashboardId: int, widgetId: int,
data: schemas.AddWidgetToDashboardPayloadSchema = Body(...),
data: schemas.UpdateWidgetPayloadSchema = Body(...),
context: schemas.CurrentContext = Depends(OR_context)):
return dashboards2.update_widget(project_id=projectId, user_id=context.user_id, dashboard_id=dashboardId,
widget_id=widgetId, data=data)

View file

@ -893,8 +893,7 @@ class EditDashboardSchema(CreateDashboardSchema):
is_pinned: Optional[bool] = Field(default=None)
class AddWidgetToDashboardPayloadSchema(BaseModel):
metric_id: int = Field(default=None)
class UpdateWidgetPayloadSchema(BaseModel):
# if you change the config attribute name, please make sure to update it in dashboard2.py
config: dict = Field(default={"col": 1, "row": 1, "position": 0})
@ -902,6 +901,13 @@ class AddWidgetToDashboardPayloadSchema(BaseModel):
alias_generator = attribute_to_camel_case
class AddWidgetToDashboardPayloadSchema(UpdateWidgetPayloadSchema):
metric_id: int = Field(default=None)
class Config:
alias_generator = attribute_to_camel_case
# these values should match the keys in metrics table
class TemplateKeys(str, Enum):
count_sessions = "count_sessions"

1
ee/api/.gitignore vendored
View file

@ -259,3 +259,4 @@ Pipfile
/build_alerts.sh
/routers/subs/metrics.py
/routers/subs/v1_api.py
/chalicelib/core/dashboards2.py

View file

@ -82,22 +82,22 @@ def get_projects(tenant_id, recording_state=False, gdpr=None, recorded=False, st
rows = cur.fetchall()
if recording_state:
project_ids = [f'({r["project_id"]})' for r in rows]
query = f"""SELECT projects.project_id, COALESCE(MAX(start_ts), 0) AS last
FROM (VALUES {",".join(project_ids)}) AS projects(project_id)
LEFT JOIN sessions USING (project_id)
GROUP BY project_id;"""
cur.execute(
query=query
)
query = cur.mogrify(f"""SELECT projects.project_id, COALESCE(MAX(start_ts), 0) AS last
FROM (VALUES {",".join(project_ids)}) AS projects(project_id)
LEFT JOIN sessions USING (project_id)
WHERE sessions.start_ts >= %(startDate)s AND sessions.start_ts <= %(endDate)s
GROUP BY project_id;""",
{"startDate": TimeUTC.now(delta_days=-3), "endDate": TimeUTC.now(delta_days=1)})
cur.execute(query=query)
status = cur.fetchall()
for r in rows:
r["status"] = "red"
for s in status:
if s["project_id"] == r["project_id"]:
if s["last"] < TimeUTC.now(-2):
r["status"] = "red"
elif s["last"] < TimeUTC.now(-1):
if TimeUTC.now(-2) <= s["last"] < TimeUTC.now(-1):
r["status"] = "yellow"
else:
elif s["last"] >= TimeUTC.now(-1):
r["status"] = "green"
break