Skip to content

refactor: DRY monitor action render wrapper#206

Open
somethingwithproof wants to merge 2 commits intoCacti:developfrom
somethingwithproof:fix/dry-action-render-205
Open

refactor: DRY monitor action render wrapper#206
somethingwithproof wants to merge 2 commits intoCacti:developfrom
somethingwithproof:fix/dry-action-render-205

Conversation

@somethingwithproof
Copy link

Summary

Implements issue #205 by consolidating repeated monitor action handling that performs an action and then renders the page.

Changes

  • added shared helper monitorRunActionAndRender() in monitor_helpers.php
  • updated monitor.php to route repeated action paths through helper:
    • ajax_mute_all
    • ajax_unmute_all
    • dbchange
    • remove
    • saveDb
  • added standalone regression test: tests/test_action_render_wrapper.php

Validation

  • php -l monitor.php
  • php -l monitor_helpers.php
  • php -l tests/test_action_render_wrapper.php
  • php tests/test_action_render_wrapper.php

Issue

Closes #205

Copilot AI review requested due to automatic review settings March 15, 2026 23:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the monitor plugin’s monitor.php action handling by introducing a shared wrapper that runs an action and then renders the monitor page, reducing repeated “do action + drawPage” branches.

Changes:

  • Added monitorRunActionAndRender() helper to centralize action-then-render flow.
  • Updated several monitor.php actions (ajax_mute_all, ajax_unmute_all, dbchange, remove, saveDb) to use the helper.
  • Added a standalone regression test to validate action-before-render ordering and ensure the wrapper is used in monitor.php.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
monitor.php Routes multiple action branches through the new action+render wrapper helper.
monitor_helpers.php Introduces monitorRunActionAndRender() shared helper.
tests/test_action_render_wrapper.php Adds a CLI regression test covering wrapper call order and usage.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +7 to +12
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — updated in 7d38d68 to use the standard Cacti header block in both new files.

* @return void
*/
function monitorRunActionAndRender(callable $action): void {
call_user_func($action);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 7d38d68 to invoke the callable directly ($action()), which also improves readability.

Comment on lines +16 to +27
if (!function_exists('monitorRunActionAndRender')) {
/**
* Execute one monitor action callback and then render the monitor page.
*
* @param callable $action Action callback with no arguments.
*
* @return void
*/
function monitorRunActionAndRender(callable $action): void {
call_user_func($action);
drawPage();
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in follow-up commit 7d38d68.

Comment on lines +1 to +2
<?php

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — updated in 7d38d68 to use the standard Cacti header block in both new files.

Comment on lines +35 to +44
$expected_calls = [
"monitorRunActionAndRender('muteAllHosts');",
"monitorRunActionAndRender('unmuteAllHosts');",
"monitorRunActionAndRender('loadDashboardSettings');",
"monitorRunActionAndRender('removeDashboard');",
"monitorRunActionAndRender('saveSettings');"
];

foreach ($expected_calls as $expected_call) {
if (strpos($source, $expected_call) === false) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 7d38d68 to use regex-based assertions with whitespace tolerance instead of exact string matching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: DRY monitor action render wrapper

2 participants