From d94df22ac9e7447890995475143f10c174459440 Mon Sep 17 00:00:00 2001 From: alphaeusmote <41258468-alphaeusmote@users.noreply.replit.com> Date: Thu, 10 Apr 2025 02:20:01 +0000 Subject: [PATCH] Add DNS metrics tracking to the system Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9111ef36-26c8-4085-84ca-a35dc1fec1b5 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7083d608-d6d3-4a6a-9a27-6286c5109627/02e6ed79-9c97-4071-86f7-9ef62362c508.jpg --- server/storage.ts | 105 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/server/storage.ts b/server/storage.ts index 1d82a4e..821b46a 100644 --- a/server/storage.ts +++ b/server/storage.ts @@ -7,7 +7,8 @@ import { type ApiToken, type InsertApiToken, type DnsHistory, type Webhook, type InsertWebhook, - type WebhookDeliveryLog, type InsertWebhookDeliveryLog + type WebhookDeliveryLog, type InsertWebhookDeliveryLog, + type DnsMetric, type InsertDnsMetric } from "@shared/schema"; import session from "express-session"; import { DatabaseStorage } from "./database-storage"; @@ -76,6 +77,13 @@ export interface IStorage { getWebhookDeliveryLog(id: string): Promise; getWebhookDeliveryLogsByWebhook(webhookId: string): Promise; + // DNS Metrics + addDnsMetric(metric: InsertDnsMetric): Promise; + getDnsMetric(id: string): Promise; + getDnsMetricsByDomain(domainId: string, metricType?: string, startDate?: Date, endDate?: Date): Promise; + getDnsMetricsByRecord(recordId: string, metricType?: string, startDate?: Date, endDate?: Date): Promise; + getDnsMetricsByType(metricType: string, startDate?: Date, endDate?: Date): Promise; + // Session store sessionStore: any; } @@ -90,6 +98,7 @@ export class MemStorage implements IStorage { private historyMap: Map; private webhooksMap: Map; private webhookDeliveryLogsMap: Map; + private metricsMap: Map; // Counters for IDs private userIdCounter: number; @@ -101,6 +110,7 @@ export class MemStorage implements IStorage { private historyIdCounter: number; private webhookIdCounter: number; private webhookDeliveryLogIdCounter: number; + private metricIdCounter: number; public sessionStore: any; @@ -114,6 +124,7 @@ export class MemStorage implements IStorage { this.historyMap = new Map(); this.webhooksMap = new Map(); this.webhookDeliveryLogsMap = new Map(); + this.metricsMap = new Map(); this.userIdCounter = 1; this.orgIdCounter = 1; @@ -124,6 +135,7 @@ export class MemStorage implements IStorage { this.historyIdCounter = 1; this.webhookIdCounter = 1; this.webhookDeliveryLogIdCounter = 1; + this.metricIdCounter = 1; // Session store is created in the DatabaseStorage class this.sessionStore = null; @@ -613,6 +625,97 @@ export class MemStorage implements IStorage { .filter(log => log.webhookId === webhookId) .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()); } + + // DNS Metrics + async addDnsMetric(metric: InsertDnsMetric): Promise { + const numId = this.metricIdCounter++; + const timestamp = new Date(); + + const newMetric: DnsMetric = { + id: numId.toString(), + domainId: metric.domainId || null, + recordId: metric.recordId || null, + metricType: metric.metricType, + value: metric.value, + source: metric.source || null, + tags: metric.tags || null, + timestamp + }; + + this.metricsMap.set(numId, newMetric); + return newMetric; + } + + async getDnsMetric(id: string): Promise { + return this.metricsMap.get(parseInt(id)); + } + + async getDnsMetricsByDomain( + domainId: string, + metricType?: string, + startDate?: Date, + endDate?: Date + ): Promise { + let metrics = Array.from(this.metricsMap.values()) + .filter(metric => metric.domainId === domainId); + + if (metricType) { + metrics = metrics.filter(metric => metric.metricType === metricType); + } + + if (startDate) { + metrics = metrics.filter(metric => metric.timestamp >= startDate); + } + + if (endDate) { + metrics = metrics.filter(metric => metric.timestamp <= endDate); + } + + return metrics.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime()); + } + + async getDnsMetricsByRecord( + recordId: string, + metricType?: string, + startDate?: Date, + endDate?: Date + ): Promise { + let metrics = Array.from(this.metricsMap.values()) + .filter(metric => metric.recordId === recordId); + + if (metricType) { + metrics = metrics.filter(metric => metric.metricType === metricType); + } + + if (startDate) { + metrics = metrics.filter(metric => metric.timestamp >= startDate); + } + + if (endDate) { + metrics = metrics.filter(metric => metric.timestamp <= endDate); + } + + return metrics.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime()); + } + + async getDnsMetricsByType( + metricType: string, + startDate?: Date, + endDate?: Date + ): Promise { + let metrics = Array.from(this.metricsMap.values()) + .filter(metric => metric.metricType === metricType); + + if (startDate) { + metrics = metrics.filter(metric => metric.timestamp >= startDate); + } + + if (endDate) { + metrics = metrics.filter(metric => metric.timestamp <= endDate); + } + + return metrics.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime()); + } } export const storage = new DatabaseStorage();