fix: Fix last week's monitor data

Fix the last week's monitoring data in the Monitor overview page

Closes-Bug: #1975376
Change-Id: Ia478872afc45049faadf3837a4840b08f58c4316
This commit is contained in:
Jingwei.Zhang 2022-05-27 16:01:16 +08:00
parent e740c89b9c
commit 59424e1c02

View File

@ -47,16 +47,14 @@ const Index = function () {
const [memCount, setMemCount] = useState(0); const [memCount, setMemCount] = useState(0);
const [weekData, setWeekData] = useState(build7DaysData()); const [weekData, setWeekData] = useState(build7DaysData());
useEffect(() => { const fetchDataByDate = async (date) => {
const end = moment(); const end = moment(date).endOf('day');
const start = moment().startOf('day'); const start = moment(date).startOf('day');
setIsLoading(true); const result = await fetchData({
fetchData({
interval: STEP, interval: STEP,
currentRange: [start, end], currentRange: [start, end],
}) });
.then((d) => { const [cpuData, memoryData] = dataHandler(result).retData;
const [cpuData, memoryData] = dataHandler(d).retData;
const newCpuCount = cpuData.reduce( const newCpuCount = cpuData.reduce(
(pre, cur, idx) => (pre, cur, idx) =>
idx > 0 && cur.x - cpuData[idx - 1].x > STEP ? pre + 1 : pre, idx > 0 && cur.x - cpuData[idx - 1].x > STEP ? pre + 1 : pre,
@ -67,14 +65,40 @@ const Index = function () {
idx > 0 && cur.x - memoryData[idx - 1].x > STEP ? pre + 1 : pre, idx > 0 && cur.x - memoryData[idx - 1].x > STEP ? pre + 1 : pre,
0 0
); );
setCpuCount(newCpuCount); const total = newCpuCount + newMemoryCount;
setMemCount(newMemoryCount); return {
weekData[6].count = newCpuCount + newMemoryCount; date,
setWeekData([...weekData]); total,
}) cpuTotal: newCpuCount,
.finally(() => { memTotal: newMemoryCount,
setIsLoading(false); };
};
const fetchWeekData = async () => {
setIsLoading(true);
const reqs = weekData.map((it) => {
const { fullDate } = it;
return fetchDataByDate(fullDate);
}); });
try {
const results = await Promise.all(reqs);
results.forEach((r, index) => {
const { total, cpuTotal, memTotal } = r;
if (index === results.length - 1) {
setCpuCount(cpuTotal);
setMemCount(memTotal);
}
weekData[index].count = total;
});
} catch (e) {
console.log(e);
}
setWeekData([...weekData]);
setIsLoading(false);
};
useEffect(() => {
fetchWeekData();
}, []); }, []);
return isLoading ? ( return isLoading ? (
@ -107,6 +131,7 @@ function build7DaysData() {
const ret = []; const ret = [];
for (let index = 6; index >= 0; index--) { for (let index = 6; index >= 0; index--) {
ret.push({ ret.push({
fullDate: today.clone().subtract(index, 'day').format('YYYY-MM-DD'),
date: today.clone().subtract(index, 'day').format('MM-DD'), date: today.clone().subtract(index, 'day').format('MM-DD'),
count: 0, count: 0,
}); });