You are here

function apachesolr_stats_exit in Apache Solr Statistics 7

Same name and namespace in other branches
  1. 6.3 apachesolr_stats.module \apachesolr_stats_exit()
  2. 6 apachesolr_stats.module \apachesolr_stats_exit()

Implements hook_exit().

This is the spot where actual logging takes place.

File

./apachesolr_stats.module, line 112
Keeps and reports statistics about Apache Solr usage and performance.

Code

function apachesolr_stats_exit() {
  $enabled_pages = variable_get('apachesolr_stats_enabled', array());
  if (!$enabled_pages) {
    return;
  }

  // Apparently there can be cases where some modules aren't loaded.
  if (!function_exists('apachesolr_has_searched')) {
    return;
  }

  // Log only for current applicable search page.
  $search_page = apachesolr_stats_get_search_page_by_path($_GET['q']);

  // If no search page matches this path, return.
  if (!$search_page) {
    return;
  }

  // Continue if current search page is enabled.
  $page_id = $search_page['page_id'];
  if (!isset($enabled_pages[$page_id]) || isset($enabled_pages[$page_id]) && !$enabled_pages[$page_id]) {
    return;
  }

  // Continue if current page request has issued a search.
  $env_id = $search_page['env_id'];
  if (!apachesolr_has_searched($env_id)) {
    return;
  }

  // Ignore certain IPs
  $ignore_list = variable_get('apachesolr_stats_ignore_ip_list', '');
  if ($ignore_list) {
    $ips_to_ignore = preg_split('/[\\s]+/', $ignore_list);
    $request_ip_address = ip_address();
    foreach ($ips_to_ignore as $ip) {
      if ($ip != "" && strpos($request_ip_address, $ip) === 0) {
        return;
      }
    }
  }

  // Ignore certain roles
  global $user;
  $roles_ignore_list = variable_get('apachesolr_stats_ignore_role_list', array());
  $test = array_intersect(array_keys($user->roles), array_values($roles_ignore_list));
  if (count($test) > 0) {
    return;
  }
  $query = apachesolr_current_query($env_id);
  $response = apachesolr_static_response_cache($query
    ->getSearcher());
  $filters = apachesolr_stats_get_active_facets($query
    ->getSearcher());
  $solrsort = $query
    ->getSolrSort();
  $keywords = $query
    ->getParam('q');
  $params = $query
    ->getParams();
  $num_suggestions = 0;

  // If spellchecking is enabled for this search page...
  if ($search_page['settings']['apachesolr_search_spellcheck']) {

    // ... and Solr returned a suggestion...
    if (isset($response->spellcheck) && isset($response->spellcheck->suggestions) && $response->spellcheck->suggestions != NULL) {

      // .. log the spellchecker suggestion.
      $num_suggestions = 1;
    }
  }
  $id = db_insert('apachesolr_stats')
    ->fields(array(
    'timestamp' => REQUEST_TIME,
    'uid' => $user->uid,
    'sid' => session_id(),
    'numfound' => $response->response->numFound,
    'showed_suggestions' => $num_suggestions,
    'total_time' => $response->debug->timing->time,
    'prepare_time' => $response->debug->timing->prepare->time,
    'process_time' => $response->debug->timing->process->time,
    'page' => isset($_GET['page']) ? $_GET['page'] : '',
    'keywords' => $keywords,
    'filters' => serialize(apachesolr_stats_get_active_facets($query
      ->getSearcher())),
    'sort' => serialize($query
      ->getSolrsort()),
    'env_id' => $env_id,
    'page_id' => $search_page['page_id'],
  ))
    ->execute();
  return;

  /*
  $times = array();
  $times['total']['total'] = $response->debug->timing->time;
  foreach (array('prepare', 'process') as $phase) {
    foreach($response->debug->timing->prepare as $key => $value) {
      if (is_object($value)) {
        $times[$phase][$key] = (int) $value->time;
      } else {
        $times[$phase]['total'] = (int) $value;
      }
    }
  }
  dsm($times);
  return;
  */
}