You are here

function elasticsearch_connector_statistics_top_referrers in Elasticsearch Connector 7.5

Same name and namespace in other branches
  1. 7 modules/elasticsearch_connector_statistics/elasticsearch_connector_statistics.admin.inc \elasticsearch_connector_statistics_top_referrers()
  2. 7.2 modules/elasticsearch_connector_statistics/elasticsearch_connector_statistics.admin.inc \elasticsearch_connector_statistics_top_referrers()

Page callback: Displays the "top referrers" in the access logs.

This displays the pages with the top referrers in a given time interval that haven't been flushed yet. The flush interval is set on the statistics settings form, but is dependent on cron running.

Return value

A render array containing the top referrers information.

1 string reference to 'elasticsearch_connector_statistics_top_referrers'
elasticsearch_connector_statistics_menu in modules/elasticsearch_connector_statistics/elasticsearch_connector_statistics.module
Implements hook_menu().

File

modules/elasticsearch_connector_statistics/elasticsearch_connector_statistics.admin.inc, line 351
Admin page callbacks for the Statistics module.

Code

function elasticsearch_connector_statistics_top_referrers() {
  $header = array(
    array(
      'data' => t('Hits'),
      'field' => 'hits',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Url'),
      'field' => 'url',
    ),
  );
  $rows = $facets = array();
  $global_facet_name = 'referer_facet';
  $field_faceting = 'referer';
  $client_id = elasticsearch_connector_statistics_get_cluster_vars();
  if (!empty($client_id)) {
    $client = elasticsearch_connector_get_client_by_id($client_id);
    if ($client) {
      try {
        $params = array();
        $index_name = elasticsearch_connector_statistics_get_cluster_vars('index');
        $params['index'] = $index_name;
        $params['type'] = variable_get('elasticsearch_connector_statistics_type', ELASTICSEARCH_CONNECTOR_STATS_DEFAULT_TYPE);
        $aggr = new \nodespark\DESConnector\Elasticsearch\Aggregations\Bucket\Terms($global_facet_name, $field_faceting);
        $aggr
          ->setSize(variable_get('elasticsearch_connector_statistics_facet_size', 1000));
        $client
          ->aggregations()
          ->setAggregation($aggr);
        $search_result = $client
          ->search($params)
          ->getRawResponse();
        if (!empty($search_result['aggregations'])) {
          foreach ($search_result['aggregations'][$global_facet_name]['buckets'] as $bucket) {
            if (!empty($bucket['key'])) {
              $bucket['key'] = l($bucket['key'], $bucket['key']);
            }
            $rows[] = array(
              $bucket['doc_count'],
              $bucket['key'],
            );
          }
        }
      } catch (Exception $e) {

        // Show the error message to the user.
        drupal_set_message($e
          ->getMessage(), 'error');
      }
    }
  }
  $build['statistics_top_referrers_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No statistics available.'),
  );
  return $build;
}