insenseanalytics /
nova-bar-metrics
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Insenseanalytics\NovaBarMetrics; |
||
| 4 | |||
| 5 | use Illuminate\Support\Facades\DB; |
||
| 6 | use Illuminate\Database\Eloquent\Builder; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Use only with partition metrics or metrics derived from the Partition class |
||
| 10 | * like BarChartMetric. |
||
| 11 | */ |
||
| 12 | trait HasFrequencyDistributions |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Return a partition/derived result showing the segments of a frequency range. |
||
| 16 | * |
||
| 17 | * @param \Illuminate\Http\Request $request |
||
| 18 | * @param \Illuminate\Database\Eloquent\Builder|string $model |
||
| 19 | * @param string $column |
||
| 20 | * @param int $stepSize |
||
| 21 | * |
||
| 22 | * @return \Laravel\Nova\Metrics\PartitionResult |
||
| 23 | */ |
||
| 24 | public function distributions($request, $model, $column, $stepSize) |
||
|
0 ignored issues
–
show
|
|||
| 25 | { |
||
| 26 | $subQuery = $model instanceof Builder ? $model : (new $model())->newQuery(); |
||
| 27 | |||
| 28 | $expression = FrequencyDistributionExpressionFactory::make( |
||
| 29 | $subQuery, |
||
| 30 | $column, |
||
| 31 | $stepSize |
||
| 32 | )->getValue(); |
||
| 33 | |||
| 34 | $subQuery = $subQuery |
||
| 35 | ->select(DB::raw("{$expression['range']} as rng, |
||
| 36 | {$expression['minVal']} as minval, count(*) as aggregate")) |
||
| 37 | ->groupBy(DB::raw('1'), DB::raw('2')); |
||
| 38 | |||
| 39 | $query = DB::table(DB::raw("({$subQuery->toSql()}) as sub")) |
||
| 40 | ->mergeBindings($subQuery->getQuery()); |
||
| 41 | |||
| 42 | $results = $query |
||
| 43 | ->select('rng', 'aggregate', 'minval') |
||
| 44 | ->orderBy('minval', 'asc') |
||
| 45 | ->get(); |
||
| 46 | |||
| 47 | return $this->result($results->mapWithKeys(function ($result) use ($column) { |
||
|
0 ignored issues
–
show
|
|||
| 48 | return $this->formatAggregateResult($result, 'rng'); |
||
| 49 | })->all()); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Return a partition/derived result showing the segments of a frequency range. |
||
| 54 | * |
||
| 55 | * @param \Illuminate\Http\Request $request |
||
| 56 | * @param \Illuminate\Database\Eloquent\Builder|string $model |
||
| 57 | * @param string $column |
||
| 58 | * @param int $maxSteps |
||
| 59 | * |
||
| 60 | * @return \Laravel\Nova\Metrics\PartitionResult |
||
| 61 | */ |
||
| 62 | public function distributionsWithSteps($request, $model, $column, $maxSteps = 15) |
||
| 63 | { |
||
| 64 | $query = $model instanceof Builder ? $model : (new $model())->newQuery(); |
||
| 65 | |||
| 66 | $difference = $query->max($column) - $query->min($column); |
||
| 67 | |||
| 68 | if (!$difference) { |
||
| 69 | $stepSize = 1; |
||
| 70 | } else { |
||
| 71 | $stepSize = round($difference / $maxSteps, 0); |
||
| 72 | } |
||
| 73 | |||
| 74 | return $this->distributions($request, $query, $column, $stepSize); |
||
| 75 | } |
||
| 76 | } |
||
| 77 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.