|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Facile\MongoDbBundle\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Facile\MongoDbBundle\DataCollector\MongoQuerySerializer; |
|
8
|
|
|
use MongoDB\BSON\UTCDateTime; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
13
|
|
|
|
|
14
|
|
|
class ProfilerController implements ContainerAwareInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var Container */ |
|
17
|
|
|
private $container; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Sets the container. |
|
21
|
|
|
* |
|
22
|
2 |
|
* @param ContainerInterface|null $container A ContainerInterface instance or null |
|
23
|
|
|
*/ |
|
24
|
2 |
|
public function setContainer(ContainerInterface $container = null) |
|
25
|
2 |
|
{ |
|
26
|
|
|
$this->container = $container; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param $token |
|
31
|
|
|
* @param $queryNumber |
|
32
|
|
|
* |
|
33
|
|
|
* @throws \Exception |
|
34
|
|
|
* |
|
35
|
2 |
|
* @return JsonResponse |
|
36
|
|
|
*/ |
|
37
|
|
|
public function explainAction($token, $queryNumber) |
|
38
|
2 |
|
{ |
|
39
|
2 |
|
/** @var $profiler \Symfony\Component\HttpKernel\Profiler\Profiler */ |
|
40
|
|
|
$profiler = $this->container->get('profiler'); |
|
41
|
2 |
|
$profiler->disable(); |
|
42
|
2 |
|
|
|
43
|
|
|
$profile = $profiler->loadProfile($token); |
|
44
|
2 |
|
$queries = $profile->getCollector('mongodb')->getQueries(); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
2 |
|
$query = $queries[$queryNumber]; |
|
47
|
|
|
|
|
48
|
2 |
|
$query->setFilters($this->walkAndConvertToUTCDatetime($query->getFilters())); |
|
49
|
|
|
|
|
50
|
|
|
$service = $this->container->get('mongo.explain_query_service'); |
|
51
|
2 |
|
|
|
52
|
1 |
|
try { |
|
53
|
1 |
|
$result = $service->execute($query); |
|
54
|
1 |
|
} catch (\InvalidArgumentException $e) { |
|
55
|
|
|
return new JsonResponse([ |
|
56
|
|
|
'err' => $e->getMessage(), |
|
57
|
|
|
]); |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
return new JsonResponse(MongoQuerySerializer::prepareItemData($result->toArray())); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $data |
|
65
|
|
|
* |
|
66
|
2 |
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
2 |
|
private function walkAndConvertToUTCDatetime($data) |
|
69
|
1 |
|
{ |
|
70
|
|
|
if (! \is_array($data)) { |
|
|
|
|
|
|
71
|
|
|
return $data; |
|
72
|
2 |
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
foreach ($data as $key => $item) { |
|
75
|
|
|
if (\is_string($item) && 0 === strpos($item, 'ISODate')) { |
|
76
|
|
|
$time = str_replace(['ISODate("', '")'], '', $item); |
|
77
|
|
|
$dateTime = new \DateTime($time); |
|
78
|
|
|
$item = new UTCDatetime($dateTime->getTimestamp() * 1000); |
|
79
|
|
|
} |
|
80
|
1 |
|
|
|
81
|
|
|
$data[$key] = $this->walkAndConvertToUTCDatetime($item); |
|
82
|
|
|
} |
|
83
|
2 |
|
|
|
84
|
|
|
return $data; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|