feat(EnamellingMachineStatus): 添加历史数据降采样功能

当查询时间范围超过一周时,自动对历史数据进行降采样处理,仅保留关键时间点的数据。添加了日期范围判断和降采样工具函数,减少大数据量时的渲染压力。
master
huangjinysf 2 months ago
parent 8881159c6a
commit 825ea42171

@ -299,6 +299,60 @@ const equipmentAxlesMap = {
'QB034': ['左边', '右边']
};
const isMoreThanOneWeek = (startDate, endDate) => {
if (!startDate || !endDate) return false;
const start = new Date(startDate);
const end = new Date(endDate);
const diffDays = (end - start) / (1000 * 60 * 60 * 24);
return diffDays > 7;
};
const downsampleHistoryToKeyTimes = (records, targetHours = [8, 12, 16, 18]) => {
if (!records?.length) return [];
const groups = new Map();
records.forEach(item => {
const dt = new Date(item.create_time);
const dateKey = dt.toISOString().split('T')[0];
if (!groups.has(dateKey)) groups.set(dateKey, []);
groups.get(dateKey).push({ dt, item });
});
const result = [];
groups.forEach((items, dateKey) => {
items.sort((a, b) => a.dt - b.dt);
const selected = new Map();
targetHours.forEach(targetH => {
let closest = null;
let minDiff = Infinity;
items.forEach(({ dt, item }) => {
const diff = Math.abs((dt.getHours() - targetH) * 60 + dt.getMinutes());
if (diff < minDiff) {
minDiff = diff;
closest = item;
}
});
if (closest && minDiff <= 90) {
selected.set(targetH, closest);
}
});
targetHours.forEach(h => {
if (selected.has(h)) {
result.push(selected.get(h));
}
});
});
result.sort((a, b) => new Date(a.create_time) - new Date(b.create_time));
return result;
};
//
const shouldShowAxle = (degree) => {
if (!degree) return false;
@ -512,8 +566,16 @@ const fetchHistoryData = (model, specification, wireDisc) => {
.then(response => response.json())
.then(data => {
if (data.code === 200 && data.data && data.data.records) {
historyData.value = data.data.records;
hasHistoryData.value = historyData.value.length > 0;
let records = data.data.records;
if (isMoreThanOneWeek(dateRange.start_date, dateRange.end_date)) {
const beforeCount = records.length;
records = downsampleHistoryToKeyTimes(records);
console.log(`库存数据降采样:${beforeCount}${records.length} 条 (时间范围超过1周)`);
}
historyData.value = records;
hasHistoryData.value = records.length > 0;
nextTick(() => {
setTimeout(() => {

Loading…
Cancel
Save