|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Database manager and query manager |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\AnalyticsBundle\Manager; |
|
7
|
|
|
|
|
8
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
9
|
|
|
use Graviton\AnalyticsBundle\Model\AnalyticModel; |
|
10
|
|
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Manager for data layer single responsibility |
|
14
|
|
|
* |
|
15
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
16
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
17
|
|
|
* @link http://swisscom.ch |
|
18
|
|
|
*/ |
|
19
|
|
|
class AnalyticsManager |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var DocumentManager */ |
|
22
|
|
|
protected $documentManager; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* AnalyticsManager constructor. |
|
26
|
|
|
* @param DocumentManager $documentManager Db manager and query control |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(DocumentManager $documentManager) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->documentManager = $documentManager; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Query db based on definition |
|
35
|
|
|
* Another option is to use: $collection->createAggregationBuilder(); |
|
36
|
|
|
* |
|
37
|
|
|
* @param AnalyticModel $model Definition |
|
38
|
|
|
* @return array|object |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getData(AnalyticModel $model) |
|
41
|
|
|
{ |
|
42
|
|
|
$conn = $this->documentManager->getConnection(); |
|
43
|
|
|
$db = $this->documentManager->getConfiguration()->getDefaultDB(); |
|
44
|
|
|
$collection = $conn->selectCollection($db, $model->getCollection()); |
|
45
|
|
|
|
|
46
|
|
|
// Build aggregation pipeline |
|
47
|
|
|
$pipeline = $this->buildPipeline($model); |
|
48
|
|
|
|
|
49
|
|
|
$iterator = $collection->aggregate($pipeline); |
|
50
|
|
|
if ('object' === $model->getType()) { |
|
51
|
|
|
return $iterator->getSingleResult(); |
|
52
|
|
|
} |
|
53
|
|
|
return $iterator->toArray(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Build a output Db Model aggregation pipeline array. |
|
58
|
|
|
* |
|
59
|
|
|
* @param AnalyticModel $model Object class |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
private function buildPipeline(AnalyticModel $model) |
|
63
|
|
|
{ |
|
64
|
|
|
$rtnPipeline = []; |
|
65
|
|
|
|
|
66
|
|
|
if ($pipeline = $model->getPipeline()) { |
|
67
|
|
|
foreach ($pipeline as $pipe) { |
|
68
|
|
|
foreach ($pipe as $op => $query) { |
|
69
|
|
|
$rtnPipeline[] = [ |
|
70
|
|
|
$op => $this->parseObjectDates($query) |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} elseif ($aggregate = $model->getAggregate()) { |
|
75
|
|
|
foreach ($aggregate as $op => $query) { |
|
76
|
|
|
$rtnPipeline[] = [ |
|
77
|
|
|
$op => $this->parseObjectDates($query) |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (empty($rtnPipeline)) { |
|
83
|
|
|
throw new InvalidArgumentException('Wrong configuration for Aggregation pipeline'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $rtnPipeline; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Enabling to possibility to create dtae queries |
|
91
|
|
|
* Will replace PARSE_DATE(date|format) |
|
92
|
|
|
* sample: PARSE_DATE(-4 years|Y) -> new DateTime(-4 years)->format(Y) -> 2013 |
|
93
|
|
|
* |
|
94
|
|
|
* @param object $query Aggregation query |
|
95
|
|
|
* @return object |
|
96
|
|
|
*/ |
|
97
|
|
|
private function parseObjectDates($query) |
|
98
|
|
|
{ |
|
99
|
|
|
$string = json_encode($query); |
|
100
|
|
|
preg_match_all('/PARSE_DATE\(([^\)]+)\)/', $string, $matches); |
|
101
|
|
|
if ($matches && array_key_exists(1, $matches) && is_array($matches[1])) { |
|
102
|
|
|
foreach ($matches[0] as $key => $value) { |
|
103
|
|
|
$formatting = explode('|', $matches[1][$key]); |
|
104
|
|
|
$date = new \DateTime($formatting[0]); |
|
105
|
|
|
$string = str_replace('"'.$value.'"', $date->format($formatting[1]), $string); |
|
106
|
|
|
} |
|
107
|
|
|
$query = json_decode($string); |
|
108
|
|
|
} |
|
109
|
|
|
return $query; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|