fix(graphs): group legends using the instance graph identity (#8528)

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
This commit is contained in:
Thomas Vincent authored and GitHub committed 2026-11-02 17:17:29 +00:00
1 parent 250a725f10
commit cd40748543
6 files changed
+89 -2

No files matched your search

+1
View File
@@ -0,0 +1 @@
Keep graph item groups and their legends within the selected instance graph
+1 -1
View File
@@ -4274,7 +4274,7 @@ function get_graph_group(int $graph_template_item_id) : array {
$params[] = $graph_item['graph_template_id'];
$sql_where = 'graph_template_id = ? AND local_graph_id = 0';
} else {
$params[] = $graph_item['sequence'];
$params[] = $graph_item['local_graph_id'];
$sql_where = 'local_graph_id = ?';
}
@@ -0,0 +1,22 @@
<?php
/*
+-------------------------------------------------------------------------+
| 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
*/
require_once dirname(__DIR__, 3) . '/Helpers/IsolatedProbe.php';
test('graph groups retain calculated legends and stop at their own boundary', function () {
$groups = cacti_test_isolated_probe(dirname(__DIR__, 2) . '/fixtures/graph_group_probe.php');
expect($groups['instance'])->toBe([101, 102, 103, 104]);
expect($groups['template'])->toBe([301, 302]);
expect($groups['non_parent'])->toBe([]);
});
+59
View File
@@ -0,0 +1,59 @@
<?php
/*
+-------------------------------------------------------------------------+
| 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
*/
require_once dirname(__DIR__, 2) . '/Helpers/RequestCoverage.php';
$database = new PDO('sqlite::memory:');
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$database->exec('CREATE TABLE graph_templates_item (id INTEGER PRIMARY KEY, sequence INTEGER, local_graph_id INTEGER, graph_template_id INTEGER, graph_type_id INTEGER, text_format TEXT, hard_return TEXT)');
$insert = $database->prepare('INSERT INTO graph_templates_item VALUES (?, ?, ?, ?, ?, ?, ?)');
foreach ([
[101, 7, 42, 5, 4, 'Series', ''],
[102, 8, 42, 5, 1, '|95:bytes:0:current:2|', ''],
[103, 9, 42, 5, 1, '|sum:0:current:2:auto|', ''],
[104, 10, 42, 5, 9, 'Last', 'on'],
[105, 11, 42, 5, 9, 'Next group', ''],
[201, 8, 7, 5, 9, 'Another graph', ''],
[301, 7, 0, 9, 4, 'Template series', ''],
[302, 8, 0, 9, 9, 'Template last', 'on'],
[303, 9, 0, 10, 9, 'Another template', ''],
] as $row) {
$insert->execute($row);
}
function db_fetch_row_prepared($sql, $params = []) {
global $database;
$statement = $database->prepare($sql);
$statement->execute($params);
return $statement->fetch(PDO::FETCH_ASSOC);
}
function db_fetch_assoc_prepared($sql, $params = []) {
global $database;
$statement = $database->prepare($sql);
$statement->execute($params);
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
require_once dirname(__DIR__, 3) . '/lib/functions.php';
$graph_item_types = [1 => 'COMMENT', 4 => 'LINE1', 9 => 'GPRINT'];
$groups = [
'instance' => array_values(get_graph_group(101)),
'template' => array_values(get_graph_group(301)),
'non_parent' => array_values(get_graph_group(104)),
];
if ($groups !== ['instance' => [101, 102, 103, 104], 'template' => [301, 302], 'non_parent' => []]) {
throw new RuntimeException('Graph grouping mixed identities or crossed its group boundary: ' . json_encode($groups));
}
print json_encode($groups, JSON_THROW_ON_ERROR);
+2
View File
@@ -49,3 +49,5 @@ run_php_probe tests/fixtures/Core/PackageArchive.php '^(lib/package\.php|tests/f
run_probe tests/e2e/installer-coverage.sh '^(lib/installer\.php|cli/(install_cacti|upgrade_database)\.php|install/(functions|preflight|report|background|install)\.php|tests/e2e/installer)' 18277
run_php_probe tests/fixtures/Installer/RetiredThemeCleanup.php '^(install/(functions\.php|upgrades/1_3_0\.php)|tests/fixtures/Installer/RetiredThemeCleanup\.php)'
run_php_probe tests/Unit/fixtures/graph_group_probe.php '^(lib/functions\.php|tests/Unit/fixtures/graph_group_probe\.php)'
@@ -40,7 +40,7 @@ class RequestDispatchTest(unittest.TestCase):
self.dispatch('lib/rrd.php', 'FallbackSourceIdentity.php')
def test_account_change_runs_the_native_audit_probe(self):
self.dispatch('lib/functions.php', 'UserAdministrationAuditCoverage.php')
self.dispatch('lib/functions.php', ['UserAdministrationAuditCoverage.php', 'graph_group_probe.php'])
def test_auth_change_runs_the_native_auth_probe(self):
self.dispatch('lib/auth.php', 'BoundaryContracts.php')
@@ -80,6 +80,9 @@ class RequestDispatchTest(unittest.TestCase):
path = repository / 'tests/fixtures' / script
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text('<?php // dispatch fixture\n')
probe = repository / 'tests/Unit/fixtures/graph_group_probe.php'
probe.parent.mkdir(parents=True, exist_ok=True)
probe.write_text('<?php // dispatch fixture\n')
git('add', '.')
git('commit', '--quiet', '-m', 'fixture base')
base = git('rev-parse', 'HEAD')