feat(DB): traces/trails index

feat(api): get all possible traces/trails actions
feat(api): search traces/trails by actions
feat(api): search traces/trails by user
This commit is contained in:
Taha Yassine Kraiem 2022-05-06 11:56:03 +02:00
parent 21d8d28a79
commit ef0edebb3d
5 changed files with 29 additions and 5 deletions

View file

@ -154,27 +154,43 @@ async def process_traces_queue():
def get_all(tenant_id, data: schemas_ee.TrailSearchPayloadSchema):
with pg_client.PostgresClient() as cur:
conditions = ["tenant_id=%(tenant_id)s", "created_at>=%(startDate)s", "created_at<=%(endDate)s"]
if data.user_id is not None:
conditions.append("user_id=%(user_id)s")
if data.action is not None:
conditions.append("action=%(action)s")
cur.execute(
cur.mogrify(
"""SELECT COUNT(*) AS count,
f"""SELECT COUNT(*) AS count,
COALESCE(JSONB_AGG(full_traces)
FILTER (WHERE rn > %(p_start)s AND rn <= %(p_end)s), '[]'::JSONB) AS sessions
FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY created_at) AS rn
FROM traces
WHERE tenant_id=%(tenant_id)s
AND created_at>=%(startDate)s
AND created_at<=%(endDate)s
WHERE {" AND ".join(conditions)}
ORDER BY created_at) AS full_traces;""",
{"tenant_id": tenant_id,
"startDate": data.startDate,
"endDate": data.endDate,
"p_start": (data.page - 1) * data.limit,
"p_end": data.page * data.limit})
"p_end": data.page * data.limit,
**data.dict()})
)
rows = cur.fetchall()
return helper.list_to_camel_case(rows)
def get_available_actions(tenant_id):
with pg_client.PostgresClient() as cur:
cur.execute(cur.mogrify(
f"""SELECT DISTINCT action
FROM traces
WHERE tenant_id=%(tenant_id)s
ORDER BY 1""",
{"tenant_id": tenant_id}))
rows = cur.fetchall()
return [r["action"] for r in rows]
cron_jobs = [
{"func": process_traces_queue, "trigger": "interval", "seconds": config("traces_period", cast=int, default=60),
"misfire_grace_time": 20}

View file

@ -67,3 +67,8 @@ def get_trails(data: schemas_ee.TrailSearchPayloadSchema = Body(...),
return {
'data': traces.get_all(tenant_id=context.tenant_id, data=data)
}
@app.post('/trails/actions', tags=["traces", "trails"])
def get_available_trail_actions(context: schemas.CurrentContext = Depends(OR_context)):
return {'data': traces.get_available_actions(tenant_id=context.tenant_id)}

View file

@ -29,6 +29,7 @@ class TrailSearchPayloadSchema(schemas._PaginatedSchema):
startDate: int = Field(default=TimeUTC.now(-7))
endDate: int = Field(default=TimeUTC.now(1))
user_id: Optional[int] = Field(default=None)
action: Optional[str] = Field(default=None)
class Config:
alias_generator = schemas.attribute_to_camel_case

View file

@ -11,4 +11,5 @@ ALTER TABLE IF EXISTS dashboards
CREATE INDEX IF NOT EXISTS traces_created_at_idx ON traces (created_at);
CREATE INDEX IF NOT EXISTS traces_action_idx ON traces (action);
COMMIT;

View file

@ -786,6 +786,7 @@ $$
CREATE INDEX IF NOT EXISTS traces_user_id_idx ON traces (user_id);
CREATE INDEX IF NOT EXISTS traces_tenant_id_idx ON traces (tenant_id);
CREATE INDEX IF NOT EXISTS traces_created_at_idx ON traces (created_at);
CREATE INDEX IF NOT EXISTS traces_action_idx ON traces (action);
CREATE TYPE metric_type AS ENUM ('timeseries','table', 'predefined');
CREATE TYPE metric_view_type AS ENUM ('lineChart','progress','table','pieChart','areaChart','barChart','stackedBarChart','stackedBarLineChart','overview','map');