// want to fetch the whole day again. $dates[0] = $dates[1]->add(new DateInterval('PT30M')); } $dates[1] = new DateTime(); $becDB->importFiltonWeatherWebCSV($filtonWeather, BEC_DB_FILTON_WEATHER_TABLE, $dates); } // Reporting ReportLog::prepend("BEC Fault Monitoring report log\n" . "===============================\n\n" . "Start time: " . $startTime->format('d/m/Y H:i') . " (UTC)\n\n"); missingPowerDataYesterday($becDB); zeroPowerYesterday($becDB); $report = ReportLog::get(); print $report . "\n\n"; // Write it to a local file so we can see it ran file_put_contents('last.log', $report); // Send email report containing report log if there was an error if (ReportLog::hasError()) { $msgBody = array($report); $gmail->sendEmail($ini['email_reports_to'], '', '', 'BEC fault monitoring report', $msgBody); } // TODO: HTML report // Generate graphs if ($becDB->graphsEnabled) { if (!file_exists('graphs')) { mkdir('graphs'); } foreach ($becDB->getMeterInfoArray() as $meter) { $powerTable = 'power'; $column = $becDB->meterDBName($meter['code']); // Skip if table has no data if (!$becDB->rowsInTable($powerTable, $column)) { if ($verbose > 0) {
/** * Record that there is an error in what we will report * * @param boolean $bool */ static function setError($bool) { self::$errorInReport = $bool; }
/** * Function to highlight when power data is missing for the previous day. * * @param resource $becDB * @return boolean TRUE if there was any missing data for previous day */ function missingPowerDataYesterday(&$becDB) { global $verbose; $dateTime = getYesterdayDateTime(); $sql = 'SELECT * FROM power WHERE DATE(power.datetime) = ' . sqlDateString($dateTime); $result = $becDB->fetchQuery($sql); if (sizeof($result) == 0) { ReportLog::append('No power data found for yesterday (' . sqlDateString($dateTime) . ")\n"); ReportLog::setError(TRUE); // Also report the latest power reading we have got $dateRange = $becDB->getDateTimeExtremesFromTable('power'); ReportLog::append('Most recent power data is from ' . $dateRange[1]->format('Y-m-d') . "\n\n"); return TRUE; } $anyHits = FALSE; $period = array(); // First check through the data returned from the database (if any) foreach ($result as $entry) { $missingCount = 0; $dateTime = new DateTime($entry['datetime']); $timestamp = $dateTime->getTimestamp(); $dateTime->setTime(0, 0); $dayTimestamp = $dateTime->getTimestamp(); $secondsIntoDay = $timestamp - $dayTimestamp; $halfHourIndex = $secondsIntoDay / (60 * 30); $period[$halfHourIndex] = TRUE; foreach ($becDB->getGenMeterArray() as $genMeter) { if ($entry[$genMeter] === NULL) { if (!$anyHits) { $anyHits = TRUE; ReportLog::append('Missing power data during ' . $dateTime->format('d/m/Y') . ":\n"); ReportLog::setError(TRUE); } $dateTime->setTimestamp($timestamp); if ($missingCount == 0) { ReportLog::append(' Period ending [' . $dateTime->format('H:i') . " (UTC)]: {$genMeter}"); $missingCount++; } else { ReportLog::append(', ' . $genMeter); $missingCount++; } } } if ($missingCount) { ReportLog::append("\n"); } } // Now check whether every half-hour period had an entry in the database at all for ($i = 0; $i < 48; $i++) { if (!key_exists($i, $period)) { if (!$anyHits) { $anyHits = TRUE; ReportLog::append('Missing power data during ' . $dateTime->format('d/m/Y') . ":\n"); ReportLog::setError(TRUE); } $hour = $i / 2; $min = $i % 2 == 1 ? 30 : 0; ReportLog::append(sprintf(' No power data recorded for any meter for period ending %02d:%02d', $hour, $min) . " (UTC)\n"); } } if ($anyHits) { ReportLog::append("\n\n"); } else { ReportLog::append('No missing power data for yesterday (' . $dateTime->format('d/m/Y') . ")\n\n"); } return $anyHits; }