From aa0e88d635e4f4730c1f2a21806327a768463e44 Mon Sep 17 00:00:00 2001 From: lumijiez <59575049+lumijiez@users.noreply.github.com> Date: Thu, 22 May 2025 21:12:22 +0300 Subject: [PATCH] working charts --- src/app/api/solar-data/route.js | 50 +++++++++++++++--------------- src/app/page.js | 54 +++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 27 deletions(-) diff --git a/src/app/api/solar-data/route.js b/src/app/api/solar-data/route.js index e99898d..44636d1 100644 --- a/src/app/api/solar-data/route.js +++ b/src/app/api/solar-data/route.js @@ -4,6 +4,7 @@ import { connectToDatabase } from '@/lib/mongodb' export async function GET(request) { const { searchParams } = new URL(request.url) const timeRange = searchParams.get('timeRange') || 'today' + const since = searchParams.get('since') try { const { db } = await connectToDatabase() @@ -13,30 +14,31 @@ export async function GET(request) { let startDate = new Date() let endDate = new Date() - switch (timeRange) { - case 'today': - startDate.setHours(0, 0, 0, 0) - endDate = now - break - case 'yesterday': - startDate.setDate(now.getDate() - 1) - startDate.setHours(0, 0, 0, 0) - endDate = new Date(startDate) - endDate.setHours(23, 59, 59, 999) - break - case 'week': - startDate.setDate(now.getDate() - 7) - startDate.setHours(0, 0, 0, 0) - endDate = now - break - case 'month': - startDate.setMonth(now.getMonth() - 1) - startDate.setHours(0, 0, 0, 0) - endDate = now - break - default: - startDate.setHours(0, 0, 0, 0) - endDate = now + // If since parameter is provided, use it as the start date + if (since) { + startDate = new Date(since) + } else { + switch (timeRange) { + case 'today': + startDate.setHours(0, 0, 0, 0) + break + case 'yesterday': + startDate.setDate(now.getDate() - 1) + startDate.setHours(0, 0, 0, 0) + endDate = new Date(startDate) + endDate.setHours(23, 59, 59, 999) + break + case 'week': + startDate.setDate(now.getDate() - 7) + startDate.setHours(0, 0, 0, 0) + break + case 'month': + startDate.setMonth(now.getMonth() - 1) + startDate.setHours(0, 0, 0, 0) + break + default: + startDate.setHours(0, 0, 0, 0) + } } const data = await collection.find({ diff --git a/src/app/page.js b/src/app/page.js index 461f8b7..e889386 100644 --- a/src/app/page.js +++ b/src/app/page.js @@ -17,12 +17,58 @@ export default function Dashboard() { const [timeRange, setTimeRange] = useState('today') const [loading, setLoading] = useState(true) const [minLoadingComplete, setMinLoadingComplete] = useState(false) + const [lastUpdateTime, setLastUpdateTime] = useState(null) const fetchCurrentData = async () => { try { - const response = await fetch('/api/solar-data?timeRange=today') + const url = lastUpdateTime + ? `/api/solar-data?timeRange=today&since=${lastUpdateTime}` + : '/api/solar-data?timeRange=today' + + const response = await fetch(url) const newData = await response.json() - setCurrentData(newData) + + if (newData.length > 0) { + setCurrentData(prevData => { + // Merge new data with existing data, avoiding duplicates + const mergedData = [...prevData] + newData.forEach(newItem => { + const newTimestamp = newItem.timestamp.$date || newItem.timestamp + const existingIndex = mergedData.findIndex(item => + (item.timestamp.$date || item.timestamp) === newTimestamp + ) + if (existingIndex === -1) { + mergedData.push(newItem) + } else { + mergedData[existingIndex] = newItem + } + }) + return mergedData + }) + + // Update last update time + const latestTimestamp = newData[newData.length - 1].timestamp.$date || newData[newData.length - 1].timestamp + setLastUpdateTime(latestTimestamp) + + // Update historical data if viewing today + if (timeRange === 'today') { + setData(prevData => { + const mergedData = [...prevData] + newData.forEach(newItem => { + const newTimestamp = newItem.timestamp.$date || newItem.timestamp + const existingIndex = mergedData.findIndex(item => + (item.timestamp.$date || item.timestamp) === newTimestamp + ) + if (existingIndex === -1) { + mergedData.push(newItem) + } else { + mergedData[existingIndex] = newItem + } + }) + return mergedData + }) + } + } } catch (error) { console.error('Error fetching current data:', error) } @@ -30,9 +76,11 @@ export default function Dashboard() { const fetchHistoricalData = async () => { try { + setLastUpdateTime(null) // Reset last update time when changing time range const response = await fetch(`/api/solar-data?timeRange=${timeRange}`) const newData = await response.json() setData(newData) + setCurrentData(newData) } catch (error) { console.error('Error fetching historical data:', error) } finally { @@ -78,7 +126,7 @@ export default function Dashboard() { } const latestData = currentData[currentData.length - 1]?.data || {} - const lastUpdateTime = currentData[currentData.length - 1]?.timestamp?.$date || currentData[currentData.length - 1]?.timestamp + const lastUpdateTimeStr = currentData[currentData.length - 1]?.timestamp?.$date || currentData[currentData.length - 1]?.timestamp const containerVariants = { hidden: { opacity: 0 },