You are here

function mandrill_reports_data in Mandrill 7

Same name and namespace in other branches
  1. 8 modules/mandrill_reports/mandrill_reports.module \mandrill_reports_data()
  2. 7.2 modules/mandrill_reports/mandrill_reports.module \mandrill_reports_data()

Return an associative array containing raw stats.

Return value

array Associative array containing report data.

2 calls to mandrill_reports_data()
mandrill_reports_dashboard_page in modules/mandrill_reports/mandrill_reports.module
Page callback for rendering stats.
mandrill_reports_summary_page in modules/mandrill_reports/mandrill_reports.module
Displays summary information for the active API user.

File

modules/mandrill_reports/mandrill_reports.module, line 52
Main module functions for mandrill_reports.

Code

function mandrill_reports_data() {
  if ($cache = cache_get('mandrill_report_data')) {
    return $cache->data;
  }
  $data = array();
  $api = mandrill_get_api_object();
  if ($api) {

    // Basic user info.
    $data['user'] = $api
      ->users_info();

    // Tags.
    $tags = $api
      ->tags_list();
    foreach ($tags as $tag) {
      if (!empty($tag['tag'])) {
        $data['tags'][$tag['tag']] = $api
          ->tags_info($tag['tag']);
        $data['tags'][$tag['tag']]['time_series'] = $api
          ->tags_time_series($tag['tag']);
      }
    }

    // All time.
    $data['all_time_series'] = $api
      ->tags_all_time_series();

    // Senders: data is not being used, and the API currently (3/12/13) behaves
    // badly if you have a bad email address anywhere in your sender history, so
    // commenting out for now:
    $senders = $api
      ->senders_list();
    foreach ($senders as $sender) {
      try {
        $data['senders'][$sender['address']] = $api
          ->senders_info($sender['address']);
        $data['senders'][$sender['address']]['time_series'] = $api
          ->senders_time_series($sender['address']);
      } catch (MandrillException $e) {
        watchdog('mandrill', 'An error occurred requesting sender information from Mandrill for address %address. "%message"', array(
          '%address' => $sender['address'],
          '%message' => $e
            ->getMessage(),
        ), WATCHDOG_ERROR);
      }
    }

    // Urls.
    $urls = $api
      ->urls_list();
    foreach ($urls as $url) {

      // Api has been intermittently tacking on incomplete $url arrays,
      // so we have to check validity first:
      if (isset($url['url'])) {
        $data['urls'][$url['url']] = $url;
        $data['urls'][$url['url']]['time_series'] = $api
          ->urls_time_series($url['url']);
      }
    }
    cache_set('mandrill_report_data', $data, 'cache', CACHE_TEMPORARY);
  }
  else {
    drupal_set_message(t('Please enter a Mandrill API key to use reports.'));
    drupal_goto('admin/config/services/mandrill');
  }
  return $data;
}