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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
import React, { useEffect, useState, useContext, useRef, useMemo } from 'react';
import { Typography, Spin, Message, Card, Grid } from '@arco-design/web-react';
import '@arco-design/web-react/dist/css/arco.css';
import { initVChartArcoTheme } from '@visactor/vchart-arco-theme';
import { VChart } from '@visactor/react-vchart';
import API_BASE_URL from '../config';
import { MarketContext, MARKET_FULL_NAMES } from '../App';
// TODO:
//
// - [ ] Cancel Promise if market changes
// - [ ] Fix Data Zoom being reset
const { Row, Col } = Grid;
function TimeAgo({ timestamp, prefix }) {
const [now, setNow] = useState(new Date());
useEffect(() => {
const timer = setInterval(() => setNow(new Date()), 1000);
return () => clearInterval(timer);
}, []);
const formatTimeAgo = () => {
if (!timestamp) return '';
const seconds = Math.floor((now - timestamp) / 1000);
if (seconds < 60) return `${seconds} sec ago`;
const minutes = Math.floor(seconds / 60);
if (minutes < 60) return `${minutes} min ago`;
const hours = Math.floor(minutes / 60);
return `${hours} hr ago`;
};
return (
<Typography.Text type="secondary" style={{ fontSize: 12, display: 'block', marginTop: 8 }}>
{prefix}: {formatTimeAgo()}
</Typography.Text>
);
}
function MarketDataPage() {
const { selectedMarket } = useContext(MarketContext);
const fiveMinChartRef = useRef(null);
const dayAheadChartRef = useRef(null);
const [fiveMinZoom, setFiveMinZoom] = useState({ start: 0.9, end: 1 });
const [dayAheadZoom, setDayAheadZoom] = useState({ start: 0.05, end: 1 });
const [fiveMinData, setFiveMinData] = useState([]);
const [dayAheadData, setDayAheadData] = useState([]);
const [loading, setLoading] = useState(true);
const [fiveMinLastUpdated, setFiveMinLastUpdated] = useState(null);
const [dayAheadLastUpdated, setDayAheadLastUpdated] = useState(null);
const [, setNow] = useState(new Date());
const handleZoomChange = (e, title) => {
if (!e?.start || !e?.end) return;
if (title === 'fiveMin') {
setFiveMinZoom({ start: e.start, end: e.end });
} else {
setDayAheadZoom({ start: e.start, end: e.end });
}
};
useEffect(() => {
const fetchFiveMinData = async () => {
try {
const res = await fetch(`${API_BASE_URL}/market/real-time?market=${selectedMarket}`);
if (!res.ok) throw new Error('Failed to fetch real-time data');
const json = await res.json();
setFiveMinData(json);
setFiveMinLastUpdated(new Date());
} catch (err) {
console.error(err);
Message.error('Failed to load real-time market data.');
}
};
const fetchDayAheadData = async () => {
try {
const res = await fetch(`${API_BASE_URL}/market/day-ahead?market=${selectedMarket}`);
if (!res.ok) throw new Error('Failed to fetch day-ahead data');
const json = await res.json();
setDayAheadData(json);
setDayAheadLastUpdated(new Date());
} catch (err) {
console.error(err);
Message.error('Failed to load day-ahead market data.');
}
};
const initialize = async () => {
setLoading(true);
await Promise.all([fetchFiveMinData(), fetchDayAheadData()]);
setLoading(false);
initVChartArcoTheme({
defaultMode: 'light',
isWatchingMode: true
});
};
initialize();
const fiveMinInterval = setInterval(fetchFiveMinData, 5 * 60 * 1000);
const dayAheadInterval = setInterval(fetchDayAheadData, 60 * 60 * 1000);
const timer = setInterval(() => setNow(new Date()), 1000);
return () => {
clearInterval(fiveMinInterval);
clearInterval(dayAheadInterval);
clearInterval(timer);
};
}, [selectedMarket]);
const fiveMinSpec = useMemo(() => ({
type: 'line',
data: {
values: fiveMinData.map(d => ({
timestamp: new Date(d.timestamp).toLocaleString(),
LMP: d.lmp,
Energy: d.energy,
Congestion: d.congestion,
Loss: d.loss
})),
transforms: [{
type: 'fold',
options: { key: 'name', value: 'value', fields: ['LMP', 'Energy', 'Congestion', 'Loss'] }
}]
},
xField: 'timestamp',
yField: 'value',
seriesField: 'name',
smooth: true,
legend: { position: 'top' },
tooltip: {
formatter: (datum) => ({
name: datum.name,
value: datum.value.toFixed(2)
})
},
dataZoom: [{
orient: 'bottom',
height: 20,
start: fiveMinZoom.start,
end: fiveMinZoom.end
}]
}), [fiveMinData, fiveMinZoom]); // <== DEPENDENCIES
const dayAheadSpec = useMemo(() => ({
type: 'line',
data: {
values: dayAheadData.map(d => ({
timestamp: new Date(d.timestamp).toLocaleString(),
LMP: d.lmp,
Energy: d.energy,
Congestion: d.congestion,
Loss: d.loss
})),
transforms: [{
type: 'fold',
options: { key: 'name', value: 'value', fields: ['LMP', 'Energy', 'Congestion', 'Loss'] }
}]
},
xField: 'timestamp',
yField: 'value',
seriesField: 'name',
smooth: true,
legend: { position: 'top' },
tooltip: {
formatter: (datum) => ({
name: datum.name,
value: datum.value.toFixed(2)
})
},
dataZoom: [{
orient: 'bottom',
height: 20,
start: dayAheadZoom.start,
end: dayAheadZoom.end
}]
}), [dayAheadData, dayAheadZoom]);
const computeKPIs = (data) => {
if (!data.length) return { avgLmp: 0, totalEnergy: 0, maxCongestion: 0, avgLoss: 0 };
const avgLmp = data.reduce((sum, d) => sum + d.lmp, 0) / data.length;
const totalEnergy = data.reduce((sum, d) => sum + d.energy, 0);
const maxCongestion = Math.max(...data.map(d => d.congestion));
const avgLoss = data.reduce((sum, d) => sum + d.loss, 0) / data.length;
return { avgLmp, totalEnergy, maxCongestion, avgLoss };
};
const fiveMinKPIs = computeKPIs(fiveMinData);
const dayAheadKPIs = computeKPIs(dayAheadData);
return (
<div className="page-container">
<Typography.Title heading={3} style={{ textAlign: 'center', marginBottom: 20 }} className="market-page-title" >
Market Data for {MARKET_FULL_NAMES[selectedMarket]}
</Typography.Title>
{loading ? (
<Spin style={{ display: 'block', margin: '80px auto' }} />
) : (
<>
<Row gutter={24} style={{ marginBottom: 30 }}>
{[
{ label: 'Avg Real-Time LMP ($/MWh)', value: fiveMinKPIs.avgLmp },
{ label: 'Total Real-Time Energy (MWh)', value: fiveMinKPIs.totalEnergy },
{ label: 'Max Real-Time Congestion ($)', value: fiveMinKPIs.maxCongestion },
{ label: 'Avg Real-Time Loss ($)', value: fiveMinKPIs.avgLoss }
].map((item, idx) => (
<Col xs={12} md={6} key={idx} style={{ marginBottom: 16 }}>
<Card
hoverable
style={{
textAlign: 'center',
borderRadius: 12,
boxShadow: '0 4px 12px rgba(0,0,0,0.05)'
}}
title={item.label}
>
<Typography.Title heading={5}>${item.value.toFixed(2)}</Typography.Title>
</Card>
</Col>
))}
</Row>
<Row gutter={24} style={{ marginBottom: 30 }}>
{[
{ label: 'Avg Day-Ahead LMP ($/MWh)', value: dayAheadKPIs.avgLmp },
{ label: 'Total Day-Ahead Energy (MWh)', value: dayAheadKPIs.totalEnergy },
{ label: 'Max Day-Ahead Congestion ($)', value: dayAheadKPIs.maxCongestion },
{ label: 'Avg Day-Ahead Loss ($)', value: dayAheadKPIs.avgLoss }
].map((item, idx) => (
<Col xs={12} md={6} key={idx} style={{ marginBottom: 16 }}>
<Card
hoverable
style={{
textAlign: 'center',
borderRadius: 12,
boxShadow: '0 4px 12px rgba(0,0,0,0.05)'
}}
title={item.label}
>
<Typography.Title heading={5}>${item.value.toFixed(2)}</Typography.Title>
</Card>
</Col>
))}
</Row>
<Row gutter={24}>
<Col xs={24} md={12}>
<Card title="Real-Time Market Data" extra={<TimeAgo timestamp={fiveMinLastUpdated} prefix="Updated" />} style={{ borderRadius: 12, marginBottom: 24 }}>
<div style={{ padding: 10 }}>
<VChart
spec={fiveMinSpec}
ref={fiveMinChartRef}
onDataZoom={(e) => handleZoomChange(e.detail, 'fiveMin')}
/>
</div>
</Card>
</Col>
<Col xs={24} md={12}>
<Card title="Day-Ahead Market Data" extra={<TimeAgo timestamp={dayAheadLastUpdated} prefix="Updated" />} style={{ borderRadius: 12, marginBottom: 24 }}>
<div style={{ padding: 10 }}>
<VChart
spec={dayAheadSpec}
ref={dayAheadChartRef}
onDataZoom={(e) => handleZoomChange(e.detail, 'dayAhead')}
/>
</div>
</Card>
</Col>
</Row>
</>
)}
<style>{`
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.arco-card-header-title {
white-space: normal !important; /* allow wrapping */
word-break: break-word;
}
/* Optional: smaller text on tiny devices */
@media (max-width: 768px) {
.arco-card-header-title {
font-size: 14px;
}
}
.market-page-title {
white-space: normal;
word-break: keep-all;
overflow-wrap: break-word;
text-align: center;
}
.page-container {
padding: 15px;
background: var(--color-fill-2);
animation: fadeIn 0.5s ease;
overflow-x: hidden; /* Also good practice */
}
/* Responsive: remove padding on small screens */
@media (max-width: 768px) {
.page-container {
padding: 0;
}
}
`}</style>
</div>
);
}
export default MarketDataPage;
|