Skip to content

Commit 951f7eb

Browse files
committed
Show last month in details and last 30 days in chart
1 parent 66848b6 commit 951f7eb

2 files changed

Lines changed: 36 additions & 38 deletions

File tree

src/extension.ts

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,8 @@ class CopilotTokenTracker implements vscode.Disposable {
813813

814814
private async calculateDailyStats(): Promise<DailyTokenStats[]> {
815815
const now = new Date();
816-
const monthStart = new Date(now.getFullYear(), now.getMonth(), 1);
816+
// Use last 30 days instead of current month for better chart visibility
817+
const thirtyDaysAgo = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 30);
817818

818819
// Map to store daily stats by date string (YYYY-MM-DD)
819820
const dailyStatsMap = new Map<string, DailyTokenStats>();
@@ -826,8 +827,8 @@ class CopilotTokenTracker implements vscode.Disposable {
826827
try {
827828
const fileStats = fs.statSync(sessionFile);
828829

829-
// Only process files modified in the current month
830-
if (fileStats.mtime >= monthStart) {
830+
// Only process files modified in the last 30 days
831+
if (fileStats.mtime >= thirtyDaysAgo) {
831832
const tokens = await this.estimateTokensFromSessionCached(sessionFile, fileStats.mtime.getTime());
832833
const interactions = await this.countInteractionsInSessionCached(sessionFile, fileStats.mtime.getTime());
833834
const modelUsage = await this.getModelUsageFromSessionCached(sessionFile, fileStats.mtime.getTime());
@@ -880,42 +881,39 @@ class CopilotTokenTracker implements vscode.Disposable {
880881
// Convert map to array and sort by date
881882
let dailyStatsArray = Array.from(dailyStatsMap.values()).sort((a, b) => a.date.localeCompare(b.date));
882883

883-
// Fill in missing dates between the first date and today
884-
if (dailyStatsArray.length > 0) {
885-
const firstDate = new Date(dailyStatsArray[0].date);
886-
const today = new Date();
887-
888-
// Create a set of existing dates for quick lookup
889-
const existingDates = new Set(dailyStatsArray.map(s => s.date));
890-
891-
// Generate all dates from first date to today
892-
const allDates: string[] = [];
893-
const currentDate = new Date(firstDate);
894-
895-
while (currentDate <= today) {
896-
const dateKey = this.formatDateKey(currentDate);
897-
allDates.push(dateKey);
898-
currentDate.setDate(currentDate.getDate() + 1);
899-
}
900-
901-
// Add missing dates with zero values
902-
for (const dateKey of allDates) {
903-
if (!existingDates.has(dateKey)) {
904-
dailyStatsMap.set(dateKey, {
905-
date: dateKey,
906-
tokens: 0,
907-
sessions: 0,
908-
interactions: 0,
909-
modelUsage: {},
910-
editorUsage: {}
911-
});
912-
}
884+
// Always fill in all 30 days to show complete chart
885+
const today = new Date();
886+
887+
// Create a set of existing dates for quick lookup
888+
const existingDates = new Set(dailyStatsArray.map(s => s.date));
889+
890+
// Generate all dates from 30 days ago to today
891+
const allDates: string[] = [];
892+
const currentDate = new Date(thirtyDaysAgo);
893+
894+
while (currentDate <= today) {
895+
const dateKey = this.formatDateKey(currentDate);
896+
allDates.push(dateKey);
897+
currentDate.setDate(currentDate.getDate() + 1);
898+
}
899+
900+
// Add missing dates with zero values
901+
for (const dateKey of allDates) {
902+
if (!existingDates.has(dateKey)) {
903+
dailyStatsMap.set(dateKey, {
904+
date: dateKey,
905+
tokens: 0,
906+
sessions: 0,
907+
interactions: 0,
908+
modelUsage: {},
909+
editorUsage: {}
910+
});
913911
}
914-
915-
// Re-convert map to array and sort by date
916-
dailyStatsArray = Array.from(dailyStatsMap.values()).sort((a, b) => a.date.localeCompare(b.date));
917912
}
918913

914+
// Re-convert map to array and sort by date
915+
dailyStatsArray = Array.from(dailyStatsMap.values()).sort((a, b) => a.date.localeCompare(b.date));
916+
919917
return dailyStatsArray;
920918
}
921919

src/webview/chart/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function renderLayout(data: InitialChartData): void {
9292
const header = el('div', 'header');
9393
const headerLeft = el('div', 'header-left');
9494
const icon = el('span', 'header-icon', '📈');
95-
const title = el('span', 'header-title', 'Token Usage Over Time');
95+
const title = el('span', 'header-title', 'Token Usage - Last 30 Days');
9696
headerLeft.append(icon, title);
9797
const buttons = el('div', 'button-row');
9898
buttons.append(
@@ -140,7 +140,7 @@ function renderLayout(data: InitialChartData): void {
140140
chartShell.append(toggles, canvasWrap);
141141
chartSection.append(chartShell);
142142

143-
const footer = el('div', 'footer', `Day-by-day token usage for the current month\nLast updated: ${new Date(data.lastUpdated).toLocaleString()}\nUpdates automatically every 5 minutes.`);
143+
const footer = el('div', 'footer', `Day-by-day token usage for the last 30 days\nLast updated: ${new Date(data.lastUpdated).toLocaleString()}\nUpdates automatically every 5 minutes.`);
144144

145145
container.append(header, summarySection, chartSection, footer);
146146
root.append(style, container);

0 commit comments

Comments
 (0)