From 2dd74ed79b9aea2684d0384e6e1cea2bf4a35191 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Sun, 21 Apr 2024 10:37:08 +0200 Subject: [PATCH] chg: [CLI] Simplify updating JSON structures --- .github/workflows/main.yml | 16 ++-------------- app/Console/Command/AdminShell.php | 16 +++++++++++----- app/Controller/ServersController.php | 5 ++++- app/Model/Server.php | 9 ++++++--- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f9d766b50..c89df8b6c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -184,20 +184,8 @@ jobs: app/Console/cake Admin setSetting "Plugin.ZeroMQ_enable" 1 app/Console/cake Admin setSetting "Plugin.ZeroMQ_audit_notifications_enable" 1 - - name: Update Galaxies - run: app/Console/cake Admin updateGalaxies - - - name: Update Taxonomies - run: app/Console/cake Admin updateTaxonomies - - - name: Update Warninglists - run: app/Console/cake Admin updateWarningLists --verbose - - - name: Update Noticelists - run: app/Console/cake Admin updateNoticeLists - - - name: Update Object Templates - run: app/Console/cake Admin updateObjectTemplates 1 + - name: Update JSON + run: app/Console/cake Admin updateJSON - name: Turn MISP live run: app/Console/cake Admin live 1 diff --git a/app/Console/Command/AdminShell.php b/app/Console/Command/AdminShell.php index 3555fcfd2..b6f61702d 100644 --- a/app/Console/Command/AdminShell.php +++ b/app/Console/Command/AdminShell.php @@ -302,16 +302,22 @@ class AdminShell extends AppShell public function updateJSON() { $this->out('Updating all JSON structures.'); - $results = $this->Server->updateJSON(); - foreach ($results as $type => $result) { + $overallSuccess = true; + foreach ($this->Server->updateJSON() as $type => $result) { $type = Inflector::pluralize(Inflector::humanize($type)); - if ($result !== false) { - $this->out(__('%s updated.', $type)); + if ($result['success']) { + $this->out(__('%s updated in %.2f seconds.', $type, $result['duration'])); } else { $this->out(__('Could not update %s.', $type)); + $this->out($result['result']); + $overallSuccess = false; } } - $this->out('All JSON structures updated. Thank you and have a very safe and productive day.'); + if ($overallSuccess) { + $this->out('All JSON structures updated. Thank you and have a very safe and productive day.'); + } else { + $this->error('Some structure could no be updated'); + } } public function updateGalaxies() diff --git a/app/Controller/ServersController.php b/app/Controller/ServersController.php index 1355d39c7..328fe0470 100644 --- a/app/Controller/ServersController.php +++ b/app/Controller/ServersController.php @@ -2084,7 +2084,10 @@ class ServersController extends AppController public function updateJSON() { - $results = $this->Server->updateJSON(); + $results = []; + foreach ($this->Server->updateJSON() as $type => $result) { + $results[$type] = $results['success']; + } return $this->RestResponse->viewData($results, $this->response->type()); } diff --git a/app/Model/Server.php b/app/Model/Server.php index aea2e9b1e..5b9224a13 100644 --- a/app/Model/Server.php +++ b/app/Model/Server.php @@ -4746,15 +4746,18 @@ class Server extends AppModel return $servers; } + /** + * @return Generator[string, array] + */ public function updateJSON() { - $results = array(); foreach (['Galaxy', 'Noticelist', 'Warninglist', 'Taxonomy', 'ObjectTemplate', 'ObjectRelationship'] as $target) { $model = ClassRegistry::init($target); + $start = microtime(true); $result = $model->update(); - $results[$target] = $result === false ? false : true; + $duration = microtime(true) - $start; + yield $target => ['success' => $result !== false, 'result' => $result, 'duration' => $duration]; } - return $results; } public function resetRemoteAuthKey($id)