diff --git a/app/(admin)/adminDashboard/page.tsx b/app/(admin)/adminDashboard/page.tsx index e0c3ab7..6c577e9 100644 --- a/app/(admin)/adminDashboard/page.tsx +++ b/app/(admin)/adminDashboard/page.tsx @@ -10,6 +10,8 @@ import html2canvas from 'html2canvas'; import jsPDF from 'jspdf'; import dynamic from 'next/dynamic'; import { fetchPowerTimeseries } from '@/app/utils/api'; +import KpiTop from '@/components/dashboards/kpitop'; +import KpiBottom from '@/components/dashboards/kpibottom'; const EnergyLineChart = dynamic(() => import('@/components/dashboards/EnergyLineChart'), { ssr: false, @@ -19,6 +21,16 @@ const MonthlyBarChart = dynamic(() => import('@/components/dashboards/MonthlyBar ssr: false, }); +type MonthlyKPI = { + site: string; month: string; + yield_kwh: number | null; consumption_kwh: number | null; grid_draw_kwh: number | null; + efficiency: number | null; peak_demand_kw: number | null; + avg_power_factor: number | null; load_factor: number | null; + error?: string; +}; + +const API = process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:8000'; + import { SiteName, SiteDetails, mockSiteData } from '@/types/SiteData'; const AdminDashboard = () => { @@ -34,6 +46,8 @@ const AdminDashboard = () => { const siteParam = searchParams?.get('site'); const validSiteNames: SiteName[] = ['Site A', 'Site B', 'Site C']; + const [kpi, setKpi] = useState(null); + const [selectedSite, setSelectedSite] = useState(() => { if (siteParam && validSiteNames.includes(siteParam as SiteName)) { return siteParam as SiteName; @@ -94,6 +108,23 @@ const AdminDashboard = () => { fetchData(); }, [selectedSite]); +// fetch KPI monthly (uses your FastAPI) + useEffect(() => { + const siteId = siteIdMap[selectedSite]; + const url = `${API}/kpi/monthly?site=${encodeURIComponent(siteId)}&month=${currentMonth}`; + fetch(url).then(r => r.json()).then(setKpi).catch(console.error); + }, [selectedSite]); + + // derived values with safe fallbacks + const yieldKwh = kpi?.yield_kwh ?? 0; + const consumptionKwh = kpi?.consumption_kwh ?? 0; + const gridDrawKwh = kpi?.grid_draw_kwh ?? Math.max(0, consumptionKwh - yieldKwh); + + const efficiencyPct = (kpi?.efficiency ?? 0) * 100; + const powerFactor = kpi?.avg_power_factor ?? 0; + const loadFactor = (kpi?.load_factor ?? 0); + + // ...your existing code above return() // Update query string when site is changed manually const handleSiteChange = (newSite: SiteName) => { setSelectedSite(newSite); @@ -169,7 +200,7 @@ const AdminDashboard = () => {

Admin Dashboard

-
+
{ lastSyncTimestamp={currentSiteDetails.lastSyncTimestamp} />
- -
- -
- -
-
- - + {/* TOP 3 CARDS */} +
+
-
+ +
+ +
+ {/* BOTTOM 3 PANELS */} +
-
- + } + right={ +
+
+ {(kpi?.peak_demand_kw ?? 0).toFixed(2)} kW +
+
+ } + />