feat: set default notification channel

This commit is contained in:
charles-gauthereau
2026-03-25 22:07:20 +01:00
parent 7a4af4e1e0
commit 61b1a2e724
7 changed files with 2804 additions and 33 deletions
+2
View File
@@ -0,0 +1,2 @@
ALTER TABLE "settings" ADD COLUMN "default_notification_channel_id" uuid;--> statement-breakpoint
ALTER TABLE "settings" ADD CONSTRAINT "settings_default_notification_channel_id_notification_channel_id_fk" FOREIGN KEY ("default_notification_channel_id") REFERENCES "public"."notification_channel"("id") ON DELETE set null ON UPDATE no action;
File diff suppressed because it is too large Load Diff
+7
View File
@@ -295,6 +295,13 @@
"when": 1774378412843,
"tag": "0041_spooky_radioactive_man",
"breakpoints": true
},
{
"idx": 42,
"version": "7",
"when": 1774472168308,
"tag": "0042_breezy_namora",
"breakpoints": true
}
]
}
+3
View File
@@ -4,6 +4,7 @@ import {z} from "zod";
import {timestamps} from "@/db/schema/00_common";
import {storageChannel} from "@/db/schema/12_storage-channel";
import {relations} from "drizzle-orm";
import {notificationChannel} from "@/db/schema/09_notification-channel";
export const setting = pgTable("settings", {
@@ -15,6 +16,8 @@ export const setting = pgTable("settings", {
smtpPort: varchar("smtp_port", {length: 255}),
smtpUser: varchar("smtp_user", {length: 255}),
smtpSecure: boolean("smtp_secure"),
defaultNotificationChannelId: uuid('default_notification_channel_id')
.references(() => notificationChannel.id, {onDelete: "set null"}),
defaultStorageChannelId: uuid('default_storage_channel_id')
.references(() => storageChannel.id, {onDelete: "set null"}),
encryption: boolean("encryption").default(false),
+169 -2
View File
@@ -1,6 +1,10 @@
import {db} from "@/db";
import * as drizzleDb from "@/db";
import {and, eq, gte, lt} from "drizzle-orm";
import {and, eq, gte, isNotNull, lt} from "drizzle-orm";
import {enforceRetention} from "@/lib/tasks/database";
import {dispatchNotification} from "@/features/notifications/dispatch";
import {EventKind, EventPayload} from "@/features/notifications/types";
import {DatabaseWith} from "@/db/schema/07_database";
export async function getHealthLast12hLogs({id}: { id: string }) {
const now = new Date()
@@ -38,4 +42,167 @@ export async function deleteHealthLogsOlderThan12h() {
)
return logsToDelete.length
}
}
//
//
// export async function sendNotificationsHealthCheck(event: EventKind) {
//
//
//
// const date = new Date();
// let level: "info" | "critical" = "info";
// let message = "";
// let error: string | null = null;
//
// switch (event) {
// case "error_backup":
// case "error_restore":
// level = "critical";
// message = `An error occurred during ${event.includes("backup") ? "backup" : "restore"} on ${date.toISOString()}.`;
// error = "Check database connection or agent";
// break;
// case "success_backup":
// case "success_restore":
// level = "info";
// message = `${event.includes("backup") ? "Backup" : "Restore"} completed successfully at ${date.toISOString()}.`;
// break;
// case "weekly_report":
// level = "info";
// message = `Weekly report generated at ${date.toISOString()}.`;
// break;
// }
//
//
//
//
//
//
//
//
//
//
// const activePolicies = database.alertPolicies.filter(policy =>
// policy.enabled && policy.eventKinds.includes(event)
// );
//
// const promises = activePolicies.map(alertPolicy => {
// const date = new Date();
// let level: "info" | "critical" = "info";
// let message = "";
// let error: string | null = null;
//
// switch (event) {
// case "error_backup":
// case "error_restore":
// level = "critical";
// message = `An error occurred during ${event.includes("backup") ? "backup" : "restore"} on ${date.toISOString()}.`;
// error = "Check database connection or agent";
// break;
// case "success_backup":
// case "success_restore":
// level = "info";
// message = `${event.includes("backup") ? "Backup" : "Restore"} completed successfully at ${date.toISOString()}.`;
// break;
// case "weekly_report":
// level = "info";
// message = `Weekly report generated at ${date.toISOString()}.`;
// break;
// }
//
// const titleMap: Record<EventKind, string> = {
// error_backup: `Backup Notification`,
// error_restore: `Restore Notification`,
// success_backup: `Backup Notification`,
// success_restore: `Restore Notification`,
// weekly_report: `Weekly Report Notification`,
// };
//
// const payload: EventPayload = {
// title: titleMap[event],
// message,
// level: level,
// event: event,
// data: {
// host: database.name,
// id: database.id,
// agentDatabaseId: database.agentDatabaseId,
// error,
// },
// };
//
// return dispatchNotification(payload, alertPolicy.id, undefined, undefined);
// });
//
//
// return Promise.all(promises);
// }
//
//
//
// export async function checkAgentsHealthError() {
//
// const agents = await db.query.agent.findMany({
// where: isNotNull(drizzleDb.schemas.agent.lastContact),
// });
//
// for (const agent of agents) {
// const now = new Date()
// if (agent.lastContact - now > 10min) {
//
// if (agent.health_error_count < 4) {
// // peux pas depasser 3 count
// alors
// // update agent field health_error_count +1
//
// // send notifcations health_ping_fail
//
//
// const payload: EventPayload = {
// title: titleMap[event],
// message,
// level: level,
// event: event,
// data: {
// host: database.name,
// id: database.id,
// agentDatabaseId: database.agentDatabaseId,
// error,
// },
// };
//
//
// await dispatchNotification(payload, alertPolicy.id, undefined, undefined);
// }else {
// pass
// }
// }
//
//
// if (!db.retentionPolicy) continue;
// await enforceRetention(db.id, db.retentionPolicy);
// }
//
//
//
// const now = new Date()
// const threshold = new Date(now.getTime() - 12 * 60 * 60 * 1000)
//
// const logsToDelete = await db
// .select()
// .from(drizzleDb.schemas.healthcheckLog)
// .where(
// lt(drizzleDb.schemas.healthcheckLog.date, threshold)
// )
//
// console.log(`Number of logs found to delete: ${logsToDelete.length}`)
//
// await db
// .delete(drizzleDb.schemas.healthcheckLog)
// .where(
// lt(drizzleDb.schemas.healthcheckLog.date, threshold)
// )
//
// return logsToDelete.length
// }