From d73ddb581c1fd5f759442dec0108fcd2f9de11c2 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 8 Aug 2021 15:08:44 +0545 Subject: [PATCH] added namespace and modified tests --- src/Appwrite/Stats/Stats.php | 28 +++++++++++++++++++++++++++- tests/unit/Stats/StatsTest.php | 34 +++++++++++++++++++++------------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/Appwrite/Stats/Stats.php b/src/Appwrite/Stats/Stats.php index 4b9eddf85f..b07c4c85c5 100644 --- a/src/Appwrite/Stats/Stats.php +++ b/src/Appwrite/Stats/Stats.php @@ -16,6 +16,11 @@ class Stats */ protected $statsd; + /** + * @var string + */ + protected $namespace = 'appwrite.usage'; + /** * Event constructor. * @@ -49,6 +54,26 @@ class Stats return (isset($this->params[$key])) ? $this->params[$key] : null; } + /** + * @param string $namespace + * + * @return $this + */ + public function setNamespace(string $namespace): self + { + $this->namespace = $namespace; + + return $this; + } + + /** + * @return string + */ + public function getNamespace() + { + return $this->namespace; + } + /** * Submit data to StatsD. */ @@ -72,7 +97,7 @@ class Stats $tags = ",project={$projectId},version=" . App::getEnv('_APP_VERSION', 'UNKNOWN'); // the global namespace is prepended to every key (optional) - $this->statsd->setNamespace('appwrite.usage'); + $this->statsd->setNamespace($this->namespace); if ($httpRequest >= 1) { $this->statsd->increment('requests.all' . $tags . ',method=' . \strtolower($httpMethod)); @@ -97,6 +122,7 @@ class Stats public function reset(): self { $this->params = []; + $this->namespace = 'appwrite.usage'; return $this; } diff --git a/tests/unit/Stats/StatsTest.php b/tests/unit/Stats/StatsTest.php index ad660330ab..784ac77fb4 100644 --- a/tests/unit/Stats/StatsTest.php +++ b/tests/unit/Stats/StatsTest.php @@ -20,7 +20,7 @@ class StatsTest extends TestCase $connection = new \Domnikl\Statsd\Connection\UdpSocket($host, $port); $statsd = new \Domnikl\Statsd\Client($connection); - + $this->object = new Stats($statsd); } @@ -28,34 +28,42 @@ class StatsTest extends TestCase { } + public function testNamespace() + { + $this->object->setNamespace('appwritetest.usage'); + $this->assertEquals('appwritetest.usage', $this->object->getNamespace()); + } + public function testParams() { $this->object - ->setParam('statsKey1', 'statsValue1') - ->setParam('statsKey2', 'statsValue2') + ->setParam('projectId', 'appwrite_test') + ->setParam('networkRequestSize', 100) ; + $this->assertEquals('appwrite_test', $this->object->getParam('projectId')); + $this->assertEquals(100, $this->object->getParam('networkRequestSize')); + $this->object->submit(); - $this->assertEquals(null, $this->object->getParam('statsKey1')); - $this->assertEquals(null, $this->object->getParam('statsKey2')); - $this->assertEquals(null, $this->object->getParam('statsKey3')); + $this->assertEquals(null, $this->object->getParam('projectId')); + $this->assertEquals(null, $this->object->getParam('networkRequestSize')); } public function testReset() { $this->object - ->setParam('statsKey1', 'statsValue1') - ->setParam('statsKey2', 'statsValue2') + ->setParam('projectId', 'appwrite_test') + ->setParam('networkRequestSize', 100) ; - $this->assertEquals('statsValue1', $this->object->getParam('statsKey1')); - $this->assertEquals('statsValue2', $this->object->getParam('statsKey2')); + $this->assertEquals('appwrite_test', $this->object->getParam('projectId')); + $this->assertEquals(100, $this->object->getParam('networkRequestSize')); $this->object->reset(); - $this->assertEquals(null, $this->object->getParam('statsKey1')); - $this->assertEquals(null, $this->object->getParam('statsKey2')); - $this->assertEquals(null, $this->object->getParam('statsKey3')); + $this->assertEquals(null, $this->object->getParam('projectId')); + $this->assertEquals(null, $this->object->getParam('networkRequestSize')); + $this->assertEquals('appwrite.usage', $this->object->getNamespace()); } }