Skip to content

Commit 8a01d64

Browse files
authored
Add option to show metrics even when empty (cachethq#297)
1 parent 43ff68a commit 8a01d64

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('metrics', function (Blueprint $table) {
15+
$table->boolean('show_when_empty')->after('display_chart')->default(false);
16+
});
17+
}
18+
};

resources/lang/en/metric.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
'visible_label' => 'Visible',
3737
'display_chart_label' => 'Display Chart',
38+
'show_when_empty_label' => 'Show when empty',
3839
],
3940
'overview' => [
4041
'metric_points_label' => 'Metric Points',

src/Filament/Resources/Metrics/MetricResource.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public static function form(Schema $schema): Schema
9191
->label(__('cachet::metric.form.display_chart_label'))
9292
->default(true)
9393
->required(),
94+
Toggle::make('show_when_empty')
95+
->label(__('cachet::metric.form.show_when_empty_label'))
96+
->default(true)
97+
->required(),
9498

9599
])->columnSpan(1),
96100
])->columns(4);

src/Models/Metric.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Metric extends Model
4848
protected $casts = [
4949
'calc_type' => MetricTypeEnum::class,
5050
'display_chart' => 'bool',
51+
'show_when_empty' => 'bool',
5152
'places' => 'int',
5253
'default_view' => MetricViewEnum::class,
5354
'visible' => ResourceVisibilityEnum::class,
@@ -68,6 +69,7 @@ class Metric extends Model
6869
'description',
6970
'calc_type',
7071
'display_chart',
72+
'show_when_empty',
7173
'places',
7274
'default_value',
7375
'default_view',

src/View/Components/Metrics.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private function metrics(Carbon $startDate): Collection
4747
'metricPoints' => fn ($query) => $query->orderBy('created_at'),
4848
])
4949
->where('display_chart', true)
50-
->whereHas('metricPoints', fn (Builder $query) => $query->where('created_at', '>=', $startDate))
50+
->where(fn (Builder $query) => $query->where('show_when_empty', true)->orWhereHas('metricPoints', fn (Builder $query) => $query->where('created_at', '>=', $startDate)))
5151
->orderBy('places')
5252
->get();
5353
}

0 commit comments

Comments
 (0)