You are here

function apachesolr_stats_generate_report_elements in Apache Solr Statistics 7

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

Generates report elements for the given granularity.

Parameters

string $granularity: Timespan to aggregate report by. Possible values: 'minute', 'hour' or 'day'

Return value

array An indexed array with the report elements; each element is an array with the indexes: 'name' => human-readable name of the element, e.g. "Total queries" 'value' => html with the result. Can be an image, a number, etc.

1 call to apachesolr_stats_generate_report_elements()
apachesolr_stats_report in ./apachesolr_stats.module
Callback for admin/reports/apachesolr/stats.

File

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

Code

function apachesolr_stats_generate_report_elements($page_id, $granularity) {

  // Initialize
  $facets = apachesolr_stats_get_facets();
  $suggestions = 0;
  $users = array();
  $sessions = array();
  $total_requests = 0;
  $last_timeslot = 0;
  $first_timestamp = 0;
  $no_keywords = 0;
  $total_queries = 0;
  $time['max'] = -1;
  $time['min'] = 9999.999;
  $report_elements = array();
  $keywords = array();
  $keywords_noresults = array();
  $simultaneous_fields = array();
  $count_per_granularity = array();
  $data_per_granularity = array();
  $sort_usage = array();

  // Scan the logfile and build statistics arrays
  $result = db_query("SELECT * FROM {apachesolr_stats} WHERE timestamp > :timestamp AND page_id = :page_id ORDER BY timestamp DESC", array(
    ':timestamp' => REQUEST_TIME - $granularity['time_before'],
    ':page_id' => $page_id,
  ));
  foreach ($result as $record) {
    $filters = unserialize($record->filters);
    $timeslot = intval($record->timestamp / $granularity['timespan']);
    if ($last_timeslot == 0) {
      $last_timeslot = $timeslot;
    }
    @$users[$record->uid]++;
    @$sessions[$record->sid]++;

    // Tally suggestions
    if ($record->showed_suggestions) {
      $suggestions++;
    }
    @$total_requests++;
    @($time['total'] += $record->total_time);

    // $time['prepare'] += $record->prepare_time;
    // $time['process'] += $record->process_time;
    // Track max and min response times
    $time['max'] = $time['max'] < $record->total_time ? $record->total_time : $time['max'];
    $time['min'] = $time['min'] > $record->total_time ? $record->total_time : $time['min'];

    // Field usage; only when on first results page (meaning it's a fresh search)
    if ($record->page == "") {
      $facet_processed_flag = array();
      foreach (unserialize($record->filters) as $facet_name => $facet_value) {
        if (isset($facets[$facet_name]) && !isset($facet_processed_flag[$facet_name])) {

          // Add 1 to usage of facet.
          $facets[$facet_name]['usage']++;

          // Mark so we don't count it again for this query.
          $facet_processed_flag[$facet_name] = TRUE;
        }
      }
      if (trim($record->keywords) != "") {
        if (!isset($facet_processed_flag['kw']) || $facet_processed_flag['kw'] != true) {
          $facets['kw']['usage']++;
          $facet_processed_flag['kw'] = true;

          // Keep track of individual keywords used
          $keys_filtered = drupal_strtolower(trim($record->keywords));
          @$keywords[$keys_filtered]++;

          // Count keywords with zero results; but only when no filters issued.
          if ($record->numfound == 0 && !$filters) {
            @$keywords_noresults[$keys_filtered]++;
          }
        }
      }
      else {
        $no_keywords++;
      }

      // Count each unique query
      $facets["any"]['usage']++;

      // Keep track of how many fields were active per query
      @$simultaneous_fields[sizeof($facet_processed_flag)]++;
      $total_queries++;
    }

    // Sort usage; count only the first page of results
    if ($record->page == "") {
      $sort = unserialize($record->sort);
      if (sizeof($sort) == 0) {
        @$sort_usage["relevance"]++;
      }
      else {
        @$sort_usage[$sort['#name']]++;
      }
    }

    // Group some stats into timeslots (minutes, hours) to show trends
    if (empty($user_slot[$record->uid][$timeslot])) {
      @$data_per_granularity['users_per_slot'][$timeslot]++;
      $user_slot[$record->uid][$timeslot] = TRUE;
    }
    if (empty($session_slot[$record->sid][$timeslot])) {
      @$data_per_granularity['sessions_per_slot'][$timeslot]++;
      $session_slot[$record->sid][$timeslot] = TRUE;
    }
    @$data_per_granularity['queries'][$timeslot]++;
    @$count_per_granularity[$timeslot]++;
    @($data_per_granularity['total_time'][$timeslot] += $record->total_time);
    $first_timestamp = $record->timestamp;
  }
  if (sizeof($sessions) == 0 || sizeof($users) == 0 || $total_queries == 0) {
    return array();
  }
  $start_timeslot = $timeslot;
  $earliest_timestamp = $start_timeslot * $granularity['timespan'];
  $report_elements['span'] = array(
    'name' => t('Report span'),
    'value' => t('Last @interval (@startdate to @enddate)', array(
      '@interval' => format_interval(3600 + REQUEST_TIME - $first_timestamp),
      '@startdate' => format_date($first_timestamp),
      '@enddate' => format_date(REQUEST_TIME),
    )) . '<br />' . t('Data points in charts are one point per @granularity.', array(
      '@granularity' => $granularity['name'],
    )),
  );

  #$report_elements['queries'] = array('name' => t('Total requests to Solr'), 'value' => $total_queries);

  // Chart for queries per timeslot
  $chart = apachesolr_stats_chart($granularity, $data_per_granularity['queries'], $start_timeslot, $last_timeslot, $total_queries, $total_queries / ($last_timeslot - $start_timeslot + 1));
  $report_elements['total_queries_per'] = array(
    'name' => t('Requests'),
    'value' => t('Total: @total', array(
      '@total' => $total_queries,
    )) . '<br />' . $chart,
  );

  // Chart for sessions per timeslot
  $chart = apachesolr_stats_chart($granularity, $data_per_granularity['sessions_per_slot'], $start_timeslot, $last_timeslot, sizeof($sessions), sizeof($sessions) / ($last_timeslot - $start_timeslot + 1));
  $report_elements['total_sessions_per'] = array(
    'name' => t('Unique sessions'),
    'value' => t('Total: @total', array(
      '@total' => sizeof($sessions),
    )) . '<br />' . $chart,
  );
  $report_elements['avg_queries_session'] = array(
    'name' => t('Average requests per session'),
    'value' => sprintf("%.1f", $total_queries / sizeof($sessions)),
  );

  // Chart for average time per timeslot
  $data = array();
  foreach ($data_per_granularity['total_time'] as $timeslot => $value) {
    $data[$timeslot] = $value / $count_per_granularity[$timeslot];
  }

  // Call with average_empty = FALSE
  $chart = apachesolr_stats_chart($granularity, $data, $start_timeslot, $last_timeslot, $total_queries, $time['total'] / $total_queries);
  $report_elements['query_avg_time'] = array(
    'name' => t('Average time per request (miliseconds)'),
    'value' => sprintf("%s: %.2f ms / %s: %.2f ms / %s: %.2f ms", t('Minimum'), $time['min'], t('Average'), $time['total'] / $total_queries, t('Maximum'), $time['max']) . '</br>' . $chart,
  );

  // Most-used keywords
  $report_elements['keywords'] = array(
    'name' => t('Top search phrases'),
    'value' => apachesolr_stats_report_frequent_keywords($page_id, $keywords, $keywords_noresults),
  );

  // Most-used keywords with no results
  $report_elements['keywords_noresults'] = array(
    'name' => t('Top search phrases with no results'),
    'value' => apachesolr_stats_report_frequent_keywords($page_id, $keywords_noresults, $keywords_noresults, "error"),
  );

  // Total spellchecker suggestions
  $report_elements['spellchecker'] = array(
    'name' => t('Total spellchecker suggestions'),
    'value' => $suggestions,
  );

  // Chart for sort usage
  $leyends = array();
  foreach ($sort_usage as $key => $value) {
    $leyends[] = drupal_encode_path($key);
  }
  $chl = implode('|', $leyends);
  $chd = implode(',', $sort_usage);
  $chart = "<img src='http://chart.apis.google.com/chart?cht=p3&chd=t:{$chd}&chs=350x100&chl={$chl}' />";
  $report_elements['sort_usage'] = array(
    'name' => t('Sort usage'),
    'value' => $chart,
  );

  // Chart for field usage
  $report_elements['field_usage'] = array(
    'name' => t('Facet usage'),
    'value' => apachesolr_stats_facet_usage_graph($facets) . apachesolr_stats_facet_usage_table($facets),
  );
  return $report_elements;
}