Skip to content

Commit e43fcd6

Browse files
committed
Backend: updated refactored audit summary
1 parent 1fb1d36 commit e43fcd6

1 file changed

Lines changed: 57 additions & 81 deletions

File tree

Lines changed: 57 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
11
import { db, event, graphqlQuery } from "#src/utils";
22

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-
};
3+
interface ItemCount {
4+
key: string;
5+
count: number;
236
}
247

258
interface AuditSummaryResp {
26-
blockers: AuditSummaryRespBlocker[];
9+
unique_url_stats: {
10+
aggregate: {
11+
count: number;
12+
};
13+
};
14+
mostCommonUrls: ItemCount[];
15+
mostCommonBlockers: ItemCount[];
16+
mostCommonTags: ItemCount[];
2717
}
2818

29-
3019
export const getAuditSummaryFast = async () => {
3120

3221
const start = performance.now();
@@ -35,81 +24,68 @@ export const getAuditSummaryFast = async () => {
3524
(event.queryStringParameters as any).mostCommonUrlsLimit ?? 5;
3625
const mostCommonBlockersLimit =
3726
(event.queryStringParameters as any).mostCommonBlockersLimit ?? 5;
38-
const mostCommonCategoriesLimit =
27+
/* const mostCommonCategoriesLimit =
3928
(event.queryStringParameters as any).mostCommonCategoriesLimit ?? 3;
29+
*/
4030
const mostCommonTagsLimit =
4131
(event.queryStringParameters as any).mostCommonTagsLimit ?? 3;
4232

4333
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-
}`,
34+
query: `query GetFullAuditSummary(
35+
$audit_id: uuid!,
36+
$urlLimit: Int,
37+
$msgLimit: Int,
38+
$tagLimit: Int
39+
) {
40+
# 1. Total Unique URLs with blockers
41+
unique_url_stats: blocker_summary_view_aggregate(
42+
where: { audit_id: { _eq: $audit_id } }
43+
) {
44+
aggregate {
45+
count(columns: url, distinct: true)
46+
}
47+
}
48+
49+
mostCommonUrls: get_most_common_urls(
50+
args: { search_audit_id: $audit_id, row_limit: $urlLimit }
51+
) {
52+
key
53+
count
54+
}
55+
56+
mostCommonBlockers: get_most_common_messages(
57+
args: { search_audit_id: $audit_id, row_limit: $msgLimit }
58+
) {
59+
key
60+
count
61+
}
62+
63+
mostCommonTags: get_most_common_tags(
64+
args: { search_audit_id: $audit_id, row_limit: $tagLimit }
65+
) {
66+
key
67+
count
68+
}
69+
}`,
5470
variables: {
5571
audit_id: auditId,
5672
urlLimit: mostCommonUrlsLimit,
57-
msgLimit: mostCommonBlockersLimit
73+
msgLimit: mostCommonBlockersLimit,
74+
tagLimit: mostCommonTagsLimit
5875
},
5976
};
6077
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);
10178

102-
10379
const end = performance.now();
10480
return {
10581
statusCode: 200,
82+
headers: { "content-type": "application/json" },
10683
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
84+
urlsWithBlockersCount: response.unique_url_stats.aggregate.count,
85+
urlsWithMostErrors: response.mostCommonUrls,
86+
mostCommonErrors: response.mostCommonBlockers,
87+
mostCommonTags: response.mostCommonTags,
88+
executionTime: end - start
11389
},
11490
};
11591
};

0 commit comments

Comments
 (0)