Skip to content

Commit

Permalink
🐛 fix: set monitorHistoryDataChecksItem
Browse files Browse the repository at this point in the history
  • Loading branch information
yunsii committed Mar 11, 2024
1 parent 7a366f8 commit 031a746
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/worker/_helpers/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export interface MonitorHistoryDataChecksItem {
*
* Key: location, No value if config.settings.collectResponseTimes=false
*/
stats?: Record<string, {
stats?: Partial<Record<string, {
count: number
totalMs: number
// totalMs / count
// avgMs: number
}>
}>>
}

export interface DataV1 {
Expand Down
14 changes: 8 additions & 6 deletions src/worker/cron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ export async function handleCronTrigger(event: FetchEvent) {
return item.date === checkDay
})

const monitorHistoryDataChecksItem: MonitorHistoryDataChecksItem = {
const monitorHistoryDataChecksItem: MonitorHistoryDataChecksItem = targetMonitorHistoryDataChecksItem || {
date: checkDay,
fails: (targetMonitorHistoryDataChecksItem?.fails || 0) + (monitorOperational ? 0 : 1),
fails: 0,
}
monitorHistoryDataChecksItem.fails = (monitorHistoryDataChecksItem.fails || 0) + (monitorOperational ? 0 : 1)

if (config.settings.collectResponseTimes && monitorOperational) {
if (!monitorHistoryDataChecksItem.stats) {
Expand All @@ -101,9 +102,8 @@ export async function handleCronTrigger(event: FetchEvent) {
totalMs: 0,
}
}
const target = targetMonitorHistoryDataChecksItem || monitorHistoryDataChecksItem
const count = target.stats![checkLocation].count + 1
const totalMs = target.stats![checkLocation].totalMs + requestTime
const count = monitorHistoryDataChecksItem.stats[checkLocation]!.count + 1
const totalMs = monitorHistoryDataChecksItem.stats[checkLocation]!.totalMs + requestTime

monitorHistoryDataChecksItem.stats[checkLocation] = {
count,
Expand All @@ -116,7 +116,9 @@ export async function handleCronTrigger(event: FetchEvent) {
}

kvData.monitorHistoryData[monitor.id] = {
checks: [...kvData.monitorHistoryData[monitor.id]?.checks || [], monitorHistoryDataChecksItem],
checks: [...(kvData.monitorHistoryData[monitor.id]?.checks || []).filter((item) => {
return item.date !== monitorHistoryDataChecksItem.date
}), monitorHistoryDataChecksItem],
firstCheck: kvData.monitorHistoryData[monitor.id]?.firstCheck || checkDay,
lastCheck: monitorLastCheck,
}
Expand Down

0 comments on commit 031a746

Please sign in to comment.