Jgleeson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/391021 )

Change subject: WIP added new implementation of stats for donations
......................................................................

WIP added new implementation of stats for donations

Change-Id: I996c79b15686af85ed12861f42e48a9e9312464f
---
M composer.lock
M sites/all/modules/queue2civicrm/DonationQueueConsumer.php
M sites/all/modules/queue2civicrm/queue2civicrm.module
3 files changed, 102 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/21/391021/1

diff --git a/composer.lock b/composer.lock
index ab5d26d..5cfa165 100644
--- a/composer.lock
+++ b/composer.lock
@@ -693,12 +693,12 @@
             "source": {
                 "type": "git",
                 "url": "https://github.com/jackgleeson/stats-collector.git";,
-                "reference": "517e4cc310a59555d3de41e3fc4a4d2822ab963a"
+                "reference": "06e2ef5b2563e1a0f58b2f05fa48ebe874238431"
             },
             "dist": {
                 "type": "zip",
-                "url": 
"https://api.github.com/repos/jackgleeson/stats-collector/zipball/517e4cc310a59555d3de41e3fc4a4d2822ab963a";,
-                "reference": "517e4cc310a59555d3de41e3fc4a4d2822ab963a",
+                "url": 
"https://api.github.com/repos/jackgleeson/stats-collector/zipball/06e2ef5b2563e1a0f58b2f05fa48ebe874238431";,
+                "reference": "06e2ef5b2563e1a0f58b2f05fa48ebe874238431",
                 "shasum": ""
             },
             "require": {
@@ -725,7 +725,7 @@
                 }
             ],
             "description": "Utility to help record, analyse and export 
statistics across any PHP process e.g. http request, batch jobs or cli-scrpts",
-            "time": "2017-11-09T22:07:12+00:00"
+            "time": "2017-11-10T18:27:43+00:00"
         },
         {
             "name": "league/csv",
diff --git a/sites/all/modules/queue2civicrm/DonationQueueConsumer.php 
b/sites/all/modules/queue2civicrm/DonationQueueConsumer.php
index 25f0136..c4be75f 100644
--- a/sites/all/modules/queue2civicrm/DonationQueueConsumer.php
+++ b/sites/all/modules/queue2civicrm/DonationQueueConsumer.php
@@ -71,30 +71,104 @@
       _queue2civicrm_log($log);
     }
 
-    if (isset($message['source_enqueued_time'])) {
-      // if the 'source_enqueued_time' is available we work out the time 
between now and 'source_enqueued_time'
-      $age = UtcDate::getUtcTimestamp() - $message['source_enqueued_time'];
-    }
-    else {
-      //otherwise we work out the time difference between now and the 
processor supplied transaction date, recieve_date
-      $age = UtcDate::getUtcTimestamp() - 
UtcDate::getUtcTimestamp($contribution['receive_date']);
-    }
+    // record donations stats
+    $this->recordDonationStats($message, $contribution);
 
-    $statsCollector = StatsCollector::getInstance();
-    $statsCollector->setNamespace("donations");
-
-    // add a stat to record the number of gateway donations
-    $statsCollector->incrementStat("{$message['gateway']}", 1);
-    // add a stat for the message age between enqueued to to now
-    $statsCollector->addStat("{$message['gateway']}.message_age", $age);
-    // workout the average message age between enqueued to to now
-    $statsCollector->addStat(
-      "{$message['gateway']}.average_message_age",
-      $statsCollector->getStatAverage("{$message['gateway']}.message_age"),
-      ['clobber' => TRUE]
-    );
-
-    // Delete any pending db entries with matching gateway and order_id
+    // delete any pending db entries with matching gateway and order_id
     PendingDatabase::get()->deleteMessage($message);
   }
