Skip to content

Commit b46de82

Browse files
committed
overrall for preperty agregate and count
1 parent e0e1fea commit b46de82

6 files changed

+218
-48
lines changed

src/Commands/ModelCountOverall.php

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

src/Commands/PropertyAggregate.php

-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ public function handle()
101101
$period = $this->buildPeriod($date, $periodType, $dateFormat);
102102
$metrics = $this->perform($model, $aggregation, $property, $period, $event, $modelsPath, $scope, $scopeValue);
103103
$metricsData = $this->prepareMetricsData($metrics, $period, $event);
104-
105104

106105
$this->sendMetricsData($metricsData, env('ELOQUENTIZE_API_TOKEN'));
107106

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

src/LaravelClientServiceProvider.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Eloquentize\LaravelClient;
44

5+
use Spatie\LaravelPackageTools\Package;
56
use Eloquentize\LaravelClient\Commands\ModelsCount;
7+
use Spatie\LaravelPackageTools\PackageServiceProvider;
8+
use Eloquentize\LaravelClient\Commands\ModelCountOverall;
69
use Eloquentize\LaravelClient\Commands\ModelsCountLegacy;
710
use Eloquentize\LaravelClient\Commands\PropertyAggregate;
811
use Eloquentize\LaravelClient\Commands\PropertyAggregateLegacy;
9-
use Spatie\LaravelPackageTools\Package;
10-
use Spatie\LaravelPackageTools\PackageServiceProvider;
12+
use Eloquentize\LaravelClient\Commands\PropertyAggregateOverall;
1113

1214
class LaravelClientServiceProvider extends PackageServiceProvider
1315
{
@@ -25,7 +27,9 @@ public function configurePackage(Package $package): void
2527
//->hasMigration('create_laravel-client_table')
2628
->hasCommand(ModelsCount::class)
2729
->hasCommand(ModelsCountLegacy::class)
30+
->hasCommand(ModelCountOverall::class)
2831
->hasCommand(PropertyAggregate::class)
29-
->hasCommand(PropertyAggregateLegacy::class);
32+
->hasCommand(PropertyAggregateLegacy::class)
33+
->hasCommand(PropertyAggregateOverall::class);
3034
}
3135
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use App\Testing\Models\Bill;
4+
use Illuminate\Console\Command;
5+
use Illuminate\Support\Facades\Http;
6+
7+
it('ensure property-aggregate-overall sum is callable', function () {
8+
Http::fake([
9+
config('eloquentize.api_url').'/api/metrics/models' => Http::response(['status' => 'ok'], 200),
10+
]);
11+
12+
$this->artisan('eloquentize:property-aggregate-overall Bill price sum --modelsPath=Testing/Models -v ')
13+
->assertExitCode(Command::SUCCESS);
14+
15+
})->with([
16+
fn () => Bill::factory()->create(['ref' => 'BILL_0000001', 'price' => 1000]),
17+
fn () => Bill::factory()->create(['ref' => 'BILL_0000001', 'price' => 500]),
18+
]);

tests/commands/RunPropertyAggregateTest.php

-44
This file was deleted.

0 commit comments

Comments
 (0)