You are here

function elasticsearch_connector_statistics_recent_hits 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_recent_hits()
  2. 7.2 modules/elasticsearch_connector_statistics/elasticsearch_connector_statistics.admin.inc \elasticsearch_connector_statistics_recent_hits()

Page callback: Displays the "recent hits" page.

This displays the pages with recent hits 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 information about the most recent hits.

1 string reference to 'elasticsearch_connector_statistics_recent_hits'
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 101
Admin page callbacks for the Statistics module.

Code

function elasticsearch_connector_statistics_recent_hits() {
  $header = array(
    array(
      'data' => t('Timestamp'),
      'field' => array(
        'timestamp',
      ),
      'sort' => 'desc',
    ),
    array(
      'data' => t('Page'),
      'field' => array(
        'path',
      ),
    ),
    array(
      'data' => t('User'),
      'field' => array(
        'username',
      ),
    ),
    array(
      'data' => t('Operations'),
    ),
  );
  $rows = array();
  $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);
        if ($client
          ->indices()
          ->exists(array(
          'index' => $index_name,
        )) && $client
          ->indices()
          ->existsType($params)) {
          $sort = tablesort_get_sort($header);
          $sort_fields = tablesort_get_order($header);
          $limit = variable_get('elasticsearch_connector_statistics_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.
          $params['body']['query'] = array(
            'match_all' => (object) array(),
          );
          $results = $client
            ->search($params)
            ->getRawResponse();
          if (!empty($results['hits']['hits'])) {
            foreach ($results['hits']['hits'] as $log) {
              $account = user_load($log['_source']['uid']);
              $rows[] = array(
                array(
                  'data' => format_date($log['_source']['timestamp'], 'short'),
                  'class' => array(
                    'nowrap',
                  ),
                ),
                _elasticsearch_connector_statistics_format_item($log['_source']['title'], $log['_source']['path']),
                theme('username', array(
                  'account' => $account,
                )),
                l(t('details'), "admin/reports/ecs-access/" . $log['_id']),
              );
            }
          }
          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');
      }
    }
  }
  $build['statistics_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No statistics available.'),
  );
  $build['statistics_pager'] = array(
    '#theme' => 'pager',
  );
  return $build;
}