210 lines
7.7 KiB
JavaScript
210 lines
7.7 KiB
JavaScript
// retoor <retoor@molodetz.nl>
|
|||
|
|
|
||
|
|
function themeColor(name, fallback) {
|
||
|
|
const value = getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
||
|
|
return value || fallback;
|
||
|
|
}
|
||
|
|
|
||
|
|
function statusCodeColor(code, colors) {
|
||
|
|
const value = Number(code);
|
||
|
|
if (value >= 200 && value < 300) return colors.success;
|
||
|
|
if (value >= 400 && value < 500) return colors.warning;
|
||
|
|
if (value >= 500) return colors.danger;
|
||
|
|
return colors.secondary;
|
||
|
|
}
|
||
|
|
|
||
|
|
export class GatewayChartManager {
|
||
|
|
constructor() {
|
||
|
|
this.charts = {};
|
||
|
|
this.colors = {
|
||
|
|
success: themeColor("--success", "#2f6f4f"),
|
||
|
|
danger: themeColor("--danger", "#a4423a"),
|
||
|
|
accent: themeColor("--accent", "#2b4c7e"),
|
||
|
|
secondary: themeColor("--text-muted", "#6a7f93"),
|
||
|
|
warning: themeColor("--warning", "#c07c2c"),
|
||
|
|
grid: themeColor("--border", "rgba(120, 130, 140, 0.15)"),
|
||
|
|
text: themeColor("--text-primary", "#3a3f44"),
|
||
|
|
};
|
||
|
|
this.palette = [
|
||
|
|
this.colors.accent,
|
||
|
|
this.colors.warning,
|
||
|
|
this.colors.secondary,
|
||
|
|
this.colors.success,
|
||
|
|
"#3f8f8f",
|
||
|
|
"#8f5b3f",
|
||
|
|
"#5b6ecf",
|
||
|
|
this.colors.danger,
|
||
|
|
];
|
||
|
|
Chart.defaults.color = this.colors.text;
|
||
|
|
Chart.defaults.font.family = "inherit";
|
||
|
|
Chart.defaults.plugins.legend.labels.boxWidth = 12;
|
||
|
|
}
|
||
|
|
|
||
|
|
statusCodeColor(code) {
|
||
|
|
return statusCodeColor(code, this.colors);
|
||
|
|
}
|
||
|
|
|
||
|
|
_ensureChart(canvasId, config) {
|
||
|
|
const existing = this.charts[canvasId];
|
||
|
|
if (existing) existing.destroy();
|
||
|
|
const canvas = document.getElementById(canvasId);
|
||
|
|
if (!canvas) return null;
|
||
|
|
this.charts[canvasId] = new Chart(canvas, config);
|
||
|
|
return this.charts[canvasId];
|
||
|
|
}
|
||
|
|
|
||
|
|
renderTimeseries(canvasId, timeseries) {
|
||
|
|
const labels = timeseries.map((row) => new Date(row.bucket_start * 1000).toLocaleString());
|
||
|
|
const successData = timeseries.map((row) => row.success || 0);
|
||
|
|
const errorData = timeseries.map((row) => (row.requests || 0) - (row.success || 0));
|
||
|
|
this._ensureChart(canvasId, {
|
||
|
|
type: "line",
|
||
|
|
data: {
|
||
|
|
labels,
|
||
|
|
datasets: [
|
||
|
|
{ label: "Successful", data: successData, borderColor: this.colors.success, backgroundColor: "transparent", tension: 0.25, pointRadius: 0 },
|
||
|
|
{ label: "Failed", data: errorData, borderColor: this.colors.danger, backgroundColor: "transparent", tension: 0.25, pointRadius: 0 },
|
||
|
|
],
|
||
|
|
},
|
||
|
|
options: {
|
||
|
|
responsive: true,
|
||
|
|
maintainAspectRatio: false,
|
||
|
|
interaction: { mode: "index", intersect: false },
|
||
|
|
scales: {
|
||
|
|
x: { grid: { color: this.colors.grid }, ticks: { maxTicksLimit: 10 } },
|
||
|
|
y: { grid: { color: this.colors.grid }, beginAtZero: true },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
renderTotals(canvasId, totals) {
|
||
|
|
const success = totals.success || 0;
|
||
|
|
const error = totals.error || 0;
|
||
|
|
this._ensureChart(canvasId, {
|
||
|
|
type: "doughnut",
|
||
|
|
data: {
|
||
|
|
labels: ["Successful", "Failed"],
|
||
|
|
datasets: [{ data: [success, error], backgroundColor: [this.colors.success, this.colors.danger] }],
|
||
|
|
},
|
||
|
|
options: { responsive: true, maintainAspectRatio: false },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
renderPerModelBar(canvasId, perModel, valueKey, label) {
|
||
|
|
const top = [...perModel].sort((a, b) => (b[valueKey] || 0) - (a[valueKey] || 0)).slice(0, 10);
|
||
|
|
this._ensureChart(canvasId, {
|
||
|
|
type: "bar",
|
||
|
|
data: {
|
||
|
|
labels: top.map((row) => `${row.model} (${row.provider})`),
|
||
|
|
datasets: [{ label, data: top.map((row) => row[valueKey] || 0), backgroundColor: this.palette }],
|
||
|
|
},
|
||
|
|
options: {
|
||
|
|
indexAxis: "y",
|
||
|
|
responsive: true,
|
||
|
|
maintainAspectRatio: false,
|
||
|
|
plugins: { legend: { display: false } },
|
||
|
|
scales: {
|
||
|
|
x: { grid: { color: this.colors.grid }, beginAtZero: true },
|
||
|
|
y: { grid: { display: false } },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
renderEndpointBreakdown(canvasId, perEndpoint) {
|
||
|
|
this._ensureChart(canvasId, {
|
||
|
|
type: "pie",
|
||
|
|
data: {
|
||
|
|
labels: perEndpoint.map((row) => row.endpoint),
|
||
|
|
datasets: [{ data: perEndpoint.map((row) => row.total_requests), backgroundColor: this.palette }],
|
||
|
|
},
|
||
|
|
options: { responsive: true, maintainAspectRatio: false },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
renderBucketBar(canvasId, rows, labelKey, valueKey, label, colorFn) {
|
||
|
|
const labels = rows.map((row) => String(row[labelKey]));
|
||
|
|
const data = rows.map((row) => row[valueKey] || 0);
|
||
|
|
const backgroundColor = colorFn ? labels.map((l) => colorFn(l)) : this.palette;
|
||
|
|
this._ensureChart(canvasId, {
|
||
|
|
type: "bar",
|
||
|
|
data: { labels, datasets: [{ label, data, backgroundColor }] },
|
||
|
|
options: {
|
||
|
|
responsive: true,
|
||
|
|
maintainAspectRatio: false,
|
||
|
|
plugins: { legend: { display: false } },
|
||
|
|
scales: {
|
||
|
|
x: { grid: { display: false } },
|
||
|
|
y: { grid: { color: this.colors.grid }, beginAtZero: true },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
renderHorizontalBar(canvasId, labels, data, label) {
|
||
|
|
this._ensureChart(canvasId, {
|
||
|
|
type: "bar",
|
||
|
|
data: { labels, datasets: [{ label, data, backgroundColor: this.palette }] },
|
||
|
|
options: {
|
||
|
|
indexAxis: "y",
|
||
|
|
responsive: true,
|
||
|
|
maintainAspectRatio: false,
|
||
|
|
plugins: { legend: { display: false } },
|
||
|
|
scales: {
|
||
|
|
x: { grid: { color: this.colors.grid }, beginAtZero: true },
|
||
|
|
y: { grid: { display: false } },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
renderDoughnutFromCounts(canvasId, labels, data, colors) {
|
||
|
|
this._ensureChart(canvasId, {
|
||
|
|
type: "doughnut",
|
||
|
|
data: { labels, datasets: [{ data, backgroundColor: colors || this.palette }] },
|
||
|
|
options: { responsive: true, maintainAspectRatio: false },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
renderStackedBar(canvasId, labels, datasets) {
|
||
|
|
this._ensureChart(canvasId, {
|
||
|
|
type: "bar",
|
||
|
|
data: { labels, datasets },
|
||
|
|
options: {
|
||
|
|
indexAxis: "y",
|
||
|
|
responsive: true,
|
||
|
|
maintainAspectRatio: false,
|
||
|
|
scales: {
|
||
|
|
x: { grid: { color: this.colors.grid }, stacked: true, beginAtZero: true },
|
||
|
|
y: { grid: { display: false }, stacked: true },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
renderScatter(canvasId, points, label, xLabel, yLabel) {
|
||
|
|
this._ensureChart(canvasId, {
|
||
|
|
type: "scatter",
|
||
|
|
data: {
|
||
|
|
datasets: [{
|
||
|
|
label,
|
||
|
|
data: points,
|
||
|
|
backgroundColor: this.colors.accent,
|
||
|
|
pointRadius: 5,
|
||
|
|
pointHoverRadius: 7,
|
||
|
|
}],
|
||
|
|
},
|
||
|
|
options: {
|
||
|
|
responsive: true,
|
||
|
|
maintainAspectRatio: false,
|
||
|
|
plugins: { legend: { display: false } },
|
||
|
|
scales: {
|
||
|
|
x: { grid: { color: this.colors.grid }, title: { display: true, text: xLabel } },
|
||
|
|
y: { grid: { color: this.colors.grid }, title: { display: true, text: yLabel } },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|