-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
164 lines (134 loc) · 7.2 KB
/
Copy pathapi.ts
File metadata and controls
164 lines (134 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const BASE = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000";
async function fetcher<T>(path: string, init?: RequestInit): Promise<T> {
const token = typeof window !== "undefined" ? localStorage.getItem("gridalytics_token") : null;
const headers: Record<string, string> = {
"Content-Type": "application/json",
...(init?.headers as Record<string, string>),
};
if (token) headers["Authorization"] = `Bearer ${token}`;
const res = await fetch(`${BASE}${path}`, { ...init, headers });
if (!res.ok) {
// Auto-redirect to login on 401 (expired/invalid token)
if (res.status === 401 && typeof window !== "undefined" && !path.includes("/auth/")) {
localStorage.removeItem("gridalytics_token");
// Don't redirect for public endpoints that work without auth
if (path.includes("/admin/")) {
window.location.href = "/login";
}
}
const err = await res.json().catch(() => ({ detail: res.statusText }));
const error = new Error(err.detail || res.statusText);
(error as any).status = res.status;
throw error;
}
return res.json();
}
// --- Health ---
export const getHealth = () => fetcher<{
status: string; models_loaded: number; database: string;
demand_latest: string | null; weather_latest: string | null;
}>("/api/v1/health/ready");
// --- Dashboard ---
export const getLive = () => fetcher<{
current_demand_mw: number | null; timestamp: string | null;
forecast_1h_mw: number | null; forecast_1h_lower: number | null; forecast_1h_upper: number | null;
weather: Record<string, number>; today_peak_mw: number | null; today_peak_time: string | null;
vs_yesterday_pct: number | null;
}>("/api/v1/dashboard/live");
export const getHistorical = (days: number, resolution = "hourly") =>
fetcher<{
timestamps: string[]; demand_mw: (number | null)[]; temperature: (number | null)[]; humidity: (number | null)[];
}>(`/api/v1/dashboard/historical?days=${days}&resolution=${resolution}`);
export const getStats = () => fetcher<{
today: Record<string, number | null>; yesterday: Record<string, number | null>;
this_week_avg: number | null; last_week_avg: number | null; season: string; demand_trend: string;
}>("/api/v1/dashboard/stats/summary");
export const getHeatmap = (days = 30) => fetcher<{
hours: number[]; days: string[]; values: number[][];
}>(`/api/v1/dashboard/heatmap?days=${days}`);
export const getModelPerformance = () => fetcher<{
champion: Record<string, any>; models_available: string[]; all_models: any[];
}>("/api/v1/dashboard/model-performance");
// --- Forecast ---
export const getForecast = (resolution: string, date: string) =>
fetcher<{
timestamps: string[]; predicted_mw: number[]; lower_bound_mw: number[]; upper_bound_mw: number[];
model_name: string; resolution: string; metadata: Record<string, any>;
}>(`/api/v1/forecast/${resolution}?date=${date}`);
export const getWhatIf = (body: { date: string; resolution: string; overrides: Record<string, any> }) =>
fetcher<{
timestamps: string[]; predicted_mw: number[]; lower_bound_mw: number[]; upper_bound_mw: number[];
model_name: string; metadata: Record<string, any>;
}>("/api/v1/forecast/what-if", { method: "POST", body: JSON.stringify(body) });
export const getForecastRange = (resolution: string, start: string, end: string, model?: string) =>
fetcher<{
timestamps: string[]; predicted_mw: number[]; lower_bound_mw: number[]; upper_bound_mw: number[];
model_name: string; resolution: string; metadata: Record<string, any>;
}>(`/api/v1/forecast/${resolution}/range?start=${start}&end=${end}${model ? `&model_name=${model}` : ""}`);
export const getForecastPeak = (resolution: string, date: string) =>
fetcher<{ date: string; peak_mw: number; peak_time: string; avg_mw: number; min_mw: number }>(
`/api/v1/forecast/${resolution}/peak?date=${date}`
);
export const getAvailableModels = () =>
fetcher<Record<string, string>>("/api/v1/forecast/models/available");
export const getSeasonalStats = () =>
fetcher<{ seasons: { season: string; min_mw: number; max_mw: number; avg_mw: number; std_mw: number; days: number }[] }>(
"/api/v1/dashboard/stats/seasonal"
);
export const getPredictionHistory = (days: number) =>
fetcher<{ entries: any[]; summary: any }>(`/api/v1/dashboard/prediction-history?days=${days}`);
export const getAccuracyTrend = (days: number) =>
fetcher<{ dates: string[]; daily_mape: number[]; rolling_7d_mape: number[]; rolling_30d_mape: number[]; drift_status: string; threshold: number }>(
`/api/v1/dashboard/accuracy-trend?days=${days}`
);
export const getForecastWithModel = (resolution: string, date: string, model: string) =>
fetcher<{
timestamps: string[]; predicted_mw: number[]; lower_bound_mw: number[]; upper_bound_mw: number[];
model_name: string; resolution: string; metadata: Record<string, any>;
}>(`/api/v1/forecast/${resolution}?date=${date}&model=${model}`);
export const getSubregionForecast = (resolution: string, date: string) =>
fetcher<{ date: string; resolution: string; regions: Record<string, { predicted_mw: number[]; peak_mw: number | null; avg_mw: number | null }> }>(
`/api/v1/forecast/${resolution}/subregion?date=${date}`
);
export const getAnomalies = (days: number = 30) =>
fetcher<any>(`/api/v1/dashboard/anomalies?days=${days}`);
export const getErrorByHour = (days: number = 30) =>
fetcher<{ hours: number[]; avg_demand: number[]; std_demand: number[]; avg_error: number[]; avg_pct_error: number[] }>(
`/api/v1/dashboard/error-by-hour?days=${days}`
);
// --- Auth ---
export const login = (email: string, password: string) =>
fetcher<{ access_token: string; token_type: string }>("/api/v1/auth/login", {
method: "POST",
body: JSON.stringify({ email, password }),
});
export const register = (email: string, password: string, full_name: string) =>
fetcher<{ access_token: string; token_type: string }>("/api/v1/auth/register", {
method: "POST",
body: JSON.stringify({ email, password, full_name }),
});
export const getMe = () => {
const token = typeof window !== "undefined" ? localStorage.getItem("gridalytics_token") : null;
if (!token) return Promise.reject(new Error("Not authenticated"));
return fetcher<{ id: number; email: string; full_name: string; role: string }>("/api/v1/auth/me");
};
// --- Admin ---
export const getAdminModels = () => fetcher<any[]>("/api/v1/admin/models");
export const triggerRetrain = (resolution: string) =>
fetcher<{ status: string }>("/api/v1/admin/retrain", {
method: "POST",
body: JSON.stringify({ resolution }),
});
export const getRetrainStatus = () =>
fetcher<{ status: string; resolution: string | null }>("/api/v1/admin/retrain/status");
export const getScraperStatus = () => fetcher<any>("/api/v1/admin/scraper-status");
export const getSchedulerJobs = () => fetcher<any[]>("/api/v1/admin/scheduler-jobs");
export const triggerPipeline = (workflow: string = "daily_predict.yml") =>
fetcher<{ status: string; workflow: string; actions_url: string }>(
"/api/v1/admin/trigger-pipeline",
{ method: "POST", body: JSON.stringify({ workflow }) }
);
export const getHourlyAccuracy = (days: number = 30) =>
fetcher<{ entries: any[]; by_hour: any[]; summary: any }>(
`/api/v1/dashboard/hourly-accuracy?days=${days}`
);