Plugin Directory

Changeset 3033925


Ignore:
Timestamp:
02/10/2024 11:05:16 AM (2 years ago)
Author:
rossigee
Message:

Update to include more metrics.

Location:
woo-prometheus-metrics/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • woo-prometheus-metrics/trunk/readme.txt

    r2009680 r3033925  
    44* Tags: wordpress
    55* Requires at least: 4.7.2
    6 * Tested up to: 4.9.5
     6* Tested up to: 6.4.3
    77* Stable tag: 0.0.4
    88* License: GPLv2
     
    1515* `woocommerce_order_count` - a count of orders on the system, by 'status'.
    1616* `woocommerce_user_count` - a count of users on the system.
     17* `woocommerce_stock` - the number of items in each stock status (no, low, in stock)
     18* `woocommerce_revenue_sum` - The total net revenue (sum of all completed orders)
     19* `woocommerce_items_sold_sum` - The total number of sold items (sum of all completed orders)
    1720
    1821We gather the metrics with the following section of Prometheus configuration:
     
    3942== Changelog ==
    4043
     44= 0.2 =
     45* More metrics (stock levels, total revenue, number of items sold)
     46
    4147= 0.1 =
    4248
  • woo-prometheus-metrics/trunk/woo-prometheus-metrics.php

    r2009680 r3033925  
    1212
    1313require_once(dirname(__FILE__) . "/woo-prometheus-metrics-options.php");
     14
    1415
    1516function woocommerce_metrics_handler_init() {
     
    5253function woocommerce_metrics_handler__handle_request($wp_query) {
    5354  global $uris_to_check;
     55  global $wpdb;
    5456
    5557  $auth_username = get_option("woocommerce_metrics_auth_username");
     
    6769
    6870  // Gather count of products
    69   $product_count = 0;
    70   $_product_count = wp_count_posts("product");
    71   if(isset($_product_count->publish)) {
    72     $product_count = $_product_count->publish;
    73   }
     71  $product_count = count(wc_get_products(['return' => 'ids']));
     72
     73  woocommerce_metrics_output_metric("woocommerce_product_count",
     74    "The number of products.",
     75    "gauge",
     76    $product_count
     77  );
    7478
    7579  // Gather count of orders by status
     80
    7681  $order_statuses = array_keys(wc_get_order_statuses());
    7782  $order_counts = array();
     
    8186  }
    8287
    83   // Gather count of users
    84   $_user_count = count_users();
    85   // Not sure why '$user_count = $_user_count['total_users']' doesn't work!
    86   foreach($_user_count as $k => $v) {
    87     if($k == 'total_users') {
    88       $user_count = $v;
    89     }
    90   }
    91 
    92   header("Content-Type: text/plain");
    93   header('Cache-Control: no-cache');
    94 
    95 
    96   woocommerce_metrics_output_metric("woocommerce_product_count",
    97     "The number of products.",
    98     "gauge",
    99     $product_count
    100   );
    101 
    10288  woocommerce_metrics_output_order_metrics("woocommerce_order_count",
    10389    "The number of orders, by status.",
     
    10591    $order_counts
    10692  );
     93
     94  // Gather count of users
     95  $_user_count = count_users();
     96  $user_count = $_user_count['total_users'];
     97
     98  header("Content-Type: text/plain");
     99  header('Cache-Control: no-cache');
     100
    107101
    108102  woocommerce_metrics_output_metric("woocommerce_user_count",
     
    112106  );
    113107
     108  // Gather stock info
     109  $stock   = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) );
     110  $nostock = absint( max( get_option( 'woocommerce_notify_no_stock_amount' ), 0 ) );
     111  $stock_info = $wpdb->get_row($wpdb->prepare("SELECT
     112          sum(case when lookup.stock_quantity <= %d then 1 else 0 end) as no_stock,
     113          sum(case when lookup.stock_quantity > %d and lookup.stock_quantity < %d then 1 else 0 end) as low_stock,
     114          sum(case when lookup.stock_quantity >= %d then 1 else 0 end) as in_stock
     115        FROM {$wpdb->posts} as pos
     116        INNER JOIN {$wpdb->wc_product_meta_lookup} AS lookup ON posts.ID = lookup.product_id
     117        WHERE posts.post_type IN ( 'product', 'product_variation' )
     118          AND posts.post_status = 'publish'",
     119                $nostock,
     120                $nostock, $stock,
     121                $stock,
     122        ),
     123        ARRAY_A);
     124  woocommerce_metrics_output_order_metrics("woocommerce_stock",
     125    "The number of products in each stock state (no, low, in stock).",
     126    "gauge",
     127    $stock_info
     128  );
     129
     130  // Gather revenue
     131  $revenue = $wpdb->get_var("select sum(net_total) from wp_wc_order_stats where status='wc-completed'");
     132  woocommerce_metrics_output_metric("woocommerce_revenue_sum",
     133    "The total net revenue (sum of all completed orders).",
     134    "gauge",
     135    $revenue
     136  );
     137
     138  // items sold
     139  $items_sold = $wpdb->get_var("select sum(num_items_sold) from wp_wc_order_stats where status='wc-completed'");
     140  woocommerce_metrics_output_metric("woocommerce_items_sold_sum",
     141    "The total number of sold items (sum of all completed orders).",
     142    "gauge",
     143    $items_sold
     144  );
     145
    114146}
Note: See TracChangeset for help on using the changeset viewer.