Skip to content

Commit ed890d6

Browse files
authored
Merge pull request #24 from neo4j-contrib/issue-18
Show what scheme was used
2 parents d061f08 + 990c903 commit ed890d6

File tree

5 files changed

+82
-7
lines changed

5 files changed

+82
-7
lines changed

Collector/Neo4jDataCollector.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ public function __construct(QueryLogger $logger)
2828
*/
2929
public function collect(Request $request, Response $response, \Exception $exception = null)
3030
{
31+
$this->data['time'] = $this->queryLogger->getElapsedTime();
3132
$this->data['nb_queries'] = count($this->queryLogger);
3233
$this->data['statements'] = $this->queryLogger->getStatements();
33-
$this->data['time'] = $this->queryLogger->getElapsedTime();
34+
$this->data['failed_statements'] = array_filter($this->queryLogger->getStatements(), function ($statement) {
35+
return !$statement['success'];
36+
});
3437
}
3538

3639
/**
@@ -42,13 +45,25 @@ public function getQueryCount()
4245
}
4346

4447
/**
45-
* @return QueryLogger
48+
* Return all statements, successful and not successful.
49+
*
50+
* @return array
4651
*/
4752
public function getStatements()
4853
{
4954
return $this->data['statements'];
5055
}
5156

57+
/**
58+
* Return not successful statements.
59+
*
60+
* @return array
61+
*/
62+
public function getFailedStatements()
63+
{
64+
return $this->data['failed_statements'];
65+
}
66+
5267
/**
5368
* @return float
5469
*/

Collector/QueryLogger.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace Neo4j\Neo4jBundle\Collector;
66

7+
use GraphAware\Bolt\Result\Result;
78
use GraphAware\Common\Cypher\StatementInterface;
89
use GraphAware\Common\Result\StatementResult as StatementResultInterface;
910
use GraphAware\Common\Result\StatementStatisticsInterface;
11+
use GraphAware\Neo4j\Client\Exception\Neo4jExceptionInterface;
1012

1113
/**
1214
* @author Xavier Coureau <[email protected]>
@@ -37,6 +39,7 @@ public function record(StatementInterface $statement)
3739
$statementParams = json_encode($statement->parameters());
3840
$tag = $statement->getTag() ?: -1;
3941

42+
// Make sure we do not record the same statement twice
4043
if (isset($this->statementsHash[$statementText][$statementParams][$tag])) {
4144
return;
4245
}
@@ -59,6 +62,11 @@ public function record(StatementInterface $statement)
5962
*/
6063
public function finish(StatementResultInterface $statementResult)
6164
{
65+
$scheme = 'Http';
66+
if ($statementResult instanceof Result) {
67+
$scheme = 'Bolt';
68+
}
69+
6270
$statement = $statementResult->statement();
6371
$statementText = $statement->text();
6472
$statementParams = $statement->parameters();
@@ -77,6 +85,22 @@ public function finish(StatementResultInterface $statementResult)
7785
'end_time' => microtime(true) * 1000,
7886
'nb_results' => $statementResult->size(),
7987
'statistics' => $this->statisticsToArray($statementResult->summarize()->updateStatistics()),
88+
'scheme' => $scheme,
89+
'success' => true,
90+
]);
91+
}
92+
93+
/**
94+
* @param Neo4jExceptionInterface $exception
95+
*/
96+
public function logException(Neo4jExceptionInterface $exception)
97+
{
98+
$idx = $this->nbQueries - 1;
99+
$this->statements[$idx] = array_merge($this->statements[$idx], [
100+
'end_time' => microtime(true) * 1000,
101+
'exceptionCode' => method_exists($exception, 'classification') ? $exception->classification() : '',
102+
'exceptionMessage' => method_exists($exception, 'getMessage') ? $exception->getMessage() : '',
103+
'success' => false,
80104
]);
81105
}
82106

EventSubscriber/LoggerSubscriber.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Neo4j\Neo4jBundle\EventSubscriber;
66

