1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AshPowell\APAnalytics; |
4
|
|
|
|
5
|
|
|
use App\User; |
|
|
|
|
6
|
|
|
use AshPowell\APAnalytics\Jobs\Track; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use MongoDB\Driver\Cursor; |
10
|
|
|
use MongoDB\Model\BSONDocument; |
11
|
|
|
|
12
|
|
|
class APAnalytics |
13
|
|
|
{ |
14
|
|
|
protected $connection; |
15
|
|
|
protected $namespace; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Instantiate a new instance. |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->connection = config('apanalytic.db_connection', 'mongodb'); |
25
|
|
|
$this->namespace = config('apanalytic.namespace', '\App\\'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Track the Analytic. |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
* @param mixed $collection |
33
|
|
|
* @param mixed $items |
34
|
|
|
* @param null|mixed $userId |
35
|
|
|
* @param mixed $params |
36
|
|
|
*/ |
37
|
|
|
public function track($collection, $items, $userId = null, $params = []) |
38
|
|
|
{ |
39
|
|
|
Track::dispatch($collection, $items, $userId, $params); |
40
|
|
|
|
41
|
|
|
return true; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the Analytics. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
* @param mixed $collection |
49
|
|
|
* @param null|mixed $timeframe |
50
|
|
|
* @param null|mixed $filters |
51
|
|
|
* @param mixed $interval |
52
|
|
|
*/ |
53
|
|
|
public function show($collection, $interval = 'count', $timeframe = null, $filters = null) |
54
|
|
|
{ |
55
|
|
|
$start = $timeframe ? array_get($timeframe, 'start') : null; |
56
|
|
|
$end = $timeframe ? array_get($timeframe, 'end') : null; |
57
|
|
|
$matchArray = []; |
58
|
|
|
$filters = json_decode($filters); |
59
|
|
|
$intervalFormat = '%Y-%m-%dT%H'; |
60
|
|
|
$aggregate = []; |
61
|
|
|
|
62
|
|
|
$model = $this->namespace.studly_case(str_singular($collection)).'Analytic'; |
63
|
|
|
|
64
|
|
|
if (! class_exists($model)) { |
65
|
|
|
throw new InvalidArgumentException("Model {$model} does not exist."); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($filters) { |
69
|
|
|
foreach ($filters as $filter) { |
70
|
|
|
$propertyValue = $filter->property_value; |
71
|
|
|
|
72
|
|
|
if (is_numeric($propertyValue)) { |
73
|
|
|
$propertyValue = (int) $propertyValue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$matchArray = array_merge($matchArray, [$filter->property_name => $propertyValue]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
abort_unless($this->canViewAnalytic($matchArray, auth()->user()), 403, 'You dont have permission to view these analytics'); |
81
|
|
|
|
82
|
|
|
if ($start) { |
83
|
|
|
$matchArray['created_at']['$gte'] = mongoTime($start); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($end) { |
87
|
|
|
$matchArray['created_at']['$lt'] = mongoTime($end); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($matchArray) { |
91
|
|
|
$aggregate[] = ['$match' => $matchArray]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($interval != 'count') { |
95
|
|
|
if ($interval == 'daily') { |
96
|
|
|
$intervalFormat = '%Y-%m-%d'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($interval == 'monthly') { |
100
|
|
|
$intervalFormat = '%Y-%m'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$aggregate[] = [ |
104
|
|
|
'$group' => [ |
105
|
|
|
'_id' => [ |
106
|
|
|
'$dateToString' => ['date' => '$created_at', 'format' => $intervalFormat] |
107
|
|
|
], |
108
|
|
|
'count' => [ |
109
|
|
|
'$sum' => 1 |
110
|
|
|
], |
111
|
|
|
'created_at' => [ |
112
|
|
|
'$first' => '$created_at' |
113
|
|
|
] |
114
|
|
|
], |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
$aggregate[] = ['$sort' => ['created_at' => 1]]; |
118
|
|
|
|
119
|
|
|
$aggregate[] = [ |
120
|
|
|
'$project' => [ |
121
|
|
|
'_id' => 0, |
122
|
|
|
'created_at' => 1, |
123
|
|
|
'count' => 1 |
124
|
|
|
], |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$data = $model::raw(function ($collection) use ($matchArray, $interval, $aggregate) { |
129
|
|
|
if ($interval == 'count') { |
130
|
|
|
return $collection->count($matchArray); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($aggregate) { |
134
|
|
|
return $collection->aggregate($aggregate, ['allowDiskUse' => true]); |
135
|
|
|
} |
136
|
|
|
}); |
137
|
|
|
|
138
|
|
|
return $data; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Convert the Cursor to Laravel Models. |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
* @param mixed $data |
146
|
|
|
* @param null|mixed $model |
147
|
|
|
*/ |
148
|
|
|
private function toModels($data, $model = null) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
if (! $model) { |
151
|
|
|
$model = '\Jenssegers\Mongodb\Eloquent\Model'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (! class_exists($model)) { |
155
|
|
|
throw new InvalidArgumentException("Model {$model} does not exist."); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ($data instanceof Cursor) { |
159
|
|
|
// Convert MongoCursor results to a collection of models. |
160
|
|
|
$data = iterator_to_array($data, false); |
161
|
|
|
|
162
|
|
|
return $model::hydrate($data); |
163
|
|
|
} elseif ($data instanceof BSONDocument) { |
164
|
|
|
// Convert Mongo BSONDocument to a single object. |
165
|
|
|
$data = $data::getArrayCopy(); |
|
|
|
|
166
|
|
|
|
167
|
|
|
return $model::newFromBuilder((array) $data); |
168
|
|
|
} elseif (is_array($data) && array_key_exists('_id', $data)) { |
169
|
|
|
// The result is a single object. |
170
|
|
|
return $model::newFromBuilder((array) $data); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $data; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Check specified user has permission to see these analytics. |
178
|
|
|
* |
179
|
|
|
* @param array $filterArray |
180
|
|
|
* @param User $user |
181
|
|
|
* |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
|
|
private function canViewAnalytic($filterArray, User $user) |
185
|
|
|
{ |
186
|
|
|
$modelsToCheck = config('apanalytics.models_require_ownership'); |
187
|
|
|
|
188
|
|
|
if (count($modelsToCheck)) { |
189
|
|
|
foreach ($modelsToCheck as $model) { |
190
|
|
|
$modelName = studly_case(str_singular($model)); |
191
|
|
|
$modelId = array_get($filterArray, strtolower($modelName).'.id'); |
192
|
|
|
|
193
|
|
|
if ($modelId) { |
194
|
|
|
$modelClass = $this->namespace.$modelName; |
195
|
|
|
$model = $modelClass::find($modelId); |
196
|
|
|
|
197
|
|
|
if ($model && $user->isOwner($model)) { |
198
|
|
|
return true; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return true; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths