Skip to content

refactor(flowview): split run_flow_query, the report filter and field checks; fix single-thread reports - #324

Draft
somethingwithproof wants to merge 17 commits into
Cacti:developfrom
somethingwithproof:refactor/flowview-query-pipeline
Draft

somethingwithproof wants to merge 17 commits into
Cacti:developfrom
somethingwithproof:refactor/flowview-query-pipeline

Conversation

@somethingwithproof

@somethingwithproof somethingwithproof commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Closes #323.

Behaviour-preserving refactor of the three largest functions in includes/functions.php, followed by fixes for the defects it exposed. The old function names stay as thin wrappers, so hooks and callers are unchanged.

How behaviour is pinned

The first commit adds characterisation tests recorded on the unmodified code; that commit changes only files under tests/. It records 87 cases for run_flow_query() (every report type, single and parallel paths, with and without Filter and time range; the SQL issued and the returned data), 23 for flowview_display_filter() (rendered markup) and 63 for flowview_check_fields(). Every refactor commit passes them unchanged. Each fix: commit then either updates only the expectations for the defect it fixes or leaves them unchanged, and adds a test that fails without the fix.

Verification

An independent differential run fed 2,220 saved-Filter and request combinations (every statistical and printed report, NAT on and off, sort fields, four Filter shapes, and the planning, parallel and one-thread paths) to the pre-refactor and the refactored code and got identical plan cache keys, SQL text and parameters. The golden traces were recorded on unmodified code, in a commit that changes only tests.

Structure

  • flowview_check_fields(): one validator per field group; complexity 101 to at most 10. It has no caller in the plugin; it is kept here so this change stays a pure split, and a follow-up removes it.
  • flowview_display_filter(): row printers; complexity 68 to at most 21. That figure does not count flowview_print_filter_script(), which is still about 822 lines, mostly emitted JavaScript. The script was moved, not reduced; a follow-up moves it to a static file.
  • run_flow_query(): a query pipeline with report catalog, SQL builder, executor and table classes under lib/. flowview_query_report() takes the report columns as a parameter rather than reading them as a global.

Fixes

  • Single-thread path: apply the Filter and time range to single-map reports; keep the Filter in two-map report 11; show the summary report (99) instead of warnings and N/A cells.
  • Head report 11's address column "Source/Destination IP" (the check compared against the wrong report id).
  • Remove the stray '> after the filter dialog button labels.
  • Escape the graph type in the filter page.
  • Start the WHERE with the Stream when a saved Filter has a Stream and no Listener. The condition was appended as ex_addr = ? without WHERE, so each map query read FROM plugin_flowview_raw_... ex_addr = ?, a syntax error on the parallel path and, once the single-thread path applied the Filter, on that path too. No golden trace changes; the plan cache key changes only for this Filter shape, whose query could not run before.
  • Drop the two curl_close() calls in includes/functions.php: the function has had no effect since PHP 8.0 and is deprecated in PHP 8.5. A source scan now fails on a new call in the plugin's own files. The call in setup.php is left for a separate change, as other open pull requests edit that file.

The first four fixes, except the dialog-button fix, also apply to v4.9.

Commits

  • f242f49 test(flowview): pin run_flow_query, the report filter and field checks
  • b53aa3e refactor(flowview): split flowview_check_fields into field validators
  • abcf338 refactor(flowview): split flowview_display_filter into row printers
  • ca5a022 refactor(flowview): add report catalog and table classes under lib/
  • 0c0c4a4 refactor(flowview): split run_flow_query into a query pipeline
  • d77cad0 fix(flowview): keep the Filter in two map reports read in one thread
  • 31f7421 fix(flowview): apply the Filter and time range to one thread reports
  • 5b69491 fix(flowview): show the summary report read in one thread
  • 5abfdc6 fix(flowview): head the Source/Destination IP report's address column
  • ddf27d9 fix(flowview): remove the stray quote from the filter dialog buttons
  • 4e23850 fix(flowview): escape the graph type in the filter page
  • ed22f93 refactor: share FlowView report totals queries
  • 8969ee5 fix(flowview): start the WHERE with a Stream that has no Listener
  • 286959d refactor(flowview): pass the report columns to flowview_query_report
  • 567e738 style(flowview): apply php-cs-fixer to lines this change touches
  • 299453b fix: drop the curl_close() calls deprecated in PHP 8.5

Overlap

#296 and #297 are open and not drafts.

Tests

Full Pest suite passes on PHP 8.3 (593 passed, 1 skipped) and 8.5 (594 passed); php -l clean on 8.2 and 8.3. Changed-line coverage against develop is 100%. The translation template check passes. PHPStan level 6 reported nothing new in plugin code before the last four commits; it has not been re-run since. php-cs-fixer's findings on lines this change touches are applied, except two re-indentations inside the filter page's inline HTML that would misalign those lines. Complexity: run_flow_query 221 to 11, flowview_display_filter 68 to 2, flowview_check_fields 101 to 1; no touched function is above 21.

Record characterisation traces of run_flow_query(),
flowview_display_filter() and flowview_check_fields() before they are
split up: the value returned, the HTML printed, every SQL statement with
its parameters, the log, raised messages, PHP notices, and the request
and session state left behind.

