Plugin Directory

Changeset 2000813


Ignore:
Timestamp:
12/24/2018 05:22:54 AM (7 years ago)
Author:
rossigee
Message:

Break order metrics down by order status.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • woo-prometheus-metrics/trunk/woo-prometheus-metrics.php

    r1961997 r2000813  
    55Plugin URI: https://wordpress.org/plugins/woo-prometheus-metrics
    66Description: Plugin to monitor the count of products and orders on a Woocommerce site.
    7 Version: 0.0.2
     7Version: 0.0.3
    88Author: Ross Golder <ross@golder.org>
    99Author URI: http://www.golder.org/
     
    4141}
    4242
     43function woocommerce_metrics_output_order_metrics($id, $desc, $type, $values) {
     44  echo "# HELP ".$id." ".$desc."\n";
     45  echo "# TYPE ".$id." ".$type."\n";
     46  foreach($values as $status => $value) {
     47    echo $id."{status=\"".$status."\"} ".$value."\n";
     48  }
     49  echo "\n";
     50}
     51
    4352function woocommerce_metrics_handler__handle_request($wp_query) {
    4453  global $uris_to_check;
     
    5059    $password = $_SERVER['PHP_AUTH_PW'];
    5160    if($auth_username != $username || $auth_password != $password) {
     61      header("HTTP/1.1 401 Unauthorized");
    5262      header('WWW-Authenticate: Basic realm="Woocommerce Metrics"');
    53       header("HTTP/1.1 401 Unauthorized");
    5463      echo "Authorisation required.";
    5564      exit(0);
     
    6473  }
    6574
    66   // Gather count of orders
    67   $order_count = wc_orders_count("processed");
     75  // Gather count of orders by status
     76  $order_statuses = array_keys(wc_get_order_statuses());
     77  $order_counts = array();
     78  foreach($order_statuses as $order_status) {
     79    $order_counts[$order_status] = wc_orders_count($order_status);
     80  }
    6881
    6982  // Gather count of users
     
    7790
    7891  header("Content-Type: text/plain");
     92  header('Cache-Control: no-cache');
     93
    7994
    8095  woocommerce_metrics_output_metric("woocommerce_product_count",
     
    8499  );
    85100
    86   woocommerce_metrics_output_metric("woocommerce_order_count",
    87     "The number of orders.",
     101  woocommerce_metrics_output_order_metrics("woocommerce_order_count",
     102    "The number of orders, by status.",
    88103    "gauge",
    89     $order_count
     104    $order_counts
    90105  );
    91106
Note: See TracChangeset for help on using the changeset viewer.