@@ -16,13 +16,13 @@ class PropertyAggregate extends BaseCommand
16
16
{
17
17
use AggregationArgument, BuildPeriod, DateArgument, GatherModels, HasVerbose, PrepareMetricsData, SendMetricsData;
18
18
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} ' ;
20
20
21
21
protected $ description = 'Perform a sum of a property of a model for a given date and event. ' ;
22
22
23
23
protected $ verbose = false ;
24
24
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 )
26
26
{
27
27
$ metrics = [];
28
28
$ modelClass = $ this ->getModelClass ($ model , $ modelsPath );
@@ -37,11 +37,36 @@ public function perform(string $model, AggregationType $aggregation, string $pro
37
37
try {
38
38
$ method = $ aggregation ->value ;
39
39
// 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 ;
41
55
//echo "handle\n ";
42
56
//echo "The ".$method." of ".$model."->".$property." is : ".$count;
43
57
$ 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
+
45
70
$ metrics [] = (object ) ['label ' => $ label , 'count ' => $ count ];
46
71
} catch (\Exception $ e ) {
47
72
$ this ->verbose ('An error occurred: ' .$ e ->getMessage (), 'error ' );
@@ -64,9 +89,20 @@ public function handle()
64
89
$ periodType = $ this ->option ('periodType ' ) ?? 'daily ' ;
65
90
$ dateFormat = $ this ->option ('dateFormat ' ) ?? $ this ->defaultDateFormat ;
66
91
$ 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
+
67
101
$ 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 );
69
103
$ metricsData = $ this ->prepareMetricsData ($ metrics , $ period , $ event );
104
+
105
+
70
106
$ this ->sendMetricsData ($ metricsData , env ('ELOQUENTIZE_API_TOKEN ' ));
71
107
72
108
return 0 ;
0 commit comments