You are here

function elasticsearch_watchdog_top in Elasticsearch Connector 7

Same name and namespace in other branches
  1. 7.5 modules/elasticsearch_watchdog/elasticsearch_watchdog.admin.inc \elasticsearch_watchdog_top()
  2. 7.2 modules/elasticsearch_watchdog/elasticsearch_watchdog.admin.inc \elasticsearch_watchdog_top()

Page callback: Shows the most frequent log messages of a given event type.

Messages are not truncated on this page because events detailed herein do not have links to a detailed view.

Parameters

string $type: Type of log events to display (e.g., 'search').

Return value

array A build array in the format expected by drupal_render().

See also

elasticsearch_watchdog_menu()

1 string reference to 'elasticsearch_watchdog_top'
elasticsearch_watchdog_menu in modules/elasticsearch_watchdog/elasticsearch_watchdog.module
Implements hook_menu().

File

modules/elasticsearch_watchdog/elasticsearch_watchdog.admin.inc, line 374
Created on Jan 06, 2014

Code

function elasticsearch_watchdog_top($type) {
  $header = array(
    array(
      'data' => t('Count'),
      'field' => 'count',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Path'),
      'field' => 'message',
    ),
  );
  $rows = array();
  $global_facet_name = 'facetname_message';
  $field_faceting = 'message';
  $client_id = elasticsearch_watchdog_get_cluster_id();
  if (!empty($client_id)) {
    $client = elasticsearch_connector_get_client_by_id($client_id);
    if ($client) {
      try {
        $params = array();
        $params['index'] = elasticsearch_watchdog_get_index_name();
        $params['type'] = elasticsearch_watchdog_get_type_name_for_view();
        $params['search_type'] = 'count';

        // TODO: Change this to be filter because this is a term query!
        $params['body']['query']['term']['type'] = $type;
        $params['body']['facets'][$global_facet_name]['terms']['field'] = $field_faceting;
        $params['body']['facets'][$global_facet_name]['terms']['size'] = variable_get('elasticsearch_watchdog_facet_size', 100);
        $search_result = $client
          ->search($params);
        if (!empty($search_result['facets'])) {
          foreach ($search_result['facets'][$global_facet_name]['terms'] as $facet) {
            $rows[] = array(
              $facet['count'],
              $facet['term'],
            );
          }
        }
      } catch (Exception $e) {
        drupal_set_message($e
          ->getMessage(), 'error');
      }
    }
  }
  $build['elasticsearch_watchdog_top_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No log messages available.'),
  );
  return $build;
}