|
| 1 | +import { db, event, graphqlQuery } from "#src/utils"; |
| 2 | + |
| 3 | +// db resp types |
| 4 | +interface AuditSummaryRespBlockerMessagesTag { |
| 5 | + tag: { |
| 6 | + content: string; |
| 7 | + }; |
| 8 | +} |
| 9 | + |
| 10 | +interface AuditSummaryRespBlockerMessages { |
| 11 | + message: { |
| 12 | + content: string; |
| 13 | + message_tags: AuditSummaryRespBlockerMessagesTag[]; |
| 14 | + category: string; |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +interface AuditSummaryRespBlocker { |
| 19 | + blocker_messages: AuditSummaryRespBlockerMessages[]; |
| 20 | + url: { |
| 21 | + url: string; |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +interface AuditSummaryResp { |
| 26 | + blockers: AuditSummaryRespBlocker[]; |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +export const getAuditSummaryFast = async () => { |
| 31 | + |
| 32 | + const start = performance.now(); |
| 33 | + const auditId = (event.queryStringParameters as any).id; |
| 34 | + const mostCommonUrlsLimit = |
| 35 | + (event.queryStringParameters as any).mostCommonUrlsLimit ?? 5; |
| 36 | + const mostCommonBlockersLimit = |
| 37 | + (event.queryStringParameters as any).mostCommonBlockersLimit ?? 5; |
| 38 | + const mostCommonCategoriesLimit = |
| 39 | + (event.queryStringParameters as any).mostCommonCategoriesLimit ?? 3; |
| 40 | + const mostCommonTagsLimit = |
| 41 | + (event.queryStringParameters as any).mostCommonTagsLimit ?? 3; |
| 42 | + |
| 43 | + const query = { |
| 44 | + query: `query GetAuditStats($audit_id: uuid!, $urlLimit: Int!, $msgLimit: Int!) { |
| 45 | + unique_urls: blocker_summary_view_aggregate( |
| 46 | + distinct_on: [url], |
| 47 | + where: { audit_id: { _eq: $audit_id } } |
| 48 | + ) { |
| 49 | + aggregate { |
| 50 | + count |
| 51 | + } |
| 52 | + } |
| 53 | + }`, |
| 54 | + variables: { |
| 55 | + audit_id: auditId, |
| 56 | + urlLimit: mostCommonUrlsLimit, |
| 57 | + msgLimit: mostCommonBlockersLimit |
| 58 | + }, |
| 59 | + }; |
| 60 | + const response = (await graphqlQuery(query)) as AuditSummaryResp; |
| 61 | + //console.log(JSON.stringify({ response })); |
| 62 | + |
| 63 | + const urlFreq: Record<string, number> = {}; |
| 64 | + const msgFreq: Record<string, number> = {}; |
| 65 | + const catFreq: Record<string, number> = {}; |
| 66 | + const tagFreq: Record<string, number> = {}; |
| 67 | + let uniqueUrlCount = 0; |
| 68 | + const urlSeen = new Set<string>(); |
| 69 | + |
| 70 | + // SINGLE PASS: We touch each piece of data exactly once |
| 71 | + for (const blocker of response.blockers) { |
| 72 | + const url = blocker.url.url; |
| 73 | + |
| 74 | + // Track unique URLs |
| 75 | + if (!urlSeen.has(url)) { |
| 76 | + urlSeen.add(url); |
| 77 | + uniqueUrlCount++; |
| 78 | + } |
| 79 | + |
| 80 | + urlFreq[url] = (urlFreq[url] || 0) + 1; |
| 81 | + |
| 82 | + // Process nested messages (assuming at least one exists) |
| 83 | + const firstMsg = blocker.blocker_messages[0]?.message; |
| 84 | + if (firstMsg) { |
| 85 | + msgFreq[firstMsg.content] = (msgFreq[firstMsg.content] || 0) + 1; |
| 86 | + catFreq[firstMsg.category] = (catFreq[firstMsg.category] || 0) + 1; |
| 87 | + |
| 88 | + for (const t of firstMsg.message_tags) { |
| 89 | + const tag = t.tag.content; |
| 90 | + tagFreq[tag] = (tagFreq[tag] || 0) + 1; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Reusable helper for sorting/slicing |
| 96 | + const getTop = (freqMap: Record<string, number>, limit: number) => |
| 97 | + Object.entries(freqMap) |
| 98 | + .map(([key, count]) => ({ key, count })) |
| 99 | + .sort((a, b) => b.count - a.count) |
| 100 | + .slice(0, limit); |
| 101 | + |
| 102 | + |
| 103 | + const end = performance.now(); |
| 104 | + return { |
| 105 | + statusCode: 200, |
| 106 | + body: { |
| 107 | + urlsWithBlockersCount: uniqueUrlCount, |
| 108 | + urlsWithMostErrors: getTop(urlFreq, mostCommonUrlsLimit), |
| 109 | + mostCommonErrors: getTop(msgFreq, mostCommonBlockersLimit), |
| 110 | + mostCommonCategory: getTop(catFreq, mostCommonCategoriesLimit), |
| 111 | + mostCommonTags: getTop(tagFreq, mostCommonTagsLimit), |
| 112 | + executionTime: end-start |
| 113 | + }, |
| 114 | + }; |
| 115 | +}; |
0 commit comments