|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Eloquentize\LaravelClient\Commands; |
| 4 | + |
| 5 | +use Carbon\CarbonPeriod; |
| 6 | +use Eloquentize\LaravelClient\Commands\Enums\AggregationType; |
| 7 | +use Eloquentize\LaravelClient\Commands\Traits\AggregationArgument; |
| 8 | +use Eloquentize\LaravelClient\Commands\Traits\BuildPeriod; |
| 9 | +use Eloquentize\LaravelClient\Commands\Traits\DateArgument; |
| 10 | +use Eloquentize\LaravelClient\Commands\Traits\GatherModels; |
| 11 | +use Eloquentize\LaravelClient\Commands\Traits\HasVerbose; |
| 12 | +use Eloquentize\LaravelClient\Commands\Traits\PrepareMetricsData; |
| 13 | +use Eloquentize\LaravelClient\Commands\Traits\SendMetricsData; |
| 14 | +use Illuminate\Support\Carbon; |
| 15 | + |
| 16 | +class PropertyAggregateOverall extends BaseCommand |
| 17 | +{ |
| 18 | + use AggregationArgument, BuildPeriod, DateArgument, GatherModels, HasVerbose, PrepareMetricsData, SendMetricsData; |
| 19 | + |
| 20 | + protected $signature = 'eloquentize:property-aggregate-overall {model} {property} {aggregation} {--modelsPath=} {--scope=} {--scopeValue=} {--dry}'; |
| 21 | + |
| 22 | + protected $description = 'Perform a sum of a property of a model for all time.'; |
| 23 | + |
| 24 | + protected $verbose = false; |
| 25 | + |
| 26 | + public function perform(string $model, AggregationType $aggregation, string $property, $modelsPath = null, ?string $scope = null, ?string $scopeValue = null) |
| 27 | + { |
| 28 | + $metrics = []; |
| 29 | + $modelClass = $this->getModelClass($model, $modelsPath); |
| 30 | + |
| 31 | + if (! $this->isModelValid($modelClass, $property)) { |
| 32 | + return 1; |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + $method = $aggregation->value; |
| 37 | + // sound avg / min / max / sum return 0 if no records found ? for now |
| 38 | + $query = $modelClass::query(); |
| 39 | + |
| 40 | + if ($scope) { |
| 41 | + if (method_exists($modelClass, 'scope'.$scope)) { |
| 42 | + if ($scope && $scopeValue) { |
| 43 | + $query = $query->$scope($scopeValue); |
| 44 | + } elseif ($scope) { |
| 45 | + $query = $query->$scope(); |
| 46 | + } |
| 47 | + } else { |
| 48 | + $this->line("Scope $scope does not exist on model $model"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + $count = $query->$method($property) ?? 0; |
| 53 | + $this->verbose('The '.$method.' of '.$model.'->'.$property.' overall is : '.$count); |
| 54 | + |
| 55 | + $label = 'Overall ' .$model; |
| 56 | + if ($scope && $scopeValue) { |
| 57 | + $label .= '::'.$scope.'('.$scopeValue.')'; |
| 58 | + } elseif ($scope) { |
| 59 | + $label .= '::'.$scope; |
| 60 | + } |
| 61 | + $label .= '::'.$property.'->'.$method.'()'; |
| 62 | + |
| 63 | + $metrics[] = (object) ['label' => $label, 'count' => $count]; |
| 64 | + } catch (\Exception $e) { |
| 65 | + $this->verbose('An error occurred: '.$e->getMessage(), 'error'); |
| 66 | + |
| 67 | + return 1; |
| 68 | + } |
| 69 | + |
| 70 | + return $metrics; |
| 71 | + } |
| 72 | + |
| 73 | + public function handle() |
| 74 | + { |
| 75 | + |
| 76 | + $this->verbose = $this->option('verbose') ?? false; |
| 77 | + $model = $this->argument('model'); |
| 78 | + $aggregation = $this->resolveAggregation($this->argument('aggregation')); |
| 79 | + $property = $this->argument('property'); |
| 80 | + $modelsPath = $this->option('modelsPath'); |
| 81 | + $scope = $this->option('scope'); |
| 82 | + $scopeValue = $this->option('scopeValue'); |
| 83 | + $oldestDate = $this->getOldestDateFromModel($model, $modelsPath); |
| 84 | + |
| 85 | + if ($scopeValue && ! $scope) { |
| 86 | + $this->error('"--scopeValue" option requires "--scope" option to be set.'); |
| 87 | + |
| 88 | + return 1; |
| 89 | + } |
| 90 | + |
| 91 | + $metrics = $this->perform($model, $aggregation, $property, $modelsPath, $scope, $scopeValue); |
| 92 | + $period = new CarbonPeriod($oldestDate, Carbon::now()->endOfDay()); |
| 93 | + $metricsData = $this->prepareMetricsData($metrics, $period, 'overall'); |
| 94 | + |
| 95 | + $this->sendMetricsData($metricsData, env('ELOQUENTIZE_API_TOKEN')); |
| 96 | + |
| 97 | + return 0; |
| 98 | + |
| 99 | + } |
| 100 | +} |
0 commit comments