mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Revert "Revert "feat: add-healthcheck""
This commit is contained in:
@@ -6,6 +6,7 @@ import {eq, and, ne, count} from "drizzle-orm";
|
||||
import {db} from "@/db";
|
||||
import * as drizzleDb from "@/db";
|
||||
import {slugify} from "@/utils/slugify";
|
||||
import {getHealthLast12hLogs} from "@/db/services/healthcheck";
|
||||
|
||||
const verifySlugUniqueness = async (slug: string, agentId?: string) => {
|
||||
const conditions = agentId ? and(eq(drizzleDb.schemas.agent.slug, slug), ne(drizzleDb.schemas.agent.id, agentId)) : eq(drizzleDb.schemas.agent.slug, slug);
|
||||
@@ -54,7 +55,10 @@ export const getAgentAction = userAction.schema(z.string()).action(async ({parse
|
||||
databases: true
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
data: agent,
|
||||
health: agent ? await getHealthLast12hLogs({ id: agent.id }) : []
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
@@ -1,17 +1,35 @@
|
||||
import { DatabaseWith } from "@/db/schema/07_database";
|
||||
import { EventKind, EventPayload } from "@/features/notifications/types";
|
||||
import { dispatchNotification } from "@/features/notifications/dispatch";
|
||||
import {db} from "@/db";
|
||||
import {eq} from "drizzle-orm";
|
||||
import * as drizzleDb from "@/db";
|
||||
|
||||
export async function sendNotificationsBackupRestore(database: DatabaseWith, event: EventKind) {
|
||||
if (!database.alertPolicies || database.alertPolicies.length === 0) {
|
||||
|
||||
const settings = await db.query.setting.findFirst({
|
||||
where: eq(drizzleDb.schemas.setting.name, "system"),
|
||||
with: { notificationChannel: true },
|
||||
});
|
||||
|
||||
const defaultPolicy = settings?.notificationChannel
|
||||
? [{
|
||||
id: null,
|
||||
notificationChannelId: settings.notificationChannel.id,
|
||||
enabled: settings.notificationChannel.enabled,
|
||||
eventKinds: ["error_backup" , "error_restore"]
|
||||
}]
|
||||
: [];
|
||||
|
||||
const policiesToUse = (database.alertPolicies && database.alertPolicies.length > 0)
|
||||
? database.alertPolicies.filter(policy => policy.enabled && policy.eventKinds.includes(event))
|
||||
: defaultPolicy;
|
||||
|
||||
if (!policiesToUse || policiesToUse.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const activePolicies = database.alertPolicies.filter(policy =>
|
||||
policy.enabled && policy.eventKinds.includes(event)
|
||||
);
|
||||
|
||||
const promises = activePolicies.map(alertPolicy => {
|
||||
const promises = policiesToUse.map(alertPolicy => {
|
||||
const date = new Date();
|
||||
let level: "info" | "critical" = "info";
|
||||
let message = "";
|
||||
@@ -41,6 +59,7 @@ export async function sendNotificationsBackupRestore(database: DatabaseWith, eve
|
||||
success_backup: `Backup Notification`,
|
||||
success_restore: `Restore Notification`,
|
||||
weekly_report: `Weekly Report Notification`,
|
||||
error_health_agent: "Health Agent Notification",
|
||||
};
|
||||
|
||||
const payload: EventPayload = {
|
||||
@@ -56,7 +75,7 @@ export async function sendNotificationsBackupRestore(database: DatabaseWith, eve
|
||||
},
|
||||
};
|
||||
|
||||
return dispatchNotification(payload, alertPolicy.id, undefined, undefined);
|
||||
return dispatchNotification(payload, alertPolicy.id == null ? undefined : alertPolicy.id, alertPolicy.id ? undefined : alertPolicy.notificationChannelId, undefined);
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -18,4 +18,4 @@ export interface EventPayload {
|
||||
data?: Record<string, any>;
|
||||
}
|
||||
|
||||
export type EventKind = ("error_backup" | "error_restore" | "success_restore" | "success_backup" | "weekly_report")
|
||||
export type EventKind = ("error_backup" | "error_restore" | "success_restore" | "success_backup" | "weekly_report" | "error_health_agent" | "error_health_database")
|
||||
|
||||
Reference in New Issue
Block a user