1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00

added namespace and modified tests

This commit is contained in:
Damodar Lohani 2021-08-08 15:08:44 +05:45
parent 72948624ee
commit d73ddb581c
2 changed files with 48 additions and 14 deletions

View file

@ -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;
}

View file

@ -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());
}
}