- RMM Overshell (migration 077): admin page, dispatch UI, executor/worker, target resolver, script registry (AD/DHCP/DNS/event-log/services/software/network/loglift) - LogLift evidence pipeline (migration 078): upload webhook, B2 storage client, receiver/matcher, EventLogCollector PowerShell script - IT Glue audit + write-back (migrations 075, 076): asset-audit runner, ticket xrefs, applications/configurations browse pages + apply/revert/audit endpoints - Link-aware analyzer bundles (migration 073) + provider toggle (migration 074): link-discovery service, OpenRouter LLM provider, related-tickets/itglue-suggestion panels, analyze-bundle endpoint - Endpoint data model + device-link reconciliation (migrations 079, 080): conflicts admin page, reconciler service, resolve endpoints - Dashboard overhaul: integration-health service + alerts, overview/health endpoints - Permissions: add itglue + rmm scopes; middleware: public /api/rmm/loglift route Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
159 lines
4.4 KiB
TypeScript
159 lines
4.4 KiB
TypeScript
/**
|
|
* Daily integration-health alert job.
|
|
*
|
|
* Calls checkIntegrationHealth(), summarizes, and posts an Adaptive Card to
|
|
* morning-summary-webhooks ONLY when something needs attention. Quiet days
|
|
* stay quiet — no spam.
|
|
*/
|
|
|
|
import postgresClient from '@/lib/services/postgres-client';
|
|
import {
|
|
checkIntegrationHealth,
|
|
summarize,
|
|
type IntegrationHealth,
|
|
type HealthSummary,
|
|
} from '@/lib/services/integration-health';
|
|
|
|
interface WebhookRow {
|
|
id: number;
|
|
label: string;
|
|
webhook_url: string;
|
|
enabled: boolean;
|
|
}
|
|
|
|
export interface HealthAlertResult {
|
|
summary: HealthSummary;
|
|
items: IntegrationHealth[];
|
|
alertSent: boolean;
|
|
webhooksDelivered: number;
|
|
}
|
|
|
|
function buildHealthAdaptiveCard(items: IntegrationHealth[], summary: HealthSummary): object {
|
|
const failed = items.filter((i) => i.status === 'auth_failed' || i.status === 'unreachable');
|
|
const expired = items.filter(
|
|
(i) => i.tokenExpiry && i.tokenExpiry.daysRemaining <= 0
|
|
);
|
|
const expiringSoon = items.filter(
|
|
(i) => i.tokenExpiry && i.tokenExpiry.daysRemaining > 0 && i.tokenExpiry.daysRemaining <= 14
|
|
);
|
|
|
|
const facts: Array<{ title: string; value: string }> = [];
|
|
for (const i of failed) {
|
|
facts.push({
|
|
title: i.name,
|
|
value: `${i.status === 'auth_failed' ? '⚠ AUTH FAILED' : '⚠ UNREACHABLE'} — ${i.error?.slice(0, 120) ?? 'no detail'}`,
|
|
});
|
|
}
|
|
for (const i of expired) {
|
|
facts.push({
|
|
title: i.name,
|
|
value: `🔑 token EXPIRED ${Math.abs(i.tokenExpiry!.daysRemaining).toFixed(0)} days ago (${i.tokenExpiry!.envVar})`,
|
|
});
|
|
}
|
|
for (const i of expiringSoon) {
|
|
facts.push({
|
|
title: i.name,
|
|
value: `🔑 token expires in ${i.tokenExpiry!.daysRemaining.toFixed(0)} days (${i.tokenExpiry!.envVar})`,
|
|
});
|
|
}
|
|
|
|
return {
|
|
type: 'AdaptiveCard',
|
|
$schema: 'http://adaptivecards.io/schemas/adaptive-card.json',
|
|
version: '1.4',
|
|
body: [
|
|
{
|
|
type: 'TextBlock',
|
|
size: 'Large',
|
|
weight: 'Bolder',
|
|
text: 'Pulse — Integration Health Alert',
|
|
},
|
|
{
|
|
type: 'TextBlock',
|
|
spacing: 'None',
|
|
isSubtle: true,
|
|
wrap: true,
|
|
text: `${summary.failed} failing · ${summary.expired} expired · ${summary.expiringWithin14Days} expiring within 14 days`,
|
|
},
|
|
{
|
|
type: 'FactSet',
|
|
facts,
|
|
},
|
|
{
|
|
type: 'TextBlock',
|
|
spacing: 'Medium',
|
|
isSubtle: true,
|
|
wrap: true,
|
|
text: `Generated ${new Date().toISOString()}. ${summary.ok}/${summary.total} integrations healthy.`,
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
async function getEnabledWebhooks(): Promise<WebhookRow[]> {
|
|
const r = await postgresClient.query<WebhookRow>(
|
|
`SELECT id, label, webhook_url, enabled
|
|
FROM morning_summary_webhooks
|
|
WHERE enabled = true`
|
|
);
|
|
return r.rows;
|
|
}
|
|
|
|
async function deliverCard(card: object, webhooks: WebhookRow[]): Promise<number> {
|
|
const envelope = {
|
|
type: 'message',
|
|
attachments: [
|
|
{
|
|
contentType: 'application/vnd.microsoft.card.adaptive',
|
|
contentUrl: null,
|
|
content: card,
|
|
},
|
|
],
|
|
};
|
|
let delivered = 0;
|
|
await Promise.all(
|
|
webhooks.map(async (w) => {
|
|
try {
|
|
const res = await fetch(w.webhook_url, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(envelope),
|
|
});
|
|
if (res.ok) delivered += 1;
|
|
else console.warn(`[integration-health-alerts] ${w.label} responded ${res.status}`);
|
|
} catch (err) {
|
|
console.warn(
|
|
`[integration-health-alerts] ${w.label} delivery failed:`,
|
|
err instanceof Error ? err.message : err
|
|
);
|
|
}
|
|
})
|
|
);
|
|
return delivered;
|
|
}
|
|
|
|
export async function runIntegrationHealthAlertJob(): Promise<HealthAlertResult> {
|
|
const items = await checkIntegrationHealth({ skipCache: true });
|
|
const summary = summarize(items);
|
|
|
|
if (!summary.hasIssues) {
|
|
return { summary, items, alertSent: false, webhooksDelivered: 0 };
|
|
}
|
|
|
|
const webhooks = await getEnabledWebhooks();
|
|
if (webhooks.length === 0) {
|
|
console.log(
|
|
'[integration-health-alerts] issues found but no morning-summary-webhooks configured; skipping delivery'
|
|
);
|
|
return { summary, items, alertSent: false, webhooksDelivered: 0 };
|
|
}
|
|
|
|
const card = buildHealthAdaptiveCard(items, summary);
|
|
const delivered = await deliverCard(card, webhooks);
|
|
return {
|
|
summary,
|
|
items,
|
|
alertSent: delivered > 0,
|
|
webhooksDelivered: delivered,
|
|
};
|
|
}
|