working charts
This commit is contained in:
@@ -4,6 +4,7 @@ import { connectToDatabase } from '@/lib/mongodb'
|
|||||||
export async function GET(request) {
|
export async function GET(request) {
|
||||||
const { searchParams } = new URL(request.url)
|
const { searchParams } = new URL(request.url)
|
||||||
const timeRange = searchParams.get('timeRange') || 'today'
|
const timeRange = searchParams.get('timeRange') || 'today'
|
||||||
|
const since = searchParams.get('since')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { db } = await connectToDatabase()
|
const { db } = await connectToDatabase()
|
||||||
@@ -13,30 +14,31 @@ export async function GET(request) {
|
|||||||
let startDate = new Date()
|
let startDate = new Date()
|
||||||
let endDate = new Date()
|
let endDate = new Date()
|
||||||
|
|
||||||
switch (timeRange) {
|
// If since parameter is provided, use it as the start date
|
||||||
case 'today':
|
if (since) {
|
||||||
startDate.setHours(0, 0, 0, 0)
|
startDate = new Date(since)
|
||||||
endDate = now
|
} else {
|
||||||
break
|
switch (timeRange) {
|
||||||
case 'yesterday':
|
case 'today':
|
||||||
startDate.setDate(now.getDate() - 1)
|
startDate.setHours(0, 0, 0, 0)
|
||||||
startDate.setHours(0, 0, 0, 0)
|
break
|
||||||
endDate = new Date(startDate)
|
case 'yesterday':
|
||||||
endDate.setHours(23, 59, 59, 999)
|
startDate.setDate(now.getDate() - 1)
|
||||||
break
|
startDate.setHours(0, 0, 0, 0)
|
||||||
case 'week':
|
endDate = new Date(startDate)
|
||||||
startDate.setDate(now.getDate() - 7)
|
endDate.setHours(23, 59, 59, 999)
|
||||||
startDate.setHours(0, 0, 0, 0)
|
break
|
||||||
endDate = now
|
case 'week':
|
||||||
break
|
startDate.setDate(now.getDate() - 7)
|
||||||
case 'month':
|
startDate.setHours(0, 0, 0, 0)
|
||||||
startDate.setMonth(now.getMonth() - 1)
|
break
|
||||||
startDate.setHours(0, 0, 0, 0)
|
case 'month':
|
||||||
endDate = now
|
startDate.setMonth(now.getMonth() - 1)
|
||||||
break
|
startDate.setHours(0, 0, 0, 0)
|
||||||
default:
|
break
|
||||||
startDate.setHours(0, 0, 0, 0)
|
default:
|
||||||
endDate = now
|
startDate.setHours(0, 0, 0, 0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await collection.find({
|
const data = await collection.find({
|
||||||
|
|||||||
@@ -17,12 +17,58 @@ export default function Dashboard() {
|
|||||||
const [timeRange, setTimeRange] = useState('today')
|
const [timeRange, setTimeRange] = useState('today')
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
const [minLoadingComplete, setMinLoadingComplete] = useState(false)
|
const [minLoadingComplete, setMinLoadingComplete] = useState(false)
|
||||||
|
const [lastUpdateTime, setLastUpdateTime] = useState(null)
|
||||||
|
|
||||||
const fetchCurrentData = async () => {
|
const fetchCurrentData = async () => {
|
||||||
try {
|
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()
|
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) {
|
} catch (error) {
|
||||||
console.error('Error fetching current data:', error)
|
console.error('Error fetching current data:', error)
|
||||||
}
|
}
|
||||||
@@ -30,9 +76,11 @@ export default function Dashboard() {
|
|||||||
|
|
||||||
const fetchHistoricalData = async () => {
|
const fetchHistoricalData = async () => {
|
||||||
try {
|
try {
|
||||||
|
setLastUpdateTime(null) // Reset last update time when changing time range
|
||||||
const response = await fetch(`/api/solar-data?timeRange=${timeRange}`)
|
const response = await fetch(`/api/solar-data?timeRange=${timeRange}`)
|
||||||
const newData = await response.json()
|
const newData = await response.json()
|
||||||
setData(newData)
|
setData(newData)
|
||||||
|
setCurrentData(newData)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching historical data:', error)
|
console.error('Error fetching historical data:', error)
|
||||||
} finally {
|
} finally {
|
||||||
@@ -78,7 +126,7 @@ export default function Dashboard() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const latestData = currentData[currentData.length - 1]?.data || {}
|
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 = {
|
const containerVariants = {
|
||||||
hidden: { opacity: 0 },
|
hidden: { opacity: 0 },
|
||||||
|
|||||||
Reference in New Issue
Block a user