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