|
3 | 3 | import React, { useEffect, useState } from "react"; |
4 | 4 | import DatePicker from 'react-datepicker'; |
5 | 5 | import 'react-datepicker/dist/react-datepicker.css'; |
6 | | -import { string } from "zod"; |
7 | 6 | import ApplicationChart from "../../components/ApplicationChart"; |
8 | 7 |
|
9 | 8 | interface DataModel { |
@@ -35,9 +34,11 @@ const DataFetcher: React.FC = () => { |
35 | 34 | const [startDate, setStartDate] = useState<Date | null>(null); |
36 | 35 | const [endDate, setEndDate] = useState<Date | null>(null); |
37 | 36 | const [sortConfig, setSortConfig] = useState<{ key: keyof DataModel | null, direction: 'ascending' | 'descending' }>({ key: 'id', direction: 'descending' }); |
38 | | - const [itemsPerPage, setItemsPerPage] = useState(5); |
| 37 | + const [itemsPerPage, setItemsPerPage] = useState(25); |
39 | 38 | const [currentPage, setCurrentPage] = useState(1); |
40 | | - const [showChart, setShowChart] = useState<boolean>(false); |
| 39 | + const [interval, setIntervalTime] = useState<number>(10); // Default interval 10 seconds |
| 40 | + const [reloadInterval, setReloadInterval] = useState<NodeJS.Timeout | null>(null); |
| 41 | + |
41 | 42 |
|
42 | 43 | useEffect(() => { |
43 | 44 | const fetchData = async () => { |
@@ -117,9 +118,59 @@ const DataFetcher: React.FC = () => { |
117 | 118 |
|
118 | 119 | const paginatedData = sortedData.slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage); |
119 | 120 |
|
| 121 | + useEffect(() => { |
| 122 | + const storedInterval = localStorage.getItem('reloadInterval'); |
| 123 | + if (storedInterval) { |
| 124 | + setIntervalTime(Number(storedInterval)); |
| 125 | + } |
| 126 | + }, []); |
| 127 | + |
| 128 | + |
| 129 | + useEffect(() => { |
| 130 | + if (interval <= 10) { |
| 131 | + const newInterval = setInterval(() => { |
| 132 | + window.location.reload(); |
| 133 | + }, 10000); |
| 134 | + |
| 135 | + |
| 136 | + return () => clearInterval(newInterval); |
| 137 | + } else { |
| 138 | + const newInterval = setInterval(() => { |
| 139 | + window.location.reload(); |
| 140 | + }, interval * 1000); |
| 141 | + } |
| 142 | + |
| 143 | + }, [interval]); |
| 144 | + |
| 145 | + |
| 146 | + useEffect(() => { |
| 147 | + if (interval > 0) { |
| 148 | + localStorage.setItem('reloadInterval', interval.toString()); |
| 149 | + } else { |
| 150 | + localStorage.removeItem('reloadInterval'); |
| 151 | + } |
| 152 | + }, [interval]); |
| 153 | + |
120 | 154 | if (loading) return <p>Loading...</p>; |
121 | 155 | if (error) return <p>Error: {error}</p>; |
122 | 156 |
|
| 157 | + var installingCounts: number = 0; |
| 158 | + var failedCounts: number = 0; |
| 159 | + var doneCounts: number = 0 |
| 160 | + var unknownCounts: number = 0; |
| 161 | + data.forEach((item) => { |
| 162 | + if (item.status === "installing") { |
| 163 | + installingCounts += 1; |
| 164 | + } else if (item.status === "failed") { |
| 165 | + failedCounts += 1; |
| 166 | + } |
| 167 | + else if (item.status === "done") { |
| 168 | + doneCounts += 1; |
| 169 | + } |
| 170 | + else { |
| 171 | + unknownCounts += 1; |
| 172 | + } |
| 173 | + }); |
123 | 174 |
|
124 | 175 | return ( |
125 | 176 | <div className="p-6 mt-20"> |
@@ -160,10 +211,24 @@ const DataFetcher: React.FC = () => { |
160 | 211 | /> |
161 | 212 | <label className="text-sm text-gray-600 mt-1 block">Set a end date</label> |
162 | 213 | </div> |
| 214 | + |
| 215 | + <div className="mb-4 flex space-x-4"> |
| 216 | + <div> |
| 217 | + <input |
| 218 | + type="number" |
| 219 | + value={interval} |
| 220 | + onChange={e => setIntervalTime(Number(e.target.value))} |
| 221 | + className="p-2 border" |
| 222 | + placeholder="Interval (seconds)" |
| 223 | + /> |
| 224 | + <label className="text-sm text-gray-600 mt-1 block">Set reload interval (0 for no reload)</label> |
| 225 | + </div> |
| 226 | + </div> |
163 | 227 | </div> |
164 | 228 | <ApplicationChart data={filteredData} /> |
165 | 229 | <div className="mb-4 flex justify-between items-center"> |
166 | 230 | <p className="text-lg font-bold">{filteredData.length} results found</p> |
| 231 | + <p className="text-lg font">Status Legend: 🔄 installing {installingCounts} | ✔️ completetd {doneCounts} | ❌ failed {failedCounts} | ❓ unknown {unknownCounts}</p> |
167 | 232 | <select value={itemsPerPage} onChange={handleItemsPerPageChange} className="p-2 border"> |
168 | 233 | <option value={25}>25</option> |
169 | 234 | <option value={50}>50</option> |
|
0 commit comments