From 14da02885d1a8170af839d160f0733c4b1b95817 Mon Sep 17 00:00:00 2001 From: Solari Systems Date: Fri, 16 Jan 2026 19:31:16 -0500 Subject: [PATCH] Fix bounty issue --- packages/db/src/drizzle/schema.new.ts | 67 +++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 packages/db/src/drizzle/schema.new.ts diff --git a/packages/db/src/drizzle/schema.new.ts b/packages/db/src/drizzle/schema.new.ts new file mode 100644 index 000000000..6ca6a0e35 --- /dev/null +++ b/packages/db/src/drizzle/schema.new.ts @@ -0,0 +1,67 @@ +import axios from 'axios'; +import { AlarmService } from './AlarmService'; +import { Logger } from './Logger'; + +interface SiteStatus { + url: string; + status: 'up' | 'down'; + responseTime?: number; +} + +class UptimeMonitor { + private siteStatus: SiteStatus = { url: '', status: 'up' }; + private consecutiveFailures: number = 0; + private lastAlertTimestamp: Date | null = null; + private readonly failureThreshold: number = 3; // configurable + private readonly alertInterval: number = 60 * 60 * 1000; // 1 hour + + constructor(private alarmService: AlarmService, private logger: Logger) {} + + async checkSiteStatus(url: string): Promise { + this.siteStatus.url = url; + try { + const response = await axios.get(url); + if (response.status === 200) { + this.handleSiteUp(); + } else { + this.handleSiteDown(response.status); + } + } catch (error) { + this.handleSiteDown(500); // assume server error + } + } + + private handleSiteUp(): void { + if (this.siteStatus.status === 'down') { + this.logger.log(`Site ${this.siteStatus.url} is back up`); + this.triggerAlarm('up'); + } + this.resetConsecutiveFailures(); + } + + private handleSiteDown(statusCode: number): void { + if (this.siteStatus.status === 'up') { + this.logger.log(`Site ${this.siteStatus.url} is down with status code ${statusCode}`); + this.triggerAlarm('down'); + } else { + this.consecutiveFailures++; + if (this.consecutiveFailures >= this.failureThreshold) { + this.logger.log(`Site ${this.siteStatus.url} has been down for ${this.consecutiveFailures} consecutive checks`); + this.triggerAlarm('down'); + } + } + } + + private triggerAlarm(status: 'up' | 'down'): void { + if (Date.now() - (this.lastAlertTimestamp?.getTime() || 0) > this.alertInterval) { + this.alarmService.sendAlarm(this.siteStatus.url, status); + this.lastAlertTimestamp = new Date(); + } + } + + private resetConsecutiveFailures(): void { + this.consecutiveFailures = 0; + } +} + +export { UptimeMonitor }; \ No newline at end of file