Skip to content

Commit d3b5aae

Browse files
committed
add scope for property aggregate
1 parent 2ef6d66 commit d3b5aae

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

database/factories/BillFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function definition()
1313
{
1414
return [
1515
'ref' => $this->faker->name,
16-
'price' => $this->faker->randomNumber(2),
16+
'price' => $this->faker->randomNumber(4),
1717
];
1818
}
1919
}

src/Commands/PropertyAggregate.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ class PropertyAggregate extends BaseCommand
1616
{
1717
use AggregationArgument, BuildPeriod, DateArgument, GatherModels, HasVerbose, PrepareMetricsData, SendMetricsData;
1818

19-
protected $signature = 'eloquentize:property-aggregate {model} {property} {aggregation} {date?} {--event=created_at} {--periodType=daily} {--dateFormat=} {--modelsPath=}';
19+
protected $signature = 'eloquentize:property-aggregate {model} {property} {aggregation} {date?} {--event=created_at} {--periodType=daily} {--dateFormat=} {--modelsPath=} {--scope=} {--scopeValue=} {--dry}';
2020

2121
protected $description = 'Perform a sum of a property of a model for a given date and event.';
2222

2323
protected $verbose = false;
2424

25-
public function perform(string $model, AggregationType $aggregation, string $property, CarbonPeriod $period, string $event, $modelsPath = null)
25+
public function perform(string $model, AggregationType $aggregation, string $property, CarbonPeriod $period, string $event, $modelsPath = null, ?string $scope = null, ?string $scopeValue = null)
2626
{
2727
$metrics = [];
2828
$modelClass = $this->getModelClass($model, $modelsPath);
@@ -37,11 +37,36 @@ public function perform(string $model, AggregationType $aggregation, string $pro
3737
try {
3838
$method = $aggregation->value;
3939
// sound avg / min / max / sum return 0 if no records found ? for now
40-
$count = $modelClass::whereBetween($event, [$period->getStartDate(), $period->getEndDate()])->$method($property) ?? 0;
40+
$query = $modelClass::whereBetween($event, [$period->getStartDate(), $period->getEndDate()]);
41+
42+
if ($scope) {
43+
if (method_exists($modelClass, 'scope'.$scope)) {
44+
if ($scope && $scopeValue) {
45+
$query = $query->$scope($scopeValue);
46+
} elseif ($scope) {
47+
$query = $query->$scope();
48+
}
49+
} else {
50+
$this->line("Scope $scope does not exist on model $model");
51+
}
52+
}
53+
54+
$count = $query->$method($property) ?? 0;
4155
//echo "handle\n ";
4256
//echo "The ".$method." of ".$model."->".$property." is : ".$count;
4357
$this->verbose('The '.$method.' of '.$model.'->'.$property.' is : '.$count);
44-
$label = $model.'::'.$property.'->'.$method.'()';
58+
if ($scope && $scopeValue) {
59+
//echo "-- WITH ".$scope."(".$scopeValue.")";
60+
}
61+
62+
$label = $model;
63+
if ($scope && $scopeValue) {
64+
$label .= '::'.$scope.'('.$scopeValue.')';
65+
} elseif ($scope) {
66+
$label .= '::'.$scope;
67+
}
68+
$label .= '::'.$property.'->'.$method.'()';
69+
4570
$metrics[] = (object) ['label' => $label, 'count' => $count];
4671
} catch (\Exception $e) {
4772
$this->verbose('An error occurred: '.$e->getMessage(), 'error');
@@ -64,9 +89,20 @@ public function handle()
6489
$periodType = $this->option('periodType') ?? 'daily';
6590
$dateFormat = $this->option('dateFormat') ?? $this->defaultDateFormat;
6691
$modelsPath = $this->option('modelsPath');
92+
$scope = $this->option('scope');
93+
$scopeValue = $this->option('scopeValue');
94+
95+
if ($scopeValue && ! $scope) {
96+
$this->error('"--scopeValue" option requires "--scope" option to be set.');
97+
98+
return 1;
99+
}
100+
67101
$period = $this->buildPeriod($date, $periodType, $dateFormat);
68-
$metrics = $this->perform($model, $aggregation, $property, $period, $event, $modelsPath);
102+
$metrics = $this->perform($model, $aggregation, $property, $period, $event, $modelsPath, $scope, $scopeValue);
69103
$metricsData = $this->prepareMetricsData($metrics, $period, $event);
104+
105+
70106
$this->sendMetricsData($metricsData, env('ELOQUENTIZE_API_TOKEN'));
71107

72108
return 0;

src/Commands/PropertyAggregateLegacy.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class PropertyAggregateLegacy extends BaseCommand
1515
{
1616
use AggregationArgument, BuildPeriod, GatherModels, HasVerbose, ModelsOption;
1717

18-
protected $signature = 'eloquentize:property-aggregate-legacy {model} {property} {aggregation} {date?} {event?} {--periodType=daily} {--dateFormat=} {--modelsPath=}';
18+
protected $signature = 'eloquentize:property-aggregate-legacy {model} {property} {aggregation} {date?} {event?} {--periodType=daily} {--dateFormat=} {--modelsPath=} {--scope=} {--scopeValue=}';
1919

2020
protected $description = 'Send to Eloquentize the aggregation of a model property from a given date or from the oldest eloquent model created_at to yesterday';
2121

@@ -69,6 +69,8 @@ public function handle()
6969
'--event' => $event,
7070
'--dateFormat' => $dateFormat,
7171
'--modelsPath' => $modelsPath,
72+
'--scope' => $this->option('scope'),
73+
'--scopeValue' => $this->option('scopeValue'),
7274
]);
7375

7476
// Advance the progress bar after processing each date
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use App\Testing\Models\Bill;
4+
use Illuminate\Console\Command;
5+
use Illuminate\Support\Facades\Artisan;
6+
use Illuminate\Support\Facades\Http;
7+
8+
// it('ensure property-aggregate sum is callable with scope priceover 500', function () {
9+
// Http::fake([
10+
// config('eloquentize.api_url').'/api/metrics/models' => Http::response(['status' => 'ok'], 200),
11+
// ]);
12+
13+
// $this->artisan('eloquentize:property-aggregate Bill price sum --modelsPath=Testing/Models -v --scope=priceOver --scopeValue=500')
14+
// ->assertExitCode(Command::SUCCESS);
15+
16+
// })->with([
17+
// fn () => Bill::factory()->create(['ref' => 'BILL_0000001', 'price' => 1000]),
18+
// //fn () => Bill::factory()->create(['ref' => 'BILL_0000002', 'price' => 500]),
19+
// ]);
20+
21+
it('ensure property-aggregate-legacy sum is callable with scope priceover 500', function () {
22+
Http::fake([
23+
config('eloquentize.api_url').'/api/metrics/models' => Http::response(['status' => 'ok'], 200),
24+
]);
25+
26+
$this->artisan('eloquentize:property-aggregate-legacy Bill price sum 13/05/2024 --modelsPath=Testing/Models -v --scope=priceOver --scopeValue=500 ')
27+
->assertExitCode(Command::SUCCESS);
28+
29+
})->with([
30+
fn () => Bill::factory()->create(['ref' => 'BILL_0000001', 'price' => 1000]),
31+
//fn () => Bill::factory()->create(['ref' => 'BILL_0000002', 'price' => 500]),
32+
]);
33+
34+
// it('ensure property-aggregate sum return an error if Model do not exists', function () {
35+
// Http::fake([
36+
// config('eloquentize.api_url').'/api/metrics/models' => Http::response(['status' => 'ok'], 200),
37+
// ]);
38+
39+
// $this->artisan('eloquentize:property-aggregate BillyBu sum price 01/02/2024 --modelsPath=Testing/Models -v ')
40+
// ->assertExitCode(Command::SUCCESS);
41+
42+
// })->with([
43+
// fn() => Bill::factory()->create(['ref' => 'BILL_0000001', 'price' => 1000]),
44+
// ])->only();

0 commit comments

Comments
 (0)