Skip to content

Commit 501622a

Browse files
bmfmanciniTheWitnessCopilot
authored
Refactor codebase for PSR12 compliance and modernization (#202)
* PSR12 - First run Rename functions to match PSR12 requirements * PSR12 run 1 - Change tabs to spaces - add consts * reduce cognetive complexity scores based on PSR12 and sonar scans * Split poller_monitor with helper file * update docblocks * code reorginization Breaking up some of the large files * modernize js Replaced legacy var with const/let. Switched loose checks to strict (===, !==, strict typeof checks). Added default param: applyFilter(action = ''). Used template literals for URL/query string assembly. Converted many callbacks to arrow functions and shorthand methods. Removed one duplicate object key in saveFilter payload (trim was defined twice with same value). Kept existing flow, DOM selectors, action names, and endpoints unchanged. * Update monitor.js Applied the Sonar-requested cleanups in monitor.js: * remove unused variables * remove unused variables * Update copilot-instructions.md * PSR12 formatting * Update db_functions.php * refactor functions Refactor rending function and Ajax call functions Lowe cognetive complexity prepare for easier testing * Add missing docblocks * Apply typing * remove checks for min php version * correct installation errors after PSR12 * try iinstall fix one more time * . * Update monitor_controller.php * Update monitor_controller.php * Update monitor_render.php * fix typing errors * apply php cs-fixer updates * missed 2 commits * Update js/monitor.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update js/monitor.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update copilot-instructions.md --------- Co-authored-by: TheWitness <thewitness@cacti.net> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent d9d2565 commit 501622a

15 files changed

Lines changed: 4367 additions & 3513 deletions

.github/copilot-instructions.md

Lines changed: 70 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -4,153 +4,112 @@
44

55
When generating code for this repository:
66

7-
1. **Version Compatibility**: Respect versions and compatibility declared by this plugin and CI.
8-
2. **Context Files**: If `.github/copilot/*` files are added later, prioritize them first.
9-
3. **Codebase Patterns**: When no explicit guidance exists, follow established patterns in this repository.
10-
4. **Architectural Consistency**: Preserve the current monolithic, procedural Cacti plugin architecture.
11-
5. **Code Quality**: Prioritize maintainability, security, performance, and testability in ways already present in this codebase.
7+
1. **Version Compatibility First**: Honor plugin and CI version constraints before all style preferences.
8+
2. **Repository Context Files**: Prioritize `.github/copilot/*` docs if they are added later.
9+
3. **Agent Profiles as Secondary Context**: Reuse conventions from `.github/agents/*.md` when applicable.
10+
4. **Pattern Matching Over Reinvention**: Mirror existing plugin patterns in the same file/flow.
11+
5. **Architectural Consistency**: Keep the plugin procedural and Cacti-native.
1212

13-
## Technology Version Detection
13+
## Verified Runtime and Compatibility
1414

15-
Before generating code, detect and honor exact versions from repository metadata:
15+
Use only capabilities compatible with observed project metadata:
1616

17-
- **Plugin metadata**: `INFO`
17+
- **Plugin metadata (`INFO`)**
18+
- `name = monitor`
1819
- `version = 2.8`
19-
- `compat = 1.2.15` (Cacti compatibility)
20+
- `compat = 1.2.15`
2021
- `requires = thold:1.2.1`
21-
- **CI runtime matrix**: `.github/workflows/plugin-ci-workflow.yml`
22+
- **CI matrix (`.github/workflows/plugin-ci-workflow.yml`)**
2223
- PHP: `8.1`, `8.2`, `8.3`, `8.4`
24+
- OS: `ubuntu-latest`
2325
- MariaDB service: `10.6`
24-
- **Language/frameworks observed**:
25-
- PHP plugin code (`setup.php`, `monitor.php`, `poller_monitor.php`)
26-
- CSS themes (`monitor.css`, `themes/*/monitor.css`)
27-
- GitHub Actions workflow YAML
26+
- **Observed technologies**
27+
- Procedural PHP plugin files
28+
- CSS theme overlays in `themes/*/monitor.css`
2829
- gettext localization (`locales/po/*.po`, `locales/LC_MESSAGES/*.mo`)
30+
- GitHub Actions integration checks
2931

30-
Do not introduce APIs or syntax incompatible with the supported Cacti/plugin environment and CI matrix.
32+
Do not introduce syntax or APIs that could fail under these versions.
3133

32-
## Context Files
34+
## Architecture and File Responsibilities
3335

34-
If present in future, prioritize `.github/copilot` files in this order:
36+
This is a single Cacti plugin with procedural flows split by responsibility:
3537

36-
- `architecture.md`
37-
- `tech-stack.md`
38-
- `coding-standards.md`
39-
- `folder-structure.md`
40-
- `exemplars.md`
38+
- `setup.php`: plugin lifecycle, hook registration, config arrays/settings, install/upgrade table management.
39+
- `monitor.php`: web entrypoint/bootstrap, includes, session/request setup.
40+
- `monitor_controller.php`: action flow, filter handling, page orchestration.
41+
- `monitor_render.php`: dashboard/group rendering and view-specific output.
42+
- `db_functions.php`: SQL filter/join helpers and status/device query utilities.
43+
- `poller_monitor.php`: CLI poller entrypoint (`--help`, `--version`, `--debug`).
44+
- `poller_functions.php`: poller helper logic (uptime checks, notifications, email payload building).
4145

42-
If these files do not exist, use repository patterns directly.
46+
Avoid OO/framework refactors unless explicitly requested.
4347

44-
## Architecture and Boundaries (Observed)
48+
## Coding Patterns to Preserve
4549

46-
This repository is a **single Cacti plugin** with procedural PHP entrypoints and Cacti hook integration.
50+
### Naming and Structure
4751

48-
- **Plugin lifecycle and hooks**: `setup.php`
49-
- Registration via `api_plugin_register_hook()` and `api_plugin_register_realm()`.
50-
- Upgrade/install logic via `plugin_monitor_install()`, `plugin_monitor_upgrade()`, `monitor_check_upgrade()`.
51-
- **Web UI controller/rendering**: `monitor.php`
52-
- Request routing by `action` switch.
53-
- Rendering and UI state via Cacti helper functions.
54-
- **CLI/poller processing**: `poller_monitor.php`
55-
- Argument parsing (`--help`, `--version`, `--force`, `--debug`).
56-
- Notification and uptime/reboot processing.
57-
- **Presentation assets**: `monitor.css`, `themes/*/monitor.css`, `sounds/`, `images/`.
58-
- **Localization assets**: gettext `.po/.mo` files under `locales/`.
52+
- Use procedural functions with **lowerCamelCase** naming, matching current core files.
53+
- Keep plugin hook callback names exactly synchronized between definitions and `api_plugin_register_hook()` registration strings.
54+
- Keep top-level entrypoints lightweight; place reusable logic in helper files.
5955

60-
Do not refactor this plugin into OO/framework patterns unless the existing code in this repository does so first.
56+
### Cacti Integration
6157

62-
## Codebase Scanning Instructions
58+
- Prefer Cacti APIs already used in this plugin:
59+
- Config/state: `read_config_option()`, `set_config_option()`, `read_user_setting()`, `set_user_setting()`
60+
- Request helpers: `get_request_var()`, `get_filter_request_var()`, `get_nfilter_request_var()`, `set_request_var()`, `validateRequestVars()`
61+
- DB helpers: `db_fetch_assoc()`, `db_fetch_row_prepared()`, `db_fetch_cell_prepared()`, `db_execute_prepared()`
62+
- Plugin hooks/realms: `api_plugin_register_hook()`, `api_plugin_register_realm()`
63+
- Use gettext calls with the `monitor` domain for user-facing strings: `__('Text', 'monitor')`.
6364

64-
For any new change:
65+
### Data and Schema Safety
6566

66-
1. Find similar logic in the same entrypoint type (`setup.php` for hooks/config, `monitor.php` for UI routing/rendering, `poller_monitor.php` for CLI/poller flows).
67-
2. Match these patterns exactly:
68-
- Function naming: `monitor_*`, `plugin_monitor_*`
69-
- Procedural flow with top-level includes and switch routing
70-
- Cacti helper/database APIs (`db_fetch_*`, `db_execute*`, `read_config_option`, `set_config_option`)
71-
- Localization calls: `__('Text', 'monitor')`
72-
3. Reuse existing request handling and sanitization helpers before adding any new input handling.
73-
4. Prefer existing table names and schema migration style in `monitor_check_upgrade()` and related setup functions.
74-
5. Avoid introducing new architectural abstractions not currently used.
67+
- Keep schema evolution in existing setup/upgrade flows (table creation/alter logic in setup lifecycle functions).
68+
- Reuse existing table names and avoid introducing parallel schema variants.
69+
- Prefer prepared DB calls when dynamic values are present.
7570

76-
## Code Quality Standards (Evidence-Based)
71+
## Quality Expectations
7772

7873
### Maintainability
7974

80-
- Keep procedural style and naming consistent with existing files.
81-
- Keep related behavior grouped by responsibility (install/upgrade/hooks in `setup.php`; UI rendering in `monitor.php`; poller logic in `poller_monitor.php`).
82-
- Prefer small helper functions as seen throughout the codebase.
75+
- Keep changes localized to the appropriate responsibility file.
76+
- Favor small helper extractions for complex branches (pattern used in `poller_functions.php`).
77+
- Do not rename public/plugin callback functions unless all call sites and hook strings are updated.
8378

8479
### Security
8580

86-
- Follow current input-handling patterns:
87-
- `get_request_var()`, `get_nfilter_request_var()`, `get_filter_request_var()`, `set_request_var()`
88-
- `validate_request_vars()` where appropriate
89-
- Escape output using existing helpers such as `html_escape()` for HTML contexts.
90-
- Use prepared database calls where parameters are dynamic (`db_fetch_*_prepared`, `db_execute_prepared`) following existing usage.
81+
- Continue current request-validation patterns before consuming request values.
82+
- Escape HTML output with existing helpers (e.g., `html_escape()`).
83+
- Avoid direct string interpolation for dynamic SQL parameters when prepared variants exist.
9184

9285
### Performance
9386

94-
- Match existing data-access style: targeted SQL queries and batched operations.
95-
- Preserve existing poller timing/stat collection behavior in `poller_monitor.php`.
96-
- Avoid adding expensive repeated queries inside loops when existing code already provides reusable query patterns.
87+
- Avoid repeated expensive queries inside loops.
88+
- Follow current query-shaping patterns (precompute lists/maps, then iterate).
89+
- Preserve current poller stat logging behavior and timing model.
9790

98-
### Testability
91+
### Documentation
9992

100-
- Keep logic in discrete functions so behavior can be linted, statically analyzed, and integration-tested as in CI.
101-
- Preserve CLI flags and deterministic output patterns used by workflow checks.
93+
- Keep function docblocks descriptive where present, especially in `poller_functions.php`.
94+
- Keep inline comments concise and only where intent is non-obvious.
95+
- Do not add boilerplate comments for trivial statements.
10296

103-
## Documentation Requirements
97+
## Validation and CI Alignment
10498

105-
Documentation level in this repository is **Standard**:
99+
Before finalizing substantial PHP changes:
106100

107-
- File-level header blocks are consistently present in PHP and workflow files.
108-
- Inline comments are concise and purpose-driven.
109-
- Follow existing style: do not over-document trivial lines.
110-
- Update `CHANGELOG.md` style only when project maintainers require release note updates.
101+
1. Run PHP syntax checks (`php -l`) on modified plugin files.
102+
2. Ensure compatibility with Cacti-driven lint/style checks used in CI (`lint`, `phpcsfixer` scripts run from Cacti workspace).
103+
3. Preserve integration behavior expected by CI:
104+
- Plugin install/enable via Cacti CLI
105+
- Poller execution path and monitor stats logging
111106

112-
## Testing Approach (Observed)
107+
Do not add a new local unit-test framework unless requested.
113108

114-
This repository relies on **integration + static checks** via GitHub Actions:
109+
## Scope Rules for Future Changes
115110

116-
- PHP syntax lint (`php -l` over plugin files)
117-
- Composer-based lint/style checks from Cacti workspace (`lint`, `phpcsfixer` scripts)
118-
- Runtime integration checks by installing Cacti + plugin and running poller
119-
- No repository-local unit test suite is currently present
111+
- Prefer minimal, surgical updates over broad rewrites.
112+
- Keep CSS/theme updates limited to existing theme file layout.
113+
- Keep localization changes aligned to existing gettext workflow/files.
114+
- If guidance conflicts, prefer behavior already validated in runtime entrypoints and CI workflow.
120115

121-
When generating code:
122-
123-
- Ensure code is syntactically valid PHP.
124-
- Keep style and lint compatibility with current Cacti-driven CI steps.
125-
- Do not invent a new test framework in this plugin unless requested.
126-
127-
## PHP-Specific Guidelines
128-
129-
- Maintain procedural PHP structure and Cacti plugin API usage.
130-
- Keep includes, globals, and helper calls consistent with existing patterns.
131-
- Match array formatting and control-flow style used in current files.
132-
- Continue using gettext domain `'monitor'` for user-facing strings.
133-
- Preserve compatibility with CI-validated PHP versions and Cacti/plugin constraints.
134-
135-
## Versioning and Change Tracking
136-
137-
- Follow existing changelog conventions in `CHANGELOG.md` (sectioned by release, `issue#` / `feature#` bullets).
138-
- Treat plugin version metadata in `INFO` as the authoritative plugin version declaration.
139-
140-
## General Best Practices for This Repository
141-
142-
- Prioritize consistency with existing code over introducing newer external patterns.
143-
- Reuse existing Cacti APIs and plugin hooks instead of custom abstractions.
144-
- Keep CSS/theme changes aligned with current theme folder structure.
145-
- Keep localization updates aligned with existing gettext files and domain usage.
146-
- If uncertain, mirror nearby code patterns in the same file first.
147-
148-
## Project-Specific Guidance
149-
150-
- Scan relevant files before generating code; do not assume patterns from unrelated projects.
151-
- Respect current architectural boundaries:
152-
- Hook/config lifecycle in `setup.php`
153-
- UI/rendering workflow in `monitor.php`
154-
- Poller/notification workflow in `poller_monitor.php`
155-
- When conflicts arise, prefer patterns that are currently active in top-level runtime paths and CI-validated flows.
156-
- Always prioritize compatibility and consistency with this repository over external “best practice” rewrites.

0 commit comments

Comments
 (0)