You are here

function apachesolr_stats_chart in Apache Solr Statistics 6

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

Generate an IMG tag with the URL to generate a chart using Google Charts API.

Parameters

string $granularity: The granularity to use.

array $data: The array of data to chart.

integer $start_timeslot: The index of the first data element to chart.

integer $last_timeslot: The index of the first data element to chart.

integer $total_queries: Integer with the total number of queries included in this chart.

boolean $average: Boolean flag: show an average value in the chart.

1 call to apachesolr_stats_chart()
apachesolr_stats_generate_report_elements in ./apachesolr_stats.module
Generates report elements for the given granularity.

File

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

Code

function apachesolr_stats_chart($granularity, $data, $start_timeslot, $last_timeslot, $total_queries, $average = FALSE) {

  // Sample: http://chart.apis.google.com/chart?cht=lc&chs=350x100&chdlp=b&chma=10,10,10,10&chd=s:[encoded chart data]
  $chart_prefix = 'http://chart.apis.google.com/chart?cht=lc&chs=350x100';
  $chart_prefix .= '&chdlp=b&chma=30,100,20,20&chd=s:';
  $chd = array();
  $chd_min = 9999999;
  $chd_max = 0;
  $total = 0;
  for ($t = $start_timeslot; $t <= $last_timeslot; $t++) {
    $num = isset($data[$t]) ? intval($data[$t]) : 0;
    $chd_min = $chd_min > $num ? $num : $chd_min;
    $chd_max = $chd_max < $num ? $num : $chd_max;
    $chd[] = $num;
  }

  // Add missing data for time between last timeslot and current time.
  $current_timeslot = intval(time() / $granularity['timespan']);
  for ($t = $last_timeslot + 1; $t <= $current_timeslot; $t++) {
    $chd_min = $chd_min > 0 ? 0 : $chd_min;

    // Insert at beginning.
    array_unshift($chd, 0);
  }

  // Fix min if min and max are the same.
  if ($chd_min == $chd_max) {
    $chd_min = 0;
  }

  // Since we read the log from the database newest-first, reverse the data for rendering.
  $chd = array_reverse($chd);

  // Generate basic image URL
  $image_url = $chart_prefix . apachesolr_stats_encodedata($chd, $chd_min, $chd_max);

  // Add labels
  $chxl = "";
  if ($chd_max > 0) {

    // Add y-axis labels.
    $chxl .= "0:|" . intval($chd_min) . "|" . intval($chd_max);

    // Show average value in a label on right-hand side.
    if ($average !== FALSE) {
      $image_url .= "&chxp=1," . intval($average / $chd_max * 100);
      $chxl .= sprintf("|1:|%s=%.2f", t('average'), $average);
      $chxt = "y,r";
    }
    else {
      $chxt = "y";
    }

    // Add time/date labels
    $earliest_timestamp = $start_timeslot * $granularity['timespan'];
    $last_timestamp = $current_timeslot * $granularity['timespan'];
    $mid_timestamp = ($last_timestamp + $earliest_timestamp) / 2;
    $time_msg_1 = drupal_urlencode(strftime($granularity['format'], $earliest_timestamp));
    $time_msg_2 = drupal_urlencode(strftime($granularity['format'], $mid_timestamp));
    $time_msg_3 = drupal_urlencode(strftime($granularity['format'], $last_timestamp));
    if ($chxt) {
      $chxt = "{$chxt},x";
      $chxl .= "|2:|{$time_msg_1}|{$time_msg_2}|{$time_msg_3}";
    }
    else {
      $chxt = "x";
      $chxl .= "|1:|{$time_msg_1}|{$time_msg_2}|{$time_msg_3}";
    }
    $image_url .= "&chxl={$chxl}&chxt={$chxt}";
  }

  // Return the image tag
  return "<img src='{$image_url}' />";
}