优化余量计算,添加预计剩余使用时间。
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
|
||||
let historyByAssignment = {};
|
||||
let assignmentNames = {};
|
||||
let currentQuotaByAssignment = {};
|
||||
let selectedAssignmentId = '';
|
||||
let selectedRange = 'today';
|
||||
let myChart = null;
|
||||
@@ -299,11 +300,20 @@
|
||||
refreshAnimationId = requestAnimationFrame(render);
|
||||
}
|
||||
|
||||
function getAverageDailyUsage(points) {
|
||||
if (points.length < 2) return 0;
|
||||
const first = points[0];
|
||||
const last = points[points.length - 1];
|
||||
const elapsedDays = (last.timestamp - first.timestamp) / 86400000;
|
||||
return elapsedDays > 0 ? Math.max(0, (last.used - first.used) / elapsedDays) : 0;
|
||||
}
|
||||
|
||||
const currentUsageLinePlugin = {
|
||||
id: 'currentUsageLine',
|
||||
afterDraw(chart) {
|
||||
const points = getAssignmentPoints(selectedAssignmentId);
|
||||
const currentPoint = points[points.length - 1];
|
||||
const quota = currentQuotaByAssignment[selectedAssignmentId];
|
||||
const yScale = chart.scales.yUsed;
|
||||
if (!currentPoint || !yScale) return;
|
||||
|
||||
@@ -311,8 +321,14 @@
|
||||
const { left, right, top, bottom } = chart.chartArea;
|
||||
if (y < top || y > bottom) return;
|
||||
|
||||
const remaining = numberOrNull(quota?.remaining);
|
||||
const dailyUsage = getAverageDailyUsage(points);
|
||||
const estimatedDays = remaining !== null && dailyUsage > 0 ? Math.ceil(remaining / dailyUsage) : null;
|
||||
const text = remaining === null
|
||||
? `当前 ${currentPoint.used.toFixed(6)} CNY`
|
||||
: `当前 ${currentPoint.used.toFixed(6)} CNY / 剩余 ${remaining.toFixed(6)} CNY${estimatedDays === null ? '' : ` [预计 ${estimatedDays} 天使用完毕]`}`;
|
||||
|
||||
const context = chart.ctx;
|
||||
const text = `当前 ${currentPoint.used.toFixed(5)} CNY`;
|
||||
context.save();
|
||||
context.setLineDash([7, 5]);
|
||||
context.strokeStyle = '#facc15';
|
||||
@@ -462,6 +478,7 @@
|
||||
const used = numberOrNull(quota && quota.used);
|
||||
if (!assignmentId || used === null) return false;
|
||||
const point = [Date.now(), used];
|
||||
currentQuotaByAssignment[assignmentId] = { remaining: numberOrNull(quota.remaining) };
|
||||
historyByAssignment[assignmentId] = historyByAssignment[assignmentId] || { recent: [], archived: [] };
|
||||
historyByAssignment[assignmentId].recent.push(point);
|
||||
historyByAssignment[assignmentId] = compactPoints([
|
||||
|
||||
Reference in New Issue
Block a user