feat: add list controls and DNS check history
This commit is contained in:
parent
11b16ac3cf
commit
24351eb897
14 changed files with 511 additions and 28 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { createDb, domain } from "@minmon/db";
|
||||
import { createDb, domain, domainCheckHistory } from "@minmon/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { resolve4 } from "node:dns/promises";
|
||||
import { Effect } from "effect";
|
||||
|
|
@ -46,22 +46,41 @@ export async function persistDomainCheck(
|
|||
database: ReturnType<typeof createDb>,
|
||||
input: {
|
||||
domainId: string;
|
||||
expectedServerIp: string;
|
||||
result: DomainCheckResult;
|
||||
},
|
||||
) {
|
||||
const [updatedDomain] = await database
|
||||
.update(domain)
|
||||
.set({
|
||||
resolutionStatus: input.result.status,
|
||||
lastResolvedIps: input.result.resolvedIps,
|
||||
lastResolvedIp: input.result.resolvedIps[0] ?? null,
|
||||
lastCheckedAt: input.result.checkedAt,
|
||||
lastCheckMessage: input.result.message,
|
||||
})
|
||||
.where(eq(domain.id, input.domainId))
|
||||
.returning();
|
||||
return database.transaction(async (transaction) => {
|
||||
const [historyEntry] = await transaction
|
||||
.insert(domainCheckHistory)
|
||||
.values({
|
||||
id: crypto.randomUUID(),
|
||||
domainId: input.domainId,
|
||||
expectedServerIp: input.expectedServerIp,
|
||||
status: input.result.status,
|
||||
resolvedIps: input.result.resolvedIps,
|
||||
message: input.result.message,
|
||||
checkedAt: input.result.checkedAt,
|
||||
})
|
||||
.returning();
|
||||
|
||||
return updatedDomain ?? null;
|
||||
const [updatedDomain] = await transaction
|
||||
.update(domain)
|
||||
.set({
|
||||
resolutionStatus: input.result.status,
|
||||
lastResolvedIps: input.result.resolvedIps,
|
||||
lastResolvedIp: input.result.resolvedIps[0] ?? null,
|
||||
lastCheckedAt: input.result.checkedAt,
|
||||
lastCheckMessage: input.result.message,
|
||||
})
|
||||
.where(eq(domain.id, input.domainId))
|
||||
.returning();
|
||||
|
||||
return {
|
||||
domain: updatedDomain ?? null,
|
||||
historyEntry: historyEntry ?? null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export async function runAndPersistDomainCheck(
|
||||
|
|
@ -79,13 +98,15 @@ export async function runAndPersistDomainCheck(
|
|||
}),
|
||||
);
|
||||
|
||||
const updatedDomain = await persistDomainCheck(database, {
|
||||
const persisted = await persistDomainCheck(database, {
|
||||
domainId: input.domainId,
|
||||
expectedServerIp: input.expectedServerIp,
|
||||
result,
|
||||
});
|
||||
|
||||
return {
|
||||
result,
|
||||
domain: updatedDomain,
|
||||
domain: persisted.domain,
|
||||
historyEntry: persisted.historyEntry,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { db } from "@minmon/db";
|
||||
import { domain, service } from "@minmon/db";
|
||||
import { domain, domainCheckHistory, service } from "@minmon/db";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
|
||||
import { runAndPersistDomainCheck } from "../lib/domain-check";
|
||||
|
|
@ -37,6 +37,16 @@ export const domainsRouter = router({
|
|||
return requireDomain(input.id);
|
||||
}),
|
||||
|
||||
history: permissionedProcedure("domains.read").input(byIdSchema).query(async ({ input }) => {
|
||||
await requireDomain(input.id);
|
||||
|
||||
return db
|
||||
.select()
|
||||
.from(domainCheckHistory)
|
||||
.where(eq(domainCheckHistory.domainId, input.id))
|
||||
.orderBy(desc(domainCheckHistory.checkedAt), desc(domainCheckHistory.id));
|
||||
}),
|
||||
|
||||
create: permissionedProcedure("domains.create").input(domainCreateSchema).mutation(async ({ input }) => {
|
||||
const { service: parentService, server: parentServer } = await requireServiceWithServer(input.serviceId);
|
||||
|
||||
|
|
@ -96,6 +106,7 @@ export const domainsRouter = router({
|
|||
return {
|
||||
domain: result.domain ?? existing,
|
||||
check: result.result,
|
||||
historyEntry: result.historyEntry,
|
||||
};
|
||||
}),
|
||||
|
||||
|
|
|
|||
17
packages/db/src/migrations/0003_add_domain_check_history.sql
Normal file
17
packages/db/src/migrations/0003_add_domain_check_history.sql
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
CREATE TABLE IF NOT EXISTS "domain_check_history" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"domain_id" text NOT NULL,
|
||||
"expected_server_ip" text NOT NULL,
|
||||
"status" "domain_resolution_status" NOT NULL,
|
||||
"resolved_ips" jsonb DEFAULT '[]'::jsonb NOT NULL,
|
||||
"message" text,
|
||||
"checked_at" timestamp DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "domain_check_history" ADD CONSTRAINT "domain_check_history_domain_id_domain_id_fk" FOREIGN KEY ("domain_id") REFERENCES "public"."domain"("id") ON DELETE cascade ON UPDATE no action;
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "domain_check_history_domain_id_idx" ON "domain_check_history" USING btree ("domain_id");
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "domain_check_history_checked_at_idx" ON "domain_check_history" USING btree ("checked_at");
|
||||
--> statement-breakpoint
|
||||
CREATE INDEX IF NOT EXISTS "domain_check_history_status_idx" ON "domain_check_history" USING btree ("status");
|
||||
|
|
@ -22,6 +22,13 @@
|
|||
"when": 1782698400000,
|
||||
"tag": "0002_user_lifecycle_permissions",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 3,
|
||||
"version": "7",
|
||||
"when": 1786104000000,
|
||||
"tag": "0003_add_domain_check_history",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -121,6 +121,29 @@ export const domain = pgTable(
|
|||
],
|
||||
);
|
||||
|
||||
export const domainCheckHistory = pgTable(
|
||||
"domain_check_history",
|
||||
{
|
||||
id: text("id").primaryKey(),
|
||||
domainId: text("domain_id")
|
||||
.notNull()
|
||||
.references(() => domain.id, { onDelete: "cascade" }),
|
||||
expectedServerIp: text("expected_server_ip").notNull(),
|
||||
status: domainResolutionStatusEnum("status").notNull(),
|
||||
resolvedIps: jsonb("resolved_ips")
|
||||
.$type<string[]>()
|
||||
.default(sql`'[]'::jsonb`)
|
||||
.notNull(),
|
||||
message: text("message"),
|
||||
checkedAt: timestamp("checked_at").defaultNow().notNull(),
|
||||
},
|
||||
(table) => [
|
||||
index("domain_check_history_domain_id_idx").on(table.domainId),
|
||||
index("domain_check_history_checked_at_idx").on(table.checkedAt),
|
||||
index("domain_check_history_status_idx").on(table.status),
|
||||
],
|
||||
);
|
||||
|
||||
export const note = pgTable(
|
||||
"note",
|
||||
{
|
||||
|
|
@ -153,11 +176,19 @@ export const serviceRelations = relations(service, ({ one, many }) => ({
|
|||
domains: many(domain),
|
||||
}));
|
||||
|
||||
export const domainRelations = relations(domain, ({ one }) => ({
|
||||
export const domainRelations = relations(domain, ({ one, many }) => ({
|
||||
service: one(service, {
|
||||
fields: [domain.serviceId],
|
||||
references: [service.id],
|
||||
}),
|
||||
checks: many(domainCheckHistory),
|
||||
}));
|
||||
|
||||
export const domainCheckHistoryRelations = relations(domainCheckHistory, ({ one }) => ({
|
||||
domain: one(domain, {
|
||||
fields: [domainCheckHistory.domainId],
|
||||
references: [domain.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
export const noteRelations = relations(note, ({ one }) => ({
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ dotenv.config({
|
|||
|
||||
import { db } from "./index";
|
||||
import { account, permission, role, rolePermission, user, userRole } from "./schema/auth";
|
||||
import { domain, note, server, service } from "./schema/monitoring";
|
||||
import { domain, domainCheckHistory, note, server, service } from "./schema/monitoring";
|
||||
|
||||
const now = new Date();
|
||||
const earlierToday = new Date(now.getTime() - 45 * 60 * 1000);
|
||||
|
|
@ -293,6 +293,20 @@ async function seedMonitoringData() {
|
|||
})),
|
||||
);
|
||||
|
||||
await db.insert(domainCheckHistory).values(
|
||||
domains
|
||||
.filter((item) => item.lastCheckedAt)
|
||||
.map((item) => ({
|
||||
id: `history_${item.id}`,
|
||||
domainId: item.id,
|
||||
expectedServerIp: item.expectedServerIp,
|
||||
status: item.resolutionStatus,
|
||||
resolvedIps: item.lastResolvedIps,
|
||||
message: item.lastCheckMessage,
|
||||
checkedAt: item.lastCheckedAt,
|
||||
})),
|
||||
);
|
||||
|
||||
await db.insert(note).values(
|
||||
notes.map((item) => ({
|
||||
...item,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue