All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m56s
89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
// app/utils/api.ts
|
|
export interface TimeSeriesEntry {
|
|
time: string;
|
|
value: number;
|
|
}
|
|
|
|
export interface TimeSeriesResponse {
|
|
consumption: TimeSeriesEntry[];
|
|
generation: TimeSeriesEntry[];
|
|
}
|
|
|
|
const API_URL =
|
|
process.env.NEXT_PUBLIC_FASTAPI_URL ;
|
|
|
|
export const crmapi = {
|
|
getProjects: async () => {
|
|
const res = await fetch(`${API_URL}/crm/projects`, {
|
|
});
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
return res.json();
|
|
},
|
|
};
|
|
|
|
export async function fetchPowerTimeseries(
|
|
site: string,
|
|
start: string,
|
|
end: string
|
|
): Promise<TimeSeriesResponse> { // <-- Change here
|
|
const params = new URLSearchParams({ site, start, end });
|
|
|
|
const res = await fetch(`${API_URL}/power-timeseries?${params.toString()}`);
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`Failed to fetch data: ${res.status}`);
|
|
}
|
|
|
|
const json = await res.json();
|
|
console.log(`🔍 API response from /power-timeseries?${params.toString()}:`, json); // ✅ log here
|
|
return json; // <-- This is a single object, not an array
|
|
}
|
|
|
|
export async function fetchForecast(
|
|
lat: number,
|
|
lon: number,
|
|
dec: number,
|
|
az: number,
|
|
kwp: number
|
|
): Promise<{ time: string; forecast: number }[]> {
|
|
const query = new URLSearchParams({
|
|
lat: lat.toString(),
|
|
lon: lon.toString(),
|
|
dec: dec.toString(),
|
|
az: az.toString(),
|
|
kwp: kwp.toString(),
|
|
}).toString();
|
|
|
|
const res = await fetch(`${API_URL}/forecast?${query}`);
|
|
if (!res.ok) throw new Error("Failed to fetch forecast");
|
|
|
|
return res.json();
|
|
}
|
|
|
|
export type MonthlyKPI = {
|
|
site: string;
|
|
month: string; // "YYYY-MM"
|
|
yield_kwh: number | null;
|
|
consumption_kwh: number | null;
|
|
grid_draw_kwh: number | null;
|
|
efficiency: number | null; // 0..1 (fraction)
|
|
peak_demand_kw: number | null;
|
|
avg_power_factor: number | null; // 0..1
|
|
load_factor: number | null; // 0..1
|
|
error?: string;
|
|
};
|
|
|
|
const API = process.env.NEXT_PUBLIC_FASTAPI_URL;
|
|
|
|
export async function fetchMonthlyKpi(params: {
|
|
site: string;
|
|
month: string; // "YYYY-MM"
|
|
consumption_topic?: string;
|
|
generation_topic?: string;
|
|
}): Promise<MonthlyKPI> {
|
|
const qs = new URLSearchParams(params as Record<string, string>);
|
|
const res = await fetch(`${API}/kpi/monthly?${qs.toString()}`, { cache: "no-store" });
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
return res.json();
|
|
}
|