-
-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing metrics into the status page #58
Draft
Ionys320
wants to merge
8
commits into
cachethq:main
Choose a base branch
from
Ionys320:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
60d2fdf
feat(metrics): Implementing metrics into the status page
Ionys320 3a13157
fix(metric): Fix datasets' label (use the `suffix` instead of `name`)
Ionys320 1f51e3b
feat(metrics): Optimize the `whereHas`
Ionys320 f019e14
feat(metrics): Simplify a bit the code of metric points transformation
Ionys320 b23afce
feat(metric): Simplify the code for setting metric/period data in Alpine
Ionys320 fbb118e
fix(metric): Fix a previous commit
Ionys320 75c1218
feat(metric): Use `MetricViewEnum` for period selection
Ionys320 5b84fab
Update styling
jbrooksuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
import 'moment'; | ||
import Chart from 'chart.js/auto' | ||
import 'chartjs-adapter-moment'; | ||
|
||
import Alpine from 'alpinejs' | ||
|
||
import Anchor from '@alpinejs/anchor' | ||
import Collapse from '@alpinejs/collapse' | ||
import Focus from '@alpinejs/focus' | ||
import Ui from '@alpinejs/ui' | ||
|
||
Chart.defaults.color = '#fff'; | ||
window.Chart = Chart | ||
|
||
Alpine.plugin(Anchor) | ||
Alpine.plugin(Collapse) | ||
Alpine.plugin(Focus) | ||
Alpine.plugin(Ui) | ||
|
||
Alpine.start() | ||
window.Alpine = Alpine | ||
Alpine.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
@props([ | ||
'metric', | ||
]) | ||
|
||
@use('\Cachet\Enums\MetricViewEnum') | ||
|
||
<div x-data="chart"> | ||
<div class="flex flex-col gap-2"> | ||
<div class="flex items-center gap-1.5"> | ||
<div class="font-semibold leading-6">{{ $metric->name }}</div> | ||
|
||
<div x-data x-popover class="flex items-center"> | ||
<button x-ref="anchor" x-popover:button> | ||
<x-heroicon-o-question-mark-circle class="size-4 text-zinc-500 dark:text-zinc-300" /> | ||
</button> | ||
<div x-popover:panel x-cloak x-transition.opacity x-anchor.right.offset.8="$refs.anchor" class="rounded bg-white px-2 py-1 text-xs font-medium text-zinc-800 drop-shadow dark:text-zinc-800"> | ||
<span class="pointer-events-none absolute -left-1 top-1.5 size-4 rotate-45 bg-white"></span> | ||
<p class="relative">{{ $metric->description }}</p> | ||
</div> | ||
</div> | ||
|
||
<!-- Period Selector --> | ||
<select x-model="period" class="ml-auto rounded-md border border-gray-300 dark:border-gray-700 bg-white dark:bg-zinc-800 text-gray-900 dark:text-gray-100 text-sm font-medium"> | ||
@foreach([MetricViewEnum::last_hour, MetricViewEnum::today, MetricViewEnum::week, MetricViewEnum::month] as $value) | ||
<option value="{{ $value }}">{{ $value->getLabel() }}</option> | ||
@endforeach | ||
</select> | ||
</div> | ||
<canvas x-ref="canvas" height="300" class="ring-1 ring-gray-900/5 dark:ring-gray-100/10 bg-white dark:bg-zinc-800 rounded-md shadow-sm text-white"></canvas> | ||
</div> | ||
</div> | ||
|
||
<script> | ||
document.addEventListener('alpine:init', () => { | ||
Alpine.data('chart', () => ({ | ||
metric: {{ Js::from($metric) }}, | ||
period: {{ Js::from($metric->default_view) }}, | ||
points: [ | ||
[], | ||
[], | ||
[], | ||
[] | ||
], | ||
chart: null, | ||
init, | ||
})) | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<script> | ||
const now = new Date(); | ||
const previousHour = new Date(now - 60 * 60 * 1000); | ||
const previous24Hours = new Date(now - 24 * 60 * 60 * 1000); | ||
const previous7Days = new Date(now - 7 * 24 * 60 * 60 * 1000); | ||
const previous30Days = new Date(now - 30 * 24 * 60 * 60 * 1000); | ||
|
||
function init() { | ||
// Parse metric points | ||
const metricPoints = this.metric.metric_points.map((point) => { | ||
return { | ||
x: new Date(point.x), | ||
y: point.y | ||
} | ||
}); | ||
|
||
// Filter points based on the selected period | ||
this.points[0] = metricPoints.filter((point) => point.x >= previousHour); | ||
this.points[1] = metricPoints.filter((point) => point.x >= previous24Hours); | ||
this.points[2] = metricPoints.filter((point) => point.x >= previous7Days); | ||
this.points[3] = metricPoints.filter((point) => point.x >= previous30Days); | ||
|
||
// Initialize chart | ||
const chart = new Chart(this.$refs.canvas, { | ||
type: 'line', | ||
data: { | ||
datasets: [{ | ||
label: this.metric.suffix, | ||
data: this.points[this.period], | ||
fill: false, | ||
borderColor: 'rgb(75, 192, 192)', | ||
tension: 0.1 | ||
}], | ||
}, | ||
options: { | ||
scales: { | ||
x: { | ||
type: 'timeseries', | ||
} | ||
}, | ||
} | ||
}); | ||
|
||
this.$watch('period', () => { | ||
chart.data.datasets[0].data = this.points[this.period]; | ||
chart.update(); | ||
}); | ||
} | ||
</script> | ||
|
||
<div class="flex flex-col gap-8"> | ||
@foreach($metrics as $metric) | ||
<x-cachet::metric :metric="$metric" /> | ||
<x-cachet::metric :metric="$metric" /> | ||
@endforeach | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Cachet\View\Components; | ||
|
||
use Cachet\Models\Metric; | ||
use Cachet\Settings\AppSettings; | ||
use Illuminate\Contracts\View\View; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\View\Component; | ||
|
||
class Metrics extends Component | ||
{ | ||
public function __construct(private AppSettings $appSettings) | ||
{ | ||
// | ||
} | ||
|
||
public function render(): View | ||
{ | ||
$startDate = Carbon::now()->subDays(30); | ||
|
||
$metrics = $this->metrics($startDate); | ||
|
||
// Convert each metric point to Chart.js format (x, y) | ||
$metrics->each(function ($metric) { | ||
$metric->metricPoints->transform(fn ($point) => [ | ||
'x' => $point->created_at->toIso8601String(), | ||
'y' => $point->value, | ||
]); | ||
}); | ||
|
||
return view('cachet::components.metrics', [ | ||
'metrics' => $metrics | ||
]); | ||
} | ||
|
||
/** | ||
* Fetch the available metrics and their points. | ||
*/ | ||
private function metrics(Carbon $startDate): Collection | ||
{ | ||
return Metric::query() | ||
->with([ | ||
'metricPoints' => fn ($query) => $query->orderBy('created_at'), | ||
]) | ||
->where('visible', '>=', !auth()->check()) | ||
->whereHas('metricPoints', fn (Builder $query) => $query->where('created_at', '>=', $startDate)) | ||
->orderBy('places', 'asc') | ||
->get(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this duplication can be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the moment, I keep it for debugging purposes (by default, we only have one metric. It allows testing the individual component with two different instances.)