Skip to content

Commit 11b880b

Browse files
author
Sean Connole
committed
feat: add metrics component to frontend
1 parent 5ca40be commit 11b880b

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

resources/views/components/metrics.blade.php

+19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
const previous7Days = new Date(now - 7 * 24 * 60 * 60 * 1000)
66
const previous30Days = new Date(now - 30 * 24 * 60 * 60 * 1000)
77
8+
const MetricView = {{
9+
Js::from([
10+
'last_hour' => \Cachet\Enums\MetricViewEnum::last_hour->value,
11+
'today' => \Cachet\Enums\MetricViewEnum::today->value,
12+
'week' => \Cachet\Enums\MetricViewEnum::week->value,
13+
'month' => \Cachet\Enums\MetricViewEnum::month->value,
14+
])
15+
}}
16+
817
function init() {
918
// Parse metric points
1019
const metricPoints = this.metric.metric_points.map((point) => {
@@ -45,8 +54,18 @@ function init() {
4554
4655
this.$watch('period', () => {
4756
chart.data.datasets[0].data = this.points[this.period]
57+
chart.options.scales.x.time.unit = getTimeUnit(this.period)
58+
4859
chart.update()
4960
})
61+
62+
function getTimeUnit(period) {
63+
if (period == MetricView.last_hour) return 'minute'
64+
if (period == MetricView.today) return 'hour'
65+
if (period == MetricView.week) return 'week'
66+
if (period == MetricView.month) return 'month'
67+
return 'day'
68+
}
5069
}
5170
</script>
5271

src/View/Components/Metrics.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function render(): View
2626
// Convert each metric point to Chart.js format (x, y)
2727
$metrics->each(function ($metric) {
2828
$metric->metricPoints->transform(fn ($point) => [
29-
'x' => $point->created_at->toIso8601String(),
29+
'x' => $point->created_at->utc(),
3030
'y' => $point->value,
3131
]);
3232
});
@@ -45,6 +45,8 @@ private function metrics(Carbon $startDate): Collection
4545
->with([
4646
'metricPoints' => fn ($query) => $query->orderBy('created_at'),
4747
])
48+
->where('display_chart', '=', 1)
49+
->where('visible', '=', 1)
4850
->where('visible', '>=', !auth()->check())
4951
->whereHas('metricPoints', fn (Builder $query) => $query->where('created_at', '>=', $startDate))
5052
->orderBy('places', 'asc')

0 commit comments

Comments
 (0)