Coverage for chalicelib/core/boarding.py: 85%
40 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:56 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:56 +0000
1from cachetools import TTLCache, cached
3from chalicelib.core import projects
4from chalicelib.core import users
5from chalicelib.core.log_tools import datadog, stackdriver, sentry
6from chalicelib.core.modules import TENANT_CONDITION
7from chalicelib.utils import pg_client
9cache = TTLCache(maxsize=1000, ttl=180)
12@cached(cache)
13def get_state(tenant_id):
14 pids = projects.get_projects_ids(tenant_id=tenant_id)
15 with pg_client.PostgresClient() as cur:
16 recorded = False
17 meta = False
19 if len(pids) > 0: 19 ↛ 53line 19 didn't jump to line 53
20 cur.execute(
21 cur.mogrify(
22 """SELECT EXISTS((SELECT 1
23 FROM public.sessions AS s
24 WHERE s.project_id IN %(ids)s)) AS exists;""",
25 {"ids": tuple(pids)},
26 )
27 )
28 recorded = cur.fetchone()["exists"]
29 meta = False
30 if recorded: 30 ↛ 31line 30 didn't jump to line 31 because the condition on line 30 was never true
31 query = cur.mogrify(
32 f"""SELECT EXISTS((SELECT 1
33 FROM public.projects AS p
34 LEFT JOIN LATERAL ( SELECT 1
35 FROM public.sessions
36 WHERE sessions.project_id = p.project_id
37 AND sessions.user_id IS NOT NULL
38 LIMIT 1) AS sessions(user_id) ON (TRUE)
39 WHERE {TENANT_CONDITION} AND p.deleted_at ISNULL
40 AND ( sessions.user_id IS NOT NULL OR p.metadata_1 IS NOT NULL
41 OR p.metadata_2 IS NOT NULL OR p.metadata_3 IS NOT NULL
42 OR p.metadata_4 IS NOT NULL OR p.metadata_5 IS NOT NULL
43 OR p.metadata_6 IS NOT NULL OR p.metadata_7 IS NOT NULL
44 OR p.metadata_8 IS NOT NULL OR p.metadata_9 IS NOT NULL
45 OR p.metadata_10 IS NOT NULL )
46 )) AS exists;""",
47 {"tenant_id": tenant_id},
48 )
49 cur.execute(query)
51 meta = cur.fetchone()["exists"]
53 return [
54 {
55 "task": "Install OpenReplay",
56 "done": recorded,
57 "URL": "https://docs.openreplay.com/getting-started/quick-start",
58 },
59 {
60 "task": "Identify Users",
61 "done": meta,
62 "URL": "https://docs.openreplay.com/data-privacy-security/metadata",
63 },
64 {
65 "task": "Invite Team Members",
66 "done": len(users.get_members(tenant_id=tenant_id)) > 1,
67 "URL": "https://app.openreplay.com/client/manage-users",
68 },
69 {
70 "task": "Integrations",
71 "done": len(datadog.get_all(tenant_id=tenant_id)) > 0
72 or len(sentry.get_all(tenant_id=tenant_id)) > 0
73 or len(stackdriver.get_all(tenant_id=tenant_id)) > 0,
74 "URL": "https://docs.openreplay.com/integrations",
75 },
76 ]
79def get_state_installing(tenant_id):
80 pids = projects.get_projects_ids(tenant_id=tenant_id)
81 with pg_client.PostgresClient() as cur:
82 recorded = False
84 if len(pids) > 0: 84 ↛ 95line 84 didn't jump to line 95
85 cur.execute(
86 cur.mogrify(
87 """SELECT EXISTS((SELECT 1
88 FROM public.sessions AS s
89 WHERE s.project_id IN %(ids)s)) AS exists;""",
90 {"ids": tuple(pids)},
91 )
92 )
93 recorded = cur.fetchone()["exists"]
95 return {
96 "task": "Install OpenReplay",
97 "done": recorded,
98 "URL": "https://docs.openreplay.com/getting-started/quick-start",
99 }
102def get_state_identify_users(tenant_id):
103 with pg_client.PostgresClient() as cur:
104 query = cur.mogrify(
105 f"""SELECT EXISTS((SELECT 1
106 FROM public.projects AS p
107 LEFT JOIN LATERAL ( SELECT 1
108 FROM public.sessions
109 WHERE sessions.project_id = p.project_id
110 AND sessions.user_id IS NOT NULL
111 LIMIT 1) AS sessions(user_id) ON (TRUE)
112 WHERE {TENANT_CONDITION} AND p.deleted_at ISNULL
113 AND ( sessions.user_id IS NOT NULL OR p.metadata_1 IS NOT NULL
114 OR p.metadata_2 IS NOT NULL OR p.metadata_3 IS NOT NULL
115 OR p.metadata_4 IS NOT NULL OR p.metadata_5 IS NOT NULL
116 OR p.metadata_6 IS NOT NULL OR p.metadata_7 IS NOT NULL
117 OR p.metadata_8 IS NOT NULL OR p.metadata_9 IS NOT NULL
118 OR p.metadata_10 IS NOT NULL )
119 )) AS exists;""",
120 {"tenant_id": tenant_id},
121 )
122 cur.execute(query)
124 meta = cur.fetchone()["exists"]
126 return {
127 "task": "Identify Users",
128 "done": meta,
129 "URL": "https://docs.openreplay.com/data-privacy-security/metadata",
130 }
133def get_state_manage_users(tenant_id):
134 return {
135 "task": "Invite Team Members",
136 "done": len(users.get_members(tenant_id=tenant_id)) > 1,
137 "URL": "https://app.openreplay.com/client/manage-users",
138 }
141def get_state_integrations(tenant_id):
142 return {
143 "task": "Integrations",
144 "done": len(datadog.get_all(tenant_id=tenant_id)) > 0
145 or len(sentry.get_all(tenant_id=tenant_id)) > 0
146 or len(stackdriver.get_all(tenant_id=tenant_id)) > 0,
147 "URL": "https://docs.openreplay.com/integrations",
148 }