wulf-pulse/app/api/dashboard/overview/route.ts
lorentz 1112a06afe feat: RMM Overshell, IT Glue audit/write-back, LogLift, link-aware bundles, dashboard overhaul
- 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>
2026-05-03 07:13:18 -04:00

168 lines
5.4 KiB
TypeScript

/**
* GET /api/dashboard/overview
* Single round-trip backing the new dashboard. All queries run in parallel.
*
* attention — counts that should pull a human's eyes
* observations — recent device_observations (loglift et al.)
* audits — recent endpoint_audits
* syncHealth — per-schedule last_run / last_status from sync_schedules
* stats — small footer: companies, CIs, xref linkage
*/
import { NextResponse } from 'next/server';
import { requireAuth } from '@/lib/auth-utils';
import postgresClient from '@/lib/services/postgres-client';
export async function GET() {
const { error } = await requireAuth();
if (error) return error;
type Counts = { count: string };
const [
linkConflictsRes,
itglueUnlinkedRes,
s1UnmappedRes,
schedulesRes,
observationsRes,
auditsRes,
syncHealthRes,
companiesRes,
ciRes,
xrefRes,
] = await Promise.all([
postgresClient.query<Counts>(
`SELECT COUNT(*)::text AS count FROM device_link_review WHERE resolved_at IS NULL`
),
postgresClient.query<Counts>(
`SELECT COUNT(*)::text AS count FROM device_external_ids WHERE source = 'itglue' AND configuration_item_id IS NULL`
),
postgresClient.query<Counts>(
`SELECT COUNT(*)::text AS count FROM device_external_ids WHERE source = 's1' AND configuration_item_id IS NULL`
),
postgresClient.query<{ enabled: string; total: string }>(
`SELECT
COUNT(*) FILTER (WHERE is_enabled)::text AS enabled,
COUNT(*)::text AS total
FROM sync_schedules`
),
postgresClient.query<{
id: string;
kind: string;
source: string;
collected_at: string;
hostname: string | null;
company_name: string | null;
run_id: string | null;
}>(
`SELECT o.id::text,
o.kind, o.source,
o.collected_at::text,
ci.reference_title AS hostname,
c.company_name,
o.run_id
FROM device_observations o
LEFT JOIN configuration_items ci ON ci.id = o.configuration_item_id
LEFT JOIN companies c ON c.id = ci.company_id
ORDER BY o.collected_at DESC
LIMIT 10`
),
postgresClient.query<{
id: string;
generated_at: string;
hostname: string | null;
company_name: string | null;
overall_score: string | null;
field_gaps_count: string;
status: string;
}>(
`SELECT a.id::text,
a.generated_at::text,
ci.reference_title AS hostname,
c.company_name,
a.overall_score::text,
jsonb_array_length(COALESCE(a.field_gaps, '[]'::jsonb))::text AS field_gaps_count,
a.status
FROM endpoint_audits a
LEFT JOIN configuration_items ci ON ci.id = a.configuration_item_id
LEFT JOIN companies c ON c.id = ci.company_id
ORDER BY a.generated_at DESC
LIMIT 10`
),
postgresClient.query<{
id: string;
name: string;
sync_type: string;
is_enabled: boolean;
last_run: string | null;
last_status: string | null;
last_error: string | null;
next_run: string | null;
}>(
`SELECT id, name, sync_type, is_enabled,
last_run::text, last_status, last_error,
next_run::text
FROM sync_schedules
ORDER BY name`
),
postgresClient.query<Counts>(
`SELECT COUNT(*)::text AS count FROM companies WHERE company_type = 1 AND is_active = true`
),
postgresClient.query<Counts>(
`SELECT COUNT(*)::text AS count FROM configuration_items WHERE is_deleted = false OR is_deleted IS NULL`
),
postgresClient.query<{ total: string; linked: string }>(
`SELECT COUNT(*)::text AS total,
COUNT(*) FILTER (WHERE configuration_item_id IS NOT NULL)::text AS linked
FROM device_external_ids`
),
]);
return NextResponse.json({
attention: {
linkConflicts: parseInt(linkConflictsRes.rows[0]?.count ?? '0', 10),
itglueUnlinked: parseInt(itglueUnlinkedRes.rows[0]?.count ?? '0', 10),
s1Unmapped: parseInt(s1UnmappedRes.rows[0]?.count ?? '0', 10),
schedules: {
enabled: parseInt(schedulesRes.rows[0]?.enabled ?? '0', 10),
total: parseInt(schedulesRes.rows[0]?.total ?? '0', 10),
},
},
observations: observationsRes.rows.map((r) => ({
id: r.id,
kind: r.kind,
source: r.source,
collectedAt: r.collected_at,
hostname: r.hostname,
companyName: r.company_name,
runId: r.run_id,
})),
audits: auditsRes.rows.map((r) => ({
id: r.id,
generatedAt: r.generated_at,
hostname: r.hostname,
companyName: r.company_name,
overallScore: r.overall_score === null ? null : Number(r.overall_score),
fieldGapsCount: parseInt(r.field_gaps_count, 10),
status: r.status,
})),
syncHealth: syncHealthRes.rows.map((r) => ({
id: r.id,
name: r.name,
syncType: r.sync_type,
isEnabled: r.is_enabled,
lastRun: r.last_run,
lastStatus: r.last_status,
lastError: r.last_error,
nextRun: r.next_run,
})),
stats: {
activeCompanies: parseInt(companiesRes.rows[0]?.count ?? '0', 10),
configurationItems: parseInt(ciRes.rows[0]?.count ?? '0', 10),
xref: {
total: parseInt(xrefRes.rows[0]?.total ?? '0', 10),
linked: parseInt(xrefRes.rows[0]?.linked ?? '0', 10),
},
},
});
}