The run_flow_query matrix covers each statistical and printed report,
unknown and missing reports, planning, each request override, each
filter column, the NAT toggle, full and partial partition scans, the
serial and the parallel query, and every result column.  The traces
are compared in tests/fixtures/golden and re-recorded only with
FLOWVIEW_GOLDEN_UPDATE=1.

The UI and session stubs the traces reach move to the unit bootstrap,
so a trace does not depend on which test file ran first.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
flowview_check_fields() repeated the address and subnet checks for the
source and destination, and the bounds check for each of seven number
lists.  Validate the report choice and dates, an address list, a number
list, the TCP flags and the cut-offs in their own functions, and check
the number lists from one table.  The checks, their order and the
messages are unchanged; the golden trace replays identically.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
flowview_display_filter() was 1,192 lines: the title, four filter rows,
the dialogs and the page script, with the Report and Sort Field options
worked out in the middle of the markup.  Work out the title, the Report
options and the Sort Field options in their own functions, and print
each row, the dialogs and the script from its own function.

The markup is moved verbatim; only the code between it moved.  The
Report options lose a branch for no Filter, which the page cannot reach,
as the Report is only shown for a Filter.  The golden trace replays
identically, HTML, SQL text and request changes included.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
ReportSql holds one report's SQL fragments, ReportCatalog maps the
statistical and printed report numbers to them in the original switch
order with loose comparison, and ReportTable and PacketSizeTable render
the result tables. Nothing calls them yet.

List lib/ in the manifest and the classes in the coverage source.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
run_flow_query now applies the request overrides, builds the Filter
WHERE and time range, looks the report up in ReportCatalog, builds the
query structures, runs the parallel or serial query and renders the
table through ReportTable or PacketSizeTable. The SQL text, the
parallel query hash and the output are unchanged; the golden traces
pin them.

Remove the unreachable empty-row branch and unused assignments.
Regenerate the POT for the moved strings; the msgid set is unchanged.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
When the time range applied to a partition, the serial query of a two
map report, such as Source/Destination IP, replaced the Filter's WHERE
with the range. The SQL started with AND and the Filter's parameters no
longer matched the placeholders. Keep the WHERE and append the range, as
the parallel query does.

The golden trace of the partial two map scan with a filter is updated
to the corrected query.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
With one parallel thread, a report with one map query read every
partition whole: the Filter's WHERE and the time range were never
added, so the report ignored both. Build the map query's WHERE the
same way for one map and two, as the parallel query does.

The golden traces of the four affected serial cases are updated to
the corrected queries.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
With one parallel thread, the packet size summary read its one row as
if it were a list of rows, so PHP warned and every cell showed N/A.
Return the row in a list, as the parallel query does.

The golden trace of the serial summary report is updated.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The heading was chosen by comparing the page's report with '11', but a
statistical report is asked for as 's11' and a saved Filter sets no
report, so the merged address column was always headed Source IP. Use
the Filter's statistical report number instead, and drop the report
value that is no longer needed.

The golden traces of the five report 11 cases are updated.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
The Save, Cancel and Continue buttons of the save and rename dialogs
showed a stray "'>" after their label. Remove it.

The golden traces of the filter page are updated; the only change in
them is the four button labels.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Write the graph type into the filter page script with json_encode, as
the other values there are, so it is always a JavaScript string.

The golden traces of the filter page are updated; the only change in
them is the graph type line.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
A saved Filter with a Stream and no Listener added "ex_addr = ?" without
WHERE, so each map query read "FROM plugin_flowview_raw_... ex_addr = ?",
a syntax error on both the one thread and the parallel path. Open the
WHERE with it, as the other conditions do.

No golden trace changes; the cache key changes only for this Filter
shape, whose query could not run before.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
flowview_query_report() read $stat_columns_array as a global, which is
set only because run_flow_query() declares it before requiring
includes/arrays.php. Pass the columns in instead. run_flow_query()
keeps the global declaration it had before the split.

No golden trace changes.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Apply what php-cs-fixer reports on the lines this branch adds or
changes in includes/functions.php and lib/ReportCatalog.php, and
document ReportCatalog::totalsBy(). Two re-indentations the fixer
suggests inside the filter page's inline HTML are left out, as they
would misalign those lines with their neighbours.

No golden trace changes.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
curl_close() has had no effect since PHP 8.0, when cURL handles became
objects freed with their last reference, and PHP 8.5 deprecates it.
Remove the calls in flowview_check_databases() and
flowview_get_owner_from_arin(); neither uses the handle afterwards, and
it is freed when the function returns.

A source scan now fails on a curl_close() call in the plugin's own
files. setup.php keeps its call for now, as other pending changes edit
that file.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
…wo map Filter

Mutation testing of the query pipeline left these unnoticed by the golden
cases: the part of a flow the time range applies to (the cases there read
whole partitions, which skip the range), the order of the parameters across
partitions that are and are not wholly in the range, the Filter and the
cut-off handed to a two map report, the summary report read in one thread
with no flows, the printed flag of a prepared report, an ampersand in the
graph type, and a start date equal to the end date.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

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.

Split run_flow_query, flowview_display_filter and flowview_check_fields; fix single-thread report defects

1 participant