7+
use GraphAware\Neo4j\Client\Event\FailureEvent;
78
use GraphAware\Neo4j\Client\Event\PostRunEvent;
89
use GraphAware\Neo4j\Client\Event\PreRunEvent;
910
use GraphAware\Neo4j\Client\Neo4jClientEvents;
@@ -33,6 +34,7 @@ public static function getSubscribedEvents()
3334
return [
3435
Neo4jClientEvents::NEO4J_PRE_RUN => 'onPreRun',
3536
Neo4jClientEvents::NEO4J_POST_RUN => 'onPostRun',
37+
Neo4jClientEvents::NEO4J_ON_FAILURE => 'onFailure',
3638
];
3739
}
3840

@@ -55,4 +57,12 @@ public function onPostRun(PostRunEvent $event)
5557
$this->queryLogger->finish($result);
5658
}
5759
}
60+
61+
/**
62+
* @param FailureEvent $event
63+
*/
64+
public function onFailure(FailureEvent $event)
65+
{
66+
$this->queryLogger->logException($event->getException());
67+
}
5868
}

Resources/public/style/neo4j.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.bg-red {
2+
background-color: #B0413E;
3+
}

Resources/views/webprofiler.html.twig

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@
1111
{% set text %}
1212
<div class="sf-toolbar-info-piece">
1313
<b>Query count</b>
14-
<span>{{ collector.queryCount }}</span>
14+
<span class="sf-toolbar-status">{{ collector.queryCount }}</span>
1515
</div>
16+
{% if collector.failedStatements|length %}
17+
<div class="sf-toolbar-info-piece">
18+
<b>Failed queries</b>
19+
<span class="sf-toolbar-status sf-toolbar-status-red">{{ collector.failedStatements|length }}</span>
20+
</div>
21+
{% endif %}
1622
<div class="sf-toolbar-info-piece">
1723
<b>Total time</b>
18-
<span>{{ '%0.2f'|format(collector.time) }}ms</span>
24+
<span class="sf-toolbar-status">{{ '%0.2f'|format(collector.time) }}ms</span>
1925
</div>
2026

2127
{% endset %}
22-
{% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %}
28+
{% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url, 'status': collector.failedStatements|length ? 'red' : '' } %}
2329
{% endif %}
2430
{% endblock %}
2531

@@ -31,11 +37,16 @@
3137

3238
{% block menu %}
3339
{# This left-hand menu appears when using the full-screen profiler. #}
34-
<span class="label {{ collector.queryCount == 0 ? 'disabled' }}">
40+
<span class="label {{ collector.queryCount == 0 ? 'disabled' }} {{ collector.failedStatements|length ? 'label-status-error' }}">
3541
<span class="icon">
3642
{{ include('@Neo4j/Icon/neo4j.svg') }}
3743
</span>
3844
<strong>Neo4j</strong>
45+
{% if collector.failedStatements|length %}
46+
<span class="count">
47+
<span>{{ collector.failedStatements|length }}</span>
48+
</span>
49+
{% endif %}
3950
</span>
4051
{% endblock %}
4152

@@ -56,7 +67,7 @@
5667
{% set start_time = statement.start_time|default(null) %}
5768
{% set end_time = statement.end_time|default(null) %}
5869

59-
<td class="nowrap">{{ idx + 1 }}</td>
70+
<td class="nowrap{% if not statement.success %} bg-red{% endif %}">{{ idx + 1 }}</td>
6071
<td class="nowrap">{% if start_time is not null and end_time is not null %}{{ '%0.2f'|format(end_time - start_time) }}ms{% endif %}</td>
6172
<td>
6273
<div>
@@ -73,12 +84,24 @@
7384
<div>
7485
<strong class="font-normal text-small">Tag</strong>: {{ statement.tag|default('N/A')|yaml_encode }}
7586
</div>
87+
{% if statement.sucess %}
7688
<div>
7789
<strong class="font-normal text-small">Number of results</strong>: {{ statement.nb_results }}
7890
</div>
91+
<div>
92+
<strong class="font-normal text-small">Scheme</strong>: {{ statement.scheme }}
93+
</div>
7994
<div>
8095
<strong class="font-normal text-small">Statistics</strong>: {{ statement.statistics|yaml_encode }}
8196
</div>
97+
{% else %}
98+
<div>
99+
<strong class="font-normal text-small">Type</strong>: {{ statement.exceptionCode }}
100+
</div>
101+
<div>
102+
<strong class="font-normal text-small">message</strong>: {{ statement.exceptionMessage }}
103+
</div>
104+
{% endif %}
82105
</div>
83106
</td>
84107
</tr>

0 commit comments

Comments
 (0)