Skip to content

Commit 4c7c64b

Browse files
authored
fixed chart query date filter (alexjustesen#223)
1 parent e03ddbc commit 4c7c64b

File tree

3 files changed

+37
-82
lines changed

3 files changed

+37
-82
lines changed

app/Filament/Widgets/RecentJitterChart.php

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class RecentJitterChart extends LineChartWidget
1212

1313
protected static ?string $maxHeight = '300px';
1414

15+
public ?string $filter = '24h';
16+
1517
protected function getHeading(): string
1618
{
1719
return 'Jitter (ms)';
@@ -20,44 +22,27 @@ protected function getHeading(): string
2022
protected function getFilters(): ?array
2123
{
2224
return [
23-
'today' => 'Today',
25+
'24h' => 'Last 24h',
2426
'week' => 'Last week',
2527
'month' => 'Last month',
2628
];
2729
}
2830

2931
protected function getData(): array
3032
{
31-
$range = [];
32-
3333
$settings = new GeneralSettings();
3434

35-
switch ($this->filter) {
36-
case 'today':
37-
$range = [
38-
['created_at', '>=', now()->startOfDay()],
39-
['created_at', '<=', now()],
40-
];
41-
break;
42-
43-
case 'week':
44-
$range = [
45-
['created_at', '>=', now()->subWeek()],
46-
['created_at', '<=', now()],
47-
];
48-
break;
49-
50-
case 'month':
51-
$range = [
52-
['created_at', '>=', now()->subMonth()],
53-
['created_at', '<=', now()],
54-
];
55-
break;
56-
}
57-
5835
$results = Result::query()
5936
->select(['data', 'created_at'])
60-
->where($range)
37+
->when($this->filter == '24h', function ($query) {
38+
$query->where('created_at', '>=', now()->subDay());
39+
})
40+
->when($this->filter == 'week', function ($query) {
41+
$query->where('created_at', '>=', now()->subWeek());
42+
})
43+
->when($this->filter == 'month', function ($query) {
44+
$query->where('created_at', '>=', now()->subMonth());
45+
})
6146
->get();
6247

6348
return [

app/Filament/Widgets/RecentPingChart.php

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class RecentPingChart extends LineChartWidget
1212

1313
protected static ?string $maxHeight = '300px';
1414

15+
public ?string $filter = '24h';
16+
1517
protected function getHeading(): string
1618
{
1719
return 'Ping (ms)';
@@ -20,44 +22,27 @@ protected function getHeading(): string
2022
protected function getFilters(): ?array
2123
{
2224
return [
23-
'today' => 'Today',
25+
'24h' => 'Last 24h',
2426
'week' => 'Last week',
2527
'month' => 'Last month',
2628
];
2729
}
2830

2931
protected function getData(): array
3032
{
31-
$range = [];
32-
3333
$settings = new GeneralSettings();
3434

35-
switch ($this->filter) {
36-
case 'today':
37-
$range = [
38-
['created_at', '>=', now()->startOfDay()],
39-
['created_at', '<=', now()],
40-
];
41-
break;
42-
43-
case 'week':
44-
$range = [
45-
['created_at', '>=', now()->subWeek()],
46-
['created_at', '<=', now()],
47-
];
48-
break;
49-
50-
case 'month':
51-
$range = [
52-
['created_at', '>=', now()->subMonth()],
53-
['created_at', '<=', now()],
54-
];
55-
break;
56-
}
57-
5835
$results = Result::query()
5936
->select(['ping', 'created_at'])
60-
->where($range)
37+
->when($this->filter == '24h', function ($query) {
38+
$query->where('created_at', '>=', now()->subDay());
39+
})
40+
->when($this->filter == 'week', function ($query) {
41+
$query->where('created_at', '>=', now()->subWeek());
42+
})
43+
->when($this->filter == 'month', function ($query) {
44+
$query->where('created_at', '>=', now()->subMonth());
45+
})
6146
->get();
6247

6348
return [

app/Filament/Widgets/RecentSpeedChart.php

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class RecentSpeedChart extends LineChartWidget
1212

1313
protected static ?string $maxHeight = '300px';
1414

15+
public ?string $filter = '24h';
16+
1517
protected function getHeading(): string
1618
{
1719
return 'Download / Upload (mbps)';
@@ -20,44 +22,27 @@ protected function getHeading(): string
2022
protected function getFilters(): ?array
2123
{
2224
return [
23-
'today' => 'Today',
25+
'24h' => 'Last 24h',
2426
'week' => 'Last week',
2527
'month' => 'Last month',
2628
];
2729
}
2830

2931
protected function getData(): array
3032
{
31-
$range = [];
32-
3333
$settings = new GeneralSettings();
3434

35-
switch ($this->filter) {
36-
case 'today':
37-
$range = [
38-
['created_at', '>=', now()->startOfDay()],
39-
['created_at', '<=', now()],
40-
];
41-
break;
42-
43-
case 'week':
44-
$range = [
45-
['created_at', '>=', now()->subWeek()],
46-
['created_at', '<=', now()],
47-
];
48-
break;
49-
50-
case 'month':
51-
$range = [
52-
['created_at', '>=', now()->subMonth()],
53-
['created_at', '<=', now()],
54-
];
55-
break;
56-
}
57-
5835
$results = Result::query()
59-
->select(['download', 'upload', 'created_at'])
60-
->where($range)
36+
->select(['id', 'download', 'upload', 'created_at'])
37+
->when($this->filter == '24h', function ($query) {
38+
$query->where('created_at', '>=', now()->subDay());
39+
})
40+
->when($this->filter == 'week', function ($query) {
41+
$query->where('created_at', '>=', now()->subWeek());
42+
})
43+
->when($this->filter == 'month', function ($query) {
44+
$query->where('created_at', '>=', now()->subMonth());
45+
})
6146
->get();
6247

6348
return [

0 commit comments

Comments
 (0)