"use client"; import { useEffect, useState, useCallback } from "react"; import { toast } from "sonner"; import { fetchEnergyReport, fetchUtilityPower, type EnergyReport, type UtilityPower, } from "@/lib/api"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Skeleton } from "@/components/ui/skeleton"; import { AreaChart, Area, LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, ReferenceLine, } from "recharts"; import { Zap, Leaf, RefreshCw, TrendingDown, DollarSign, Activity } from "lucide-react"; import { cn } from "@/lib/utils"; const SITE_ID = "sg-01"; // Singapore grid emission factor (kgCO2e/kWh) — Energy Market Authority 2023 const GRID_EF_KG_CO2_KWH = 0.4168; // Approximate WUE for air-cooled DC in Singapore climate const WUE_EST = 1.4; function KpiTile({ label, value, sub, icon: Icon, iconClass, warn, }: { label: string; value: string; sub?: string; icon?: React.ElementType; iconClass?: string; warn?: boolean; }) { return (
{Icon && }

{label}

{value}

{sub &&

{sub}

}
); } function SectionHeader({ children }: { children: React.ReactNode }) { return (

{children}

); } export default function EnergyPage() { const [energy, setEnergy] = useState(null); const [utility, setUtility] = useState(null); const [loading, setLoading] = useState(true); const load = useCallback(async () => { try { const [e, u] = await Promise.all([ fetchEnergyReport(SITE_ID, 30), fetchUtilityPower(SITE_ID).catch(() => null), ]); setEnergy(e); setUtility(u); } catch { toast.error("Failed to load energy data"); } finally { setLoading(false); } }, []); useEffect(() => { load(); const id = setInterval(load, 60_000); return () => clearInterval(id); }, [load]); const co2e_kg = energy ? Math.round(energy.kwh_total * GRID_EF_KG_CO2_KWH) : null; const co2e_t = co2e_kg ? (co2e_kg / 1000).toFixed(2) : null; const wue_water = energy ? (energy.kwh_total * (WUE_EST - 1)).toFixed(0) : null; const itKwChart = (energy?.pue_trend ?? []).map((d) => ({ day: new Date(d.day).toLocaleDateString("en-GB", { month: "short", day: "numeric" }), kw: d.avg_it_kw, pue: d.pue_est, })); const avgPue30 = energy?.pue_estimated ?? null; const pueWarn = avgPue30 != null && avgPue30 > 1.5; return (
{/* Header */}

Energy & Sustainability

Singapore DC01 — 30-day energy analysis · refreshes every 60s

{!loading && (
{co2e_t ? `${co2e_t} tCO₂e this month` : "—"}
)}
{/* Site energy banner */} {!loading && utility && (
Current IT load: {utility.total_kw.toFixed(1)} kW
Tariff: SGD {utility.tariff_sgd_kwh.toFixed(3)}/kWh
Month-to-date: {utility.kwh_month_to_date.toFixed(0)} kWh (SGD {utility.cost_sgd_mtd.toFixed(0)})
Singapore · SP Group grid
)} {/* 30-day KPIs */} 30-Day Energy Summary {loading ? (
{Array.from({ length: 4 }).map((_, i) => )}
) : (
)} {/* IT Load trend */} {(loading || itKwChart.length > 0) && ( Daily IT Load — 30 Days {loading ? ( ) : ( `${v} kW`} domain={["auto", "auto"]} /> [`${Number(v).toFixed(1)} kW`, "IT Load"]} /> )} )} {/* PUE trend */} {(loading || itKwChart.length > 0) && ( PUE Trend — 30 Days (target: < 1.4) {loading ? ( ) : ( [Number(v).toFixed(3), "PUE"]} /> )} )} {/* Sustainability */} Sustainability Metrics {loading ? (
{Array.from({ length: 3 }).map((_, i) => )}
) : (

Carbon Footprint

{co2e_t ?? "—"} tCO₂e

30-day estimate · {energy?.kwh_total.toFixed(0) ?? "—"} kWh × {GRID_EF_KG_CO2_KWH} kgCO₂e/kWh

Singapore grid emission factor (EMA 2023)

Water Usage (WUE)

{WUE_EST.toFixed(1)}

Estimated WUE (L/kWh) · air-cooled DC

Est. {wue_water ? `${Number(wue_water).toLocaleString()} L` : "—"} consumed (30d)

Efficiency

{avgPue30?.toFixed(3) ?? "—"}

Avg PUE · {avgPue30 != null && avgPue30 < 1.4 ? "Excellent — Tier IV class" : avgPue30 != null && avgPue30 < 1.6 ? "Good — industry average" : "Above average — optimise cooling"}

IT energy efficiency: {avgPue30 != null ? `${(1 / avgPue30 * 100).toFixed(1)}%` : "—"} of total power to IT

)} {/* Reference info */}

Singapore Energy Context

Grid emission factor: {GRID_EF_KG_CO2_KWH} kgCO₂e/kWh (EMA 2023, predominantly natural gas + growing solar)

Electricity tariff: SGD {utility?.tariff_sgd_kwh.toFixed(3) ?? "0.298"}/kWh (SP Group commercial rate)

BCA Green Mark: Targeting GoldPLUS certification · PUE target < 1.4

CO₂e and WUE estimates are indicative. Actual values depend on metered chilled water and cooling tower data.

); }