Prechádzať zdrojové kódy

Added handling for div by 0

When max == 0, return 0
Henry Whitaker 6 rokov pred
rodič
commit
c95e547d83
1 zmenil súbory, kde vykonal 11 pridanie a 2 odobranie
  1. 11 2
      api/functions/netdata-functions.php

+ 11 - 2
api/functions/netdata-functions.php

@@ -140,7 +140,7 @@ function disk($dimension, $url)
         if ($response->success) {
             $json = json_decode($response->body, true);
             $data['value'] = $json['latest_values'][0] / 1000;
-            $data['percent'] = ($json['latest_values'][0] / $json['max']) * 100;
+            $data['percent'] =  getPercent($json['latest_values'][0], $json['max']);
             $data['units'] = 'MiB/s';
             $data['max'] = $json['max'];
         }
@@ -183,7 +183,7 @@ function net($dimension, $url)
         if ($response->success) {
             $json = json_decode($response->body, true);
             $data['value'] = $json['latest_values'][0] / 1000;
-            $data['percent'] = ($json['latest_values'][0] / $json['max']) * 100;
+            $data['percent'] = getPercent($json['latest_values'][0], $json['max']);
             $data['units'] = 'Mbit/s';
             $data['max'] = $json['max'];
         }
@@ -278,4 +278,13 @@ function ipmiTemp($url, $unit)
     };
 
     return $data;
+}
+
+function getPercent($val, $max)
+{
+    if($max == 0) {
+        return 0;
+    } else {
+        return ( $val / $max ) * 100;
+    }
 }