SqlDbCollector.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace KarmaFW\App\Middlewares\DebugBar;
  3. use \DebugBar\DataCollector\DataCollector;
  4. use \DebugBar\DataCollector\Renderable;
  5. use \DebugBar\DataCollector\AssetProvider;
  6. /* USE THIS WITH DEBUGBAR (see http://phpdebugbar.com/ or https://github.com/maximebf/php-debugbar ) */
  7. class SqlDbCollector extends DataCollector implements Renderable, AssetProvider
  8. {
  9. protected $sql_queries = [];
  10. protected $totalExecTime = 0;
  11. protected $total_memory = 0;
  12. protected $max_memory = 0;
  13. protected $nb_failed_statements = 0;
  14. public function collect()
  15. {
  16. return [
  17. 'nb_statements' => count($this->sql_queries),
  18. 'accumulated_duration' => $this->totalExecTime,
  19. 'accumulated_duration_str' => round($this->totalExecTime, 4),
  20. 'statements' => $this->sql_queries,
  21. 'nb_failed_statements' => $this->nb_failed_statements,
  22. 'memory_usage' => $this->total_memory,
  23. 'memory_usage_str' => round($this->total_memory/1000000, 1) . " Mo",
  24. 'peak_memory_usage' => $this->max_memory,
  25. 'peak_memory_usage_str' => round($this->max_memory/1000000, 1) . " Mo",
  26. ];
  27. }
  28. public function getName()
  29. {
  30. return 'sql_queries';
  31. }
  32. public function addQuery($query)
  33. {
  34. $this->sql_queries[] = $query;
  35. $this->totalExecTime += $query['duration'];
  36. $this->total_memory += $query['memory'];
  37. $this->max_memory = max($this->max_memory, $query['memory']);
  38. if ($query['is_success'] === false) {
  39. $this->nb_failed_statements += 1;
  40. }
  41. }
  42. public function getWidgets()
  43. {
  44. return [
  45. "sql" => [
  46. "icon" => "inbox",
  47. "tooltip" => "SQL Queries",
  48. //"widget" => "PhpDebugBar.Widget",
  49. "widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
  50. //"widget" => "PhpDebugBar.Widgets.MessagesWidget",
  51. "map" => "sql_queries",
  52. "default" => "[]"
  53. ],
  54. "sql:badge" => [
  55. "map" => "sql_queries.nb_statements",
  56. "default" => 0
  57. ],
  58. ];
  59. }
  60. public function getAssets()
  61. {
  62. return array(
  63. 'css' => 'widgets/sqlqueries/widget.css',
  64. 'js' => 'widgets/sqlqueries/widget.js'
  65. );
  66. }
  67. }