create_rrd.php creates rrd from old data

fetch_rrd.php calculates today energy from rrd
This commit is contained in:
2012-11-27 09:32:20 +01:00
parent 17de1dca8a
commit e47d24d0fe
2 changed files with 120 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
#!/usr/local/bin/php
<?php
$aRRDFile = 'today.rrd';
$sDataDirectory = 'data';
$aRRDCreate = array(
'inverter.rrd' => 'create %s
--step 5
--start %d
DS:TEMP:GAUGE:120:U:U
DS:VPV:GAUGE:120:U:U
DS:IAC:GAUGE:120:U:U
DS:VAC:GAUGE:120:U:U
DS:FAC:GAUGE:120:U:U
DS:PAC:GAUGE:120:0:U
DS:ETOTAL:GAUGE:120:0:U
DS:ETODAY:GAUGE:120:0:U
RRA:AVERAGE:0.5:1:720
RRA:AVERAGE:0.5:17:1017
RRA:AVERAGE:0.5:120:1008
RRA:AVERAGE:0.5:535:1002
RRA:AVERAGE:0.5:6324:1001',
'today.rrd' => 'create %s
--step 5
--start %d
DS:PAC:GAUGE:120:0:U
DS:ETODAY:GAUGE:120:0:U
RRA:AVERAGE:0.5:1:17280');
$aRRDFiles = array_keys($aRRDCreate);
/* Spawn process that accepts commands from STDIN */
$rHandle = popen('rrdtool - > /dev/null', 'w');
$bFirst = true;
$aRRDKeys = array();
$i = 0;
foreach (glob($sDataDirectory . '/*.csv') as $sFile) {
/* Extract header from csv file */
$aData = explode("\n", trim(file_get_contents($sFile)));
$sHeader = array_shift($aData);
if ($bFirst) {
$aHeader = array_flip(explode(',', $sHeader));
foreach ($aRRDCreate as $sRRDFile => $sRRDCreate) {
/* Determine fields to update in RRD database */
preg_match_all('~DS:([^:]+):~', $sRRDCreate, $aMatches);
$aKeys = array();
foreach ($aMatches[1] as $sField) {
$aKeys[] = $aHeader[$sField] - 1;
}
$aRRDKeys[$sRRDFile] = array_flip($aKeys);
}
}
foreach ($aData as $sEntry) {
$aValues = array_slice(explode(',', $sEntry), 1, 12);
if ($bFirst) {
foreach ($aRRDCreate as $sRRDFile => $sRRDCreate) {
/* Create RRD database */
echo $sCommand = str_replace("\n", ' ', sprintf($sRRDCreate, $sRRDFile, $aValues[0] - 1)) . "\n";
fwrite($rHandle, $sCommand);
}
$bFirst = false;
}
++$i;
foreach ($aRRDFiles as $sRRDFile) {
/* Update relevant fields in RRD database */
$aRRDValues = array_intersect_key($aValues, $aRRDKeys[$sRRDFile]);
$sCommand = sprintf("update %s %d:%s\n", $sRRDFile, $aValues[0], implode(':', $aRRDValues));
printf('[%d] %s', $i, $sCommand);
fwrite($rHandle, $sCommand);
}
}
}
fwrite($rHandle, "quit\n");
pclose($rHandle);

View File

@@ -0,0 +1,46 @@
#!/usr/local/bin/php
<?php
echo '<pre>';
$sRRDFile = 'inverter.rrd';
$sRRDFetch = 'rrdtool fetch %s AVERAGE -r %d -s %d -e %d';
$sField = 'PAC';
/* Constrain data to fetch */
$iLast = strtotime('9:00');
$iStart = $iLast;
$iEnd = strtotime('17:00');
$iResolution = 5;
/* Extract fields */
$sData = command(sprintf($sRRDFetch, $sRRDFile, $iResolution, $iStart, $iEnd));
$aData = explode("\n", trim($sData));
$aFields = preg_split("~[\s]+~", array_shift($aData));
array_shift($aData);
$aFields = array_flip($aFields);
$bFirst = true;
$fTotal = 0;
if (isset($aFields[$sField])) {
$iField = $aFields[$sField] + 1;
array_shift($aData);
foreach ($aData as $sRow) {
$aRow = explode(' ', $sRow);
if ($bFirst) var_dump($aRow);
$iDate = substr($aRow[0], 0, -1);
$iInterval = $bFirst ? (($bFirst = false) || $iResolution) : $iDate - $iLast;
if (($fValue = floatval($aRow[$iField])) > 0) {
$fTotal += $iInterval * $fValue;
}
$iLast = $iDate;
}
}
/* Convert to kWh */
var_dump(count($aData));
var_dump($fTotal / 1000 / 3600);
function command($sCommand) {
ob_start();
system($sCommand);
return ob_get_clean();
}