Skip to content

Commit 43cb190

Browse files
committed
refactor more
1 parent 85e1f75 commit 43cb190

1 file changed

Lines changed: 25 additions & 21 deletions

File tree

LibreNMS/Data/Graphing/Device/ProcessorGraph.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,20 @@ public function definition(array $vars = []): array
8181

8282
$vars = array_merge($this->vars, $vars);
8383

84-
// Count how many files actually exist on disk for the layout
85-
$rrd_count = 0;
84+
// Filter valid datasets and run checkRrdExists exactly once per processor
85+
$valid_datasets = [];
8686
foreach ($this->processors as $proc) {
8787
$rrd_filename = Rrd::name($this->device->hostname, ['processor', $proc->processor_type, $proc->processor_index]);
8888
if (Rrd::checkRrdExists($rrd_filename)) {
89-
$rrd_count++;
89+
$valid_datasets[] = [
90+
'filename' => $rrd_filename,
91+
'descr' => $proc->getFormattedDescription(),
92+
];
9093
}
9194
}
9295

96+
$rrd_count = count($valid_datasets);
97+
9398
if (LibrenmsConfig::getOsSetting($this->device->os, 'processor_stacked')) {
9499
$builder = (new MultiSimplexSeparatedGraphBuilder())
95100
->unitText('Load %')
@@ -101,26 +106,25 @@ public function definition(array $vars = []): array
101106
->divider(max(1, $rrd_count))
102107
->textOrig()
103108
->noTotal();
104-
} else {
105-
$builder = (new MultiLineGraphBuilder())
106-
->unitText('Load %')
107-
->units('')
108-
->totalUnits('%')
109-
->colours('mixed')
110-
->scaleMin(0)
111-
->scaleMax(100)
112-
->noTotal();
113-
}
114109

115-
foreach ($this->processors as $proc) {
116-
$rrd_filename = Rrd::name($this->device->hostname, ['processor', $proc->processor_type, $proc->processor_index]);
117-
if (Rrd::checkRrdExists($rrd_filename)) {
118-
if ($builder instanceof MultiLineGraphBuilder) {
119-
$builder->addDataset($rrd_filename, 'usage', $proc->getFormattedDescription(), area: true);
120-
} else {
121-
$builder->addDataset($rrd_filename, 'usage', $proc->getFormattedDescription());
122-
}
110+
foreach ($valid_datasets as $dataset) {
111+
$builder->addDataset($dataset['filename'], 'usage', $dataset['descr']);
123112
}
113+
114+
return $builder->build($vars);
115+
}
116+
117+
$builder = (new MultiLineGraphBuilder())
118+
->unitText('Load %')
119+
->units('')
120+
->totalUnits('%')
121+
->colours('mixed')
122+
->scaleMin(0)
123+
->scaleMax(100)
124+
->noTotal();
125+
126+
foreach ($valid_datasets as $dataset) {
127+
$builder->addDataset($dataset['filename'], 'usage', $dataset['descr'], area: true);
124128
}
125129

126130
return $builder->build($vars);

0 commit comments

Comments
 (0)