Skip to content

Commit 40c6b45

Browse files
murrantStyleCIBot
andauthored
fix some undefined variables in smart app page (librenms#18321)
* fix some undefined variables in smart app page and make it a little bit more readable * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 696d2f2 commit 40c6b45

1 file changed

Lines changed: 96 additions & 140 deletions

File tree

includes/html/pages/device/apps/smart.inc.php

Lines changed: 96 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,71 @@
22

33
print_optionbar_start();
44

5-
$link_array = [
5+
$baseLink = [
66
'page' => 'device',
77
'device' => $device['device_id'],
88
'tab' => 'apps',
99
'app' => 'smart',
1010
];
1111

12-
$drives = [];
12+
$disks = $app->data['disks'] ?? [];
1313

14-
$app_data = $app->data;
15-
16-
if (isset($app_data['disks']) && is_array($app_data['disks'])) {
17-
array_multisort(array_keys($app_data['disks']), SORT_ASC, $app_data['disks']);
14+
if (! empty($disks)) {
15+
ksort($disks);
1816
}
1917

20-
foreach ($app_data['disks'] as $label => $disk_data) {
21-
$disk = $label;
22-
$health_status = '';
18+
$driveLinks = [];
19+
foreach ($disks as $diskName => $diskData) {
20+
$label = $diskName;
2321

24-
if (isset($vars['disk']) && $vars['disk'] == $disk) {
25-
$label = '<span class="pagemenu-selected">' . $label . '</span>';
22+
if (isset($vars['disk']) && $vars['disk'] === $diskName) {
23+
$label = "<span class=\"pagemenu-selected\">{$label}</span>";
2624
}
2725

28-
if (isset($app_data['disks'][$disk]['health_pass'])) {
29-
if ($app_data['disks'][$disk]['health_pass'] == 1) {
30-
$health_status = '(OK)';
31-
} else {
32-
$health_status = '(FAIL)';
33-
}
34-
}
26+
$healthStatus = match ($diskData['health_pass'] ?? null) {
27+
1 => ' (OK)',
28+
0 => ' (FAIL)',
29+
default => '',
30+
};
3531

36-
array_push($drives, generate_link($label, $link_array, ['disk' => $disk]) . $health_status);
32+
$driveLinks[] = generate_link($label, $baseLink, ['disk' => $diskName]) . $healthStatus;
3733
}
3834

39-
printf('%s | drives: %s', generate_link('All Drives', $link_array), implode(', ', $drives));
35+
echo generate_link('All Drives', $baseLink) . ' | drives: ' . implode(', ', $driveLinks);
4036

4137
print_optionbar_end();
4238

4339
if (isset($vars['disk'])) {
44-
if (! isset($app_data['legacy'])) {
40+
$currentDisk = $disks[$vars['disk']] ?? [];
41+
42+
if (! isset($app->data['legacy']) && ! empty($currentDisk)) {
4543
print_optionbar_start();
46-
if (isset($app_data['disks'][$vars['disk']]['disk'])) {
47-
echo 'Disk: ' . $app_data['disks'][$vars['disk']]['disk'] . "<br>\n";
48-
}
49-
if (isset($app_data['disks'][$vars['disk']]['serial'])) {
50-
echo 'Serial: ' . $app_data['disks'][$vars['disk']]['serial'] . "<br>\n";
51-
}
52-
if (isset($app_data['disks'][$vars['disk']]['vendor'])) {
53-
echo 'Vendor: ' . $app_data['disks'][$vars['disk']]['vendor'] . "<br>\n";
54-
}
55-
if (isset($app_data['disks'][$vars['disk']]['product'])) {
56-
echo 'Product: ' . $app_data['disks'][$vars['disk']]['product'] . "<br>\n";
57-
}
58-
if (isset($app_data['disks'][$vars['disk']]['model_family'])) {
59-
echo 'Model Family: ' . $app_data['disks'][$vars['disk']]['model_family'] . "<br>\n";
60-
}
61-
if (isset($app_data['disks'][$vars['disk']]['model_number'])) {
62-
echo 'Model Number: ' . $app_data['disks'][$vars['disk']]['model_number'] . "<br>\n";
63-
}
64-
if (isset($app_data['disks'][$vars['disk']]['device_model'])) {
65-
echo 'Device Model: ' . $app_data['disks'][$vars['disk']]['device_model'] . "<br>\n";
66-
}
67-
if (isset($app_data['disks'][$vars['disk']]['revision'])) {
68-
echo 'Revision: ' . $app_data['disks'][$vars['disk']]['revision'] . "<br>\n";
69-
}
70-
if (isset($app_data['disks'][$vars['disk']]['fw_version'])) {
71-
echo 'FW Version: ' . $app_data['disks'][$vars['disk']]['fw_version'] . "<br>\n";
44+
45+
$diskFields = [
46+
'disk' => 'Disk',
47+
'serial' => 'Serial',
48+
'vendor' => 'Vendor',
49+
'product' => 'Product',
50+
'model_family' => 'Model Family',
51+
'model_number' => 'Model Number',
52+
'device_model' => 'Device Model',
53+
'revision' => 'Revision',
54+
'fw_version' => 'FW Version',
55+
];
56+
57+
foreach ($diskFields as $field => $label) {
58+
if (isset($currentDisk[$field])) {
59+
echo "{$label}: {$currentDisk[$field]}<br>\n";
60+
}
7261
}
73-
if (isset($app_data['disks'][$vars['disk']]['selftest_log'])) {
74-
echo '<pre>' . str_replace('n#', "\n#", $app_data['disks'][$vars['disk']]['selftest_log']) . "</pre><br>\n";
62+
63+
if (isset($currentDisk['selftest_log'])) {
64+
echo '<pre>' . str_replace('n#', "\n#", $currentDisk['selftest_log']) . "</pre><br>\n";
7565
}
66+
67+
print_optionbar_end();
7668
}
77-
print_optionbar_end();
69+
7870
$graphs = [
7971
'smart_big5' => 'Reliability / Age',
8072
'smart_temp' => 'Temperature',
@@ -84,110 +76,74 @@
8476
'smart_tests_ran' => 'S.M.A.R.T self-tests run count',
8577
'smart_runtime' => 'Power On Hours',
8678
];
87-
if ($app_data['disks'][$vars['disk']]['is_ssd'] != 1) {
79+
80+
if (($currentDisk['is_ssd'] ?? 0) !== 1) {
8881
unset($graphs['smart_ssd']);
8982
}
9083
} else {
91-
$graphs = [];
92-
93-
if ($app_data['has']['id5'] == 1) {
94-
$graphs['smart_id5'] = 'ID# 5, Reallocated Sectors Count';
95-
}
96-
97-
if ($app_data['has']['id9'] == 1) {
98-
$graphs['smart_id9'] = 'ID# 9, Power On Hours';
99-
}
100-
101-
if ($app_data['has']['id10'] == 1) {
102-
$graphs['smart_id10'] = 'ID# 10, Spin Retry Count';
103-
}
104-
105-
if ($app_data['has']['id173'] == 1) {
106-
$graphs['smart_id173'] = 'ID# 173, SSD Wear Leveller Worst Case Erase Count';
107-
}
108-
109-
if ($app_data['has']['id177'] == 1) {
110-
$graphs['smart_id177'] = 'ID# 177, SSD Wear Leveling Count';
111-
}
112-
113-
if ($app_data['has']['id183'] == 1) {
114-
$graphs['smart_id183'] = 'ID# 183, Detected Uncorrectable Bad Blocks';
115-
}
116-
117-
if ($app_data['has']['id184'] == 1) {
118-
$graphs['smart_id184'] = 'ID# 184, End-to-End error / IOEDC';
119-
}
120-
121-
if ($app_data['has']['id187'] == 1) {
122-
$graphs['smart_id187'] = 'ID# 187, Reported Uncorrectable Errors';
123-
}
124-
125-
if ($app_data['has']['id188'] == 1) {
126-
$graphs['smart_id188'] = 'ID# 188, Command Timeout';
127-
}
128-
129-
if ($app_data['has']['id190'] == 1 || $app_data['has']['id194'] == 1) {
130-
$graphs['smart_maxtemp'] = 'Max Temp(C), Airflow Temperature or Device';
131-
}
132-
133-
if ($app_data['has']['id190'] == 1) {
134-
$graphs['smart_id190'] = 'ID# 190, Airflow Temperature (C)';
135-
}
136-
137-
if ($app_data['has']['id194'] == 1) {
138-
$graphs['smart_id194'] = 'ID# 194, Temperature (C)';
139-
}
140-
141-
if ($app_data['has']['id196'] == 1) {
142-
$graphs['smart_id196'] = 'ID# 196, Reallocation Event Count';
143-
}
144-
145-
if ($app_data['has']['id197'] == 1) {
146-
$graphs['smart_id197'] = 'ID# 197, Current Pending Sector Count';
147-
}
148-
149-
if ($app_data['has']['id198'] == 1) {
150-
$graphs['smart_id198'] = 'ID# 198, Uncorrectable Sector Count / Offline Uncorrectable / Off-Line Scan Uncorrectable Sector Count';
151-
}
152-
153-
if ($app_data['has']['id199'] == 1) {
154-
$graphs['smart_id199'] = 'ID# 199, UltraDMA CRC Error Count';
155-
}
84+
$smartAttributes = [
85+
'id5' => ['smart_id5', 'ID# 5, Reallocated Sectors Count'],
86+
'id9' => ['smart_id9', 'ID# 9, Power On Hours'],
87+
'id10' => ['smart_id10', 'ID# 10, Spin Retry Count'],
88+
'id173' => ['smart_id173', 'ID# 173, SSD Wear Leveller Worst Case Erase Count'],
89+
'id177' => ['smart_id177', 'ID# 177, SSD Wear Leveling Count'],
90+
'id183' => ['smart_id183', 'ID# 183, Detected Uncorrectable Bad Blocks'],
91+
'id184' => ['smart_id184', 'ID# 184, End-to-End error / IOEDC'],
92+
'id187' => ['smart_id187', 'ID# 187, Reported Uncorrectable Errors'],
93+
'id188' => ['smart_id188', 'ID# 188, Command Timeout'],
94+
'id190' => ['smart_id190', 'ID# 190, Airflow Temperature (C)'],
95+
'id194' => ['smart_id194', 'ID# 194, Temperature (C)'],
96+
'id196' => ['smart_id196', 'ID# 196, Reallocation Event Count'],
97+
'id197' => ['smart_id197', 'ID# 197, Current Pending Sector Count'],
98+
'id198' => ['smart_id198', 'ID# 198, Uncorrectable Sector Count / Offline Uncorrectable / Off-Line Scan Uncorrectable Sector Count'],
99+
'id199' => ['smart_id199', 'ID# 199, UltraDMA CRC Error Count'],
100+
'id231' => ['smart_id231', 'ID# 231, SSD Life Left'],
101+
'id232' => ['smart_id232', 'ID# 232, Available Reserved Space'],
102+
'id233' => ['smart_id233', 'ID# 233, Media Wearout Indicator'],
103+
];
156104

157-
if ($app_data['has']['id231'] == 1) {
158-
$graphs['smart_id231'] = 'ID# 231, SSD Life Left';
159-
}
105+
$graphs = [];
106+
$hasData = $app->data['has'] ?? [];
160107

161-
if ($app_data['has']['id232'] == 1) {
162-
$graphs['smart_id232'] = 'ID# 232, Available Reservd Space';
108+
foreach ($smartAttributes as $attribute => [$graphKey, $graphLabel]) {
109+
if (($hasData[$attribute] ?? 0) === 1) {
110+
$graphs[$graphKey] = $graphLabel;
111+
}
163112
}
164113

165-
if ($app_data['has']['id233'] == 1) {
166-
$graphs['smart_id233'] = 'ID# 233, Media Wearout Indicator';
114+
if (($hasData['id190'] ?? 0) === 1 || ($hasData['id194'] ?? 0) === 1) {
115+
$graphs = ['smart_maxtemp' => 'Max Temp(C), Airflow Temperature or Device'] + $graphs;
167116
}
168117
}
169118

170-
foreach ($graphs as $key => $text) {
171-
$graph_type = $key;
172-
$graph_array['height'] = '100';
173-
$graph_array['width'] = '215';
174-
$graph_array['to'] = \App\Facades\LibrenmsConfig::get('time.now');
175-
$graph_array['id'] = $app['app_id'];
176-
$graph_array['type'] = 'application_' . $key;
177-
$graph_array['scale_min'] = '0';
119+
foreach ($graphs as $graphKey => $graphTitle) {
120+
$graph_array = [
121+
'height' => '100',
122+
'width' => '215',
123+
'to' => \App\Facades\LibrenmsConfig::get('time.now'),
124+
'id' => $app['app_id'],
125+
'type' => "application_{$graphKey}",
126+
'scale_min' => '0',
127+
];
178128

179129
if (isset($vars['disk'])) {
180130
$graph_array['disk'] = $vars['disk'];
181131
}
182132

183-
echo '<div class="panel panel-default">
184-
<div class="panel-heading">
185-
<h3 class="panel-title">' . $text . '</h3>
186-
</div>
187-
<div class="panel-body">
188-
<div class="row">';
133+
echo <<<HTML
134+
<div class="panel panel-default">
135+
<div class="panel-heading">
136+
<h3 class="panel-title">{$graphTitle}</h3>
137+
</div>
138+
<div class="panel-body">
139+
<div class="row">
140+
HTML;
141+
189142
include 'includes/html/print-graphrow.inc.php';
190-
echo '</div>';
191-
echo '</div>';
192-
echo '</div>';
143+
144+
echo <<<'HTML'
145+
</div>
146+
</div>
147+
</div>
148+
HTML;
193149
}

0 commit comments

Comments
 (0)