+
+  /**
+   * Record donation stats including:
+   * - Number of donations by gateway
+   * - Average time between gateway transaction datetime and imported to civi 
(now)
+   * - Average time between donation message enqueued datetime and imported to 
civi (now)
+   *
+   * @param array $message
+   * @param array $contribution
+   */
+  protected function recordDonationStats($message, $contribution) {
+    $paymentGateway = $message['gateway'];
+
+    // set the root namespace for the stats to follow.
+    StatsCollector::getInstance()->setNamespace("donations");
+    $this->recordGatewayDonation($paymentGateway);
+    $this->recordOverallDonations();
+    $this->recordGatewayTransactionAge($paymentGateway, 
$contribution['receive_date']);
+    $this->recordOverallGatewayTransactionAge();
+    if (isset($message['source_enqueued_time'])) {
+      $this->recordMessageEnqueuedAge($paymentGateway, 
$message['source_enqueued_time']);
+      $this->recordOverallMessageEnqueuedAge();
+    }
+  }
+
+  protected function recordGatewayDonation($paymentGateway) {
+    $statsCollector = StatsCollector::getInstance();
+    // record a stat to record the number of gateway specific donations
+    $statsCollector->incrementStat("gateway.{$paymentGateway}", 1);
+  }
+
+  protected function recordOverallDonations() {
+    $statsCollector = StatsCollector::getInstance();
+    // set/update the current total sum of gateway donations
+    $statOptions = ['clobber' => TRUE];
+    $statsCollector->addStat(
+      "overall.donations",
+      $statsCollector->getStatSum("gateway.*"),
+      $statOptions
+    );
+  }
+
+  protected function recordGatewayTransactionAge($paymentGateway, 
$transactionDate) {
+    $statsCollector = StatsCollector::getInstance();
+    // work out time between gateway's official transaction time and now
+    $gatewayReceivedAge = UtcDate::getUtcTimestamp() - 
UtcDate::getUtcTimestamp($transactionDate);
+
+    // record a stat for the gateway received age
+    $statsCollector->addStat("received_age.{$paymentGateway}",
+      $gatewayReceivedAge);
+  }
+
+  protected function recordOverallGatewayTransactionAge() {
+    $statsCollector = StatsCollector::getInstance();
+
+    // set/update the overall current moving average for all payment gateway 
received ages
+    $statOptions = ['clobber' => TRUE];
+    $statsCollector->addStat(
+      "overall.average.received_age",
+      $statsCollector->getStatAverage("received_age.*"),
+      $statOptions
+    );
+  }
+
+  protected function recordMessageEnqueuedAge($paymentGateway, $enqueuedTime) {
+    $statsCollector = StatsCollector::getInstance();
+    // work out time between the message enqueued time and now if 
'source_enqueued_time' is set
+    $enqueuedAge = UtcDate::getUtcTimestamp() - $enqueuedTime;
+
+    // record a stat for the message enqueued age
+    $statsCollector->addStat("enqueued_age.{$paymentGateway}", $enqueuedAge);
+
+    // set/update the current moving average of enqueued message ages
+    $statOptions = ['clobber' => TRUE];
+    $statsCollector->addStat(
+      "average.enqueued_age.{$paymentGateway}",
+      $statsCollector->getStatAverage("enqueued_age.{$paymentGateway}"),
+      $statOptions
+    );
+
+  }
+
+  protected function recordOverallMessageEnqueuedAge() {
+    $statsCollector = StatsCollector::getInstance();
+
+    // set/update the current moving average of enqueued message ages
+    $statOptions = ['clobber' => TRUE];
+    $statsCollector->addStat(
+      "overall.average.enqueued_age",
+      $statsCollector->getStatAverage("enqueued_age.*"),
+      $statOptions
+    );
+  }
+
 }
diff --git a/sites/all/modules/queue2civicrm/queue2civicrm.module 
b/sites/all/modules/queue2civicrm/queue2civicrm.module
index feeface..e57d281 100644
--- a/sites/all/modules/queue2civicrm/queue2civicrm.module
+++ b/sites/all/modules/queue2civicrm/queue2civicrm.module
@@ -151,7 +151,7 @@
    */
   $statsCollector = StatsCollector::getInstance();
   $prometheusExporter = new 
PrometheusStatsExporter('donation_stats','/var/spool/prometheus');
-  $stats = $prometheusExporter->export($statsCollector);
+  
$prometheusExporter->export($statsCollector->getStats(['donations.average.*', 
'donations.overall.*'],true));
 
   if ($processed > 0) {
     watchdog('queue2civicrm', 'Successfully processed ' . $processed . ' 
contribution(s).');

-- 
To view, visit https://gerrit.wikimedia.org/r/391021
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I996c79b15686af85ed12861f42e48a9e9312464f
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Jgleeson <jglee...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to