Changeset 3033925
- Timestamp:
- 02/10/2024 11:05:16 AM (2 years ago)
- Location:
- woo-prometheus-metrics/trunk
- Files:
-
- 2 edited
-
readme.txt (modified) (3 diffs)
-
woo-prometheus-metrics.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
woo-prometheus-metrics/trunk/readme.txt
r2009680 r3033925 4 4 * Tags: wordpress 5 5 * Requires at least: 4.7.2 6 * Tested up to: 4.9.56 * Tested up to: 6.4.3 7 7 * Stable tag: 0.0.4 8 8 * License: GPLv2 … … 15 15 * `woocommerce_order_count` - a count of orders on the system, by 'status'. 16 16 * `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) 17 20 18 21 We gather the metrics with the following section of Prometheus configuration: … … 39 42 == Changelog == 40 43 44 = 0.2 = 45 * More metrics (stock levels, total revenue, number of items sold) 46 41 47 = 0.1 = 42 48 -
woo-prometheus-metrics/trunk/woo-prometheus-metrics.php
r2009680 r3033925 12 12 13 13 require_once(dirname(__FILE__) . "/woo-prometheus-metrics-options.php"); 14 14 15 15 16 function woocommerce_metrics_handler_init() { … … 52 53 function woocommerce_metrics_handler__handle_request($wp_query) { 53 54 global $uris_to_check; 55 global $wpdb; 54 56 55 57 $auth_username = get_option("woocommerce_metrics_auth_username"); … … 67 69 68 70 // 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 ); 74 78 75 79 // Gather count of orders by status 80 76 81 $order_statuses = array_keys(wc_get_order_statuses()); 77 82 $order_counts = array(); … … 81 86 } 82 87 83 // Gather count of users84 $_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_count100 );101 102 88 woocommerce_metrics_output_order_metrics("woocommerce_order_count", 103 89 "The number of orders, by status.", … … 105 91 $order_counts 106 92 ); 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 107 101 108 102 woocommerce_metrics_output_metric("woocommerce_user_count", … … 112 106 ); 113 107 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 114 146 }
Note: See TracChangeset
for help on using the changeset viewer.