diff --git a/fhem/contrib/statistics/statistics.cgi b/fhem/contrib/statistics/statistics.cgi index ebb44e5fe..7b8ab60ba 100755 --- a/fhem/contrib/statistics/statistics.cgi +++ b/fhem/contrib/statistics/statistics.cgi @@ -22,6 +22,25 @@ # GNU General Public License for more details. # ################################################################ +use Time::HiRes qw(gettimeofday); + +sub +Log($) +{ + my $text = shift; + my @t = localtime; + my $tim = sprintf("%04d.%02d.%02d %02d:%02d:%02d", + $t[5]+1900,$t[4]+1,$t[3], $t[2],$t[1],$t[0]); + my ($seconds, $microseconds) = gettimeofday(); + $tim .= sprintf(".%03d", $microseconds/1000); + print LOG "$tim $text\n"; +} + +open(LOG, ">>log.out"); +LOG->autoflush(1); +Log("Starting"); + + use CGI qw(:standard Vars); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use DBI; #requires libdbd-sqlite3-perl @@ -29,6 +48,7 @@ use lib "./lib"; use Geo::IP; use strict; use warnings; + no warnings 'uninitialized'; sub createDB(); @@ -79,6 +99,7 @@ if(index($ua,"Fhem") > -1) { print header("application/x-www-form-urlencoded"); insertDB(); print "==> ok"; + Log("Upload-End"); } else { viewStatistics(); } @@ -198,6 +219,7 @@ sub viewStatistics() { END print end_html; + Log("Stat-End"); } sub googleVisualizationLib($) { @@ -308,7 +330,6 @@ sub drawColumnChartTop10ModDef(@) { $sth->execute(); my $res = $sth->{NAME}; $sth->finish; - my %hash = (); foreach my $column (@$res) { my ($sum) = $dbh->selectrow_array("SELECT sum($column) FROM $table"); @@ -487,6 +508,7 @@ sub drawTable2cols(@) { my %hash = (); foreach my $column (@$res) { + next if($column =~ m/["']/); my ($sum) = $dbh->selectrow_array("SELECT sum(\"$column\") FROM $table"); $hash{$column} = $sum; } @@ -531,19 +553,36 @@ END sub drawTable3cols(@) { my ($table,$postfix,$type1,$title1,$type2,$title2,$type3,$title3,$divID) = @_; - $sth = $dbh->prepare("SELECT * FROM $table where 1=0"); + +################### +# Results in "Internal Error 500", and 1und1 will not give me an error-log +# extract. +################### +# $sth = $dbh->prepare("SELECT * FROM $table where 1=0"); +# $sth->execute(); +# my $res = $sth->{NAME}; +# $sth->finish; +# +# my %hash = (); +# +# foreach my $column (@$res) { +# my ($count) = $dbh->selectrow_array("SELECT count(\"$column\") FROM $table WHERE \"$column\" != 0"); +# my ($sum) = $dbh->selectrow_array("SELECT sum(\"$column\") FROM $table"); +# $hash{$column}{count} = $count; +# $hash{$column}{sum} = $sum; +# } + + $sth = $dbh->prepare("SELECT * FROM $table"); $sth->execute(); - my $res = $sth->{NAME}; - $sth->finish; - - my %hash = (); - foreach my $column (@$res) { - my ($count) = $dbh->selectrow_array("SELECT count(\"$column\") FROM $table WHERE \"$column\" != 0"); - my ($sum) = $dbh->selectrow_array("SELECT sum(\"$column\") FROM $table"); - $hash{$column}{count} = $count; - $hash{$column}{sum} = $sum; + my %hash; + while(my $h = $sth->fetchrow_hashref) { + foreach my $k (keys %{$h}) { + $hash{$k}{count}++ if($h->{$k}); + $hash{$k}{sum} += $h->{$k} if($h->{$k}); + } } - + $sth->finish; + my $data; foreach my $column (sort {$hash{$b} <=> $hash{$a}} keys %hash) { next if($column eq "uniqueID"); @@ -663,12 +702,20 @@ sub checkColumn($$) { # get table info my %column = %{ $dbh->column_info(undef, undef,$t, undef)->fetchall_hashref('COLUMN_NAME') }; - # check if column exists - if(!exists $column{$k}) { + my $found; + foreach my $col (keys %column) { + if(lc($col) eq lc($k)) { + $found = 1; + last; + } + } + if(!$found) { + Log "Adding column >$k<"; $sth = $dbh->prepare("ALTER TABLE $t ADD COLUMN '$k' INTEGER DEFAULT 0"); $sth->execute(); $sth->finish; + Log "..Add ok"; } return;