You are here

function _elasticsearch_watchdog_get_facets in Elasticsearch Connector 7

Same name and namespace in other branches
  1. 7.5 modules/elasticsearch_watchdog/elasticsearch_watchdog.module \_elasticsearch_watchdog_get_facets()
  2. 7.2 modules/elasticsearch_watchdog/elasticsearch_watchdog.module \_elasticsearch_watchdog_get_facets()

Return message types FACET.

Return value

array

1 call to _elasticsearch_watchdog_get_facets()
elasticsearch_watchdog_filters in modules/elasticsearch_watchdog/elasticsearch_watchdog.admin.inc
Creates a list of log administration filters that can be applied.

File

modules/elasticsearch_watchdog/elasticsearch_watchdog.module, line 177
Created on Jan 06, 2014

Code

function _elasticsearch_watchdog_get_facets($facet_field, $facet_type) {
  $watchdog_facets =& drupal_static(__FUNCTION__);
  $result = array();
  if (!isset($watchdog_facets)) {
    $facet_name_prefix = 'facetname_';
    $facets = array(
      $facet_name_prefix . 'type' => array(
        'type' => 'terms',
        'field' => 'type',
      ),
      $facet_name_prefix . 'domain' => array(
        'type' => 'terms',
        'field' => 'domain',
      ),
      $facet_name_prefix . 'severity' => array(
        'type' => 'terms',
        'field' => 'severity',
      ),
      $facet_name_prefix . 'username' => array(
        'type' => 'terms',
        'field' => 'username',
      ),
    );
    $params = array();
    $params['index'] = elasticsearch_watchdog_get_index_name();
    $params['type'] = elasticsearch_watchdog_get_type_name_for_view();
    $params['search_type'] = 'count';
    foreach ($facets as $facet_name => $facet_params) {
      $watchdog_facets[$facet_params['field'] . '_' . $facet_params['type']] = array();
      $params['body']['aggs'][$facet_name][$facet_params['type']]['field'] = $facet_params['field'];
      $params['body']['aggs'][$facet_name][$facet_params['type']]['size'] = variable_get('elasticsearch_watchdog_facet_size', 100);
    }
    $client_id = elasticsearch_watchdog_get_cluster_id();
    if (!empty($client_id)) {
      $client = elasticsearch_connector_get_client_by_id($client_id);
      if ($client) {
        try {
          $search_result = $client
            ->search($params);
          if (!empty($search_result['aggregations'])) {
            foreach ($search_result['aggregations'] as $result_facet_name => $result_params) {
              $type = $facets[$result_facet_name]['type'];
              $field = str_replace($facet_name_prefix, '', $result_facet_name);
              $result = array();
              foreach ($result_params['buckets'] as $facet) {
                $result[$facet['key']] = $facet;
              }
              $watchdog_facets[$field . '_' . $type] = $result;
            }
          }
        } catch (Exception $e) {
          error_log($e
            ->getMessage());
        }
      }
    }
  }
  return $watchdog_facets[$facet_field . '_' . $facet_type];
}