2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-13 05:06:35 +00:00

Fixing statistics.cgi ...

git-svn-id: https://svn.fhem.de/fhem/trunk@3295 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2013-06-17 13:35:27 +00:00
parent 9378ddb9fa
commit d83190002e

View File

@ -22,6 +22,25 @@
# GNU General Public License for more details. # 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 qw(:standard Vars);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use DBI; #requires libdbd-sqlite3-perl use DBI; #requires libdbd-sqlite3-perl
@ -29,6 +48,7 @@ use lib "./lib";
use Geo::IP; use Geo::IP;
use strict; use strict;
use warnings; use warnings;
no warnings 'uninitialized'; no warnings 'uninitialized';
sub createDB(); sub createDB();
@ -79,6 +99,7 @@ if(index($ua,"Fhem") > -1) {
print header("application/x-www-form-urlencoded"); print header("application/x-www-form-urlencoded");
insertDB(); insertDB();
print "==> ok"; print "==> ok";
Log("Upload-End");
} else { } else {
viewStatistics(); viewStatistics();
} }
@ -198,6 +219,7 @@ sub viewStatistics() {
</div> </div>
END END
print end_html; print end_html;
Log("Stat-End");
} }
sub googleVisualizationLib($) { sub googleVisualizationLib($) {
@ -308,7 +330,6 @@ sub drawColumnChartTop10ModDef(@) {
$sth->execute(); $sth->execute();
my $res = $sth->{NAME}; my $res = $sth->{NAME};
$sth->finish; $sth->finish;
my %hash = (); my %hash = ();
foreach my $column (@$res) { foreach my $column (@$res) {
my ($sum) = $dbh->selectrow_array("SELECT sum($column) FROM $table"); my ($sum) = $dbh->selectrow_array("SELECT sum($column) FROM $table");
@ -487,6 +508,7 @@ sub drawTable2cols(@) {
my %hash = (); my %hash = ();
foreach my $column (@$res) { foreach my $column (@$res) {
next if($column =~ m/["']/);
my ($sum) = $dbh->selectrow_array("SELECT sum(\"$column\") FROM $table"); my ($sum) = $dbh->selectrow_array("SELECT sum(\"$column\") FROM $table");
$hash{$column} = $sum; $hash{$column} = $sum;
} }
@ -531,18 +553,35 @@ END
sub drawTable3cols(@) { sub drawTable3cols(@) {
my ($table,$postfix,$type1,$title1,$type2,$title2,$type3,$title3,$divID) = @_; my ($table,$postfix,$type1,$title1,$type2,$title2,$type3,$title3,$divID) = @_;
$sth = $dbh->prepare("SELECT * FROM $table where 1=0");
$sth->execute();
my $res = $sth->{NAME};
$sth->finish;
my %hash = (); ###################
foreach my $column (@$res) { # Results in "Internal Error 500", and 1und1 will not give me an error-log
my ($count) = $dbh->selectrow_array("SELECT count(\"$column\") FROM $table WHERE \"$column\" != 0"); # extract.
my ($sum) = $dbh->selectrow_array("SELECT sum(\"$column\") FROM $table"); ###################
$hash{$column}{count} = $count; # $sth = $dbh->prepare("SELECT * FROM $table where 1=0");
$hash{$column}{sum} = $sum; # $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 %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; my $data;
foreach my $column (sort {$hash{$b} <=> $hash{$a}} keys %hash) { foreach my $column (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
@ -663,12 +702,20 @@ sub checkColumn($$) {
# get table info # get table info
my %column = %{ $dbh->column_info(undef, undef,$t, undef)->fetchall_hashref('COLUMN_NAME') }; my %column = %{ $dbh->column_info(undef, undef,$t, undef)->fetchall_hashref('COLUMN_NAME') };
# check if column exists # 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 = $dbh->prepare("ALTER TABLE $t ADD COLUMN '$k' INTEGER DEFAULT 0");
$sth->execute(); $sth->execute();
$sth->finish; $sth->finish;
Log "..Add ok";
} }
return; return;