You are here

function antispam_generate_statistics_graph in AntiSpam 7

Same name and namespace in other branches
  1. 6 antispam.admin.inc \antispam_generate_statistics_graph()

Generate a graph of service statistics using Google Chart API

1 call to antispam_generate_statistics_graph()
antispam_callback_queue in ./antispam.admin.inc
Menu callback; Moderation queue.

File

./antispam.admin.inc, line 402
The antispam admin theme.

Code

function antispam_generate_statistics_graph() {

  /**
   * Chart 1: Element Ratio Pie Chart
   */

  // Get sum.
  $counts = antispam_get_total_counter();

  // Construct URL for Google Chart API.
  $total = $counts['total_spam'] + $counts['total_ham'];
  $counts['total_spam'] -= $counts['total_fpositive'];
  $counts['total_ham'] -= $counts['total_fnegative'];
  $chart_width = variable_get('antispam_chart_width', 480);
  $output = '<p><img src="http://chart.apis.google.com/chart?';
  $output .= 'chtt=' . t('Total Statistics');

  // Char type: line chart.
  $output .= '&cht=p';

  // Chart size.
  $output .= '&chs=' . $chart_width . 'x' . $chart_width / 2;
  $output .= '&chds=0,' . $total;
  $output .= '&chl=' . t('Spam') . '|' . t('Ham') . '|';

  // Legend.
  $output .= t('False Negative') . '|' . t('False Positive');
  $output .= '&chd=t:' . $counts['total_spam'] . ',' . $counts['total_ham'] . ',';
  $output .= $counts['total_fnegative'] . ',' . $counts['total_fpositive'];

  // Line colors.
  $output .= '&chco=dd6666,ffdd33,444444,bbbbbb';
  $output .= '" alt="Total Statistics Chart" class="antispam-chart" /></p>' . "\n";

  /**
   * Chart 2: Daily Statistics Line Chart (max 1 year)
   */

  // Get max.
  $max_counts = antispam_get_max_counter();
  $y_max = max($max_counts['max_spam'], $max_counts['max_ham']) + 1;

  // Get oldest and latest date within the range.
  $rec = db_query_range("SELECT MIN(date) AS oldest, MAX(date) AS latest FROM {antispam_counter} ORDER BY date DESC", 0, 365)
    ->fetchObject();
  $oldest = $rec ? date('M-d', strtotime($rec->oldest)) : '';
  $latest = $rec ? date('M-d', strtotime($rec->latest)) : date('M-d');

  // Construct URL for Google Chart API.
  $output .= '<p><img src="http://chart.apis.google.com/chart?';
  $output .= 'chtt=' . t('Daily Statistics');

  // Char type: line chart.
  $output .= '&cht=lc';

  // Chart size.
  $output .= '&chs=' . $chart_width . 'x' . (int) ($chart_width * 2 / 3);

  // Legend.
  $output .= '&chdl=' . t('Spam') . '|' . t('Ham');

  // Legend at top.
  $output .= '&chdlp=t';

  // Axis.
  $output .= '&chxt=x,y';

  // X-axis label.
  $output .= '&chxl=0:|' . $latest . '|' . $oldest;

  // X-axis, y-axis range.
  $output .= '&chxr=0,0,365|1,0,' . $y_max;
  $output .= '&chds=0,' . $y_max;

  // Line colors.
  $output .= '&chco=dd6666,ffdd33';
  $output .= '&chd=t:';
  $spam = $ham = array();

  // Max 1 year.
  $result = db_query_range("SELECT * FROM {antispam_counter} ORDER BY date DESC", 0, 365);
  foreach ($result as $rec) {
    $spam[] = $rec->spam_detected;
    $ham[] = $rec->ham_detected;
  }
  foreach ($spam as $spam_count) {
    $output .= $spam_count . ',';
  }

  // Drop trailing comma.
  $output = rtrim($output, ',');
  foreach ($ham as $ham_count) {
    $output .= $ham_count . ',';
  }

  // Drop trailing comma.
  $output = rtrim($output, ',');
  $output .= '" alt="Daily Statistics Chart" class="antispam-chart" /></p>' . "\n";
  return $output;
}