-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathinject-audit-data.ts
More file actions
173 lines (148 loc) · 7.31 KB
/
inject-audit-data.ts
File metadata and controls
173 lines (148 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { Script } from "../scriptRunner";
import { PrismaClient } from "../../dist";
import { confirmAction } from "../utils";
// Generate realistic audit data for analytics testing
// Simulates 50 engineers with varying activity patterns
export const injectAuditData: Script = {
run: async (prisma: PrismaClient) => {
const orgId = 1;
// Check if org exists
const org = await prisma.org.findUnique({
where: { id: orgId }
});
if (!org) {
console.error(`Organization with id ${orgId} not found. Please create it first.`);
return;
}
console.log(`Injecting audit data for organization: ${org.name} (${org.domain})`);
// Generate 50 fake user IDs
const userIds = Array.from({ length: 50 }, (_, i) => `user_${String(i + 1).padStart(3, '0')}`);
// Actions we're tracking
const actions = [
'user.performed_code_search',
'user.performed_find_references',
'user.performed_goto_definition',
'user.created_ask_chat'
];
// Generate data for the last 90 days
const endDate = new Date();
const startDate = new Date();
startDate.setDate(startDate.getDate() - 90);
console.log(`Generating data from ${startDate.toISOString().split('T')[0]} to ${endDate.toISOString().split('T')[0]}`);
confirmAction();
// Generate data for each day
for (let d = new Date(startDate); d <= endDate; d.setDate(d.getDate() + 1)) {
const currentDate = new Date(d);
const dayOfWeek = currentDate.getDay(); // 0 = Sunday, 6 = Saturday
const isWeekend = dayOfWeek === 0 || dayOfWeek === 6;
// For each user, generate activity for this day
for (const userId of userIds) {
// Determine if user is active today (higher chance on weekdays)
const isActiveToday = isWeekend
? Math.random() < 0.15 // 15% chance on weekends
: Math.random() < 0.85; // 85% chance on weekdays
if (!isActiveToday) continue;
// Generate code searches (2-5 per day)
const codeSearches = isWeekend
? Math.floor(Math.random() * 2) + 1 // 1-2 on weekends
: Math.floor(Math.random() * 4) + 2; // 2-5 on weekdays
// Generate navigation actions (5-10 per day)
const navigationActions = isWeekend
? Math.floor(Math.random() * 3) + 1 // 1-3 on weekends
: Math.floor(Math.random() * 6) + 5; // 5-10 on weekdays
// Create code search records
for (let i = 0; i < codeSearches; i++) {
const timestamp = new Date(currentDate);
// Spread throughout the day (9 AM to 6 PM on weekdays, more random on weekends)
if (isWeekend) {
timestamp.setHours(9 + Math.floor(Math.random() * 12));
timestamp.setMinutes(Math.floor(Math.random() * 60));
} else {
timestamp.setHours(9 + Math.floor(Math.random() * 9));
timestamp.setMinutes(Math.floor(Math.random() * 60));
}
timestamp.setSeconds(Math.floor(Math.random() * 60));
await prisma.audit.create({
data: {
timestamp,
action: 'user.performed_code_search',
actorId: userId,
actorType: 'user',
targetId: `search_${Math.floor(Math.random() * 1000)}`,
targetType: 'search',
sourcebotVersion: '1.0.0',
orgId
}
});
}
// Create navigation action records
for (let i = 0; i < navigationActions; i++) {
const timestamp = new Date(currentDate);
if (isWeekend) {
timestamp.setHours(9 + Math.floor(Math.random() * 12));
timestamp.setMinutes(Math.floor(Math.random() * 60));
} else {
timestamp.setHours(9 + Math.floor(Math.random() * 9));
timestamp.setMinutes(Math.floor(Math.random() * 60));
}
timestamp.setSeconds(Math.floor(Math.random() * 60));
// Randomly choose between find references and goto definition
const action = Math.random() < 0.6 ? 'user.performed_find_references' : 'user.performed_goto_definition';
await prisma.audit.create({
data: {
timestamp,
action,
actorId: userId,
actorType: 'user',
targetId: `symbol_${Math.floor(Math.random() * 1000)}`,
targetType: 'symbol',
sourcebotVersion: '1.0.0',
orgId
}
});
}
// Generate Ask chat sessions (0-2 per day on weekdays, 0-1 on weekends)
const askChats = isWeekend
? Math.floor(Math.random() * 2) // 0-1 on weekends
: Math.floor(Math.random() * 3); // 0-2 on weekdays
// Create Ask chat records
for (let i = 0; i < askChats; i++) {
const timestamp = new Date(currentDate);
if (isWeekend) {
timestamp.setHours(9 + Math.floor(Math.random() * 12));
timestamp.setMinutes(Math.floor(Math.random() * 60));
} else {
timestamp.setHours(9 + Math.floor(Math.random() * 9));
timestamp.setMinutes(Math.floor(Math.random() * 60));
}
timestamp.setSeconds(Math.floor(Math.random() * 60));
await prisma.audit.create({
data: {
timestamp,
action: 'user.created_ask_chat',
actorId: userId,
actorType: 'user',
targetId: orgId.toString(),
targetType: 'org',
sourcebotVersion: '1.0.0',
orgId
}
});
}
}
}
console.log(`\nAudit data injection complete!`);
console.log(`Users: ${userIds.length}`);
console.log(`Date range: ${startDate.toISOString().split('T')[0]} to ${endDate.toISOString().split('T')[0]}`);
// Show some statistics
const stats = await prisma.audit.groupBy({
by: ['action'],
where: { orgId },
_count: { action: true }
});
console.log('\nAction breakdown:');
stats.forEach(stat => {
console.log(` ${stat.action}: ${stat._count.action}`);
});
},
};