From 9cd08908dcab97fa8da047aa7af205166697af32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Danyi?= Date: Tue, 28 Mar 2017 17:34:07 +0200 Subject: [PATCH] * caching added to nightly config * routes for getting nightly config data --- config/autoload/routes.global.php | 14 +++++++ src/App/Action/NightlyByNodesAction.php | 27 ++++++++++++ src/App/Action/NightlyByNodesFactory.php | 15 +++++++ src/App/Action/NightlyByStreamsAction.php | 27 ++++++++++++ src/App/Action/NightlyByStreamsFactory.php | 15 +++++++ src/App/Service/CiExecutorService.php | 49 +++++++++++++++++----- src/App/Service/CiNodeService.php | 7 ++++ 7 files changed, 143 insertions(+), 11 deletions(-) create mode 100644 src/App/Action/NightlyByNodesAction.php create mode 100644 src/App/Action/NightlyByNodesFactory.php create mode 100644 src/App/Action/NightlyByStreamsAction.php create mode 100644 src/App/Action/NightlyByStreamsFactory.php diff --git a/config/autoload/routes.global.php b/config/autoload/routes.global.php index 1e0406b..0ca2bdc 100644 --- a/config/autoload/routes.global.php +++ b/config/autoload/routes.global.php @@ -12,6 +12,8 @@ return [ App\Action\CiExecutorAction::class => App\Action\CiExecutorFactory::class, App\Action\JcatPackageAction::class => App\Action\JcatPackageFactory::class, App\Action\CiStreamAction::class => App\Action\CiStreamFactory::class, + App\Action\NightlyByNodesAction::class => App\Action\NightlyByNodesFactory::class, + App\Action\NightlyByStreamsAction::class => App\Action\NightlyByStreamsFactory::class, App\Action\HomePageAction::class => App\Action\HomePageFactory::class, ], ], @@ -72,5 +74,17 @@ return [ 'middleware' => App\Action\CiStreamAction::class, 'allowed_methods' => ['GET'], ], + [ + 'name' => 'api.nightly-by-nodes', + 'path' => '/api/nightly-by-nodes', + 'middleware' => App\Action\NightlyByNodesAction::class, + 'allowed_methods' => ['GET'], + ], + [ + 'name' => 'api.nightly-by-streams', + 'path' => '/api/nightly-by-streams', + 'middleware' => App\Action\NightlyByStreamsAction::class, + 'allowed_methods' => ['GET'], + ], ], ]; diff --git a/src/App/Action/NightlyByNodesAction.php b/src/App/Action/NightlyByNodesAction.php new file mode 100644 index 0000000..b451362 --- /dev/null +++ b/src/App/Action/NightlyByNodesAction.php @@ -0,0 +1,27 @@ +ciExecutorService = $ciExecutorService; + } + + public function getList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) + { + return new JsonCorsResponse($this->ciExecutorService->getActiveNightlyConfigurationByNode()); + } +} diff --git a/src/App/Action/NightlyByNodesFactory.php b/src/App/Action/NightlyByNodesFactory.php new file mode 100644 index 0000000..e5a9e42 --- /dev/null +++ b/src/App/Action/NightlyByNodesFactory.php @@ -0,0 +1,15 @@ +get(CiExecutorService::class); + return new NightlyByNodesAction($service); + } +} diff --git a/src/App/Action/NightlyByStreamsAction.php b/src/App/Action/NightlyByStreamsAction.php new file mode 100644 index 0000000..e475526 --- /dev/null +++ b/src/App/Action/NightlyByStreamsAction.php @@ -0,0 +1,27 @@ +ciExecutorService = $ciExecutorService; + } + + public function getList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) + { + return new JsonCorsResponse($this->ciExecutorService->getActiveNightlyConfiguration()); + } +} diff --git a/src/App/Action/NightlyByStreamsFactory.php b/src/App/Action/NightlyByStreamsFactory.php new file mode 100644 index 0000000..a439f6c --- /dev/null +++ b/src/App/Action/NightlyByStreamsFactory.php @@ -0,0 +1,15 @@ +get(CiExecutorService::class); + return new NightlyByStreamsAction($service); + } +} diff --git a/src/App/Service/CiExecutorService.php b/src/App/Service/CiExecutorService.php index 86b251b..451ad03 100644 --- a/src/App/Service/CiExecutorService.php +++ b/src/App/Service/CiExecutorService.php @@ -25,6 +25,7 @@ class CiExecutorService const JCAT_CACHE_KEY = "jcat"; const PACKAGE_CACHE_KEY = "packages"; const STREAM_CACHE_KEY = "stream"; + const NIGHTLY_CACHE_KEY = "nightly"; private $testTypeMap = [ 'isDoRollbackBefore' => 'rollback_before', @@ -692,19 +693,45 @@ class CiExecutorService throw new \Exception("Jenkins redirect url is not working, return code was: " . $response->getStatusCode()); } - public function getActiveNightlyConfiguration() + public function getActiveNightlyConfigurationByNode(bool $forceReload = false) { - $nightlyConfigHtml = $this->getNightlyConfigHtml(); - $streams = $this->getHtmlDropdownValues($nightlyConfigHtml, "STREAM"); - - sort($streams, SORT_NATURAL); - $stapler = $this->getStaplerTokens($nightlyConfigHtml, "STREAM"); - - $result = []; - foreach($streams as $stream) { - $result[$stream] = $this->getNightlyNodesForStream($stream, $stapler['url'], $stapler['token']); + $activeNightlyByStream = $this->getActiveNightlyConfiguration($forceReload); + $configByNodes = []; + foreach ($activeNightlyByStream as $stream => $nodes) { + foreach($nodes as $node) { + if(!isset($configByNodes[$node])) { $configByNodes[$node] = []; } + array_push($configByNodes[$node], $stream); + } } - return $result; + return $configByNodes; + } + + public function getActiveNightlyConfiguration(bool $forceReload = false) + { + $cache = $this->getCache(self::NIGHTLY_CACHE_KEY); + $result = $cache->getItem(self::NIGHTLY_CACHE_KEY, $success); + if (!$success || $forceReload) { + $nightlyConfigHtml = $this->getNightlyConfigHtml(); + $streams = $this->getHtmlDropdownValues($nightlyConfigHtml, "STREAM"); + + sort($streams, SORT_NATURAL); + $stapler = $this->getStaplerTokens($nightlyConfigHtml, "STREAM"); + + $ownNodes = $this->nodeService->getNodeNames(); + + $nightly = []; + foreach($streams as $stream) { + $streamNodes = $this->getNightlyNodesForStream($stream, $stapler['url'], $stapler['token']); + $nodeDiff = array_intersect($streamNodes, $ownNodes); + if($nodeDiff) { + $nightly[$stream] = array_values($nodeDiff); + } + } + $cache->setItem(self::NIGHTLY_CACHE_KEY, serialize($nightly)); + } else { + $nightly = unserialize($result); + } + return $nightly; } private function getHtmlDropdownValues(string $html, string $fieldName): array diff --git a/src/App/Service/CiNodeService.php b/src/App/Service/CiNodeService.php index aae0e61..00908fd 100644 --- a/src/App/Service/CiNodeService.php +++ b/src/App/Service/CiNodeService.php @@ -133,6 +133,13 @@ class CiNodeService return $this->nodes; } + public function getNodeNames(): array + { + return array_map(function($node) { + return $node["name"]; + }, $this->nodes); + } + public function getNode($node): ?array { return array_pop(array_filter($this->nodes, function ($item) use ($node) {