You are here

function apachesolr_stats_exit in Apache Solr Statistics 6.3

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

Implementation of hook_exit().

This is the spot where actual logging takes place.

File

./apachesolr_stats.module, line 114
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;
    }
  }
  db_query("INSERT INTO {apachesolr_stats}\n    (timestamp, uid, sid, numfound, showed_suggestions, total_time, prepare_time, process_time, page, keywords, filters, sort, params, env_id, page_id)\n    VALUES\n    (%d, %d, '%s', %d, %d, %d, %d, %d, '%s', '%s','%s','%s','%s', '%s', '%s')", time(), $user->uid, session_id(), $response->response->numFound, $num_suggestions, $response->debug->timing->time, $response->debug->timing->prepare->time, $response->debug->timing->process->time, isset($_GET['page']) ? $_GET['page'] : '', $keywords, json_encode($filters), json_encode($solrsort), json_encode($params), $env_id, $search_page['page_id']);
  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;
  */
}