diff --git a/ee/api/chalicelib/core/traces.py b/ee/api/chalicelib/core/traces.py index d77b0f580..64c1c6df1 100644 --- a/ee/api/chalicelib/core/traces.py +++ b/ee/api/chalicelib/core/traces.py @@ -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} diff --git a/ee/api/routers/ee.py b/ee/api/routers/ee.py index f63d0dd3a..9a79551b7 100644 --- a/ee/api/routers/ee.py +++ b/ee/api/routers/ee.py @@ -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)} diff --git a/ee/api/schemas_ee.py b/ee/api/schemas_ee.py index 06ae8f2ba..50eb3d03f 100644 --- a/ee/api/schemas_ee.py +++ b/ee/api/schemas_ee.py @@ -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 diff --git a/ee/scripts/helm/db/init_dbs/postgresql/1.6.1/1.6.1.sql b/ee/scripts/helm/db/init_dbs/postgresql/1.6.1/1.6.1.sql index 00d871cac..b28f28b62 100644 --- a/ee/scripts/helm/db/init_dbs/postgresql/1.6.1/1.6.1.sql +++ b/ee/scripts/helm/db/init_dbs/postgresql/1.6.1/1.6.1.sql @@ -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; \ No newline at end of file diff --git a/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql b/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql index d78a99c27..a59e25e54 100644 --- a/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql +++ b/ee/scripts/helm/db/init_dbs/postgresql/init_schema.sql @@ -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');