From c2f538879ec54ead6def9037debc907cc692753a Mon Sep 17 00:00:00 2001 From: Arkadiusz Kondas Date: Thu, 29 Oct 2020 14:39:20 +0100 Subject: [PATCH 1/4] Allow to return statistics endpoint data --- src/Powerdns.php | 10 ++++++++++ tests/PowerdnsTest.php | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Powerdns.php b/src/Powerdns.php index 95f12d3..95b80a3 100644 --- a/src/Powerdns.php +++ b/src/Powerdns.php @@ -215,6 +215,16 @@ public function cryptokeys(string $canonicalDomain): Cryptokey return new Cryptokey($this->connector, $canonicalDomain); } + /** + * Query PowerDNS internal statistics. + * + * @return mixed[] + */ + public function statistics(): array + { + return $this->connector->get('statistics'); + } + /** * Get the PowerDNS server version. * diff --git a/tests/PowerdnsTest.php b/tests/PowerdnsTest.php index cd5078d..00f2149 100644 --- a/tests/PowerdnsTest.php +++ b/tests/PowerdnsTest.php @@ -35,6 +35,23 @@ public function testConfigViaMethods(): void $this->assertSame('test-key', $config['apiKey']); } + public function testStatistics(): void + { + $connector = Mockery::mock(Connector::class); + $connector->shouldReceive('get')->withArgs(['statistics'])->once()->andReturn($example = [ + [ + 'name' => 'corrupt-packets', + 'type' => 'StatisticItem', + 'value' => 0, + ], + ]); + + $powerDns = new Powerdns(null, null, null, null, $connector); + $stats = $powerDns->statistics(); + + self::assertEquals($example, $stats); + } + public function testZone(): void { $connector = Mockery::mock(Connector::class); From b7a72fd062e3f087d396fa69a4725b42847f3d19 Mon Sep 17 00:00:00 2001 From: Styxit Date: Fri, 30 Oct 2020 14:35:03 +0100 Subject: [PATCH 2/4] Add statistics parameters --- src/Powerdns.php | 23 ++++++++++++++++++++--- tests/PowerdnsTest.php | 19 ++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/Powerdns.php b/src/Powerdns.php index 95b80a3..9401a19 100644 --- a/src/Powerdns.php +++ b/src/Powerdns.php @@ -217,12 +217,29 @@ public function cryptokeys(string $canonicalDomain): Cryptokey /** * Query PowerDNS internal statistics. + * The ring statistics are disabled by default to speedup the request and reduce the response size. * - * @return mixed[] + * The $statistics and $includeRings parameters are supported in PowerDNS 4.3 and newer. + * On older PowerDNS instances these parameters are ignored. + * + * @param null|string $statistic Optional name of a specific statistic to get. + * @param bool $includeRings Include ring statistics or not. + * + * @return array An array with statistics. */ - public function statistics(): array + public function statistics($statistic = null, $includeRings = false): array { - return $this->connector->get('statistics'); + // Convert $includeRings param to string. + $includeRings = $includeRings ? 'true' : 'false'; + + $endpoint = 'statistics?includerings='.$includeRings; + + // Request a specific statistic. + if ($statistic) { + $endpoint .= '&statistic='.$statistic; + } + + return $this->connector->get($endpoint); } /** diff --git a/tests/PowerdnsTest.php b/tests/PowerdnsTest.php index 00f2149..55fed56 100644 --- a/tests/PowerdnsTest.php +++ b/tests/PowerdnsTest.php @@ -38,7 +38,7 @@ public function testConfigViaMethods(): void public function testStatistics(): void { $connector = Mockery::mock(Connector::class); - $connector->shouldReceive('get')->withArgs(['statistics'])->once()->andReturn($example = [ + $connector->shouldReceive('get')->withArgs(['statistics?includerings=false'])->once()->andReturn($example = [ [ 'name' => 'corrupt-packets', 'type' => 'StatisticItem', @@ -52,6 +52,23 @@ public function testStatistics(): void self::assertEquals($example, $stats); } + public function testStatisticsWithParams(): void + { + $connector = Mockery::mock(Connector::class); + $connector->shouldReceive('get')->withArgs(['statistics?includerings=true&statistic=corrupt-packets'])->once()->andReturn($example = [ + [ + 'name' => 'corrupt-packets', + 'type' => 'StatisticItem', + 'value' => 0, + ], + ]); + + $powerDns = new Powerdns(null, null, null, null, $connector); + $stats = $powerDns->statistics('corrupt-packets', true); + + self::assertEquals($example, $stats); + } + public function testZone(): void { $connector = Mockery::mock(Connector::class); From 2b3f70fe824dfdb56bc8e95fee08983b3dc78bcb Mon Sep 17 00:00:00 2001 From: Styxit Date: Fri, 30 Oct 2020 14:37:02 +0100 Subject: [PATCH 3/4] Update changelog --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a63696b..1e15823 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,11 @@ All notable changes to `powerdns-php` will be documented in this file. Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles. ## Unreleased -[Compare v2.4.0 - Unreleased](https://github.com/exonet/powerdns-php/compare/v2.4.0...develop) +[Compare v2.5.0 - Unreleased](https://github.com/exonet/powerdns-php/compare/v2.5.0...develop) + +## [v2.5.0](https://github.com/exonet/powerdns-php/releases/tag/v2.5.0) - 2020-10-30 +### Added +- Get PowerDNS statistics. ## [v2.4.0](https://github.com/exonet/powerdns-php/releases/tag/v2.4.0) - 2020-08-04 ### Added From 1f508b5585f66b9640d6b0197ca0ef6c8ff229d1 Mon Sep 17 00:00:00 2001 From: Styxit Date: Fri, 30 Oct 2020 14:43:43 +0100 Subject: [PATCH 4/4] Disable composer platform check --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 206bd56..a18989d 100755 --- a/composer.json +++ b/composer.json @@ -39,6 +39,7 @@ } }, "config": { - "sort-packages": true + "sort-packages": true, + "platform-check": false } }