-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsQueryTime.js
52 lines (43 loc) · 1.63 KB
/
dnsQueryTime.js
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
let initial = {
longestQueryTime: 0,
longestQueryDomain: "",
totalQueryTime: 0,
numberOfQuery: 0,
lastProcessedTime: {} // Track only the last processed timestamp per domain
};
let history = JSON.parse(
$persistentStore.read("DnsQueryTime") || JSON.stringify(initial)
);
$httpAPI("GET", "/v1/dns", null, (body) => {
body.dnsCache?.forEach((dnsCache) => {
let domain = dnsCache.domain;
let timeCost = dnsCache.timeCost;
let expiresTime = dnsCache.expiresTime;
// Check if this domain has a recorded timestamp and if the current one is newer
if (history.lastProcessedTime[domain] >= expiresTime) return;
// Update with the new timestamp for this domain
history.lastProcessedTime[domain] = expiresTime;
// Update longest query time if this is the longest so far
if (timeCost > history.longestQueryTime) {
history.longestQueryTime = timeCost;
history.longestQueryDomain = domain;
}
// Accumulate query time and count only if this is a new unique query
history.totalQueryTime += timeCost;
history.numberOfQuery += 1;
});
// Save updated aggregated data back to persistent storage
$persistentStore.write(JSON.stringify(history), "DnsQueryTime");
// Calculate the average query time
let averageQueryTime = (
(history.totalQueryTime * 1000) / history.numberOfQuery
).toFixed(2);
// Display results with longest query time converted to milliseconds
$done({
title: "DNS Query Time",
content: `Count: ${history.numberOfQuery}\nAverage: ${averageQueryTime} ms\nLongest Query: ${history.longestQueryDomain} ${(
history.longestQueryTime * 1000
).toFixed(2)} ms`,
icon: "bolt.horizontal.circle.fill",
});
});