Introduce NullStatsdDataFactory
authoraddshore <addshorewiki@gmail.com>
Sat, 2 Apr 2016 09:18:54 +0000 (12:18 +0300)
committerReedy <reedy@wikimedia.org>
Sat, 2 Apr 2016 13:02:37 +0000 (13:02 +0000)
I have left the phpdocs in NullStatsdDataFactory
to clearly show the return types of the interface
implemented.

Change-Id: I96cb64b4af16fc087028269a53d539f8c132f81c

autoload.php
includes/WatchedItemStore.php
includes/libs/NullStatsdDataFactory.php [new file with mode: 0644]
tests/phpunit/includes/WatchedItemStoreUnitTest.php

index a4c09e0..c62e99d 100644 (file)
@@ -893,6 +893,7 @@ $wgAutoloadLocalClasses = [
        'NullJob' => __DIR__ . '/includes/jobqueue/jobs/NullJob.php',
        'NullLockManager' => __DIR__ . '/includes/filebackend/lockmanager/LockManager.php',
        'NullRepo' => __DIR__ . '/includes/filerepo/NullRepo.php',
+       'NullStatsdDataFactory' => __DIR__ . '/includes/libs/NullStatsdDataFactory.php',
        'OOUIHTMLForm' => __DIR__ . '/includes/htmlform/OOUIHTMLForm.php',
        'ORAField' => __DIR__ . '/includes/db/DatabaseOracle.php',
        'ORAResult' => __DIR__ . '/includes/db/DatabaseOracle.php',
index 4f3d640..2aa294b 100644 (file)
@@ -57,20 +57,22 @@ class WatchedItemStore {
        /**
         * @param LoadBalancer $loadBalancer
         * @param HashBagOStuff $cache
-        * @param StatsdDataFactoryInterface $stats
         */
        public function __construct(
                LoadBalancer $loadBalancer,
-               HashBagOStuff $cache,
-               StatsdDataFactoryInterface $stats
+               HashBagOStuff $cache
        ) {
                $this->loadBalancer = $loadBalancer;
                $this->cache = $cache;
-               $this->stats = $stats;
+               $this->stats = new NullStatsdDataFactory();
                $this->deferredUpdatesAddCallableUpdateCallback = [ 'DeferredUpdates', 'addCallableUpdate' ];
                $this->revisionGetTimestampFromIdCallback = [ 'Revision', 'getTimestampFromId' ];
        }
 
+       public function setStatsdDataFactory( StatsdDataFactoryInterface $stats ) {
+               $this->stats = $stats;
+       }
+
        /**
         * Overrides the DeferredUpdates::addCallableUpdate callback
         * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
@@ -155,9 +157,9 @@ class WatchedItemStore {
                if ( !self::$instance ) {
                        self::$instance = new self(
                                wfGetLB(),
-                               new HashBagOStuff( [ 'maxKeys' => 100 ] ),
-                               RequestContext::getMain()->getStats()
+                               new HashBagOStuff( [ 'maxKeys' => 100 ] )
                        );
+                       self::$instance->setStatsdDataFactory( RequestContext::getMain()->getStats() );
                }
                return self::$instance;
        }
diff --git a/includes/libs/NullStatsdDataFactory.php b/includes/libs/NullStatsdDataFactory.php
new file mode 100644 (file)
index 0000000..3b272e2
--- /dev/null
@@ -0,0 +1,111 @@
+<?php
+
+use Liuggio\StatsdClient\Entity\StatsdData;
+use Liuggio\StatsdClient\Entity\StatsdDataInterface;
+use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
+
+/**
+ * @author Addshore
+ * @since 1.27
+ */
+class NullStatsdDataFactory implements StatsdDataFactoryInterface {
+
+       /**
+        * This function creates a 'timing' StatsdData.
+        *
+        * @param string|array $key The metric(s) to set.
+        * @param float $time The elapsed time (ms) to log
+        **/
+       public function timing( $key, $time ) {
+       }
+
+       /**
+        * This function creates a 'gauge' StatsdData.
+        *
+        * @param string|array $key The metric(s) to set.
+        * @param float $value The value for the stats.
+        **/
+       public function gauge( $key, $value ) {
+       }
+
+       /**
+        * This function creates a 'set' StatsdData object
+        * A "Set" is a count of unique events.
+        * This data type acts like a counter, but supports counting
+        * of unique occurrences of values between flushes. The backend
+        * receives the number of unique events that happened since
+        * the last flush.
+        *
+        * The reference use case involved tracking the number of active
+        * and logged in users by sending the current userId of a user
+        * with each request with a key of "uniques" (or similar).
+        *
+        * @param  string|array $key The metric(s) to set.
+        * @param  float $value The value for the stats.
+        *
+        * @return array
+        **/
+       public function set( $key, $value ) {
+               return [];
+       }
+
+       /**
+        * This function creates a 'increment' StatsdData object.
+        *
+        * @param string|array $key The metric(s) to increment.
+        * @param float|1      $sampleRate The rate (0-1) for sampling.
+        *
+        * @return array
+        **/
+       public function increment( $key ) {
+               return [];
+       }
+
+       /**
+        * This function creates a 'decrement' StatsdData object.
+        *
+        *
+        * @param string|array $key The metric(s) to decrement.
+        * @param float|1      $sampleRate The rate (0-1) for sampling.
+        *
+        * @return mixed
+        **/
+       public function decrement( $key ) {
+               return [];
+       }
+
+       /**
+        * This function creates a 'updateCount' StatsdData object.
+        *
+        * @param string|array $key The metric(s) to decrement.
+        * @param integer $delta The delta to add to the each metric
+        *
+        * @return mixed
+        **/
+       public function updateCount( $key, $delta ) {
+               return [];
+       }
+
+       /**
+        * Produce a StatsdDataInterface Object.
+        *
+        * @param string $key The key of the metric
+        * @param int $value The amount to increment/decrement each metric by.
+        * @param string $metric The metric type
+        *                      ("c" for count, "ms" for timing, "g" for gauge, "s" for set)
+        *
+        * @return StatsdDataInterface
+        **/
+       public function produceStatsdData(
+               $key,
+               $value = 1,
+               $metric = StatsdDataInterface::STATSD_METRIC_COUNT
+       ) {
+               $data = new StatsdData();
+               $data->setKey( $key );
+               $data->setValue( $value );
+               $data->setMetric( $metric );
+               return $data;
+       }
+
+}
index bc846d3..9dd38df 100644 (file)
@@ -86,8 +86,7 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase {
        private function newWatchedItemStore( LoadBalancer $loadBalancer, HashBagOStuff $cache ) {
                return new WatchedItemStore(
                        $loadBalancer,
-                       $cache,
-                       $this->getMock( StatsdDataFactory::class )
+                       $cache
                );
        }