44
55namespace Neo4j \Neo4jBundle \Collector ;
66
7- use Laudis \Neo4j \Databags \ResultSummary ;
87use Neo4j \Neo4jBundle \EventListener \Neo4jProfileListener ;
98use Symfony \Bundle \FrameworkBundle \DataCollector \AbstractDataCollector ;
109use Symfony \Component \HttpFoundation \Request ;
1110use Symfony \Component \HttpFoundation \Response ;
1211
1312/**
1413 * @var array{
15- * successful_statements: array<array-key, array<string, mixed>>,
16- * failed_statements: list<array{
17- * statement: mixed,
18- * exception: mixed,
19- * alias: string|null
20- * }>
14+ * successful_statements_count: int,
15+ * failed_statements_count: int,
16+ * statements: array<array-key, array<string, mixed>> | list<array{
17+ * statement: mixed,
18+ * exception: mixed,
19+ * alias: string|null
20+ * }>,
2121 * } $data
2222 */
2323final class Neo4jDataCollector extends AbstractDataCollector
2424{
2525 public function __construct (
26- private Neo4jProfileListener $ subscriber
26+ private readonly Neo4jProfileListener $ subscriber
2727 ) {
2828 }
2929
3030 public function collect (Request $ request , Response $ response , ?\Throwable $ exception = null ): void
3131 {
32- $ this ->data ['successful_statements ' ] = array_map (
33- static fn (ResultSummary $ summary ) => $ summary ->toArray (),
34- $ this ->subscriber ->getProfiledSummaries ()
32+ $ profiledSummaries = $ this ->subscriber ->getProfiledSummaries ();
33+ $ successfulStatements = array_map (
34+ static function (string $ key , mixed $ value ) {
35+ if ('result ' !== $ key ) {
36+ return [...$ value , 'status ' => 'success ' ];
37+ }
38+
39+ return array_map (
40+ static function (string $ key , mixed $ obj ) {
41+ if (is_object ($ obj ) && method_exists ($ obj , 'toArray ' )) {
42+ return $ obj ->toArray ();
43+ }
44+
45+ return $ obj ;
46+ },
47+ $ value ['result ' ]->toArray ()
48+ );
49+ },
50+ array_keys ($ profiledSummaries ),
51+ array_values ($ profiledSummaries )
3552 );
3653
37- $ this -> data [ ' failed_statements ' ] = array_map (
54+ $ failedStatements = array_map (
3855 static fn (array $ x ) => [
39- 'statement ' => $ x ['statement ' ]->toArray (),
56+ 'status ' => 'failure ' ,
57+ 'time ' => $ x ['time ' ],
58+ 'timestamp ' => $ x ['timestamp ' ],
59+ 'result ' => [
60+ 'statement ' => $ x ['statement ' ]->toArray (),
61+ ],
4062 'exception ' => [
4163 'code ' => $ x ['exception ' ]->getErrors ()[0 ]->getCode (),
4264 'message ' => $ x ['exception ' ]->getErrors ()[0 ]->getMessage (),
@@ -48,6 +70,15 @@ public function collect(Request $request, Response $response, ?\Throwable $excep
4870 ],
4971 $ this ->subscriber ->getProfiledFailures ()
5072 );
73+
74+ $ this ->data ['successful_statements_count ' ] = count ($ successfulStatements );
75+ $ this ->data ['failed_statements_count ' ] = count ($ failedStatements );
76+ $ mergedArray = array_merge ($ successfulStatements , $ failedStatements );
77+ uasort (
78+ $ mergedArray ,
79+ static fn (array $ a , array $ b ) => $ a ['start_time ' ] <=> $ b ['timestamp ' ]
80+ );
81+ $ this ->data ['statements ' ] = $ mergedArray ;
5182 }
5283
5384 public function reset (): void
@@ -61,19 +92,40 @@ public function getName(): string
6192 return 'neo4j ' ;
6293 }
6394
64- public function getFailedStatements (): array
95+ public function getStatements (): array
6596 {
66- return $ this ->data ['failed_statements ' ];
97+ return $ this ->data ['statements ' ];
6798 }
6899
69100 public function getSuccessfulStatements (): array
70101 {
71- return $ this ->data ['successful_statements ' ];
102+ return array_filter (
103+ $ this ->data ['statements ' ],
104+ static fn (array $ x ) => 'success ' === $ x ['status ' ]
105+ );
106+ }
107+
108+ public function getFailedStatements (): array
109+ {
110+ return array_filter (
111+ $ this ->data ['statements ' ],
112+ static fn (array $ x ) => 'failure ' === $ x ['status ' ]
113+ );
114+ }
115+
116+ public function getFailedStatementsCount (): array
117+ {
118+ return $ this ->data ['failed_statements_count ' ];
119+ }
120+
121+ public function getSuccessfulStatementsCount (): array
122+ {
123+ return $ this ->data ['successful_statements_count ' ];
72124 }
73125
74126 public function getQueryCount (): int
75127 {
76- return count ($ this ->data ['successful_statements ' ]) + count ( $ this -> data [ ' failed_statements ' ]);
128+ return count ($ this ->data ['statements ' ]);
77129 }
78130
79131 public static function getTemplate (): ?string
0 commit comments