a cache for slack profile pictures and emojis
1import { Database } from "bun:sqlite";
2import { Migration } from "./types";
3
4/**
5 * Migration to fix endpoint grouping in analytics
6 * This migration updates existing analytics data to use consistent endpoint grouping
7 */
8export const endpointGroupingMigration: Migration = {
9 version: "0.3.1",
10 description: "Fix endpoint grouping in analytics data",
11
12 async up(db: Database): Promise<void> {
13 console.log("Running endpoint grouping migration...");
14
15 // Get all request_analytics entries with specific URLs
16 const results = db.query(`
17 SELECT id, endpoint FROM request_analytics
18 WHERE endpoint LIKE '/users/%' OR endpoint LIKE '/emojis/%'
19 `).all() as Array<{ id: string; endpoint: string }>;
20
21 console.log(`Found ${results.length} entries to update`);
22
23 // Process each entry and update with the correct grouping
24 for (const entry of results) {
25 let newEndpoint = entry.endpoint;
26
27 // Apply the same grouping logic we use in the analytics
28 if (entry.endpoint.match(/^\/users\/[^\/]+$/)) {
29 // Keep as is - these are already correctly grouped
30 continue;
31 } else if (entry.endpoint.match(/^\/users\/[^\/]+\/r$/)) {
32 // Keep as is - these are already correctly grouped
33 continue;
34 } else if (entry.endpoint.match(/^\/emojis\/[^\/]+$/)) {
35 // Keep as is - these are already correctly grouped
36 continue;
37 } else if (entry.endpoint.match(/^\/emojis\/[^\/]+\/r$/)) {
38 // Keep as is - these are already correctly grouped
39 continue;
40 } else if (entry.endpoint.includes("/users/") && entry.endpoint.includes("/r")) {
41 // This is a user redirect with a non-standard format
42 newEndpoint = "/users/USER_ID/r";
43 } else if (entry.endpoint.includes("/users/")) {
44 // This is a user data endpoint with a non-standard format
45 newEndpoint = "/users/USER_ID";
46 } else if (entry.endpoint.includes("/emojis/") && entry.endpoint.includes("/r")) {
47 // This is an emoji redirect with a non-standard format
48 newEndpoint = "/emojis/EMOJI_NAME/r";
49 } else if (entry.endpoint.includes("/emojis/")) {
50 // This is an emoji data endpoint with a non-standard format
51 newEndpoint = "/emojis/EMOJI_NAME";
52 }
53
54 // Only update if the endpoint has changed
55 if (newEndpoint !== entry.endpoint) {
56 db.run(`
57 UPDATE request_analytics
58 SET endpoint = ?
59 WHERE id = ?
60 `, [newEndpoint, entry.id]);
61 }
62 }
63
64 console.log("Endpoint grouping migration completed");
65 }
66};