fix
This commit is contained in:
@@ -503,6 +503,12 @@ export async function GET(request) {
|
||||
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)
|
||||
|
||||
@@ -13,27 +13,42 @@ import { Navbar } from '@/components/Navbar'
|
||||
|
||||
export default function Dashboard() {
|
||||
const [data, setData] = useState([])
|
||||
const [currentData, setCurrentData] = useState([])
|
||||
const [timeRange, setTimeRange] = useState('today')
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
const fetchData = async () => {
|
||||
const fetchCurrentData = async () => {
|
||||
try {
|
||||
const response = await fetch('/api/solar-data?timeRange=today')
|
||||
const newData = await response.json()
|
||||
setCurrentData(newData)
|
||||
} catch (error) {
|
||||
console.error('Error fetching current data:', error)
|
||||
}
|
||||
}
|
||||
|
||||
const fetchHistoricalData = async () => {
|
||||
try {
|
||||
const response = await fetch(`/api/solar-data?timeRange=${timeRange}`)
|
||||
const newData = await response.json()
|
||||
setData(newData)
|
||||
} catch (error) {
|
||||
console.error('Error fetching data:', error)
|
||||
console.error('Error fetching historical data:', error)
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
fetchData()
|
||||
const interval = setInterval(fetchData, 20000)
|
||||
return () => clearInterval(interval)
|
||||
fetchCurrentData()
|
||||
const currentInterval = setInterval(fetchCurrentData, 20000)
|
||||
return () => clearInterval(currentInterval)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
fetchHistoricalData()
|
||||
}, [timeRange])
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center min-h-screen bg-gradient-to-br from-gray-900 via-gray-800 to-gray-900">
|
||||
@@ -52,8 +67,8 @@ export default function Dashboard() {
|
||||
)
|
||||
}
|
||||
|
||||
const latestData = data[data.length - 1]?.data || {}
|
||||
const lastUpdateTime = data[data.length - 1]?.timestamp?.$date || data[data.length - 1]?.timestamp
|
||||
const latestData = currentData[currentData.length - 1]?.data || {}
|
||||
const lastUpdateTime = currentData[currentData.length - 1]?.timestamp?.$date || currentData[currentData.length - 1]?.timestamp
|
||||
|
||||
const containerVariants = {
|
||||
hidden: { opacity: 0 },
|
||||
@@ -87,13 +102,13 @@ export default function Dashboard() {
|
||||
className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 sm:gap-6"
|
||||
>
|
||||
<motion.div variants={itemVariants} className="h-full">
|
||||
<BatteryStatus data={latestData} history={data} />
|
||||
<BatteryStatus data={latestData} history={currentData} />
|
||||
</motion.div>
|
||||
<motion.div variants={itemVariants} className="h-full">
|
||||
<PowerStats data={latestData} />
|
||||
</motion.div>
|
||||
<motion.div variants={itemVariants} className="h-full">
|
||||
<SystemStatus data={latestData} history={data} />
|
||||
<SystemStatus data={latestData} history={currentData} />
|
||||
</motion.div>
|
||||
</motion.div>
|
||||
|
||||
|
||||
@@ -4,7 +4,13 @@ import Image from 'next/image'
|
||||
export function Navbar({ lastUpdateTime }) {
|
||||
const handleExport = async (timeRange) => {
|
||||
try {
|
||||
console.log('Exporting report for:', timeRange)
|
||||
const response = await fetch(`/api/export-report?timeRange=${timeRange}`)
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`)
|
||||
}
|
||||
|
||||
const blob = await response.blob()
|
||||
const url = window.URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
@@ -16,6 +22,7 @@ export function Navbar({ lastUpdateTime }) {
|
||||
document.body.removeChild(a)
|
||||
} catch (error) {
|
||||
console.error('Error exporting report:', error)
|
||||
alert(`Failed to export report: ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,33 +8,40 @@ export function SolarChart({ data, type, timeRange, onTimeRangeChange }) {
|
||||
const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate())
|
||||
const startOfYesterday = new Date(startOfDay)
|
||||
startOfYesterday.setDate(startOfDay.getDate() - 1)
|
||||
const endOfYesterday = new Date(startOfYesterday)
|
||||
endOfYesterday.setHours(23, 59, 59, 999)
|
||||
const startOfWeek = new Date(startOfDay)
|
||||
startOfWeek.setDate(startOfDay.getDate() - 7)
|
||||
const startOfMonth = new Date(startOfDay)
|
||||
startOfMonth.setMonth(startOfDay.getMonth() - 1)
|
||||
|
||||
let startTime
|
||||
let startTime, endTime
|
||||
switch (timeRange) {
|
||||
case 'today':
|
||||
startTime = startOfDay
|
||||
endTime = now
|
||||
break
|
||||
case 'yesterday':
|
||||
startTime = startOfYesterday
|
||||
endTime = endOfYesterday
|
||||
break
|
||||
case 'week':
|
||||
startTime = startOfWeek
|
||||
endTime = now
|
||||
break
|
||||
case 'month':
|
||||
startTime = startOfMonth
|
||||
endTime = now
|
||||
break
|
||||
default:
|
||||
startTime = startOfDay
|
||||
endTime = now
|
||||
}
|
||||
|
||||
return data.filter(item => {
|
||||
const dateStr = item.timestamp.$date || item.timestamp
|
||||
const date = new Date(dateStr)
|
||||
return date >= startTime && date <= now
|
||||
return date >= startTime && date <= endTime
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user