performance.details.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @file
  4. * Performance module detail logging related functions. Can be overridden by
  5. * adding $conf['performance_detail_logging'] = 'path/to/your/file' in
  6. * settings.php.
  7. */
  8. /**
  9. * Helper function to store detailed data in database.
  10. * @param $params
  11. * an array containing the following keys: mem, timer, query_count, query_timer
  12. * anon, path, language, data.
  13. * @return void
  14. */
  15. function performance_log_details($params) {
  16. $fields = array(
  17. 'timestamp' => REQUEST_TIME,
  18. 'bytes' => $params['mem'],
  19. 'ms' => (int)$params['timer'],
  20. 'query_count' => $params['query_count'],
  21. 'query_timer' => (int)$params['query_timer'],
  22. 'anon' => $params['anon'],
  23. 'path' => $params['path'],
  24. 'language' => $params['language'],
  25. 'data' => $params['data'],
  26. );
  27. try {
  28. db_insert('performance_detail')
  29. ->fields($fields)
  30. ->execute();
  31. }
  32. catch (Exception $e) {
  33. watchdog_exception('performance', $e, NULL, array(), WATCHDOG_ERROR);
  34. }
  35. }
  36. /**
  37. * Detail page callback.
  38. * @return array
  39. * Drupal render array.
  40. */
  41. function performance_view_details() {
  42. drupal_set_title(t('Performance logs: Details'));
  43. $header = array(
  44. array('data' => t('#'), 'field' => 'pid', 'sort' => 'desc'),
  45. array('data' => t('Path'), 'field' => 'path'),
  46. array('data' => t('Date'), 'field' => 'timestamp'),
  47. array('data' => t('Memory (MB)'), 'field' => 'bytes'),
  48. array('data' => t('ms (Total)'), 'field' => 'ms'),
  49. array('data' => t('Language'), 'field' => 'language'),
  50. array('data' => t('Anonymous?'), 'field' => 'anon'),
  51. );
  52. if (variable_get(PERFORMANCE_QUERY_VAR, 0)) {
  53. $header[] = array('data' => t('# Queries'), 'field' => 'query_count');
  54. $header[] = array('data' => t('Query ms'), 'field' => 'query_timer');
  55. }
  56. $pager_height = 50;
  57. $result = db_select('performance_detail', 'p')
  58. ->fields('p')
  59. ->extend('PagerDefault')
  60. ->limit($pager_height)
  61. ->extend('TableSort')
  62. ->orderByHeader($header)
  63. ->execute();
  64. $rows = array();
  65. foreach ($result as $data) {
  66. $row_data = array();
  67. $row_data[] = $data->pid;
  68. $row_data[] = l($data->path, $data->path);
  69. $row_data[] = format_date($data->timestamp, 'small');
  70. $row_data[] = number_format($data->bytes / 1024 / 1024, 2);
  71. $row_data[] = $data->ms;
  72. $row_data[] = $data->language;
  73. $row_data[] = ($data->anon) ? t('Yes') : t('No');
  74. if (variable_get(PERFORMANCE_QUERY_VAR, 0)) {
  75. $row_data[] = $data->query_count;
  76. $row_data[] = $data->query_timer;
  77. }
  78. $rows[] = array('data' => $row_data);
  79. }
  80. if (empty($rows) && !variable_get('performance_detail', 0)) {
  81. return array(
  82. 'content' => array(
  83. '#markup' => t('Detail performance log is not enabled. Go to the !link to enable it.', array('!link' => l(t('settings page'), PERFORMANCE_SETTINGS, array('query' => drupal_get_destination()))))
  84. ),
  85. );
  86. }
  87. elseif (!variable_get('performance_detail', 0)) {
  88. drupal_set_message(t('Detail performance log is not enabled! Showing stored logs.'), 'warning');
  89. }
  90. // Return a renderable array.
  91. return array(
  92. 'query_data_detail' => array(
  93. '#theme' => 'table',
  94. '#header' => $header,
  95. '#rows' => $rows,
  96. '#sticky' => TRUE,
  97. '#empty' => t('No log messages available.'),
  98. ),
  99. 'clear' => array(
  100. '#markup' => l(t('Clear logs'), 'admin/reports/performance-logging/clear/details'),
  101. ),
  102. 'pager' => array(
  103. '#theme' => 'pager',
  104. '#quantity' => $pager_height,
  105. ),
  106. );
  107. }
  108. /**
  109. * Helper function to clear the detail logs.
  110. * @return void
  111. */
  112. function performance_clear_details() {
  113. db_truncate('performance_detail')->execute();
  114. }
  115. /**
  116. * Helper function to prune detail log on cron.
  117. * @return void
  118. */
  119. function performance_prune_details() {
  120. db_delete('performance_detail')
  121. ->condition('timestamp', REQUEST_TIME - (24 * 60 * 60), '<=')
  122. ->execute();
  123. }