1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelMetrics\Metrics; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelMetrics\Metrics\Concerns\HasExpressions; |
8
|
|
|
use Arcanedev\LaravelMetrics\Results\PartitionResult; |
9
|
|
|
use Illuminate\Support\Facades\DB; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class NullablePartition |
13
|
|
|
* |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
abstract class NullablePartition extends Metric |
17
|
|
|
{ |
18
|
|
|
/* ----------------------------------------------------------------- |
19
|
|
|
| Traits |
20
|
|
|
| ----------------------------------------------------------------- |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
use HasExpressions; |
24
|
|
|
|
25
|
|
|
/* ----------------------------------------------------------------- |
26
|
|
|
| Getters |
27
|
|
|
| ----------------------------------------------------------------- |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the metric's type. |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
4 |
|
public function type(): string |
36
|
|
|
{ |
37
|
4 |
|
return 'partition'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/* ----------------------------------------------------------------- |
41
|
|
|
| Main Methods |
42
|
|
|
| ----------------------------------------------------------------- |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Calculate the `count` of the metric. |
47
|
|
|
* |
48
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
49
|
|
|
* @param string $groupBy |
50
|
|
|
* @param string|null $column |
51
|
|
|
* |
52
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\PartitionResult|mixed |
53
|
|
|
*/ |
54
|
4 |
|
protected function count($model, string $groupBy, $column = null) |
55
|
|
|
{ |
56
|
4 |
|
return $this->aggregate('count', $model, $column, $groupBy); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/* ----------------------------------------------------------------- |
60
|
|
|
| Other Methods |
61
|
|
|
| ----------------------------------------------------------------- |
62
|
|
|
*/ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Handle the aggregate calculation of the metric. |
66
|
|
|
* |
67
|
|
|
* @param string $method |
68
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|string $model |
69
|
|
|
* @param string $column |
70
|
|
|
* @param string $groupBy |
71
|
|
|
* |
72
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\PartitionResult|mixed |
73
|
|
|
*/ |
74
|
4 |
|
protected function aggregate(string $method, $model, ?string $column, string $groupBy) |
75
|
|
|
{ |
76
|
4 |
|
$query = static::getQuery($model); |
77
|
4 |
|
$wrappedColumn = $query->getQuery()->getGrammar()->wrap( |
78
|
4 |
|
$column ?? $query->getModel()->getQualifiedKeyName() |
|
|
|
|
79
|
|
|
); |
80
|
|
|
|
81
|
4 |
|
$groupAlias = "{$groupBy}_count"; |
82
|
4 |
|
$expression = static::getExpression($query, 'if_null', $groupBy); |
83
|
|
|
|
84
|
4 |
|
$value = $query->select([ |
|
|
|
|
85
|
4 |
|
DB::raw("{$expression} as {$groupAlias}"), |
86
|
4 |
|
DB::raw("{$method}({$wrappedColumn}) as aggregate"), |
87
|
|
|
]) |
88
|
4 |
|
->groupBy($groupAlias) |
89
|
4 |
|
->get() |
90
|
4 |
|
->mapWithKeys(function ($result) use ($groupAlias) { |
91
|
4 |
|
return $this->formatAggregateResult($result, $groupAlias); |
92
|
4 |
|
}) |
93
|
4 |
|
->all(); |
94
|
|
|
|
95
|
4 |
|
return $this->result($value); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Format the aggregate result for the partition. |
100
|
|
|
* |
101
|
|
|
* @param \Illuminate\Database\Eloquent\Model $result |
102
|
|
|
* @param string $groupBy |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
4 |
|
protected function formatAggregateResult($result, $groupBy): array |
107
|
|
|
{ |
108
|
4 |
|
$key = $result->getAttribute(last(explode('.', $groupBy))); |
109
|
|
|
|
110
|
|
|
return [ |
111
|
4 |
|
$key => $result->getAttribute('aggregate'), |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Make a new result instance. |
117
|
|
|
* |
118
|
|
|
* @param mixed|null $value |
119
|
|
|
* |
120
|
|
|
* @return \Arcanedev\LaravelMetrics\Results\PartitionResult|mixed |
121
|
|
|
*/ |
122
|
4 |
|
protected function result($value = null) |
123
|
|
|
{ |
124
|
4 |
|
return new PartitionResult($value); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: