Refactored script for better readability
The code has been refactored to improve readability and maintainability. The main changes include removing unnecessary condition checks, simplifying the flow of control, and reorganizing the code blocks for better logical grouping. This should make it easier to understand and modify in the future.
This commit is contained in:
parent
b52abb69c7
commit
f7f3cb9704
29
script.php
29
script.php
@ -14,9 +14,7 @@ if(file_exists($cacheFile)) {
|
||||
} else {
|
||||
$oldconfig = [];
|
||||
}
|
||||
|
||||
if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
|
||||
foreach ($config as $key => &$metric) {
|
||||
foreach ($config as $key => &$metric) {
|
||||
if (!isset($metric['submetrics'])) {
|
||||
$metric['submetrics'] = [$metric];
|
||||
}
|
||||
@ -90,13 +88,13 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($pids as $pid) {
|
||||
}
|
||||
foreach ($pids as $pid) {
|
||||
pcntl_waitpid($pid, $none);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
foreach ($config as $key => &$metric) {
|
||||
foreach ($config as $key => &$metric) {
|
||||
foreach ($metric['submetrics'] as $subkey => &$submetric) {
|
||||
$submetric['value'] = file_get_contents('/tmp/.metrics.' . $key . '.' . $subkey);
|
||||
if (!empty($oldconfig) && !isset($submetric['value']) && (isset($submetric['fallback']) && !empty($submetric['fallback']))) {
|
||||
@ -116,17 +114,16 @@ if(php_sapi_name() == 'cli' || $_SERVER["REQUEST_URI"] == '/metrics') {
|
||||
}
|
||||
unlink('/tmp/.metrics.' . $key . '.' . $subkey);
|
||||
}
|
||||
}
|
||||
|
||||
file_put_contents($cacheFile, json_encode($config, JSON_PRETTY_PRINT));
|
||||
|
||||
$loader = new \Twig\Loader\FilesystemLoader('templates');
|
||||
$twig = new \Twig\Environment($loader, [
|
||||
'cache' => 'twig_cache',
|
||||
]);
|
||||
echo $twig->render('metrics.twig', ['config' => $config]);
|
||||
}
|
||||
|
||||
file_put_contents($cacheFile, json_encode($config, JSON_PRETTY_PRINT));
|
||||
|
||||
$loader = new \Twig\Loader\FilesystemLoader('templates');
|
||||
$twig = new \Twig\Environment($loader, [
|
||||
'cache' => 'twig_cache',
|
||||
]);
|
||||
echo $twig->render('metrics.twig', ['config' => $config]);
|
||||
|
||||
function getArrayValue($elem, $array) {
|
||||
$elem = explode('.', $elem);
|
||||
$new_array = $array;
|
||||
|
Loading…
Reference in New Issue
Block a user