77use Elastica \Exception \ResponseException ;
88use Elastica \Request ;
99use Elastica \Response ;
10- use Exception ;
11- use Nette ;
10+ use Nette \Http \Url ;
1211use Nette \Utils \Html ;
1312use Nette \Utils \Json ;
13+ use Nette \Utils \JsonException ;
1414use Throwable ;
1515use Tracy \Debugger ;
1616use Tracy \Dumper ;
1717use Tracy \IBarPanel ;
1818
19- /**
20- * @codeCoverageIgnore
21- */
2219class Panel implements IBarPanel
2320{
2421
25- use Nette \SmartObject ;
22+ public float $ totalTime = 0 ;
2623
27- /** @var float */
28- public $ totalTime = 0 ;
29-
30- /** @var int */
31- public $ queriesCount = 0 ;
24+ public int $ queriesCount = 0 ;
3225
3326 /** @var mixed[] */
34- public $ queries = [];
27+ public array $ queries = [];
28+
29+ private Client $ client ;
30+
31+ /**
32+ * @return array<string, string>|NULL
33+ */
34+ public static function renderException (Throwable |null $ e = null ): ?array
35+ {
36+ if (!$ e instanceof ExceptionInterface) {
37+ return null ;
38+ }
39+
40+ $ panel = null ;
41+
42+ if ($ e instanceof ResponseException) {
43+ $ panel .= '<h3>Request</h3> ' ;
44+ $ panel .= Dumper::toHtml ($ e ->getRequest ());
45+
46+ $ panel .= '<h3>Response</h3> ' ;
47+ $ panel .= Dumper::toHtml ($ e ->getResponse ());
48+
49+ } elseif ($ e instanceof \Elastica \Exception \Bulk \ResponseException) {
50+ $ panel .= '<h3>Failures</h3> ' ;
51+ $ panel .= Dumper::toHtml ($ e ->getFailures ());
52+
53+ }
3554
36- /** @var Client */
37- private $ client ;
55+ return $ panel ? [
56+ 'tab ' => 'ElasticSearch ' ,
57+ 'panel ' => $ panel ,
58+ ] : null ;
59+ }
3860
3961 public function getTab (): string
4062 {
@@ -52,7 +74,6 @@ public function getTab(): string
5274 return $ tab ->addHtml ($ title )->toHtml ();
5375 }
5476
55-
5677 public function getPanel (): ?string
5778 {
5879 if (!$ this ->queries ) {
@@ -72,21 +93,19 @@ public function getPanel(): ?string
7293
7394 try {
7495 return !is_array ($ data ) ? Json::decode ($ data , Json::FORCE_ARRAY ) : $ data ;
75- } catch (Nette \ Utils \ JsonException $ e ) {
96+ } catch (JsonException $ e ) {
7697 try {
7798 /** @phpstan-var mixed $data */
78- return array_map (function ($ row ) {
79- return Json::decode ((string ) $ row , Json::FORCE_ARRAY );
80- }, is_string ($ data ) ? explode ("\n" , trim ($ data )) : []);
81- } catch (Nette \Utils \JsonException $ e ) {
99+ return array_map (fn ($ row ) => Json::decode ((string ) $ row , Json::FORCE_ARRAY ), is_string ($ data ) ? explode ("\n" , trim ($ data )) : []);
100+ } catch (JsonException $ e ) {
82101 return $ data ;
83102 }
84103 }
85104 };
86105
87106 $ processedQueries = [];
88107 $ allQueries = $ this ->queries ;
89- $ totalTime = $ this ->totalTime ;
108+ $ totalTime = $ this ->totalTime ; // @phpcs:ignore
90109
91110 foreach ($ allQueries as $ authority => $ requests ) {
92111 /** @var Request[] $item */
@@ -131,15 +150,13 @@ public function getPanel(): ?string
131150 return $ result === false ? null : $ result ;
132151 }
133152
134-
135153 public function success (Client $ client , Request $ request , Response $ response , float $ time ): void
136154 {
137155 $ this ->queries [$ this ->requestAuthority ($ response )][] = [$ request , $ response , $ time ];
138156 $ this ->totalTime += $ time ;
139157 $ this ->queriesCount ++;
140158 }
141159
142-
143160 public function failure (Client $ client , Request $ request , Throwable $ e , float $ time ): void
144161 {
145162 /** @var Response $response */
@@ -150,62 +167,28 @@ public function failure(Client $client, Request $request, Throwable $e, float $t
150167 $ this ->queriesCount ++;
151168 }
152169
170+ public function register (Client $ client ): void
171+ {
172+ $ this ->client = $ client ;
173+ $ client ->onSuccess [] = [$ this , 'success ' ];
174+ $ client ->onFailure [] = [$ this , 'failure ' ];
153175
176+ Debugger::getBar ()->addPanel ($ this );
177+ }
154178
155179 protected function requestAuthority (?Response $ response = null ): string
156180 {
157181 if ($ response ) {
158182 $ info = $ response ->getTransferInfo ();
159- $ url = new Nette \ Http \ Url ($ info ['url ' ]);
183+ $ url = new Url ($ info ['url ' ]);
160184
161185 } else {
162186 /** @var string $current */
163187 $ current = key ($ this ->queries );
164- $ url = new Nette \ Http \ Url ($ current ?: 'http://localhost:9200/ ' );
188+ $ url = new Url ($ current ?: 'http://localhost:9200/ ' );
165189 }
166190
167191 return $ url ->hostUrl ;
168192 }
169193
170- /**
171- * @param Exception|Throwable $e
172- * @return array<string, string>|NULL
173- */
174- public static function renderException ($ e = null ): ?array
175- {
176- if (!$ e instanceof ExceptionInterface) {
177- return null ;
178- }
179-
180- $ panel = null ;
181-
182- if ($ e instanceof ResponseException) {
183- $ panel .= '<h3>Request</h3> ' ;
184- $ panel .= Dumper::toHtml ($ e ->getRequest ());
185-
186- $ panel .= '<h3>Response</h3> ' ;
187- $ panel .= Dumper::toHtml ($ e ->getResponse ());
188-
189- } elseif ($ e instanceof \Elastica \Exception \Bulk \ResponseException) {
190- $ panel .= '<h3>Failures</h3> ' ;
191- $ panel .= Dumper::toHtml ($ e ->getFailures ());
192-
193- }
194-
195- return $ panel ? [
196- 'tab ' => 'ElasticSearch ' ,
197- 'panel ' => $ panel ,
198- ] : null ;
199- }
200-
201-
202- public function register (Client $ client ): void
203- {
204- $ this ->client = $ client ;
205- $ client ->onSuccess [] = [$ this , 'success ' ];
206- $ client ->onFailure [] = [$ this , 'failure ' ];
207-
208- Debugger::getBar ()->addPanel ($ this );
209- }
210-
211194}
0 commit comments