mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-26 17:09:28 +08:00
feat(panel): upgrade modular dashboard UI, 6-segment context meter, and consumption analytics
This commit is contained in:
@@ -0,0 +1,244 @@
|
||||
/**
|
||||
* @fileoverview Reactive telemetry store with async non-overlapping poll scheduler
|
||||
* and granular slice updates.
|
||||
*/
|
||||
|
||||
import { createReactiveStore } from '../core/reactive.js';
|
||||
import { callSidecarApi } from '../core/httpClient.js';
|
||||
|
||||
const initialConversationId =
|
||||
(typeof window !== 'undefined' && window.sidecar?.conversationId) || '';
|
||||
|
||||
export const telemetryStore = createReactiveStore({
|
||||
selectedConversationId: initialConversationId,
|
||||
selectedTimeRange: 'today',
|
||||
customStartMs: Date.now() - 24 * 3600 * 1000,
|
||||
customEndMs: Date.now(),
|
||||
syncState: 'idle', // 'idle' | 'syncing' | 'synced' | 'error'
|
||||
telemetry: null,
|
||||
});
|
||||
|
||||
let pollTimer = null;
|
||||
let feedbackTimer = null;
|
||||
let activeBundleVersion = 0;
|
||||
let latestRequestSeq = 0;
|
||||
|
||||
function isEquivalentTelemetry(prev, next) {
|
||||
if (!prev || !next) return false;
|
||||
if (prev === next) return true;
|
||||
|
||||
const pAct = prev.activeConversation;
|
||||
const nAct = next.activeConversation;
|
||||
if (pAct?.cascadeId !== nAct?.cascadeId) return false;
|
||||
if (pAct?.stepCount !== nAct?.stepCount) return false;
|
||||
if (pAct?.isRunning !== nAct?.isRunning) return false;
|
||||
if (pAct?.latestTokPerSec !== nAct?.latestTokPerSec) return false;
|
||||
if (pAct?.latestTtftSec !== nAct?.latestTtftSec) return false;
|
||||
const pCtx = pAct?.context;
|
||||
const nCtx = nAct?.context;
|
||||
if (pCtx?.totalTokens !== nCtx?.totalTokens) return false;
|
||||
if (pCtx?.messagesTokens !== nCtx?.messagesTokens) return false;
|
||||
if (pCtx?.systemToolsTokens !== nCtx?.systemToolsTokens) return false;
|
||||
if (pCtx?.mcpToolsTokens !== nCtx?.mcpToolsTokens) return false;
|
||||
if (pCtx?.skillsTokens !== nCtx?.skillsTokens) return false;
|
||||
if (pCtx?.systemPromptTokens !== nCtx?.systemPromptTokens) return false;
|
||||
if (pCtx?.userRulesTokens !== nCtx?.userRulesTokens) return false;
|
||||
if ((pAct?.recentSteps?.length || 0) !== (nAct?.recentSteps?.length || 0)) return false;
|
||||
|
||||
const pRange = prev.consumption?.selectedRange;
|
||||
const nRange = next.consumption?.selectedRange;
|
||||
if (pRange?.key !== nRange?.key) return false;
|
||||
if (nRange?.key === 'custom') {
|
||||
if (pRange?.startMs !== nRange?.startMs || pRange?.endMs !== nRange?.endMs) return false;
|
||||
}
|
||||
|
||||
const pCons = prev.consumption?.session;
|
||||
const nCons = next.consumption?.session;
|
||||
if (pCons?.totalTokens !== nCons?.totalTokens) return false;
|
||||
if (pCons?.costUsd !== nCons?.costUsd) return false;
|
||||
|
||||
const pAcct = prev.consumption?.account;
|
||||
const nAcct = next.consumption?.account;
|
||||
if (pAcct?.totalTokens !== nAcct?.totalTokens) return false;
|
||||
if (pAcct?.calls !== nAcct?.calls) return false;
|
||||
if (pAcct?.costUsd !== nAcct?.costUsd) return false;
|
||||
|
||||
const pTrend = prev.consumption?.trendSeries || [];
|
||||
const nTrend = next.consumption?.trendSeries || [];
|
||||
if (pTrend.length !== nTrend.length) return false;
|
||||
for (let i = 0; i < pTrend.length; i++) {
|
||||
if (pTrend[i]?.tokens !== nTrend[i]?.tokens || pTrend[i]?.label !== nTrend[i]?.label) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const pModels = prev.modelBreakdown || [];
|
||||
const nModels = next.modelBreakdown || [];
|
||||
if (pModels.length !== nModels.length) return false;
|
||||
for (let i = 0; i < pModels.length; i++) {
|
||||
if (
|
||||
pModels[i]?.label !== nModels[i]?.label ||
|
||||
pModels[i]?.totalTokens !== nModels[i]?.totalTokens ||
|
||||
pModels[i]?.calls !== nModels[i]?.calls
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const pSources = prev.consumption?.sourcesBreakdown || [];
|
||||
const nSources = next.consumption?.sourcesBreakdown || [];
|
||||
if (pSources.length !== nSources.length) return false;
|
||||
for (let i = 0; i < pSources.length; i++) {
|
||||
if (
|
||||
pSources[i]?.name !== nSources[i]?.name ||
|
||||
pSources[i]?.totalTokens !== nSources[i]?.totalTokens ||
|
||||
pSources[i]?.calls !== nSources[i]?.calls
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const pQuot = prev.quotas?.activePool;
|
||||
const nQuot = next.quotas?.activePool;
|
||||
if (pQuot?.usedPct !== nQuot?.usedPct) return false;
|
||||
if (pQuot?.remainingPct !== nQuot?.remainingPct) return false;
|
||||
if (prev.quotas?.weekly?.usedPct !== next.quotas?.weekly?.usedPct) return false;
|
||||
|
||||
if ((prev.conversations?.length || 0) !== (next.conversations?.length || 0)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function fetchTelemetry({ force = false } = {}) {
|
||||
const st = telemetryStore.getState();
|
||||
const reqSeq = ++latestRequestSeq;
|
||||
const startMs = Date.now();
|
||||
|
||||
if (force) {
|
||||
telemetryStore.setState({ syncState: 'syncing' });
|
||||
}
|
||||
|
||||
try {
|
||||
const params = new URLSearchParams();
|
||||
if (st.selectedConversationId) {
|
||||
params.set('conversationId', st.selectedConversationId);
|
||||
}
|
||||
if (st.selectedTimeRange) {
|
||||
params.set('range', st.selectedTimeRange);
|
||||
}
|
||||
if (st.selectedTimeRange === 'custom') {
|
||||
if (st.customStartMs > 0) params.set('startMs', String(st.customStartMs));
|
||||
if (st.customEndMs > 0) params.set('endMs', String(st.customEndMs));
|
||||
}
|
||||
if (force) {
|
||||
params.set('force', '1');
|
||||
}
|
||||
const qs = params.toString();
|
||||
const payload = await callSidecarApi(`/api/telemetry${qs ? `?${qs}` : ''}`, {
|
||||
method: 'GET',
|
||||
abortKey: 'telemetry-poll',
|
||||
});
|
||||
|
||||
if (reqSeq !== latestRequestSeq) {
|
||||
return payload;
|
||||
}
|
||||
|
||||
if (payload?.uiBundleVersion) {
|
||||
if (activeBundleVersion && activeBundleVersion !== payload.uiBundleVersion) {
|
||||
if (typeof window !== 'undefined' && typeof window.location?.reload === 'function') {
|
||||
window.location.reload();
|
||||
return payload;
|
||||
}
|
||||
}
|
||||
activeBundleVersion = payload.uiBundleVersion;
|
||||
}
|
||||
|
||||
const latestSt = telemetryStore.getState();
|
||||
if (force) {
|
||||
const elapsed = Date.now() - startMs;
|
||||
if (elapsed < 220) {
|
||||
await new Promise((r) => setTimeout(r, 220 - elapsed));
|
||||
}
|
||||
if (reqSeq !== latestRequestSeq) {
|
||||
return payload;
|
||||
}
|
||||
if (feedbackTimer) clearTimeout(feedbackTimer);
|
||||
telemetryStore.setState({
|
||||
telemetry: payload,
|
||||
syncState: 'synced',
|
||||
});
|
||||
feedbackTimer = setTimeout(() => {
|
||||
feedbackTimer = null;
|
||||
telemetryStore.setState({ syncState: 'idle' });
|
||||
}, 1100);
|
||||
} else {
|
||||
const isSame = isEquivalentTelemetry(latestSt.telemetry, payload);
|
||||
const nextTelemetry = isSame ? latestSt.telemetry : payload;
|
||||
telemetryStore.setState({
|
||||
telemetry: nextTelemetry,
|
||||
syncState: latestSt.syncState === 'synced' ? 'synced' : 'idle',
|
||||
});
|
||||
}
|
||||
return payload;
|
||||
} catch (err) {
|
||||
if (err?.name === 'AbortError' || reqSeq !== latestRequestSeq) return null;
|
||||
telemetryStore.setState({ syncState: 'error' });
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveSettingsPatch(patch) {
|
||||
const prevTelemetry = telemetryStore.getState().telemetry;
|
||||
if (prevTelemetry?.settings) {
|
||||
telemetryStore.setState({
|
||||
telemetry: {
|
||||
...prevTelemetry,
|
||||
settings: { ...prevTelemetry.settings, ...patch },
|
||||
activeConversation: patch.contextWindowLimit
|
||||
? {
|
||||
...prevTelemetry.activeConversation,
|
||||
context: {
|
||||
...prevTelemetry.activeConversation?.context,
|
||||
limitTokens: patch.contextWindowLimit,
|
||||
usedPct: Math.min(
|
||||
100,
|
||||
Math.round(
|
||||
((prevTelemetry.activeConversation?.context?.totalTokens || 0) /
|
||||
Math.max(1, patch.contextWindowLimit)) *
|
||||
100
|
||||
)
|
||||
),
|
||||
},
|
||||
}
|
||||
: prevTelemetry.activeConversation,
|
||||
},
|
||||
});
|
||||
}
|
||||
await callSidecarApi('/api/settings', { method: 'POST', body: patch });
|
||||
await fetchTelemetry({ force: true });
|
||||
}
|
||||
|
||||
export function startAsyncTelemetryLoop(intervalMs = 2000) {
|
||||
stopAsyncTelemetryLoop();
|
||||
let active = true;
|
||||
|
||||
const tick = async () => {
|
||||
if (!active) return;
|
||||
await fetchTelemetry({ force: false });
|
||||
if (!active) return;
|
||||
pollTimer = setTimeout(tick, intervalMs);
|
||||
};
|
||||
|
||||
tick();
|
||||
return () => {
|
||||
active = false;
|
||||
stopAsyncTelemetryLoop();
|
||||
};
|
||||
}
|
||||
|
||||
export function stopAsyncTelemetryLoop() {
|
||||
if (pollTimer) {
|
||||
clearTimeout(pollTimer);
|
||||
pollTimer = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* @fileoverview UI navigation and view state store.
|
||||
*/
|
||||
|
||||
import { createReactiveStore } from '../core/reactive.js';
|
||||
|
||||
export const VIEW_TABS = Object.freeze([
|
||||
{ value: 'overview', label: '实时监控', icon: 'pulse' },
|
||||
{ value: 'consumption', label: '消耗统计', icon: 'chart-bar' },
|
||||
{ value: 'steps', label: '步骤明细', icon: 'table' },
|
||||
{ value: 'settings', label: '系统设置', icon: 'sliders' },
|
||||
]);
|
||||
|
||||
export const uiStore = createReactiveStore({
|
||||
activeView: 'overview',
|
||||
stepsSubTab: 'steps', // 'steps' | 'models'
|
||||
showAllPools: true,
|
||||
ctxBreakdownOpen: true,
|
||||
});
|
||||
Reference in New Issue
Block a user