You are here

function elasticsearch_watchdog_top in Elasticsearch Connector 7.5

Same name and namespace in other branches
  1. 7 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 292
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['body']['query']['bool']['must']['match_all'] = (object) array();
        $params['body']['query']['bool']['filter']['term']['type'] = $type;
        $aggregation = new \nodespark\DESConnector\Elasticsearch\Aggregations\Bucket\Terms($global_facet_name, $field_faceting);
        $aggregation
          ->setSize(variable_get('elasticsearch_watchdog_facet_size', 100));
        $client
          ->aggregations()
          ->setAggregation($aggregation);
        $search_result = $client
          ->search($params)
          ->getRawResponse();
        if (!empty($search_result['aggregations'])) {
          foreach ($search_result['aggregations'][$global_facet_name]['buckets'] as $bucket) {
            $rows[] = array(
              $bucket['doc_count'],
              $bucket['key'],
            );
          }
        }
      } 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;
}