Skip to content

Commit e157c2b

Browse files
committed
refactor analytics graphs and add ask audit and graph
1 parent cb1fa3c commit e157c2b

6 files changed

Lines changed: 152 additions & 413 deletions

File tree

docs/docs/configuration/audit-logs.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ curl --request GET '$SOURCEBOT_URL/api/ee/audit' \
119119
| `user.performed_code_search` | `user` | `org` |
120120
| `user.performed_find_references` | `user` | `org` |
121121
| `user.performed_goto_definition` | `user` | `org` |
122+
| `user.created_ask_chat` | `user` | `org` |
122123
| `user.jit_provisioning_failed` | `user` | `org` |
123124
| `user.jit_provisioned` | `user` | `org` |
124125
| `user.join_request_creation_failed` | `user` | `org` |

packages/db/tools/scripts/inject-audit-data.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export const injectAuditData: Script = {
2727
const actions = [
2828
'user.performed_code_search',
2929
'user.performed_find_references',
30-
'user.performed_goto_definition'
30+
'user.performed_goto_definition',
31+
'user.created_ask_chat'
3132
];
3233

3334
// Generate data for the last 90 days
@@ -119,6 +120,37 @@ export const injectAuditData: Script = {
119120
}
120121
});
121122
}
123+
124+
// Generate Ask chat sessions (0-2 per day on weekdays, 0-1 on weekends)
125+
const askChats = isWeekend
126+
? Math.floor(Math.random() * 2) // 0-1 on weekends
127+
: Math.floor(Math.random() * 3); // 0-2 on weekdays
128+
129+
// Create Ask chat records
130+
for (let i = 0; i < askChats; i++) {
131+
const timestamp = new Date(currentDate);
132+
if (isWeekend) {
133+
timestamp.setHours(9 + Math.floor(Math.random() * 12));
134+
timestamp.setMinutes(Math.floor(Math.random() * 60));
135+
} else {
136+
timestamp.setHours(9 + Math.floor(Math.random() * 9));
137+
timestamp.setMinutes(Math.floor(Math.random() * 60));
138+
}
139+
timestamp.setSeconds(Math.floor(Math.random() * 60));
140+
141+
await prisma.audit.create({
142+
data: {
143+
timestamp,
144+
action: 'user.created_ask_chat',
145+
actorId: userId,
146+
actorType: 'user',
147+
targetId: orgId.toString(),
148+
targetType: 'org',
149+
sourcebotVersion: '1.0.0',
150+
orgId
151+
}
152+
});
153+
}
122154
}
123155
}
124156

packages/web/src/ee/features/analytics/actions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export const getAnalytics = async (domain: string, apiKey: string | undefined =
3333
AND action IN (
3434
'user.performed_code_search',
3535
'user.performed_find_references',
36-
'user.performed_goto_definition'
36+
'user.performed_goto_definition',
37+
'user.created_ask_chat'
3738
)
3839
),
3940
@@ -77,6 +78,7 @@ export const getAnalytics = async (domain: string, apiKey: string | undefined =
7778
END AS bucket,
7879
COUNT(*) FILTER (WHERE c.action = 'user.performed_code_search') AS code_searches,
7980
COUNT(*) FILTER (WHERE c.action IN ('user.performed_find_references', 'user.performed_goto_definition')) AS navigations,
81+
COUNT(*) FILTER (WHERE c.action = 'user.created_ask_chat') AS ask_chats,
8082
COUNT(DISTINCT c."actorId") AS active_users
8183
FROM core c
8284
JOIN LATERAL (
@@ -90,6 +92,7 @@ export const getAnalytics = async (domain: string, apiKey: string | undefined =
9092
b.bucket,
9193
COALESCE(a.code_searches, 0)::int AS code_searches,
9294
COALESCE(a.navigations, 0)::int AS navigations,
95+
COALESCE(a.ask_chats, 0)::int AS ask_chats,
9396
COALESCE(a.active_users, 0)::int AS active_users
9497
FROM buckets b
9598
LEFT JOIN aggregated a

0 commit comments

Comments
 (0)