import { useState, useEffect } from 'react'; import { Box, Paper, Typography, Fade, useTheme, AppBar, Toolbar, Chip } from '@mui/material'; import Plot from 'react-plotly.js'; import { Layout, PlotData } from 'plotly.js'; interface DataItem { now_timestamp: string; future_timestamp: string; power: string; power_future_15min: string; positive_3p: string; negative_3p: string; positive_7p: string; negative_7p: string; flag: string; } const Maintenance = () => { const theme = useTheme(); const [chartData, setChartData] = useState[]>([]); const [currentFlag, setCurrentFlag] = useState(''); useEffect(() => { const fetchData = async () => { try { const response = await fetch('http://141.196.83.136:8003/prom/get_chart_data/maintenance/20'); const result = await response.json(); if (result.data && result.data.length > 0) { const last20Data = result.data.slice(-20); setCurrentFlag(last20Data[last20Data.length - 1].flag); const traces: Partial[] = [ { x: last20Data.map((item: DataItem) => item.now_timestamp), y: last20Data.map((item: DataItem) => parseFloat(item.power)), type: 'scatter' as const, mode: 'lines+markers' as const, name: 'Current Power', line: { color: '#2196f3', width: 2 }, marker: { size: 6, symbol: 'circle' } }, { x: last20Data.map((item: DataItem) => item.future_timestamp), y: last20Data.map((item: DataItem) => parseFloat(item.power_future_15min)), type: 'scatter' as const, mode: 'lines+markers' as const, name: 'Predicted (15min)', line: { color: '#4caf50', width: 2, dash: 'dot' }, marker: { size: 6, symbol: 'circle' } }, { x: last20Data.map((item: DataItem) => item.future_timestamp), y: last20Data.map((item: DataItem) => parseFloat(item.positive_3p)), type: 'scatter' as const, mode: 'lines' as const, name: '+3% Positive', line: { color: '#2ca02c', width: 1.5 }, showlegend: true, }, { x: last20Data.map((item: DataItem) => item.future_timestamp), y: last20Data.map((item: DataItem) => parseFloat(item.negative_3p)), type: 'scatter' as const, mode: 'lines' as const, name: '-3% Negative', line: { color: '#d62728', width: 1.5 }, showlegend: true, }, { x: last20Data.map((item: DataItem) => item.future_timestamp), y: last20Data.map((item: DataItem) => parseFloat(item.positive_7p)), type: 'scatter' as const, mode: 'lines' as const, name: '+7% Positive', line: { color: '#9467bd', width: 1.5 }, showlegend: true, }, { x: last20Data.map((item: DataItem) => item.future_timestamp), y: last20Data.map((item: DataItem) => parseFloat(item.negative_7p)), type: 'scatter' as const, mode: 'lines' as const, name: '-7% Negative', line: { color: '#8c564b', width: 1.5 }, showlegend: true, } ]; setChartData(traces); } } catch (error) { console.error('Error fetching data:', error); } }; fetchData(); const interval = setInterval(fetchData, 5000); return () => clearInterval(interval); }, []); const layout: Partial = { xaxis: { title: { text: 'Time', font: { size: 14, color: '#666', family: undefined } }, type: 'date', gridcolor: '#eee', tickfont: { size: 12, family: undefined, color: '#666' }, showgrid: true, gridwidth: 1, rangeslider: { visible: true } }, yaxis: { title: { text: 'Power (W)', font: { size: 14, color: '#666', family: undefined } }, gridcolor: '#eee', tickfont: { size: 12, family: undefined, color: '#666' }, showgrid: true, gridwidth: 1, rangemode: 'tozero' as const, fixedrange: false, range: [0, Math.max(...chartData.flatMap(trace => trace.y as number[]).filter(Boolean)) * 1.1] }, showlegend: true, legend: { orientation: 'h', y: -0.2, x: 0.5, xanchor: 'center', yanchor: 'top', font: { size: 12, family: theme.typography.fontFamily, color: theme.palette.text.secondary }, bgcolor: 'rgba(255, 255, 255, 0)', bordercolor: 'rgba(255, 255, 255, 0)' }, margin: { t: 60, b: 100, l: 60, r: 20 }, plot_bgcolor: 'rgba(0,0,0,0)', paper_bgcolor: 'rgba(0,0,0,0)', hovermode: 'closest', modebar: { bgcolor: 'rgba(255, 255, 255, 0)', color: theme.palette.text.secondary, activecolor: theme.palette.primary.main } }; return ( Preventive Maintenance {currentFlag && ( )} ); }; export default Maintenance;