feat(chalice): refactored projects code
This commit is contained in:
parent
220f9f244d
commit
b8dfe90ce8
5 changed files with 8 additions and 41 deletions
|
|
@ -52,36 +52,25 @@ def __create(tenant_id, name):
|
|||
return get_project(tenant_id=tenant_id, project_id=project_id, include_gdpr=True)
|
||||
|
||||
|
||||
def get_projects(tenant_id, recording_state=False, gdpr=None, recorded=False, stack_integrations=False):
|
||||
stack_integrations = False
|
||||
def get_projects(tenant_id: int, gdpr: bool = False, recorded: bool = False):
|
||||
with pg_client.PostgresClient() as cur:
|
||||
extra_projection = ""
|
||||
extra_join = ""
|
||||
if gdpr:
|
||||
extra_projection += ',s.gdpr'
|
||||
if recorded:
|
||||
extra_projection += """,COALESCE(nullif(EXTRACT(EPOCH FROM s.first_recorded_session_at) * 1000, NULL)::BIGINT,
|
||||
extra_projection += """,COALESCE(EXTRACT(EPOCH FROM s.first_recorded_session_at) * 1000::BIGINT,
|
||||
(SELECT MIN(sessions.start_ts)
|
||||
FROM public.sessions
|
||||
WHERE sessions.project_id = s.project_id
|
||||
AND sessions.start_ts >= (EXTRACT(EPOCH FROM
|
||||
COALESCE(s.sessions_last_check_at, s.created_at)) * 1000-24*60*60*1000)
|
||||
AND sessions.start_ts <= %(now)s
|
||||
LIMIT 1), NULL) AS first_recorded"""
|
||||
if stack_integrations:
|
||||
extra_projection += ',stack_integrations.count>0 AS stack_integrations'
|
||||
|
||||
if stack_integrations:
|
||||
extra_join = """LEFT JOIN LATERAL (SELECT COUNT(*) AS count
|
||||
FROM public.integrations
|
||||
WHERE s.project_id = integrations.project_id
|
||||
LIMIT 1) AS stack_integrations ON TRUE"""
|
||||
)) AS first_recorded"""
|
||||
|
||||
query = cur.mogrify(f"""{"SELECT *, first_recorded IS NOT NULL AS recorded FROM (" if recorded else ""}
|
||||
SELECT s.project_id, s.name, s.project_key, s.save_request_payloads, s.first_recorded_session_at,
|
||||
created_at {extra_projection}
|
||||
FROM public.projects AS s
|
||||
{extra_join}
|
||||
WHERE s.deleted_at IS NULL
|
||||
ORDER BY s.name {") AS raw" if recorded else ""};""", {"now": TimeUTC.now()})
|
||||
cur.execute(query)
|
||||
|
|
@ -107,26 +96,6 @@ def get_projects(tenant_id, recording_state=False, gdpr=None, recorded=False, st
|
|||
else:
|
||||
for r in rows:
|
||||
r["created_at"] = TimeUTC.datetime_to_timestamp(r["created_at"])
|
||||
if recording_state and len(rows) > 0:
|
||||
project_ids = [f'({r["project_id"]})' for r in rows]
|
||||
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 TimeUTC.now(-2) <= s["last"] < TimeUTC.now(-1):
|
||||
r["status"] = "yellow"
|
||||
elif s["last"] >= TimeUTC.now(-1):
|
||||
r["status"] = "green"
|
||||
break
|
||||
|
||||
return helper.list_to_camel_case(rows)
|
||||
|
||||
|
|
|
|||
|
|
@ -941,7 +941,7 @@ def search_query_parts(data: schemas.SessionsSearchPayloadSchema, error_status,
|
|||
|
||||
def search_by_metadata(tenant_id, user_id, m_key, m_value, project_id=None):
|
||||
if project_id is None:
|
||||
all_projects = projects.get_projects(tenant_id=tenant_id, recording_state=False)
|
||||
all_projects = projects.get_projects(tenant_id=tenant_id)
|
||||
else:
|
||||
all_projects = [
|
||||
projects.get_project(tenant_id=tenant_id, project_id=int(project_id), include_last_session=False,
|
||||
|
|
|
|||
|
|
@ -496,8 +496,7 @@ def set_password_invitation(user_id, new_password):
|
|||
|
||||
c = tenants.get_by_tenant_id(tenant_id)
|
||||
c.pop("createdAt")
|
||||
c["projects"] = projects.get_projects(tenant_id=tenant_id, recording_state=True, recorded=True,
|
||||
stack_integrations=True)
|
||||
c["projects"] = projects.get_projects(tenant_id=tenant_id, recorded=True)
|
||||
c["smtp"] = helper.has_smtp()
|
||||
c["iceServers"] = assist.get_ice_servers()
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -148,8 +148,7 @@ async def search_sessions_by_metadata(key: str, value: str, projectId: Optional[
|
|||
|
||||
@app.get('/projects', tags=['projects'])
|
||||
async def get_projects(context: schemas.CurrentContext = Depends(OR_context)):
|
||||
return {"data": projects.get_projects(tenant_id=context.tenant_id, recording_state=True, gdpr=True, recorded=True,
|
||||
stack_integrations=True)}
|
||||
return {"data": projects.get_projects(tenant_id=context.tenant_id, gdpr=True, recorded=True)}
|
||||
|
||||
|
||||
# for backward compatibility
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ async def run_scheduled_jobs() -> None:
|
|||
jobs.execute_jobs()
|
||||
|
||||
|
||||
async def weekly_report2() -> None:
|
||||
async def weekly_report_cron() -> None:
|
||||
weekly_report.cron()
|
||||
|
||||
|
||||
|
|
@ -21,6 +21,6 @@ cron_jobs = [
|
|||
"misfire_grace_time": 60 * 60, "max_instances": 1},
|
||||
{"func": run_scheduled_jobs, "trigger": CronTrigger(day_of_week="*", hour=0, minute=15),
|
||||
"misfire_grace_time": 20, "max_instances": 1},
|
||||
{"func": weekly_report2, "trigger": CronTrigger(day_of_week="mon", hour=5),
|
||||
{"func": weekly_report_cron, "trigger": CronTrigger(day_of_week="mon", hour=5),
|
||||
"misfire_grace_time": 60 * 60, "max_instances": 1}
|
||||
]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue