Skip to content

The only tool you need to know what is happening and how to fix it.

more progress

dduxf127992fac0a
diff --git a/backend/app/controllers/dashboard.controller.go b/backend/app/controllers/dashboard.controller.go
index b10d7487..74494948 100644
--- a/backend/app/controllers/dashboard.controller.go
+++ b/backend/app/controllers/dashboard.controller.go
@@ -48,7 +48,7 @@ func (d dashboardController) GetDashboard(c *gin.Context) {
prevStart := now.Add(-48 * time.Hour)
prevEnd := start
- metrics := make([]models.DashboardMetric, 0, 6)
+ metrics := make([]models.DashboardMetric, 0, 11)
// 1. Requests count
requestsTrend, err := repositories.TransactionRepository.CountByHour(c, projectId, start, now)
@@ -89,23 +89,73 @@ func (d dashboardController) GetDashboard(c *gin.Context) {
metrics = append(metrics, buildMetric("error_rate", "Error Rate", errorRateCurrent, "%", errorRateTrend, errorRatePrev, "error_rate"))
// 5. CPU Usage
- cpuTrend, err := repositories.MetricRecordRepository.GetAverageByHour(c, projectId, "cpu", start, now)
+ cpuTrend, err := repositories.MetricRecordRepository.GetAverageByHour(c, projectId, models.MetricNameCpuUsage, start, now)
if err != nil {
panic(err)
}
cpuCurrent := getLastValue(cpuTrend)
- cpuPrev, _ := repositories.MetricRecordRepository.GetAverageBetween(c, projectId, "cpu", prevStart, prevEnd)
+ cpuPrev, _ := repositories.MetricRecordRepository.GetAverageBetween(c, projectId, models.MetricNameCpuUsage, prevStart, prevEnd)
metrics = append(metrics, buildMetric("cpu_usage", "CPU Usage", cpuCurrent, "%", cpuTrend, cpuPrev, "cpu"))
- // 6. Memory Usage
- memTrend, err := repositories.MetricRecordRepository.GetAverageByHour(c, projectId, "ram", start, now)
+ // 6. Memory Usage (MB)
+ memTrend, err := repositories.MetricRecordRepository.GetAverageByHour(c, projectId, models.MetricNameMemoryUsage, start, now)
if err != nil {
panic(err)
}
memCurrent := getLastValue(memTrend)
- memPrev, _ := repositories.MetricRecordRepository.GetAverageBetween(c, projectId, "ram", prevStart, prevEnd)
+ memPrev, _ := repositories.MetricRecordRepository.GetAverageBetween(c, projectId, models.MetricNameMemoryUsage, prevStart, prevEnd)
metrics = append(metrics, buildMetric("memory_usage", "Memory Usage", memCurrent, "MB", memTrend, memPrev, "memory"))
+ // 7. Memory Usage Percentage
+ memPcntTrend, err := repositories.MetricRecordRepository.GetAverageByHour(c, projectId, models.MetricNameMemoryUsagePcnt, start, now)
+ if err != nil {
+ panic(err)
+ }
+ memPcntCurrent := getLastValue(memPcntTrend)
+ memPcntPrev, _ := repositories.MetricRecordRepository.GetAverageBetween(c, projectId, models.MetricNameMemoryUsagePcnt, prevStart, prevEnd)
+ metrics = append(metrics, buildMetric("memory_usage_pcnt", "Memory %", memPcntCurrent, "%", memPcntTrend, memPcntPrev, "memory_pcnt"))
+
+ // 8. Go Routines
+ goRoutinesTrend, err := repositories.MetricRecordRepository.GetAverageByHour(c, projectId, models.MetricNameGoRoutines, start, now)
+ if err != nil {
+ panic(err)
+ }
+ goRoutinesCurrent := getLastValue(goRoutinesTrend)
+ goRoutinesPrev, _ := repositories.MetricRecordRepository.GetAverageBetween(c, projectId, models.MetricNameGoRoutines, prevStart, prevEnd)
+ metrics = append(metrics, buildMetric("go_routines", "Go Routines", goRoutinesCurrent, "", goRoutinesTrend, goRoutinesPrev, "go_routines"))
+
+ // 9. Heap Objects
+ heapObjectsTrend, err := repositories.MetricRecordRepository.GetAverageByHour(c, projectId, models.MetricNameHeapObjects, start, now)
+ if err != nil {
+ panic(err)
+ }
+ heapObjectsCurrent := getLastValue(heapObjectsTrend)
+ heapObjectsPrev, _ := repositories.MetricRecordRepository.GetAverageBetween(c, projectId, models.MetricNameHeapObjects, prevStart, prevEnd)
+ metrics = append(metrics, buildMetric("heap_objects", "Heap Objects", heapObjectsCurrent, "", heapObjectsTrend, heapObjectsPrev, "heap_objects"))
+
+ // 10. Num GC
+ numGCTrend, err := repositories.MetricRecordRepository.GetAverageByHour(c, projectId, models.MetricNameNumGC, start, now)
+ if err != nil {
+ panic(err)
+ }
+ numGCCurrent := getLastValue(numGCTrend)
+ numGCPrev, _ := repositories.MetricRecordRepository.GetAverageBetween(c, projectId, models.MetricNameNumGC, prevStart, prevEnd)
+ metrics = append(metrics, buildMetric("num_gc", "GC Cycles", numGCCurrent, "", numGCTrend, numGCPrev, "num_gc"))
+
+ // 11. GC Pause Total (convert from nanoseconds to milliseconds)
+ gcPauseTrend, err := repositories.MetricRecordRepository.GetAverageByHour(c, projectId, models.MetricNameGCPauseTotal, start, now)
+ if err != nil {
+ panic(err)
+ }
+ // Convert nanoseconds to milliseconds for display
+ for i := range gcPauseTrend {
+ gcPauseTrend[i].Value = gcPauseTrend[i].Value / 1_000_000
+ }
+ gcPauseCurrent := getLastValue(gcPauseTrend)
+ gcPausePrevRaw, _ := repositories.MetricRecordRepository.GetAverageBetween(c, projectId, models.MetricNameGCPauseTotal, prevStart, prevEnd)
+ gcPausePrev := gcPausePrevRaw / 1_000_000
+ metrics = append(metrics, buildMetric("gc_pause", "GC Pause", gcPauseCurrent, "ms", gcPauseTrend, gcPausePrev, "gc_pause"))
+
c.JSON(http.StatusOK, models.DashboardResponse{
Metrics: metrics,
LastUpdated: now,
@@ -185,13 +235,40 @@ func calculateStatus(value float64, metricType string) string {
}
return "healthy"
case "memory":
- // Higher is worse for memory
+ // Higher is worse for memory (MB)
if value > 900 {
return "critical"
} else if value > 700 {
return "warning"
}
return "healthy"
+ case "memory_pcnt":
+ // Higher is worse for memory percentage
+ if value > 90 {
+ return "critical"
+ } else if value > 70 {
+ return "warning"
+ }
+ return "healthy"
+ case "go_routines":
+ // Higher may indicate goroutine leaks
+ if value > 10000 {
+ return "critical"
+ } else if value > 5000 {
+ return "warning"
+ }
+ return "healthy"
+ case "heap_objects":
+ // Higher may indicate memory pressure
+ if value > 1000000 {
+ return "critical"
+ } else if value > 500000 {
+ return "warning"
+ }
+ return "healthy"
+ case "num_gc", "gc_pause":
+ // These are informational, always healthy
+ return "healthy"
}
return "healthy"
}
diff --git a/frontend/src/lib/components/dashboard/metric-card.svelte b/frontend/src/lib/components/dashboard/metric-card.svelte
index 94fed6db..0ea3744c 100644
--- a/frontend/src/lib/components/dashboard/metric-card.svelte
+++ b/frontend/src/lib/components/dashboard/metric-card.svelte
@@ -8,7 +8,10 @@
import type { DashboardMetric, MetricTrendPoint } from '$lib/types/dashboard';
import { min, max } from 'd3-array';
- let { metric } = $props<{ metric: DashboardMetric }>();
+ let { metric, timeDomain = null } = $props<{
+ metric: DashboardMetric;
+ timeDomain?: [Date, Date] | null;
+ }>();
const statusColors: Record<string, string> = {
healthy: 'bg-green-500',
@@ -16,8 +19,46 @@
critical: 'bg-red-500'
};
+ // Smart number formatting based on unit type
+ function formatMetricValue(value: number, unit: string): string {
+ // Percentages
+ if (unit === '%') {
+ if (value === 0) return '0';
+ if (Math.abs(value) < 0.1) return value.toFixed(2);
+ if (Math.abs(value) < 10) return value.toFixed(1);
+ return Math.round(value).toString();
+ }
+
+ // Durations (ms)
+ if (unit === 'ms') {
+ if (value < 1) return (value * 1000).toFixed(0);
+ if (value < 10) return value.toFixed(1);
+ if (value < 1000) return Math.round(value).toString();
+ return (value / 1000).toFixed(1);
+ }
+
+ // Counts
+ if (unit === 'count' || unit === '') {
+ if (value >= 1_000_000) return (value / 1_000_000).toFixed(1) + 'M';
+ if (value >= 1_000) return (value / 1_000).toFixed(1) + 'K';
+ return Math.round(value).toString();
+ }
+
+ // Memory (MB)
+ if (unit === 'MB') {
+ if (value >= 1024) return (value / 1024).toFixed(1);
+ return Math.round(value).toString();
+ }
+
+ // Default: round to 1 decimal
+ if (Number.isInteger(value)) return value.toString();
+ return value.toFixed(1);
+ }
+
const formattedValue = $derived(
- metric.formatValue ? metric.formatValue(metric.value) : metric.value.toString()
+ metric.formatValue
+ ? metric.formatValue(metric.value)
+ : formatMetricValue(metric.value, metric.unit)
);
const TrendChangeIcon = $derived(
@@ -32,14 +73,65 @@
: 'text-muted-foreground'
);
+ const chartConfig = {
+ value: { label: "Value", color: "var(--chart-1)" },
+ } satisfies Chart.ChartConfig;
+
+ const yMin = $derived(min(metric.trend, (d: MetricTrendPoint) => d.value) ?? 0);
+ const yMax = $derived(max(metric.trend, (d: MetricTrendPoint) => d.value) ?? 0);
+ const padding = $derived((yMax - yMin) * 0.1 || 1);
- const chartConfig = {
- value: { label: "Value", color: "var(--chart-1)" },
- } satisfies Chart.ChartConfig;
+ // Calculate xScale with shared time domain
+ const xScaleWithDomain = $derived(() => {
+ const scale = scaleUtc();
+ if (timeDomain) {
+ return scale.domain(timeDomain);
+ }
+ // Fallback to data range
+ if (metric.trend.length > 0) {
+ const minTime = min(metric.trend, (d: MetricTrendPoint) => d.timestamp);
+ const maxTime = max(metric.trend, (d: MetricTrendPoint) => d.timestamp);
+ if (minTime && maxTime) {
+ return scale.domain([minTime, maxTime]);
+ }
+ }
+ return scale;
+ });
- const yMin = $derived(min(metric.trend, (d: MetricTrendPoint) => d.value) ?? 0);
- const yMax = $derived(max(metric.trend, (d: MetricTrendPoint) => d.value) ?? 0);
- const padding = $derived((yMax - yMin) * 0.1);
+ // Detect expected interval and filter out gaps for sparse data
+ const expectedIntervalMs = $derived(() => {
+ if (metric.trend.length < 2) return 3600000; // 1 hour default
+ const intervals: number[] = [];
+ for (let i = 1; i < Math.min(metric.trend.length, 10); i++) {
+ intervals.push(metric.trend[i].timestamp.getTime() - metric.trend[i - 1].timestamp.getTime());
+ }
+ // Use median interval * 2 as threshold
+ intervals.sort((a, b) => a - b);
+ const median = intervals[Math.floor(intervals.length / 2)];
+ return median * 2;
+ });
+
+ // Split data into continuous segments to avoid drawing lines across gaps
+ function splitIntoSegments(data: MetricTrendPoint[], maxGapMs: number): MetricTrendPoint[][] {
+ if (data.length === 0) return [];
+ const segments: MetricTrendPoint[][] = [];
+ let current: MetricTrendPoint[] = [data[0]];
+
+ for (let i = 1; i < data.length; i++) {
+ const gap = data[i].timestamp.getTime() - data[i - 1].timestamp.getTime();
+ if (gap <= maxGapMs) {
+ current.push(data[i]);
+ } else {
+ if (current.length > 0) segments.push(current);
+ current = [data[i]];
+ }
+ }
+ if (current.length > 0) segments.push(current);
+ return segments;
+ }
+
+ const dataSegments = $derived(splitIntoSegments(metric.trend, expectedIntervalMs()));
+ const hasData = $derived(metric.trend.length > 0 && metric.trend.some((d: MetricTrendPoint) => d.value !== 0));
</script>
<Card.Root class="gap-3">
@@ -77,32 +169,38 @@
</Chart> -->
<Chart.Container config={chartConfig}>
- <LineChart
- data={metric.trend}
- x="timestamp"
- xScale={scaleUtc()}
- series={[
- {
- key: "value",
- label: "Value",
- color: chartConfig.value.color,
- },
- ]}
- yDomain={[yMin - padding, yMax + padding]}
- seriesLayout="stack"
- props={{
- xAxis: {
- format: () => ""
- },
- yAxis: {
- format: (a: number) => a > 999 ? (a/1000).toFixed(0) + "k" : `${a}`,
- },
- }}
- >
- {#snippet tooltip()}
- <Chart.Tooltip hideLabel />
- {/snippet}
- </LineChart>
+ {#if hasData}
+ <LineChart
+ data={metric.trend}
+ x="timestamp"
+ xScale={xScaleWithDomain()}
+ series={[
+ {
+ key: "value",
+ label: "Value",
+ color: chartConfig.value.color,
+ },
+ ]}
+ yDomain={[Math.max(0, yMin - padding), yMax + padding]}
+ seriesLayout="stack"
+ props={{
+ xAxis: {
+ format: () => ""
+ },
+ yAxis: {
+ format: (a: number) => a > 999 ? (a/1000).toFixed(0) + "k" : `${a}`,
+ },
+ }}
+ >
+ {#snippet tooltip()}
+ <Chart.Tooltip hideLabel />
+ {/snippet}
+ </LineChart>
+ {:else}
+ <div class="flex h-[100px] items-center justify-center text-sm text-muted-foreground">
+ No data in this period
+ </div>
+ {/if}
</Chart.Container>
<!-- 24h Change -->
diff --git a/frontend/src/lib/components/ui/time-range-picker/time-range-picker.svelte b/frontend/src/lib/components/ui/time-range-picker/time-range-picker.svelte
index 6b61fae9..6bc3cba6 100644
--- a/frontend/src/lib/components/ui/time-range-picker/time-range-picker.svelte
+++ b/frontend/src/lib/components/ui/time-range-picker/time-range-picker.svelte
@@ -215,7 +215,7 @@
<Popover.Root bind:open={isOpen} onOpenChange={handleOpenChange}>
<Popover.Trigger>
- <Button variant="outline" class="h-9 min-w-[340px] justify-between gap-2 font-normal">
+ <Button variant="outline" class="h-9 justify-between gap-2 font-normal">
<span class="flex items-center gap-2">
<Clock class="h-4 w-4 text-muted-foreground" />
<span class="truncate">{displayLabel()}</span>
diff --git a/frontend/src/routes/metrics/+page.svelte b/frontend/src/routes/metrics/+page.svelte
index e0bfea7f..fbf2fa5e 100644
--- a/frontend/src/routes/metrics/+page.svelte
+++ b/frontend/src/routes/metrics/+page.svelte
@@ -9,19 +9,57 @@
import { api } from '$lib/api';
import { ErrorDisplay } from '$lib/components/ui/error-display';
import { projectsState } from '$lib/state/projects.svelte';
+ import { TimeRangePicker } from '$lib/components/ui/time-range-picker';
+ import { CalendarDate, getLocalTimeZone, today } from '@internationalized/date';
let dashboardData = $state<DashboardData | null>(null);
let loading = $state(true);
let error = $state('');
let errorStatus = $state<number>(0);
+ // Date Range State - default to last 24 hours
+ let fromDate = $state<CalendarDate>(today(getLocalTimeZone()).subtract({ days: 1 }));
+ let toDate = $state<CalendarDate>(today(getLocalTimeZone()));
+ let fromTime = $state('00:00');
+ let toTime = $state('23:59');
+ let sharedTimeDomain = $state<[Date, Date] | null>(null);
+
+ // Combine date and time into ISO datetime string
+ function getFromDateTime(): string {
+ const dateStr = `${fromDate.year}-${String(fromDate.month).padStart(2, '0')}-${String(fromDate.day).padStart(2, '0')}`;
+ return `${dateStr}T${fromTime || '00:00'}`;
+ }
+
+ function getToDateTime(): string {
+ const dateStr = `${toDate.year}-${String(toDate.month).padStart(2, '0')}-${String(toDate.day).padStart(2, '0')}`;
+ return `${dateStr}T${toTime || '23:59'}`;
+ }
+
+ function handleTimeRangeChange(from: { date: CalendarDate; time: string }, to: { date: CalendarDate; time: string }) {
+ fromDate = from.date;
+ fromTime = from.time;
+ toDate = to.date;
+ toTime = to.time;
+ loadDashboard();
+ }
+
async function loadDashboard() {
loading = true;
error = '';
errorStatus = 0;
try {
- const response = await api.get('/dashboard', { projectId: projectsState.currentProjectId ?? undefined });
+ const fromDateTime = new Date(getFromDateTime());
+ const toDateTime = new Date(getToDateTime());
+
+ // Store shared time domain for charts
+ sharedTimeDomain = [fromDateTime, toDateTime];
+
+ // Build query params for date range
+ const dateParams = `fromDate=${fromDateTime.toISOString()}&toDate=${toDateTime.toISOString()}`;
+ const response = await api.get(`/dashboard?${dateParams}`, {
+ projectId: projectsState.currentProjectId ?? undefined
+ });
// Transform the API response to match the DashboardData type
// Convert timestamp strings to Date objects
@@ -69,17 +107,25 @@
<div class="space-y-4">
<!-- Header -->
- <div class="flex items-center justify-between">
+ <div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
<div>
<h2 class="text-3xl font-bold tracking-tight">Metrics</h2>
{#if dashboardData}
<p class="mt-1 text-sm text-muted-foreground">Last updated: {lastUpdatedFormatted}</p>
{/if}
</div>
- <Button variant="outline" size="sm" onclick={loadDashboard} disabled={loading}>
- <RefreshCw class="mr-2 h-4 w-4 {loading ? 'animate-spin' : ''}" />
- Refresh
- </Button>
+ <div class="flex items-center gap-2">
+ <TimeRangePicker
+ bind:fromDate
+ bind:toDate
+ bind:fromTime
+ bind:toTime
+ onApply={handleTimeRangeChange}
+ />
+ <Button variant="outline" size="sm" onclick={loadDashboard} disabled={loading}>
+ <RefreshCw class="h-4 w-4 {loading ? 'animate-spin' : ''}" />
+ </Button>
+ </div>
</div>
{#if error && !loading}
@@ -93,9 +139,9 @@
<!-- Metrics Grid -->
{#if !error}
- <div class="grid grid-cols-1 gap-4 md:grid-cols-3">
+ <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
{#if loading}
- {#each Array(6) as _}
+ {#each Array(11) as _}
<Card>
<CardContent class="flex gap-3 flex-col">
<div class="flex items-center justify-between">
@@ -110,7 +156,7 @@
{/each}
{:else if dashboardData}
{#each dashboardData.metrics as metric (metric.id)}
- <MetricCard {metric} />
+ <MetricCard {metric} timeDomain={sharedTimeDomain} />
{/each}
{/if}
</div>