mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-27 01:19:25 +08:00
perf(panel): instant consumption time-range switching via pruned scope API and client preset cache
This commit is contained in:
@@ -44,7 +44,7 @@ import {
|
||||
syncCloudCatalog,
|
||||
} from './plugin-updater.mjs';
|
||||
import { getCloudStatus } from './pricing-catalog.mjs';
|
||||
import { buildTelemetryPayload } from './telemetry-builder.mjs';
|
||||
import { buildTelemetryPayload, clearTelemetryBuilderMemo } from './telemetry-builder.mjs';
|
||||
import {
|
||||
ensureDesktopLaunchEntriesConfigured,
|
||||
triggerDesktopHostRelaunch,
|
||||
@@ -192,10 +192,14 @@ export async function executeTelemetryAction(data, { onRestart = null } = {}) {
|
||||
}
|
||||
}
|
||||
}
|
||||
const rawScope = String(data?.scope || '').trim();
|
||||
const scope =
|
||||
rawScope === 'consumption' || rawScope === 'consumption-presets' ? rawScope : '';
|
||||
const filterOpts = {
|
||||
range: data?.range || 'all',
|
||||
startMs: sanitizePositiveFiniteMs(data?.startMs),
|
||||
endMs: sanitizePositiveFiniteMs(data?.endMs),
|
||||
...(scope ? { scope } : {}),
|
||||
};
|
||||
let payload;
|
||||
try {
|
||||
@@ -216,7 +220,7 @@ export async function executeTelemetryAction(data, { onRestart = null } = {}) {
|
||||
}
|
||||
if (
|
||||
bootBundleVersion &&
|
||||
payload.uiBundleVersion &&
|
||||
payload?.uiBundleVersion &&
|
||||
String(payload.uiBundleVersion) !== String(bootBundleVersion)
|
||||
) {
|
||||
if (typeof onRestart === 'function') {
|
||||
@@ -225,9 +229,12 @@ export async function executeTelemetryAction(data, { onRestart = null } = {}) {
|
||||
scheduleSidecarRestart(150);
|
||||
}
|
||||
}
|
||||
const responsePayload = data?.stepDetails === 'display'
|
||||
? projectDisplayTelemetry(payload, data.stepRevision)
|
||||
: payload;
|
||||
const isScopedConsumptionPayload =
|
||||
payload?.scope === 'consumption' || payload?.scope === 'consumption-presets';
|
||||
const responsePayload =
|
||||
!isScopedConsumptionPayload && data?.stepDetails === 'display'
|
||||
? projectDisplayTelemetry(payload, data.stepRevision)
|
||||
: payload;
|
||||
return {
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
@@ -295,11 +302,20 @@ export function executeSettingsAction(data) {
|
||||
body: JSON.stringify({ ok: false, error: 'Failed to persist settings' }),
|
||||
};
|
||||
}
|
||||
inFlightRequests.clear();
|
||||
const prevMountMode = normalizeWindowMountMode(
|
||||
current.windowMountMode,
|
||||
current.enableWindowOverlay
|
||||
);
|
||||
const affectsBackendTelemetry =
|
||||
updated.weeklyLimitMode !== current.weeklyLimitMode ||
|
||||
updated.weeklyTokenBudget !== current.weeklyTokenBudget ||
|
||||
updated.weeklyCostBudget !== current.weeklyCostBudget ||
|
||||
updated.enableWindowOverlay !== (current.enableWindowOverlay !== false) ||
|
||||
updated.windowMountMode !== prevMountMode;
|
||||
inFlightRequests.clear();
|
||||
if (affectsBackendTelemetry) {
|
||||
clearTelemetryBuilderMemo();
|
||||
}
|
||||
if (
|
||||
updated.enableWindowOverlay !== (current.enableWindowOverlay !== false) ||
|
||||
updated.windowMountMode !== prevMountMode
|
||||
@@ -364,6 +380,7 @@ export async function executeModelContextWindowAction(data) {
|
||||
return respond(500, { error: 'Failed to persist model context window' });
|
||||
}
|
||||
inFlightRequests.clear();
|
||||
clearTelemetryBuilderMemo();
|
||||
void notifyModelContextWindowsChanged();
|
||||
return respond(200, { modelContextWindows: current.modelContextWindows });
|
||||
}
|
||||
@@ -381,6 +398,7 @@ export function executeResetSettingsAction() {
|
||||
};
|
||||
}
|
||||
inFlightRequests.clear();
|
||||
clearTelemetryBuilderMemo();
|
||||
const prevMountMode = normalizeWindowMountMode(
|
||||
current.windowMountMode,
|
||||
current.enableWindowOverlay
|
||||
@@ -429,6 +447,7 @@ export function executeRebuildCacheAction() {
|
||||
}
|
||||
displayStepsCache.clear();
|
||||
clearScannerRuntimeMemos();
|
||||
clearTelemetryBuilderMemo();
|
||||
return {
|
||||
ok: true,
|
||||
status: 200,
|
||||
@@ -440,6 +459,7 @@ export function executeRebuildCacheAction() {
|
||||
|
||||
export async function executeCloudSyncAction() {
|
||||
const cloudStatus = await syncCloudCatalog({ force: true });
|
||||
clearTelemetryBuilderMemo();
|
||||
return {
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
@@ -460,6 +480,9 @@ export async function executePluginUpdateAction() {
|
||||
|
||||
export async function executeAccountCredentialAction(data) {
|
||||
const result = await handleAccountCredentialAction(data);
|
||||
if (data?.action === 'switch') {
|
||||
clearTelemetryBuilderMemo();
|
||||
}
|
||||
return {
|
||||
status: 200,
|
||||
contentType: 'application/json',
|
||||
|
||||
@@ -46,8 +46,11 @@ import { getCloudStatus, getPricingPhaseEpoch, pricingCatalogRevision } from './
|
||||
import { buildOfficialQuotaPools } from './quota-pools.mjs';
|
||||
import { getDefaultWindowBridgeStatus } from './window-bridge.mjs';
|
||||
import {
|
||||
aggregateConsumptionPresetBundle,
|
||||
aggregateMultiWindowUsage,
|
||||
aggregateSelectedWindowUsage,
|
||||
buildScopedModelBreakdown,
|
||||
clearUsageAggregatorMemo,
|
||||
getNextMondayResetIso,
|
||||
} from './usage-aggregator.mjs';
|
||||
|
||||
@@ -56,6 +59,13 @@ const POOL_PRIORITY_ORDER = ['Gemini', 'Argon', 'Carbon', 'Claude & GPT'];
|
||||
const inFlightTelemetrySources = new Map();
|
||||
const trajectoryModelFacts = new WeakMap();
|
||||
let lastMainPreparation = null;
|
||||
let lastCompletedTelemetrySnapshot = null;
|
||||
|
||||
export function clearTelemetryBuilderMemo() {
|
||||
lastMainPreparation = null;
|
||||
lastCompletedTelemetrySnapshot = null;
|
||||
clearUsageAggregatorMemo();
|
||||
}
|
||||
|
||||
function roundSessionCost(val) {
|
||||
if (!Number.isFinite(val) || val <= 0) return 0;
|
||||
@@ -1237,6 +1247,7 @@ export async function buildTelemetryPayloadInternal(
|
||||
|
||||
// 5. 多时间窗口聚合(预设窗口 + 当前选定窗口 + 趋势 + 月序列 + 来源与模型拆解 + 各配额池独立周期统计)
|
||||
const {
|
||||
usageRevision,
|
||||
selectedWindow,
|
||||
trendStartMs,
|
||||
trendEndMs,
|
||||
@@ -1816,6 +1827,7 @@ export async function buildTelemetryPayloadInternal(
|
||||
aggregated: aggregatedTelemetry,
|
||||
},
|
||||
consumption: {
|
||||
revision: usageRevision,
|
||||
selectedRange: {
|
||||
key: selectedWindow.key,
|
||||
label: selectedWindow.label,
|
||||
@@ -1883,6 +1895,24 @@ export async function buildTelemetryPayloadInternal(
|
||||
modelBreakdown,
|
||||
};
|
||||
|
||||
lastCompletedTelemetrySnapshot = {
|
||||
lsAddr: String(lsRuntimeEndpoint.addr || ''),
|
||||
weeklyLimitMode: settings.weeklyLimitMode,
|
||||
catalogRevision: modelInputs.catalogRevision,
|
||||
phaseEpoch: modelInputs.phaseEpoch,
|
||||
basePayload: finalPayload,
|
||||
aggContext: {
|
||||
cycleAnchors,
|
||||
isCustomWeeklyMode,
|
||||
currentAccountName: accountScope,
|
||||
activeCascadeId,
|
||||
activeFamilyCascadeIds,
|
||||
resolveStepModel,
|
||||
quotaPoolsMap,
|
||||
modelInputs,
|
||||
},
|
||||
};
|
||||
|
||||
if (lsRuntimeEndpoint.addr !== 'test.invalid') {
|
||||
try {
|
||||
saveJson(CLI_STATUS_SNAPSHOT_FILE, buildCliStatusSnapshot(finalPayload, Date.now()));
|
||||
@@ -1894,6 +1924,65 @@ export async function buildTelemetryPayloadInternal(
|
||||
return finalPayload;
|
||||
}
|
||||
|
||||
function resolveValidFastSnapshot() {
|
||||
const snap = lastCompletedTelemetrySnapshot;
|
||||
if (!snap || !snap.basePayload || !snap.aggContext) return null;
|
||||
if (snap.lsAddr !== String(lsRuntimeEndpoint.addr || '')) return null;
|
||||
if (
|
||||
snap.catalogRevision !== pricingCatalogRevision ||
|
||||
snap.phaseEpoch !== getPricingPhaseEpoch()
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const latestSettings = loadSettings();
|
||||
if (latestSettings.weeklyLimitMode !== snap.weeklyLimitMode) {
|
||||
return null;
|
||||
}
|
||||
return snap;
|
||||
}
|
||||
|
||||
function tryBuildFastConsumptionPayload(filterOpts = {}) {
|
||||
const snap = resolveValidFastSnapshot();
|
||||
if (!snap) return null;
|
||||
const agg = aggregateSelectedWindowUsage({
|
||||
filterOpts,
|
||||
...snap.aggContext,
|
||||
});
|
||||
return {
|
||||
scope: 'consumption',
|
||||
timestamp: new Date().toISOString(),
|
||||
uiBundleVersion: snap.basePayload.uiBundleVersion || computeRuntimeCodeVersion(),
|
||||
usageRevision: agg.usageRevision,
|
||||
consumption: {
|
||||
revision: agg.usageRevision,
|
||||
selectedRange: {
|
||||
key: agg.selectedWindow.key,
|
||||
label: agg.selectedWindow.label,
|
||||
startMs: agg.trendStartMs,
|
||||
endMs: agg.trendEndMs,
|
||||
},
|
||||
session: agg.finalizedSelectedSession,
|
||||
account: agg.finalizedSelectedAccount,
|
||||
trendSeries: agg.trendSeries,
|
||||
sourcesBreakdown: agg.sourcesBreakdown,
|
||||
},
|
||||
modelBreakdown: agg.modelBreakdown,
|
||||
};
|
||||
}
|
||||
|
||||
function tryBuildConsumptionPresetsPayload() {
|
||||
const snap = resolveValidFastSnapshot();
|
||||
if (!snap) return null;
|
||||
const bundle = aggregateConsumptionPresetBundle(snap.aggContext);
|
||||
return {
|
||||
scope: 'consumption-presets',
|
||||
timestamp: new Date().toISOString(),
|
||||
uiBundleVersion: snap.basePayload.uiBundleVersion || computeRuntimeCodeVersion(),
|
||||
usageRevision: bundle.usageRevision,
|
||||
ranges: bundle.ranges,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildCliStatusSnapshot(finalPayload, updatedAtMs = Date.now()) {
|
||||
const ac = finalPayload?.activeConversation || {};
|
||||
const capSource = ac.aggregated || ac;
|
||||
@@ -1938,7 +2027,7 @@ export function buildCliStatusSnapshot(finalPayload, updatedAtMs = Date.now()) {
|
||||
*
|
||||
* @param {string} requestedConvId
|
||||
* @param {boolean} [forceQuotaRefresh=false]
|
||||
* @param {{ range?: string, startMs?: number, endMs?: number }} [filterOpts={}]
|
||||
* @param {{ range?: string, startMs?: number, endMs?: number, scope?: string }} [filterOpts={}]
|
||||
* @returns {Promise<object>}
|
||||
*/
|
||||
export function buildTelemetryPayload(
|
||||
@@ -1946,6 +2035,31 @@ export function buildTelemetryPayload(
|
||||
forceQuotaRefresh = false,
|
||||
filterOpts = {}
|
||||
) {
|
||||
if (!forceQuotaRefresh) {
|
||||
if (filterOpts?.scope === 'consumption-presets') {
|
||||
const presetsPayload = tryBuildConsumptionPresetsPayload();
|
||||
if (presetsPayload) {
|
||||
return Promise.resolve(presetsPayload);
|
||||
}
|
||||
const key = `${requestedConvId || '__auto__'}::presets-cold`;
|
||||
if (inFlightRequests.has(key)) {
|
||||
return inFlightRequests.get(key);
|
||||
}
|
||||
const p = buildTelemetryPayloadInternal(requestedConvId, false, { range: 'today' })
|
||||
.then(() => tryBuildConsumptionPresetsPayload())
|
||||
.finally(() => {
|
||||
inFlightRequests.delete(key);
|
||||
});
|
||||
inFlightRequests.set(key, p);
|
||||
return p;
|
||||
}
|
||||
if (filterOpts?.scope === 'consumption') {
|
||||
const litePayload = tryBuildFastConsumptionPayload(filterOpts);
|
||||
if (litePayload) {
|
||||
return Promise.resolve(litePayload);
|
||||
}
|
||||
}
|
||||
}
|
||||
const rangeSig = `${filterOpts.range || 'all'}:${filterOpts.startMs || 0}:${filterOpts.endMs || 0}`;
|
||||
const key = `${requestedConvId || '__auto__'}::${forceQuotaRefresh ? 'force' : 'normal'}::${rangeSig}`;
|
||||
if (inFlightRequests.has(key)) {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user