Repository navigation
Expand file tree
/
Copy pathusage.js
More file actions
307 lines (259 loc) · 10.7 KB
/
Copy pathusage.js
File metadata and controls
307 lines (259 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
class CursorUsageStats {
constructor() {
this.yearSelect = document.getElementById('yearSelect');
this.monthSelect = document.getElementById('monthSelect');
this.loading = document.getElementById('loading');
this.error = document.getElementById('error');
this.summary = document.getElementById('summary');
this.details = document.getElementById('details');
this.summaryBody = document.getElementById('summaryBody');
this.detailsBody = document.getElementById('detailsBody');
this.rawEvents = [];
this.processedEvents = [];
this.init();
}
init() {
// Set current month as default
const now = new Date();
this.monthSelect.value = now.getMonth().toString();
// Event listeners
this.yearSelect.addEventListener('change', () => this.fetchData());
this.monthSelect.addEventListener('change', () => this.fetchData());
// Initial load
this.fetchData();
}
async fetchData() {
const year = parseInt(this.yearSelect.value);
const month = parseInt(this.monthSelect.value);
this.showLoading(true);
this.hideError();
try {
// Try cursor.com first (covers cn and other subdomains), then fallback to www.cursor.com
const endpoints = [
'https://cursor.com/api/dashboard/get-monthly-invoice',
'https://www.cursor.com/api/dashboard/get-monthly-invoice'
];
let response;
let lastError;
for (const endpoint of endpoints) {
try {
response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
month: month,
year: year,
includeUsageEvents: true
})
});
if (response.ok) {
break; // Success, exit the loop
}
} catch (err) {
lastError = err;
continue; // Try next endpoint
}
}
if (!response || !response.ok) {
throw new Error(`HTTP error! status: ${response?.status || 'Network error'}`);
}
const data = await response.json();
if (!data.usageEvents || !Array.isArray(data.usageEvents)) {
throw new Error('No usage events found in response');
}
this.rawEvents = data.usageEvents;
this.processEvents();
this.renderUI();
} catch (err) {
this.showError(`Failed to fetch data: ${err.message}`);
} finally {
this.showLoading(false);
}
}
processEvents() {
// Parse and transform events
const parsed = this.rawEvents.map(event => this.parseEvent(event));
// Handle fastApply aggregation
const aggregated = this.aggregateToolCalls(parsed);
// Filter out fastApply events
this.processedEvents = aggregated.filter(event => event.type !== 'fastApply');
}
// Safe JSON stringify to avoid CSP violations
safeStringify(obj) {
try {
// Create a sanitized version without functions or dangerous content
const sanitized = JSON.parse(JSON.stringify(obj, (key, value) => {
if (typeof value === 'function') return '[Function]';
if (typeof value === 'symbol') return '[Symbol]';
if (typeof value === 'undefined') return '[Undefined]';
return value;
}));
return JSON.stringify(sanitized);
} catch (error) {
console.error('Safe stringify failed:', error);
return '[Object]';
}
}
parseEvent(event) {
const details = event.details || {};
// Format time - using timestamp field
const eventTime = new Date(parseInt(event.timestamp)).toLocaleString('sv-SE', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}).replace('T', ' ');
// Determine type from details object keys
const validTypes = ['toolCallComposer', 'composer', 'fastApply', 'chat', 'cmdK'];
let type = 'other';
let typeOther = '';
let nestedDetails = {};
// Find the type from the details object keys
for (const validType of validTypes) {
if (details[validType]) {
type = validType;
nestedDetails = details[validType];
break;
}
}
if (type === 'other') {
typeOther = this.safeStringify(details);
}
// Calculate request count - prioritize priceCents calculation per guide
let requestCount = 0;
if (event.priceCents !== undefined) {
requestCount = Math.round((event.priceCents / 4) * 10) / 10;
} else if (nestedDetails.overrideNumRequestsCounted !== undefined) {
requestCount = nestedDetails.overrideNumRequestsCounted;
} else if (details.overrideNumRequestsCounted !== undefined) {
requestCount = details.overrideNumRequestsCounted;
}
return {
event_time: eventTime,
type: type,
model: nestedDetails.modelIntent || nestedDetails.model || '',
type_other: typeOther,
maxMode: nestedDetails.maxMode || false,
isTokenBasedCall: nestedDetails.isTokenBasedCall || false,
requestCount: requestCount,
subscriptionProductId: event.subscriptionProductId || event.usagePriceId || '',
isSlow: event.isSlow || false,
status: event.status || '',
toolcall: 0, // Will be set during aggregation
originalEvent: event
};
}
aggregateToolCalls(events) {
let toolCallCounter = 0;
for (let i = 0; i < events.length; i++) {
const event = events[i];
if (event.type === 'fastApply') {
toolCallCounter++;
} else {
event.toolcall = toolCallCounter;
toolCallCounter = 0;
}
}
return events;
}
calculateSummary() {
const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const thisMonth = new Date(now.getFullYear(), now.getMonth(), 1);
const last4Hours = new Date(now.getTime() - 4 * 60 * 60 * 1000);
const last24Hours = new Date(now.getTime() - 24 * 60 * 60 * 1000);
const last48Hours = new Date(now.getTime() - 48 * 60 * 60 * 1000);
const periods = [
{ name: 'Today', start: today },
{ name: 'This Month', start: thisMonth },
{ name: 'Last 4 Hours', start: last4Hours },
{ name: 'Last 24 Hours', start: last24Hours },
{ name: 'Last 48 Hours', start: last48Hours }
];
return periods.map(period => {
const filteredEvents = this.processedEvents.filter(event => {
const eventDate = new Date(parseInt(event.originalEvent.timestamp));
return eventDate >= period.start;
});
return {
period: period.name,
recordCount: filteredEvents.length,
requestTotal: filteredEvents.reduce((sum, event) => sum + (event.requestCount || 0), 0)
};
});
}
renderUI() {
this.renderSummary();
this.renderDetails();
this.summary.style.display = 'block';
this.details.style.display = 'block';
}
renderSummary() {
const summaryData = this.calculateSummary();
// Create rows for Record Count and Request Total
const recordCountRow = `
<tr>
<td><strong>Record Count</strong></td>
${summaryData.map(item => `<td>${item.recordCount}</td>`).join('')}
</tr>
`;
const requestTotalRow = `
<tr>
<td><strong>Request Total</strong></td>
${summaryData.map(item => `<td>${item.requestTotal.toFixed(1)}</td>`).join('')}
</tr>
`;
this.summaryBody.innerHTML = recordCountRow + requestTotalRow;
}
renderDetails() {
this.detailsBody.innerHTML = this.processedEvents.map((event, index) => {
let rowClass = '';
if (event.maxMode) rowClass += ' max-mode';
if (event.status === 'errored') rowClass += ' error-status';
const slowText = event.isSlow ? `<span class="slow-request">Yes</span>` : 'No';
const maxText = event.maxMode ? 'Yes' : 'No';
const tokenText = event.isTokenBasedCall ? 'Yes' : 'No';
const toolcallText = event.toolcall > 0 ? event.toolcall : '';
return `
<tr class="${rowClass}">
<td>${index + 1}</td>
<td>${event.event_time}</td>
<td>${event.type}</td>
<td>${event.model}</td>
<td>${event.requestCount.toFixed(1)}</td>
<td>${event.subscriptionProductId}</td>
<td>${slowText}</td>
<td>${event.status}</td>
<td>${event.type_other}</td>
<td>${toolcallText}</td>
<td>${maxText}</td>
<td>${tokenText}</td>
</tr>
`;
}).join('');
}
showLoading(show) {
this.loading.style.display = show ? 'block' : 'none';
if (show) {
this.summary.style.display = 'none';
this.details.style.display = 'none';
}
}
showError(message) {
this.error.textContent = message;
this.error.style.display = 'block';
this.summary.style.display = 'none';
this.details.style.display = 'none';
}
hideError() {
this.error.style.display = 'none';
}
}
// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
new CursorUsageStats();
});