You are here

function elasticsearch_watchdog_overview in Elasticsearch Connector 7

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

Page callback: Displays a listing of log messages.

Messages are truncated at 56 chars. Full-length messages can be viewed on the message details page.

See also

elasticsearch_watchdog_clear_log_form()

elasticsearch_watchdog_event()

elasticsearch_watchdog_filter_form()

elasticsearch_watchdog_menu()

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

File

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

Code

function elasticsearch_watchdog_overview() {
  $filter = elasticsearch_watchdog_build_filter_query();
  $results = $rows = array();
  $classes = array(
    WATCHDOG_DEBUG => 'elasticlog-debug',
    WATCHDOG_INFO => 'elasticlog-info',
    WATCHDOG_NOTICE => 'elasticlog-notice',
    WATCHDOG_WARNING => 'elasticlog-warning',
    WATCHDOG_ERROR => 'elasticlog-error',
    WATCHDOG_CRITICAL => 'elasticlog-critical',
    WATCHDOG_ALERT => 'elasticlog-alert',
    WATCHDOG_EMERGENCY => 'elasticlog-emerg',
  );
  $build['elasticsearch_watchdog_filter_form'] = drupal_get_form('elasticsearch_watchdog_filter_form');
  $build['elasticsearch_watchdog_clear_log_form'] = drupal_get_form('elasticsearch_watchdog_clear_log_form');
  $header = array(
    '',
    // Icon column.
    array(
      'data' => t('Type'),
      'field' => array(
        'type',
      ),
    ),
    array(
      'data' => t('Date'),
      'field' => array(
        'date',
        'microtime',
      ),
      'sort' => 'desc',
    ),
    t('Message'),
    array(
      'data' => t('User'),
      'field' => array(
        'username',
      ),
    ),
    array(
      'data' => t('Operations'),
    ),
  );
  $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();
        $index_name = elasticsearch_watchdog_get_index_name();
        $params['index'] = $index_name;
        $params['type'] = elasticsearch_watchdog_get_type_name_for_view();
        if ($client
          ->indices()
          ->exists(array(
          'index' => $index_name,
        ))) {
          $sort = tablesort_get_sort($header);
          $sort_fields = tablesort_get_order($header);
          $limit = variable_get('elasticsearch_watchdog_page_limit', 50);
          $current_page = pager_find_page();
          $params['body']['size'] = $limit;
          $params['body']['from'] = $current_page * $limit;
          foreach ($sort_fields['sql'] as $sort_field) {
            $params['body']['sort'][$sort_field]['order'] = $sort;
          }

          // Filter the results if there are filters specified.
          if (!empty($filter)) {
            $params['body']['query']['filtered'] = $filter;
          }
          $results = $client
            ->search($params);
          if (!empty($results['hits']['total'])) {
            pager_default_initialize($results['hits']['total'], $limit);
          }
        }
      } catch (Exception $e) {

        // Show the error message to the user.
        drupal_set_message($e
          ->getMessage(), 'error');
      }
    }
  }
  $hits = FALSE;
  if (!empty($results['hits']['hits'])) {
    $hits = TRUE;
    foreach ((array) $results['hits']['hits'] as $doc) {
      $account = user_load($doc['_source']['uid']);
      $rows[] = array(
        'data' => array(
          // Cells
          array(
            'class' => 'icon',
          ),
          t($doc['_source']['type']),
          format_date($doc['_source']['timestamp'], 'short'),
          theme('elasticsearch_watchdog_message', array(
            'event_id' => $doc['_id'],
            'event' => $doc['_source'],
            'link' => TRUE,
          )),
          theme('username', array(
            'account' => $account,
          )),
          filter_xss($doc['_source']['link']),
        ),
        // Attributes for tr
        'class' => array(
          drupal_html_class('elasticlog-' . $doc['_source']['type']),
          $classes[$doc['_source']['severity']],
        ),
      );
    }
  }
  if ($hits) {
    $elasticsearch_connector_path = elasticsearch_connector_main_settings_path();
    $cluster = elasticsearch_connector_cluster_load($client_id);
    $build['info'] = array(
      '#type' => 'fieldset',
      '#title' => t('Info'),
    );
    $build['info']['total_messages'] = array(
      '#theme' => 'table',
      '#rows' => array(
        array(
          array(
            'data' => t('Messages: @messages', array(
              '@messages' => $results['hits']['total'],
            )),
            'header' => TRUE,
          ),
        ),
        array(
          array(
            'data' => t('Cluster status: <a href="@clusters">!cluster</a>', array(
              '@clusters' => url($elasticsearch_connector_path . '/clusters/' . $client_id . '/info', array()),
              '!cluster' => $cluster->name,
            )),
            'header' => TRUE,
          ),
        ),
      ),
      '#attributes' => array(
        'class' => array(
          'elasticlog-event',
        ),
      ),
    );
  }
  $build['elasticsearch_watchdog_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => array(
      'id' => 'admin-elasticlog',
    ),
    '#empty' => t('No log messages available.'),
  );
  $build['elasticsearch_watchdog_pager'] = array(
    '#theme' => 'pager',
  );
  return $build;
}