Skip to content

Commit 46f611f

Browse files
dmason30StyleCIBot
andauthored
Upgrade to chart v5 (#12)
* Update to Laravel Chart Tile 5.x * Apply fixes from StyleCI Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent a40f89c commit 46f611f

File tree

9 files changed

+72
-130
lines changed

9 files changed

+72
-130
lines changed

.github/workflows/psalm.yml

Lines changed: 0 additions & 33 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ In `app\Console\Kernel.php` you should schedule the below to run every `x` minut
6969
// in app/console/Kernel.php
7070
protected function schedule(Schedule $schedule)
7171
{
72-
$schedule->command(Fidum\VaporMetricsTile\Commands\FetchVaporCacheMetricsCommand::class)->everyThirtyMinutes();
73-
$schedule->command(Fidum\VaporMetricsTile\Commands\FetchVaporDatabaseMetricsCommand::class)->everyThirtyMinutes();
74-
$schedule->command(Fidum\VaporMetricsTile\Commands\FetchVaporEnvironmentMetricsCommand::class)->everyThirtyMinutes();
72+
$schedule->command(\Fidum\VaporMetricsTile\Commands\FetchVaporCacheMetricsCommand::class)->everyThirtyMinutes();
73+
$schedule->command(\Fidum\VaporMetricsTile\Commands\FetchVaporDatabaseMetricsCommand::class)->everyThirtyMinutes();
74+
$schedule->command(\Fidum\VaporMetricsTile\Commands\FetchVaporEnvironmentMetricsCommand::class)->everyThirtyMinutes();
7575
}
7676
```
7777

composer.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919
],
2020
"require": {
2121
"php": "^7.4|^8.0",
22-
"fidum/laravel-dashboard-chart-tile": "^4.1.0",
23-
"laravel/vapor-cli": "^1.36",
22+
"fidum/laravel-dashboard-chart-tile": "^5.0",
23+
"laravel/vapor-cli": "^1.43",
2424
"spatie/laravel-dashboard": "^2.1"
2525
},
2626
"require-dev": {
2727
"mockery/mockery": "^1.5",
2828
"nunomaduro/laravel-mojito": "^0.2.10",
2929
"orchestra/testbench": "^6.0|^7.0",
30-
"phpunit/phpunit": "^9.5",
31-
"vimeo/psalm": "^4.20"
30+
"phpunit/phpunit": "^9.5"
3231
},
3332
"autoload": {
3433
"psr-4": {
@@ -41,7 +40,6 @@
4140
}
4241
},
4342
"scripts": {
44-
"psalm": "vendor/bin/psalm",
4543
"test": "vendor/bin/phpunit"
4644
},
4745
"config": {

psalm.xml

Lines changed: 0 additions & 24 deletions
This file was deleted.

resources/views/environment/chart.blade.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
@php /** @var \Fidum\VaporMetricsTile\Charts\BarChart $chart */ @endphp
2+
13
@livewire('chart-tile', [
2-
'chartClass' => \Fidum\VaporMetricsTile\Charts\BarChart::class,
4+
'chartFactory' => \Fidum\VaporMetricsTile\Charts\BarChart::class,
35
'chartFilters' => compact('tileName', 'type'),
46
'height' => $height,
57
'position' => $position,

src/Charts/BarChart.php

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,64 @@
22

33
namespace Fidum\VaporMetricsTile\Charts;
44

5-
use Chartisan\PHP\Chartisan;
65
use Fidum\ChartTile\Charts\Chart;
6+
use Fidum\ChartTile\Contracts\ChartFactory;
77
use Fidum\VaporMetricsTile\Stores\VaporEnvironmentMetricsStore;
88
use Fidum\VaporMetricsTile\VaporMetricsClient;
9-
use Illuminate\Http\Request;
109
use Illuminate\Support\Arr;
1110
use Illuminate\Support\Str;
1211

13-
class BarChart extends Chart
12+
class BarChart implements ChartFactory
1413
{
15-
public ?string $name = 'vapor_environment_metrics_chart';
14+
private string $tileName;
15+
private string $chartType;
1616

17-
public function handler(Request $request): Chartisan
17+
public function __construct(string $tileName, string $chartType)
1818
{
19-
$tileName = $this->getTileName($request);
20-
$type = $request->get('type') ?: ChartType::DEFAULT;
19+
$this->tileName = $tileName;
20+
$this->chartType = $chartType;
21+
}
22+
23+
public static function make(array $settings): ChartFactory
24+
{
25+
$defaultTile = Arr::get(array_keys(config('dashboard.tiles.vapor_metrics.environments', [])), 0, '');
26+
27+
return new static(
28+
Arr::get($settings, 'tileName', $defaultTile),
29+
Arr::get($settings, 'type', ChartType::DEFAULT)
30+
);
31+
}
32+
33+
public function chart(): Chart
34+
{
35+
$chart = new Chart();
2136

22-
$key = VaporEnvironmentMetricsStore::key($tileName);
37+
$key = VaporEnvironmentMetricsStore::key($this->tileName);
2338
$metrics = VaporEnvironmentMetricsStore::make()->metrics($key);
24-
$field = ChartType::field($type);
39+
$field = ChartType::field($this->chartType);
2540
$data = collect($metrics[$field] ?? []);
2641

2742
$dataset = $data->map(fn ($metric, $date) => [
2843
'x' => $date,
2944
'y' => number_format($metric, 0, '.', ''),
3045
])->values();
3146

32-
return Chartisan::build()
33-
->labels($data->keys()->toArray())
34-
->dataset(ChartType::label($tileName, $type), $dataset->toArray());
35-
}
47+
$chart
48+
->loader(false)
49+
->labels($data->keys())
50+
->options($this->options(), true)
51+
->dataset(ChartType::label($this->tileName, $this->chartType), 'bar', $dataset)
52+
->backgroundColor('#848584');
3653

37-
public function type(): string
38-
{
39-
return 'bar';
54+
return $chart;
4055
}
4156

42-
public function colors(): array
43-
{
44-
return ['#848584'];
45-
}
46-
47-
public function options(): array
57+
private function options(): array
4858
{
4959
return [
60+
'animation' => [
61+
'duration' => 0,
62+
],
5063
'responsive' => true,
5164
'maintainAspectRatio' => false,
5265
'legend' => [
@@ -82,8 +95,7 @@ public function options(): array
8295

8396
private function period(): string
8497
{
85-
$tileName = $this->getTileName(app(Request::class));
86-
$tileConfig = config('dashboard.tiles.vapor_metrics.environments.'.$tileName) ?? [];
98+
$tileConfig = config('dashboard.tiles.vapor_metrics.environments.'.$this->tileName) ?? [];
8799

88100
return $tileConfig['period']
89101
?? config('dashboard.tiles.vapor_metrics.period')
@@ -107,11 +119,4 @@ private function unit(): string
107119

108120
return $availableUnits[$unit] ?? 'hour';
109121
}
110-
111-
private function getTileName(Request $request): string
112-
{
113-
$defaultTile = Arr::get(array_keys(config('dashboard.tiles.vapor_metrics.environments', [])), 0, '');
114-
115-
return $request->get('tileName') ?: $defaultTile;
116-
}
117122
}

src/Components/VaporEnvironmentMetricsChartComponent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class VaporEnvironmentMetricsChartComponent extends Component
1616

1717
public int $refreshIntervalInSeconds;
1818

19-
public ?string $tileName;
19+
public string $tileName;
2020

21-
public ?string $type;
21+
public string $type;
2222

2323
public function mount(
2424
string $position = '',

src/VaporMetricsTileServiceProvider.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Fidum\VaporMetricsTile;
44

5-
use ConsoleTVs\Charts\Registrar;
6-
use Fidum\VaporMetricsTile\Charts\BarChart;
75
use Fidum\VaporMetricsTile\Commands\FetchVaporCacheMetricsCommand;
86
use Fidum\VaporMetricsTile\Commands\FetchVaporDatabaseMetricsCommand;
97
use Fidum\VaporMetricsTile\Commands\FetchVaporEnvironmentMetricsCommand;
@@ -18,10 +16,6 @@ class VaporMetricsTileServiceProvider extends ServiceProvider
1816
{
1917
public function boot(): void
2018
{
21-
app(Registrar::class)->register([
22-
BarChart::class,
23-
]);
24-
2519
Livewire::component('vapor-environment-metrics-tile', VaporEnvironmentMetricsComponent::class);
2620
Livewire::component('vapor-environment-metrics-chart-tile', VaporEnvironmentMetricsChartComponent::class);
2721
Livewire::component('vapor-cache-metrics-tile', VaporCacheMetricsComponent::class);

tests/Unit/Charts/BarChartTest.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@
77
use Fidum\VaporMetricsTile\Stores\VaporEnvironmentMetricsStore;
88
use Fidum\VaporMetricsTile\Tests\TestCase;
99
use Illuminate\Support\Collection;
10-
use Illuminate\Support\Facades\Request;
1110

1211
class BarChartTest extends TestCase
1312
{
1413
public function testChartEmptyData()
1514
{
16-
$chart = app(BarChart::class);
17-
$chartisan = $chart->handler(Request::merge([]));
18-
$object = $chartisan->toObject();
15+
$factory = BarChart::make([]);
16+
$chart = $factory->chart();
1917

20-
$this->assertSame([], $object->chart->labels);
21-
$this->assertSame($this->expectedOptions('hour'), $chart->options());
22-
$this->assertSame([], $object->datasets[0]->values);
18+
$this->assertSame([], $chart->labels);
19+
$this->assertSame($this->expectedOptions('hour'), $chart->options);
20+
$this->assertSame([], $chart->datasets[0]->values);
2321
}
2422

2523
public function testChartDefaults()
@@ -30,16 +28,15 @@ public function testChartDefaults()
3028
'averageFunctionDurationByInterval' => $data->toArray(),
3129
]);
3230

33-
$chart = app(BarChart::class);
34-
$chartisan = $chart->handler(Request::merge([]));
35-
$object = $chartisan->toObject();
31+
$factory = BarChart::make([]);
32+
$chart = $factory->chart();
3633

37-
$this->assertSame($data->keys()->toArray(), $object->chart->labels);
38-
$this->assertSame($this->expectedOptions('hour'), $chart->options());
34+
$this->assertSame($data->keys()->toArray(), $chart->labels);
35+
$this->assertSame($this->expectedOptions('hour'), $chart->options);
3936

4037
$this->assertSame(
4138
$data->map(fn ($y, $x) => compact('x', 'y'))->values()->toArray(),
42-
$object->datasets[0]->values,
39+
$chart->datasets[0]->values,
4340
);
4441
}
4542

@@ -51,16 +48,15 @@ public function testChartWithSettings()
5148
'totalCliFunctionInvocationsByInterval' => $data->toArray(),
5249
]);
5350

54-
$chart = app(BarChart::class);
55-
$chartisan = $chart->handler(Request::merge(['tileName' => 'My Env Changed', 'type' => 'cli-invocations-total']));
56-
$object = $chartisan->toObject();
51+
$factory = BarChart::make(['tileName' => 'My Env Changed', 'type' => 'cli-invocations-total']);
52+
$chart = $factory->chart();
5753

58-
$this->assertSame($data->keys()->toArray(), $object->chart->labels);
59-
$this->assertSame($this->expectedOptions('day'), $chart->options());
54+
$this->assertSame($data->keys()->toArray(), $chart->labels);
55+
$this->assertSame($this->expectedOptions('day'), $chart->options);
6056

6157
$this->assertSame(
6258
$data->map(fn ($y, $x) => compact('x', 'y'))->values()->toArray(),
63-
$object->datasets[0]->values,
59+
$chart->datasets[0]->values,
6460
);
6561
}
6662

@@ -69,23 +65,24 @@ public function testUnit(string $period, string $expectedUnit)
6965
{
7066
config()->set('dashboard.tiles.vapor_metrics.period', $period);
7167

72-
$chart = app(BarChart::class);
68+
$factory = BarChart::make([]);
69+
$chart = $factory->chart();
7370

74-
$this->assertSame($this->expectedOptions($expectedUnit), $chart->options());
71+
$this->assertSame($this->expectedOptions($expectedUnit), $chart->options);
7572
}
7673

7774
public function unitProvider(): array
7875
{
7976
//1m, 5m, 30m, 1h, 8h, 1d (default), 3d, 7d, 1M
8077
return [
8178
['1m', 'second'],
82-
['12m', 'minute'],
79+
['5m', 'minute'],
8380
['1h', 'minute'],
84-
['12h', 'hour'],
81+
['8h', 'hour'],
8582
['1d', 'hour'],
86-
['12d', 'day'],
83+
['7d', 'day'],
8784
['1M', 'day'],
88-
['12M', 'week'],
85+
['2M', 'week'],
8986
];
9087
}
9188

@@ -106,6 +103,9 @@ private function data(): Collection
106103
private function expectedOptions(string $unit): array
107104
{
108105
return [
106+
'animation' => [
107+
'duration' => 0,
108+
],
109109
'responsive' => true,
110110
'maintainAspectRatio' => false,
111111
'legend' => [

0 commit comments

Comments
 (0)