function apachesolr_stats_generate_report_elements in Apache Solr Statistics 6
Same name and namespace in other branches
- 6.3 apachesolr_stats.module \apachesolr_stats_generate_report_elements()
 - 7 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.
3 calls to apachesolr_stats_generate_report_elements()
- apachesolr_stats_report in ./
apachesolr_stats.module  - Callback for admin/reports/apachesolr/stats.
 - apachesolr_stats_report_gadget in ./
apachesolr_stats.module  - Callback function that outputs an XML description for a Google Gadget and terminates PHP execution.
 - apachesolr_stats_report_gadget_element in ./
apachesolr_stats.module  - Callback function used by Gadget javascript to fetch a particular element.
 
File
- ./
apachesolr_stats.module, line 750  - Keeps and reports statistics about Apache Solr usage and performance.
 
Code
function apachesolr_stats_generate_report_elements($granularity) {
  // Initialize
  $facets = apachesolr_stats_get_facets();
  $suggestions = 0;
  $queries = 0;
  $users = array();
  $sessions = array();
  $start_timeslot = 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();
  // Scan the logfile and build statistics arrays
  $result = db_query("SELECT * FROM {apachesolr_stats} WHERE timestamp > %d ORDER BY timestamp DESC", time() - $granularity['time_before']);
  while ($record = db_fetch_object($result)) {
    $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();
      $ok = preg_match_all('/[^ ]+/', $record->filters, $matches);
      foreach ($matches[0] as $word) {
        $fieldname = apachesolr_stats_determine_field_from_query($facets, $word);
        if ($fieldname != false) {
          // Count this facet field only once per query
          if (!isset($facet_processed_flag[$fieldname])) {
            // Add 1 to usage of term from vocabulary
            $facets[$fieldname]['usage']++;
            // Mark so we don't count it again for this query
            $facet_processed_flag[$fieldname] = 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 && !$record->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_usage[$record->sort ? $record->sort : "relevance"]++;
    }
    // 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 + time() - $first_timestamp),
      '@startdate' => format_date($first_timestamp),
      '@enddate' => format_date(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($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($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_urlencode($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;
}