mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-01-31 06:39:11 +00:00
93_DbLog: change SQL-Statement for SVG delta-h and delta-d plots, Forum: 65860.msg982171.html#msg982171
git-svn-id: https://svn.fhem.de/fhem/trunk@20445 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
c36800b26f
commit
3f68a324a3
@ -1,5 +1,7 @@
|
||||
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
|
||||
# Do not insert empty lines here, update check depends on it.
|
||||
- change: 93_DbLog: change SQL-Statement for SVG delta-h and delta-d plots,
|
||||
Forum: 65860.msg982171.html#msg982171
|
||||
- update: 98_MSwitch: New Version 2.63 / add usesetextensions for dummymode
|
||||
- bugfix: 70_CanOverEthernet: sendDataDigital crash fixed
|
||||
- bugfix: 70_CanOverEthernet: module crashed when UDP port was occupied
|
||||
|
@ -30,9 +30,10 @@ no if $] >= 5.017011, warnings => 'experimental::smartmatch';
|
||||
|
||||
# Version History intern by DS_Starter:
|
||||
our %DbLog_vNotesIntern = (
|
||||
"4.8.0" => "14.10.2019 change SQL-Statement for delta-h, delta-d (SVG getter) ",
|
||||
"4.7.5" => "07.10.2019 fix warning \"error valueFn: Global symbol \$CN requires ...\" in DbLog_addCacheLine ".
|
||||
"enhanced configCheck by insert mode check ",
|
||||
"4.7.4" => "03.10.2019 bugfix test of TIMESTAMP got from DbLogValueFn or valueFn in DbLog_Log and DbLog_AddLog",
|
||||
"4.7.4" => "03.10.2019 bugfix test of TIMESTAMP got from DbLogValueFn or valueFn in DbLog_Log and DbLog_AddLog ",
|
||||
"4.7.3" => "02.10.2019 improved log out entries of DbLog_Get for SVG ",
|
||||
"4.7.2" => "28.09.2019 change cache from %defs to %data ",
|
||||
"4.7.1" => "10.09.2019 release the memcache memory: https://www.effectiveperlprogramming.com/2018/09/undef-a-scalar-to-release-its-memory/ in asynchron mode: https://www.effectiveperlprogramming.com/2018/09/undef-a-scalar-to-release-its-memory/ ",
|
||||
@ -2146,7 +2147,7 @@ sub DbLog_execmemcache ($) {
|
||||
Log3 $hash->{NAME}, 5, "DbLog $name -> MemCache contains: ".$data{DbLog}{$name}{cache}{memcache}{$key};
|
||||
push(@row_array, delete($data{DbLog}{$name}{cache}{memcache}{$key}));
|
||||
}
|
||||
undef $data{DbLog}{$name}{cache}{memcache}; # sicherheitshalber Memory freigeben: https://perlmaven.com/undef-on-perl-arrays-and-hashes, bzw. https://www.effectiveperlprogramming.com/2018/09/undef-a-scalar-to-release-its-memory/
|
||||
delete $data{DbLog}{$name}{cache}{memcache}; # sicherheitshalber Memory freigeben: https://perlmaven.com/undef-on-perl-arrays-and-hashes , bzw. https://www.effectiveperlprogramming.com/2018/09/undef-a-scalar-to-release-its-memory/
|
||||
|
||||
my $rowlist = join('§', @row_array);
|
||||
$rowlist = encode_base64($rowlist,"");
|
||||
@ -3087,8 +3088,8 @@ sub DbLog_Get($@) {
|
||||
my @readings = ();
|
||||
my (%sqlspec, %from_datetime, %to_datetime);
|
||||
|
||||
#uebergebenen Timestamp anpassen
|
||||
#moegliche Formate: YYYY | YYYY-MM | YYYY-MM-DD | YYYY-MM-DD_HH24
|
||||
# uebergebenen Timestamp anpassen
|
||||
# moegliche Formate: YYYY | YYYY-MM | YYYY-MM-DD | YYYY-MM-DD_HH24
|
||||
$from =~ s/_/\ /g;
|
||||
$to =~ s/_/\ /g;
|
||||
%from_datetime = DbLog_explode_datetime($from, DbLog_explode_datetime("2000-01-01 00:00:00", ()));
|
||||
@ -3129,7 +3130,6 @@ sub DbLog_Get($@) {
|
||||
Log3($name, 4, "DbLog $name -> main PID: $hash->{PID}, secondary PID: $$");
|
||||
|
||||
my $nh = ($hash->{MODEL} ne 'SQLITE')?1:0;
|
||||
# $hash->{PID} != $$ -> create new connection for plotfork
|
||||
if ($nh || $hash->{PID} != $$) { # 17.04.2019 Forum: https://forum.fhem.de/index.php/topic,99719.0.html
|
||||
$dbh = DbLog_ConnectNewDBH($hash);
|
||||
return "Can't connect to database." if(!$dbh);
|
||||
@ -3208,18 +3208,47 @@ sub DbLog_Get($@) {
|
||||
$lastd[$i] = "undef";
|
||||
$mind[$i] = "undef";
|
||||
$maxd[$i] = "undef";
|
||||
$minval = (~0 >> 1);
|
||||
$maxval = -(~0 >> 1);
|
||||
$minval = (~0 >> 1); # ist "9223372036854775807"
|
||||
$maxval = -(~0 >> 1); # ist "-9223372036854775807"
|
||||
$deltacalc = 0;
|
||||
|
||||
if($readings[$i]->[3] && ($readings[$i]->[3] eq "delta-h" || $readings[$i]->[3] eq "delta-d")) {
|
||||
$deltacalc = 1;
|
||||
Log3($name, 4, "DbLog $name -> deltacalc: hour") if($readings[$i]->[3] eq "delta-h"); # geändert V4.8.0 / 14.10.2019
|
||||
Log3($name, 4, "DbLog $name -> deltacalc: day") if($readings[$i]->[3] eq "delta-d"); # geändert V4.8.0 / 14.10.2019
|
||||
}
|
||||
|
||||
my $stm;
|
||||
my $stm2;
|
||||
my $stmdelta;
|
||||
$stm = "SELECT
|
||||
my ($stm);
|
||||
if($deltacalc) {
|
||||
# delta-h und delta-d , geändert V4.8.0 / 14.10.2019
|
||||
$stm = "SELECT Z.TIMESTAMP, Z.DEVICE, Z.READING, Z.VALUE from ";
|
||||
|
||||
$stm .= "(SELECT $sqlspec{get_timestamp} AS TIMESTAMP,
|
||||
DEVICE AS DEVICE,
|
||||
READING AS READING,
|
||||
VALUE AS VALUE ";
|
||||
|
||||
$stm .= "FROM $current " if($inf eq "current");
|
||||
$stm .= "FROM $history " if($inf eq "history");
|
||||
|
||||
$stm .= "WHERE 1=1 ";
|
||||
|
||||
$stm .= "AND DEVICE = '".$readings[$i]->[0]."' " if ($readings[$i]->[0] !~ m(\%));
|
||||
$stm .= "AND DEVICE LIKE '".$readings[$i]->[0]."' " if(($readings[$i]->[0] !~ m(^\%$)) && ($readings[$i]->[0] =~ m(\%)));
|
||||
|
||||
$stm .= "AND READING = '".$readings[$i]->[1]."' " if ($readings[$i]->[1] !~ m(\%));
|
||||
$stm .= "AND READING LIKE '".$readings[$i]->[1]."' " if(($readings[$i]->[1] !~ m(^%$)) && ($readings[$i]->[1] =~ m(\%)));
|
||||
|
||||
$stm .= "AND TIMESTAMP < $sqlspec{from_timestamp} ";
|
||||
$stm .= "AND TIMESTAMP > $sqlspec{day_before} ";
|
||||
|
||||
$stm .= "ORDER BY TIMESTAMP DESC LIMIT 1 ) AS Z
|
||||
UNION ALL " if($readings[$i]->[3] eq "delta-h");
|
||||
|
||||
$stm .= "ORDER BY TIMESTAMP) AS Z
|
||||
UNION ALL " if($readings[$i]->[3] eq "delta-d");
|
||||
|
||||
$stm .= "SELECT
|
||||
MAX($sqlspec{get_timestamp}) AS TIMESTAMP,
|
||||
MAX(DEVICE) AS DEVICE,
|
||||
MAX(READING) AS READING,
|
||||
@ -3237,47 +3266,38 @@ sub DbLog_Get($@) {
|
||||
$stm .= "AND READING = '".$readings[$i]->[1]."' " if ($readings[$i]->[1] !~ m(\%));
|
||||
$stm .= "AND READING LIKE '".$readings[$i]->[1]."' " if(($readings[$i]->[1] !~ m(^%$)) && ($readings[$i]->[1] =~ m(\%)));
|
||||
|
||||
$stmdelta = $stm;
|
||||
$stm .= "AND TIMESTAMP >= $sqlspec{from_timestamp} ";
|
||||
$stm .= "AND TIMESTAMP <= $sqlspec{to_timestamp} "; # 03.09.2018 : https://forum.fhem.de/index.php/topic,65860.msg815640.html#msg815640
|
||||
|
||||
$stm .= "AND TIMESTAMP < $sqlspec{from_timestamp} ";
|
||||
$stm .= "AND TIMESTAMP > $sqlspec{day_before} ";
|
||||
$stm .= "GROUP BY $sqlspec{order_by_hour} " if($deltacalc);
|
||||
$stm .= "ORDER BY TIMESTAMP";
|
||||
|
||||
$stm .= "UNION ALL ";
|
||||
|
||||
$stm2 = "SELECT
|
||||
} else {
|
||||
# kein deltacalc
|
||||
$stm = "SELECT
|
||||
$sqlspec{get_timestamp},
|
||||
DEVICE,
|
||||
READING,
|
||||
VALUE
|
||||
$sqlspec{all} ";
|
||||
|
||||
$stm2 .= "FROM $current " if($inf eq "current");
|
||||
$stm2 .= "FROM $history " if($inf eq "history");
|
||||
$stm .= "FROM $current " if($inf eq "current");
|
||||
$stm .= "FROM $history " if($inf eq "history");
|
||||
|
||||
$stm2 .= "WHERE 1=1 ";
|
||||
$stm .= "WHERE 1=1 ";
|
||||
|
||||
$stm2 .= "AND DEVICE = '".$readings[$i]->[0]."' " if ($readings[$i]->[0] !~ m(\%));
|
||||
$stm2 .= "AND DEVICE LIKE '".$readings[$i]->[0]."' " if(($readings[$i]->[0] !~ m(^\%$)) && ($readings[$i]->[0] =~ m(\%)));
|
||||
$stm .= "AND DEVICE = '".$readings[$i]->[0]."' " if ($readings[$i]->[0] !~ m(\%));
|
||||
$stm .= "AND DEVICE LIKE '".$readings[$i]->[0]."' " if(($readings[$i]->[0] !~ m(^\%$)) && ($readings[$i]->[0] =~ m(\%)));
|
||||
|
||||
$stm2 .= "AND READING = '".$readings[$i]->[1]."' " if ($readings[$i]->[1] !~ m(\%));
|
||||
$stm2 .= "AND READING LIKE '".$readings[$i]->[1]."' " if(($readings[$i]->[1] !~ m(^%$)) && ($readings[$i]->[1] =~ m(\%)));
|
||||
$stm .= "AND READING = '".$readings[$i]->[1]."' " if ($readings[$i]->[1] !~ m(\%));
|
||||
$stm .= "AND READING LIKE '".$readings[$i]->[1]."' " if(($readings[$i]->[1] !~ m(^%$)) && ($readings[$i]->[1] =~ m(\%)));
|
||||
|
||||
$stm2 .= "AND TIMESTAMP >= $sqlspec{from_timestamp} ";
|
||||
$stm2 .= "AND TIMESTAMP <= $sqlspec{to_timestamp} "; # 03.09.2018 : https://forum.fhem.de/index.php/topic,65860.msg815640.html#msg815640
|
||||
$stm2 .= "ORDER BY TIMESTAMP";
|
||||
|
||||
if($deltacalc) {
|
||||
$stmdelta .= "AND TIMESTAMP >= $sqlspec{from_timestamp} ";
|
||||
$stmdelta .= "AND TIMESTAMP <= $sqlspec{to_timestamp} "; # 03.09.2018 : https://forum.fhem.de/index.php/topic,65860.msg815640.html#msg815640
|
||||
|
||||
$stmdelta .= "GROUP BY $sqlspec{order_by_hour} " if($deltacalc);
|
||||
$stmdelta .= "ORDER BY TIMESTAMP";
|
||||
$stm .= $stmdelta;
|
||||
} else {
|
||||
$stm = $stm2;
|
||||
$stm .= "AND TIMESTAMP >= $sqlspec{from_timestamp} ";
|
||||
$stm .= "AND TIMESTAMP <= $sqlspec{to_timestamp} "; # 03.09.2018 : https://forum.fhem.de/index.php/topic,65860.msg815640.html#msg815640
|
||||
$stm .= "ORDER BY TIMESTAMP";
|
||||
}
|
||||
|
||||
Log3 ($name, 4, "$name - Processing Statement: $stm");
|
||||
Log3 ($name, 4, "$name - Processing Statement:\n$stm");
|
||||
|
||||
my $sth = $dbh->prepare($stm) || return "Cannot prepare statement $stm: $DBI::errstr";
|
||||
my $rc = $sth->execute() || return "Cannot execute statement $stm: $DBI::errstr";
|
||||
@ -3293,14 +3313,15 @@ sub DbLog_Get($@) {
|
||||
$retval .= "=====================================================\n";
|
||||
}
|
||||
|
||||
################################
|
||||
####################################################################################
|
||||
# Select Auswertung
|
||||
################################
|
||||
####################################################################################
|
||||
while($sth->fetch()) {
|
||||
no warnings 'uninitialized';
|
||||
my $ds = "TS: $sql_timestamp, DEV: $sql_device, RD: $sql_reading, VAL: $sql_value";
|
||||
Log3 ($name, 5, "$name - SQL-result -> $ds");
|
||||
use warnings;
|
||||
no warnings 'uninitialized'; # geändert V4.8.0 / 14.10.2019
|
||||
my $ds = "TS: $sql_timestamp, DEV: $sql_device, RD: $sql_reading, VAL: $sql_value"; # geändert V4.8.0 / 14.10.2019
|
||||
Log3 ($name, 5, "$name - SQL-result -> $ds"); # geändert V4.8.0 / 14.10.2019
|
||||
use warnings; # geändert V4.8.0 / 14.10.2019
|
||||
$writeout = 0; # eingefügt V4.8.0 / 14.10.2019
|
||||
|
||||
############ Auswerten des 5. Parameters: Regexp ###################
|
||||
# die Regexep wird vor der Function ausgewertet und der Wert im Feld
|
||||
@ -3326,8 +3347,8 @@ sub DbLog_Get($@) {
|
||||
if($sql_timestamp lt $from && $deltacalc) {
|
||||
if(Scalar::Util::looks_like_number($sql_value)) {
|
||||
# nur setzen wenn numerisch
|
||||
$minval = $sql_value if($sql_value < $minval);
|
||||
$maxval = $sql_value if($sql_value > $maxval);
|
||||
$minval = $sql_value if($sql_value < $minval || ($minval = (~0 >> 1)) ); # geändert V4.8.0 / 14.10.2019
|
||||
$maxval = $sql_value if($sql_value > $maxval || ($maxval = -(~0 >> 1)) ); # geändert V4.8.0 / 14.10.2019
|
||||
$lastv[$i] = $sql_value;
|
||||
}
|
||||
|
||||
@ -3387,7 +3408,7 @@ sub DbLog_Get($@) {
|
||||
$retvaldummy = "";
|
||||
|
||||
if(($tstamp{hour}-$lasttstamp{hour}) > 1) {
|
||||
for (my $j=$lasttstamp{hour}+1; $j < $tstamp{hour}; $j++) {
|
||||
for (my $j = $lasttstamp{hour}+1; $j < $tstamp{hour}; $j++) {
|
||||
$out_value = "0";
|
||||
$hour = $j;
|
||||
$hour = '0'.$j if $j<10;
|
||||
@ -3430,15 +3451,12 @@ sub DbLog_Get($@) {
|
||||
$sum[$i] += $out_value;
|
||||
$cnt[$i]++;
|
||||
$out_tstamp = DbLog_implode_datetime($lasttstamp{year}, $lasttstamp{month}, $lasttstamp{day}, $lasttstamp{hour}, "30", "00");
|
||||
# $minval = (~0 >> 1);
|
||||
$writeout = 1 if($minval != (~0 >> 1) && $maxval != -(~0 >> 1)); # geändert V4.8.0 / 14.10.2019
|
||||
$minval = $maxval;
|
||||
# $maxval = -(~0 >> 1);
|
||||
$writeout = 1;
|
||||
|
||||
Log3 ($name, 5, "$name - Output delta-h -> TS: $tstamp{hour}, LASTTS: $lasttstamp{hour}, OUTTS: $out_tstamp, OUTVAL: $out_value");
|
||||
Log3 ($name, 5, "$name - Output delta-h -> TS: $tstamp{hour}, LASTTS: $lasttstamp{hour}, OUTTS: $out_tstamp, OUTVAL: $out_value, WRITEOUT: $writeout");
|
||||
}
|
||||
|
||||
} elsif ($readings[$i]->[3] && $readings[$i]->[3] eq "delta-d") { # Berechnung eines Tageswertes
|
||||
} elsif ($readings[$i]->[3] && $readings[$i]->[3] eq "delta-d") { # Berechnung eines Tages-Deltas
|
||||
%tstamp = DbLog_explode_datetime($sql_timestamp, ());
|
||||
|
||||
if($lastd[$i] eq "undef") {
|
||||
@ -3448,17 +3466,14 @@ sub DbLog_Get($@) {
|
||||
}
|
||||
|
||||
if("$tstamp{day}" ne "$lasttstamp{day}") {
|
||||
# Aenderung des Tages, Berechne Delta
|
||||
$out_value = sprintf("%g", $maxval - $minval);
|
||||
# Aenderung des Tages, berechne Delta
|
||||
$out_value = sprintf("%g", $maxval - $minval); # %g - a floating-point number
|
||||
$sum[$i] += $out_value;
|
||||
$cnt[$i]++;
|
||||
$out_tstamp = DbLog_implode_datetime($lasttstamp{year}, $lasttstamp{month}, $lasttstamp{day}, "12", "00", "00");
|
||||
# $minval = (~0 >> 1);
|
||||
$writeout = 1 if($minval != (~0 >> 1) && $maxval != -(~0 >> 1)); # geändert V4.8.0 / 14.10.2019
|
||||
$minval = $maxval;
|
||||
# $maxval = -(~0 >> 1);
|
||||
$writeout = 1;
|
||||
|
||||
Log3 ($name, 5, "$name - Output delta-d -> TS: $tstamp{day}, LASTTS: $lasttstamp{day}, OUTTS: $out_tstamp, OUTVAL: $out_value");
|
||||
Log3 ($name, 5, "$name - Output delta-d -> TS: $tstamp{day}, LASTTS: $lasttstamp{day}, OUTTS: $out_tstamp, OUTVAL: $out_value, WRITEOUT: $writeout");
|
||||
}
|
||||
|
||||
} else {
|
||||
@ -3532,9 +3547,10 @@ sub DbLog_Get($@) {
|
||||
}
|
||||
$lastd[$i] = $sql_timestamp;
|
||||
}
|
||||
} # while fetchrow Ende
|
||||
} ##### while fetchrow Ende #####
|
||||
|
||||
######## den letzten Abschlusssatz rausschreiben ##########
|
||||
|
||||
if($readings[$i]->[3] && ($readings[$i]->[3] eq "delta-h" || $readings[$i]->[3] eq "delta-d")) {
|
||||
if($lastd[$i] eq "undef") {
|
||||
$out_value = "0";
|
||||
@ -3558,6 +3574,8 @@ sub DbLog_Get($@) {
|
||||
$out_tstamp =~ s/\ /_/g; #needed by generating plots
|
||||
$retval .= "$out_tstamp $out_value\n";
|
||||
}
|
||||
|
||||
Log3 ($name, 5, "$name - Output last DS -> OUTTS: $out_tstamp, OUTVAL: $out_value, WRITEOUT: implicit ");
|
||||
}
|
||||
|
||||
# Datentrenner setzen
|
||||
|
Loading…
Reference in New Issue
Block a user