From 144696289318cee09821f9fad3a20472a78a144e Mon Sep 17 00:00:00 2001 From: Tomas Plesnik <plesnik@ics.muni.cz> Date: Tue, 14 Oct 2014 14:19:39 +0200 Subject: [PATCH] uprava db dotazu pro vetsi prehlednost; lepsi prehlednost kodu (nahrazeni negovanych ifu za unless); smazani stareho dotazu --- src/warden-server/bin/getStatus.pl | 33 +++++++++++++----------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/warden-server/bin/getStatus.pl b/src/warden-server/bin/getStatus.pl index e11d875..52291e2 100755 --- a/src/warden-server/bin/getStatus.pl +++ b/src/warden-server/bin/getStatus.pl @@ -105,12 +105,11 @@ $sth->execute($WardenCommon::DB_NAME, 'events') or die "Cannot execute statement my $size = $sth->fetchrow(); my $db_size = defined $size ? Format::Human::Bytes::base10($size) : "none"; - # sum of all records in table events $sth = $dbh->prepare("SELECT count(*) FROM events;") or die "Cannot prepare statement: " . $dbh->errstr; $sth->execute or die "Cannot execute statement: " . $sth->errstr; my $events_sum = $sth->fetchrow(); -if (!defined $events_sum) { +unless (defined $events_sum) { $events_sum = "none"; } @@ -118,7 +117,7 @@ if (!defined $events_sum) { $sth = $dbh->prepare("SELECT count(*) FROM events WHERE valid = 't';") or die "Cannot prepare statement: " . $dbh->errstr; $sth->execute or die "Cannot execute statement: " . $sth->errstr; my $events_sum_valid = $sth->fetchrow(); -if (!defined $events_sum_valid) { +unless (defined $events_sum_valid) { $events_sum_valid = "none"; } @@ -126,7 +125,7 @@ if (!defined $events_sum_valid) { $sth = $dbh->prepare("SELECT count(*) FROM events WHERE valid = 'o';") or die "Cannot prepare statement: " . $dbh->errstr; $sth->execute or die "Cannot execute statement: " . $sth->errstr; my $events_sum_obsolete = $sth->fetchrow(); -if (!defined $events_sum_obsolete) { +unless (defined $events_sum_obsolete) { $events_sum_obsolete = "none"; } @@ -134,25 +133,23 @@ if (!defined $events_sum_obsolete) { $sth = $dbh->prepare("SELECT count(*) FROM events WHERE valid = 'f';") or die "Cannot prepare statement: " . $dbh->errstr; $sth->execute or die "Cannot execute statement: " . $sth->errstr; my $events_sum_invalid = $sth->fetchrow(); -if (!defined $events_sum_invalid) { +unless (defined $events_sum_invalid) { $events_sum_invalid = "none"; } - # id of last record in table events $sth = $dbh->prepare("SELECT max(id) FROM events WHERE valid = 't';") or die "Cannot prepare statement: " . $dbh->errstr; $sth->execute or die "Cannot execute statement: " . $sth->errstr; my $events_last_id = $sth->fetchrow(); -if (!defined $events_last_id) { +unless (defined $events_last_id) { $events_last_id = "none"; } - # timestamp of first record in table events $sth = $dbh->prepare("SELECT received FROM events WHERE id = (SELECT min(id) FROM events WHERE valid = 't');") or die "Cannot prepare statement: " . $dbh->errstr; $sth->execute or die "Cannot execute statement: " . $sth->errstr; my $events_first_timestamp = $sth->fetchrow(); -if (!defined $events_first_timestamp) { +unless (defined $events_first_timestamp) { $events_first_timestamp = "none"; } @@ -160,7 +157,7 @@ if (!defined $events_first_timestamp) { $sth = $dbh->prepare("SELECT received FROM events WHERE id = (SELECT max(id) FROM events WHERE valid = 't');") or die "Cannot prepare statement: " . $dbh->errstr; $sth->execute or die "Cannot execute statement: " . $sth->errstr; my $events_last_timestamp = $sth->fetchrow(); -if (!defined $events_last_timestamp) { +unless (defined $events_last_timestamp) { $events_last_timestamp = "none"; } @@ -169,7 +166,7 @@ if (!defined $events_last_timestamp) { $sth = $dbh->prepare("SELECT count(*) FROM clients;") or die "Cannot prepare statement: " . $dbh->errstr; $sth->execute or die "Cannot execute statement: " . $sth->errstr; my $clients_sum = $sth->fetchrow(); -if (!defined $clients_sum) { +unless (defined $clients_sum) { $clients_sum = "none"; } @@ -177,25 +174,23 @@ if (!defined $clients_sum) { $sth = $dbh->prepare("SELECT count(*) FROM clients WHERE valid = 't';") or die "Cannot prepare statement: " . $dbh->errstr; $sth->execute or die "Cannot execute statement: " . $sth->errstr; my $clients_sum_valid = $sth->fetchrow(); -if (!defined $clients_sum_valid) { +unless (defined $clients_sum_valid) { $clients_sum_valid = "none"; } -# sum of invaled records in table clients +# sum of invalid records in table clients $sth = $dbh->prepare("SELECT count(*) FROM clients WHERE valid = 'f';") or die "Cannot prepare statement: " . $dbh->errstr; $sth->execute or die "Cannot execute statement: " . $sth->errstr; my $clients_sum_invalid = $sth->fetchrow(); -if (!defined $clients_sum_invalid) { +unless (defined $clients_sum_invalid) { $clients_sum_invalid = "none"; } #------------------------------------------------------------------------------- # Statistics of active registered senders my $hash_ref; -if ($clients_sum != 0) { -# $sth = $dbh->prepare("SELECT clients.client_id, clients.hostname, clients.service, count(*), max(received) FROM events LEFT JOIN clients ON (events.hostname=clients.hostname AND events.service=clients.service) WHERE clients.valid='t' AND events.valid='t' GROUP BY client_id;") or die "Cannot prepare statement: " . $dbh->errstr; - $sth = $dbh->prepare("SELECT clients.client_id, clients.hostname, clients.service, count(*), max(received) FROM events LEFT JOIN clients ON (events.client_id=clients.client_id) WHERE clients.valid='t' AND events.valid='t' GROUP BY client_id;") or die "Cannot prepare statement: " . $dbh->errstr; - +unless ($clients_sum == 0) { + $sth = $dbh->prepare("SELECT clients.client_id, hostname, service, count(*), max(received) FROM events LEFT JOIN clients ON events.client_id = clients.client_id WHERE clients.valid='t' AND events.valid='t' GROUP BY clients.client_id;") or die "Cannot prepare statement: " . $dbh->errstr; $sth->execute or die "Cannot execute statement: " . $sth->errstr; my ($client_id, $hostname, $service, $count, $timestamp, $client_status); $hash_ref = $sth->fetchall_hashref("client_id"); @@ -234,7 +229,7 @@ print " - count of valid registered clients:\t$clients_sum_valid\n"; print " - count of invalid registered clients:\t$clients_sum_invalid\n"; print "\n"; -if ($clients_sum != 0) { +unless ($clients_sum == 0) { print "Statistics of ACTIVE and VALID registered senders:\n"; print "==================================================\n"; print "+------------------------------------------------------------------------------------------------------------------------+\n"; -- GitLab