Exemplo n.º 1
0
 function getChartData(array &$chartData, array &$contents, $patchIt = true)
 {
     //need to know depth of log file (numer of data points)
     $depth = $this->logFileDepth;
     //select 6,12 or 24 hour charts, 24 is default
     $dataSet = LoadAvg::$_settings->general['settings']['chart_type'];
     //echo 'dataSet ' . $dataSet  .  '<br>';
     // this is based on logger interval of 5, 5 min = 300 aprox we add 100 to be safe
     //$interval = 360;  // 5 minutes is 300 seconds + slippage of 20% aprox
     $interval = $this->getLoggerInterval();
     $interval = $interval * 1.2;
     //add 20% to interval for system lag
     //get size of array for parsing
     $totalContents = (int) count($contents);
     //echo 'TOTAL ' . $totalContents;
     //trim the dataset if we are only reading 6 or 12 hours of info
     //revise for 6 and 12 hour charts
     if ($dataSet == 6 || $dataSet == 12) {
         //logger is every 5 min then $this->getLoggerInterval() / 60 = 5;
         //so 300 / 60 = 5 min; 60 / 5 = 12 datasets per hour
         $dataFrame = 60 / ($this->getLoggerInterval() / 60);
         $dataNeeded = $dataFrame * $dataSet;
         //TODO: only trim if there is more than we need...
         $contents = array_slice($contents, $totalContents - $dataNeeded);
     }
     //contents is a array of strings for the dataset/logfile with the charts log data
     //we explode each value in each line of the array into datapoints in chartData
     //and send it back as a array!
     $patch = $chartData = array();
     $numPatches = 0;
     //delimiter is based on logger type used to explode data
     $delimiter = LoadUtility::getDelimiter();
     //if there is only one item in data set then we just chart it and return
     if ($totalContents == 1) {
         $data = explode($delimiter, $contents[0]);
         LoadUtility::cleanDataPoint($data, $depth);
         $chartData[0] = $data;
     } else {
         //subtract one from totalContents as arrays start at 0 not 1
         for ($i = 0; $i < $totalContents; ++$i) {
             //grab the first dataset
             $data = explode($delimiter, $contents[$i]);
             LoadUtility::cleanDataPoint($data, $depth);
             $chartData[$i] = $data;
             /*
              * if there is more than one item in dataset then we can check for downtime between points
              * and patch the dataset so they render ok
              * this is becuase when rendering we connect lines in the chart (prev to next)
              * and so downtime comes across as span not a null or 0
              *
              * check if difference is more than logging interval and patch for when server is offline
              * we patch for time between last data (system went down) and next data (system came up)
              * need to check if we need the nextData patch as well ie if system came up within 
              * the next interval time
              * 
              * for local data we dont check the first value in the data set as if its there it means it was up
              */
             if ($i > 0 && $i < $totalContents - 1) {
                 //dont do this for last value in dataset! as it will have no difference
                 $nextData = explode($delimiter, $contents[$i + 1]);
                 //difference in timestamps
                 $difference = $nextData[0] - $data[0];
                 //if more time that the logging interval has passed it means
                 //the system was down as logger should send data every interval
                 if ($difference >= $interval) {
                     //$chartData[$i-1]['redline'] = true;
                     //echo 'patch difference:' . $difference;
                     //patches are spans ie fall between datapoints
                     //so each patch has a start and end
                     $patch[$numPatches] = array($data[0] + $interval, "REDLINE", $i);
                     $patch[$numPatches + 1] = array($nextData[0] - $interval / 2, "REDLINE", $i);
                     $numPatches += 2;
                 }
             }
         }
     }
     //if there are patches to be applied, we iterate through the patcharray
     //and patch the dataset by inserting patch spans into it based on time
     $totalPatch = (int) count($patch);
     if ($totalPatch > 0 && $patchIt == true) {
         //echo "PATCHCOUNT: " . $totalPatch . "<br>";
         for ($i = 0; $i < $totalPatch; ++$i) {
             //patch time is really patch key position in array
             $patch_key_position = $patch[$i][2] + $i;
             $newdata[0] = strval($patch[$i][0]);
             $thepatch[0] = array_pad($newdata, $depth + 1, '0.0');
             $thepatch[0]['redline'] = true;
             //echo '<pre>patch'; var_dump ($thepatch); echo '</pre>';
             array_splice($chartData, $patch_key_position, 0, $thepatch);
             //echo "PATCHED: " . $patch_key_position . " count: " . count( $chartData ) . "<br>";
         }
     }
     //echo '<pre>'; var_dump ($chartData); echo '</pre>';
 }