import { api } from './client'; export interface DashboardDeviceRow { deviceId: string; deviceName: string; siteName: string | null; kwh: number; peakKw: number | null; lastSeen: string | null; cost: number | null; } export interface DashboardChartPoint { time: string; totalKw: number; } export interface DashboardSummary { totalKwh: number; currentActivePowerKw: number | null; activeDeviceCount: number; totalCost: number | null; devices: DashboardDeviceRow[]; chart: DashboardChartPoint[]; } export async function fetchDashboardSummary( fromUtc: string, toUtc: string, ): Promise { const { data } = await api.get('/dashboard/summary', { params: { from: fromUtc, to: toUtc }, }); return data; } // Trigger a browser download. Same-origin → cookie travels automatically. // We use window.location rather than axios+blob since the file is the user's // intent (a download), not an in-page resource we need to manipulate. export function downloadDashboardSummaryXlsx(fromUtc: string, toUtc: string) { const params = new URLSearchParams({ from: fromUtc, to: toUtc }); window.location.href = `/api/dashboard/summary/export.xlsx?${params.toString()}`; } export function downloadRawMeasurementsXlsx(fromUtc: string, toUtc: string, rowCap = 100_000) { const params = new URLSearchParams({ from: fromUtc, to: toUtc, rowCap: String(rowCap) }); window.location.href = `/api/dashboard/measurements/export.xlsx?${params.toString()}`; } export function downloadFleetDashboardXlsx() { window.location.href = `/api/fleet/dashboard/export.xlsx`; } export function downloadFleetCustomerCostXlsx(customerId: string, fromUtc: string, toUtc: string) { const params = new URLSearchParams({ from: fromUtc, to: toUtc }); window.location.href = `/api/fleet/customers/${encodeURIComponent(customerId)}/cost/export.xlsx?${params.toString()}`; }