forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.js
More file actions
194 lines (181 loc) · 5.55 KB
/
usage.js
File metadata and controls
194 lines (181 loc) · 5.55 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
// Copilot API Admin — usage dashboard interactivity.
// Loaded as an external script under default-src 'self' CSP.
// Reads the JSON payload emitted by page.tsx and renders three uPlot charts.
//
// Expected payload shape:
// {
// rpm: [{ ts:number, model:string, count:number }, ...],
// tokens: [{ ts:number, prompt_tokens:number, completion_tokens:number }, ...],
// latency: [{ ts:number, p95:number }, ...],
// filter: { since:number, until:number, ... }
// }
(function () {
"use strict"
function readPayload() {
var el = document.getElementById("usage-data")
if (!el) return null
try {
return JSON.parse(el.textContent || "{}")
} catch (_e) {
return null
}
}
// ---------------------------------------------------------------------------
// Theme colours — kept in sync with src/admin/assets/style.css
// ---------------------------------------------------------------------------
var THEME = {
bg: "#1a1a1e",
text: "#e2e2e8",
grid: "#2e2e35",
accent: "#7c6af7",
accent2: "#4ade80",
accent3: "#f87171",
series: [
"#7c6af7",
"#4ade80",
"#f87171",
"#fbbf24",
"#60a5fa",
"#a78bfa",
"#f472b6",
"#34d399",
],
}
function makeAxes() {
return [
{ stroke: THEME.text, grid: { stroke: THEME.grid } },
{ stroke: THEME.text, grid: { stroke: THEME.grid } },
]
}
function pickColour(i) {
return THEME.series[i % THEME.series.length]
}
// ---------------------------------------------------------------------------
// Helpers: bucket alignment + sparse → dense
// ---------------------------------------------------------------------------
function uniqSorted(values) {
var s = {}
for (var i = 0; i < values.length; i++) s[values[i]] = true
var out = Object.keys(s)
.map(Number)
.sort(function (a, b) {
return a - b
})
return out
}
// ---------------------------------------------------------------------------
// requests-per-minute: one series per model
// ---------------------------------------------------------------------------
function renderRpm(points) {
var el = document.getElementById("chart-rpm")
if (!el || !window.uPlot) return
if (!points || points.length === 0) {
el.textContent = "(no data)"
return
}
var times = uniqSorted(points.map(function (p) { return p.ts / 1000 }))
var modelSet = {}
for (var i = 0; i < points.length; i++) modelSet[points[i].model] = true
var models = Object.keys(modelSet).sort()
// Build a values-per-(model,time) lookup
var lookup = {}
for (var j = 0; j < points.length; j++) {
var p = points[j]
var key = p.model + "|" + (p.ts / 1000)
lookup[key] = p.count
}
var data = [times]
var series = [{}]
for (var m = 0; m < models.length; m++) {
var col = []
for (var t = 0; t < times.length; t++) {
col.push(lookup[models[m] + "|" + times[t]] || 0)
}
data.push(col)
series.push({
label: models[m],
stroke: pickColour(m),
width: 2,
})
}
var opts = {
width: el.clientWidth || 800,
height: 240,
series: series,
axes: makeAxes(),
legend: { show: true },
}
new window.uPlot(opts, data, el)
}
// ---------------------------------------------------------------------------
// tokens-per-hour: stacked prompt + completion (rendered as two series)
// ---------------------------------------------------------------------------
function renderTokens(points) {
var el = document.getElementById("chart-tph")
if (!el || !window.uPlot) return
if (!points || points.length === 0) {
el.textContent = "(no data)"
return
}
var times = []
var prompt = []
var completion = []
for (var i = 0; i < points.length; i++) {
times.push(points[i].ts / 1000)
prompt.push(points[i].prompt_tokens)
completion.push(points[i].completion_tokens)
}
var opts = {
width: el.clientWidth || 800,
height: 240,
series: [
{},
{ label: "prompt", stroke: THEME.accent, fill: "rgba(124,106,247,0.2)", width: 2 },
{ label: "completion", stroke: THEME.accent2, fill: "rgba(74,222,128,0.2)", width: 2 },
],
axes: makeAxes(),
legend: { show: true },
}
new window.uPlot(opts, [times, prompt, completion], el)
}
// ---------------------------------------------------------------------------
// p95-latency-per-hour
// ---------------------------------------------------------------------------
function renderLatency(points) {
var el = document.getElementById("chart-p95")
if (!el || !window.uPlot) return
if (!points || points.length === 0) {
el.textContent = "(no data)"
return
}
var times = []
var values = []
for (var i = 0; i < points.length; i++) {
times.push(points[i].ts / 1000)
values.push(points[i].p95)
}
var opts = {
width: el.clientWidth || 800,
height: 240,
series: [
{},
{ label: "p95 ms", stroke: THEME.accent3, width: 2 },
],
axes: makeAxes(),
legend: { show: true },
}
new window.uPlot(opts, [times, values], el)
}
function run() {
var payload = readPayload()
if (!payload) return
renderRpm(payload.rpm || [])
renderTokens(payload.tokens || [])
renderLatency(payload.latency || [])
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", run)
} else {
run()
}
})()