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