You are here

function mongodb_watchdog_page_top in MongoDB 6

Same name and namespace in other branches
  1. 7 mongodb_watchdog/mongodb_watchdog.admin.inc \mongodb_watchdog_page_top()

Page callback for "admin/reports/[access-denied|page-not-found]".

Return value

array A render array.

1 string reference to 'mongodb_watchdog_page_top'
mongodb_watchdog_menu in mongodb_watchdog/mongodb_watchdog.module
Implements hook_menu().

File

mongodb_watchdog/mongodb_watchdog.admin.inc, line 466
Settings for mongodb. Moved back to module file.

Code

function mongodb_watchdog_page_top($type) {
  $ret = '';
  $message_wrapper = array(
    '#prefix' => '<div class="mongodb-watchdog-message">',
    '#suffix' => '</div>',
  );
  $type_param = array(
    '%type' => $type,
  );
  $limit = 50;

  // Safety net.
  $types = array(
    'page not found',
    'access denied',
  );
  if (!in_array($type, $types)) {
    drupal_set_message(t('Unknown top report type: %type', $type_param), 'error');
    watchdog('mongodb_watchdog', 'Unknown top report type: %type', $type_param, WATCHDOG_WARNING);
    $ret = '';
    return $ret;
  }

  // Find _id for the error type.
  $watchdog = mongodb_collection(variable_get('mongodb_watchdog', 'watchdog'));
  $template = $watchdog
    ->findOne(array(
    'type' => $type,
  ), array(
    '_id',
  ));

  // MongoDB findOne() will return NULL if no row is found.
  if (empty($template)) {
    $ret['empty'] = array(
      '#value' => t('No "%type" message found', $type_param),
    ) + $message_wrapper;
    $ret = drupal_render($ret);
    return $ret;
  }

  // Find occurrences of error type.
  $key = $template['_id'];
  $event_collection = mongodb_collection('watchdog_event_' . $key);
  $reduce = <<<EOT
function (doc, accumulator) {
  accumulator.count++;
}
EOT;
  $counts = $event_collection
    ->group(array(
    'variables.@param' => 1,
  ), array(
    'count' => array(),
  ), $reduce);
  if (!$counts['ok']) {
    drupal_set_message(t('No "%type" occurrence found', $type_param), 'error');
    $ret = '';
    return $ret;
  }
  $counts = $counts['retval'];
  usort($counts, '_mongodb_watchdog_sort_top');
  $counts = array_slice($counts, 0, $limit);
  $header = array(
    t('#'),
    t('Paths'),
  );
  $rows = array();
  foreach ($counts as $count) {
    $rows[] = array(
      $count['variables.@param'],
      $count['count'],
    );
  }
  $ret = array(
    '#value' => theme('table', $header, $rows),
  ) + $message_wrapper;
  $ret = drupal_render($ret);
  return $ret;
}