Completed
Push — master ( b6846d...800a06 )
by Tobias
06:55
created

QueryLogger::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Neo4j\Neo4jBundle\Collector;
6
7
use GraphAware\Bolt\Result\Result;
8
use GraphAware\Common\Cypher\StatementInterface;
9
use GraphAware\Common\Result\StatementResult as StatementResultInterface;
10
use GraphAware\Common\Result\StatementStatisticsInterface;
11
use GraphAware\Neo4j\Client\Exception\Neo4jExceptionInterface;
12
13
/**
14
 * @author Xavier Coureau <[email protected]>
15
 */
16
class QueryLogger implements \Countable
17
{
18
    /**
19
     * @var int
20
     */
21
    private $nbQueries = 0;
22
23
    /**
24
     * @var array
25
     */
26
    private $statements = [];
27
28
    /**
29
     * @var array
30
     */
31
    private $statementsHash = [];
32
33
    /**
34
     * @param StatementInterface $statement
35
     */
36
    public function record(StatementInterface $statement)
37
    {
38
        $statementText = $statement->text();
39
        $statementParams = json_encode($statement->parameters());
40
        $tag = $statement->getTag() ?: -1;
41
42
        // Make sure we do not record the same statement twice
43
        if (isset($this->statementsHash[$statementText][$statementParams][$tag])) {
44
            return;
45
        }
46
47
        $idx = $this->nbQueries++;
48
        $this->statements[$idx] = [
49
            'start_time' => microtime(true) * 1000,
50
            'end_time' => microtime(true) * 1000, // same
51
            'nb_results' => 0,
52
            'query' => $statementText,
53
            'parameters' => $statementParams,
54
            'tag' => $statement->getTag(),
55
            'statistics' => [],
56
        ];
57
        $this->statementsHash[$statementText][$statementParams][$tag] = $idx;
58
    }
59
60
    /**
61
     * @param StatementResultInterface $statementResult
62
     */
63
    public function finish(StatementResultInterface $statementResult)
64
    {
65
        $scheme = 'Http';
66
        if ($statementResult instanceof Result) {
67
            $scheme = 'Bolt';
68
        }
69
70
        $statement = $statementResult->statement();
71
        $statementText = $statement->text();
72
        $statementParams = $statement->parameters();
73
        $encodedParameters = json_encode($statementParams);
74
        $tag = $statement->getTag() ?: -1;
75
76
        if (!isset($this->statementsHash[$statementText][$encodedParameters][$tag])) {
77
            $idx = $this->nbQueries++;
78
            $this->statements[$idx]['start_time'] = null;
79
            $this->statementsHash[$idx] = $idx;
80
        } else {
81
            $idx = $this->statementsHash[$statementText][$encodedParameters][$tag];
82
        }
83
84
        $this->statements[$idx] = array_merge($this->statements[$idx], [
85
            'end_time' => microtime(true) * 1000,
86
            'nb_results' => $statementResult->size(),
87
            'statistics' => $this->statisticsToArray($statementResult->summarize()->updateStatistics()),
88
            'scheme' => $scheme,
89
            'success' => true,
90
        ]);
91
    }
92
93
    public function reset()
94
    {
95
        $this->nbQueries = 0;
96
        $this->statements = [];
97
        $this->statementsHash = [];
98
    }
99
100
    /**
101
     * @param Neo4jExceptionInterface $exception
102
     */
103
    public function logException(Neo4jExceptionInterface $exception)
104
    {
105
        $idx = $this->nbQueries - 1;
106
        $this->statements[$idx] = array_merge($this->statements[$idx], [
107
            'end_time' => microtime(true) * 1000,
108
            'exceptionCode' => method_exists($exception, 'classification') ? $exception->classification() : '',
109
            'exceptionMessage' => method_exists($exception, 'getMessage') ? $exception->getMessage() : '',
110
            'success' => false,
111
        ]);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function count()
118
    {
119
        return $this->nbQueries;
120
    }
121
122
    /**
123
     * @return array[]
124
     */
125
    public function getStatements()
126
    {
127
        return $this->statements;
128
    }
129
130
    /**
131
     * @return array
132
     */
133
    public function getStatementsHash()
134
    {
135
        return $this->statementsHash;
136
    }
137
138
    /**
139
     * @return int
140
     */
141
    public function getElapsedTime()
142
    {
143
        $time = 0;
144
145
        foreach ($this->statements as $statement) {
146
            if (!isset($statement['start_time'], $statement['end_time'])) {
147
                continue;
148
            }
149
150
            $time += $statement['end_time'] - $statement['start_time'];
151
        }
152
153
        return $time;
154
    }
155
156
    private function statisticsToArray(StatementStatisticsInterface $statementStatistics)
157
    {
158
        $data = [
159
            'contains_updates' => $statementStatistics->containsUpdates(),
160
            'nodes_created' => $statementStatistics->nodesCreated(),
161
            'nodes_deleted' => $statementStatistics->nodesDeleted(),
162
            'relationships_created' => $statementStatistics->relationshipsCreated(),
163
            'relationships_deleted' => $statementStatistics->relationshipsDeleted(),
164
            'properties_set' => $statementStatistics->propertiesSet(),
165
            'labels_added' => $statementStatistics->labelsAdded(),
166
            'labels_removed' => $statementStatistics->labelsRemoved(),
167
            'indexes_added' => $statementStatistics->indexesAdded(),
168
            'indexes_removed' => $statementStatistics->indexesRemoved(),
169
            'constraints_added' => $statementStatistics->constraintsAdded(),
170
            'constraints_removed' => $statementStatistics->constraintsRemoved(),
171
        ];
172
173
        return $data;
174
    }
175
}
176