Commit 70b7d68d authored by Gaël Berthaud-Müller's avatar Gaël Berthaud-Müller Committed by Alexandre
Browse files

create new result_entries table

parent c13acab1
......@@ -168,12 +168,15 @@ sub test_results {
if ( $results );
my $result;
my ( $hrefs ) = $dbh->selectall_hashref( "SELECT id, hash_id, creation_time at time zone current_setting('TIMEZONE') at time zone 'UTC' as creation_time, params, results FROM test_results WHERE hash_id=?", 'hash_id', undef, $test_id );
my ( $hrefs ) = $dbh->selectall_hashref( "SELECT id, hash_id, creation_time at time zone current_setting('TIMEZONE') at time zone 'UTC' as creation_time, params FROM test_results WHERE hash_id=?", 'hash_id', undef, $test_id );
$result = $hrefs->{$test_id};
die Zonemaster::Backend::Error::ResourceNotFound->new( message => "Test not found", data => { test_id => $test_id } )
unless defined $result;
my @result_entries = $dbh->selectall_array( "SELECT level, module, testcase, tag, timestamp, args FROM result_entries WHERE hash_id=?", { Slice => {} }, $test_id );
eval {
# This workaround is needed to properly handle all versions of perl and the DBD::Pg module
# More details in the zonemaster backend issue #570
......@@ -184,16 +187,14 @@ sub test_results {
$result->{params} = decode_json( $result->{params} );
}
if (defined $result->{results} ) {
if (utf8::is_utf8($result->{results} ) ) {
$result->{results} = decode_json( encode_utf8($result->{results}) );
}
else {
$result->{results} = decode_json( $result->{results} );
@result_entries = map {
{
%$_,
args => utf8::is_utf8( $_->{args} ) ? decode_json( encode_utf8( $_->{args} ) ) : decode_json( $_->{args} ) ,
}
} else {
$result->{results} = [];
}
} @result_entries;
$result->{results} = \@result_entries;
};
die Zonemaster::Backend::Error::JsonError->new( reason => "$@", data => { test_id => $test_id })
......@@ -217,9 +218,9 @@ sub get_test_history {
my @results;
my $query = "
SELECT
(SELECT count(*) FROM (SELECT json_array_elements(results) AS result) AS t1 WHERE result->>'level'='CRITICAL') AS nb_critical,
(SELECT count(*) FROM (SELECT json_array_elements(results) AS result) AS t1 WHERE result->>'level'='ERROR') AS nb_error,
(SELECT count(*) FROM (SELECT json_array_elements(results) AS result) AS t1 WHERE result->>'level'='WARNING') AS nb_warning,
(SELECT count(*) FROM result_entries where result_entries.hash_id = test_results.hash_id AND level = 5) AS nb_critical,
(SELECT count(*) FROM result_entries where result_entries.hash_id = test_results.hash_id AND level = 4) AS nb_error,
(SELECT count(*) FROM result_entries where result_entries.hash_id = test_results.hash_id AND level = 3) AS nb_warning,
id,
hash_id,
undelegated,
......@@ -349,6 +350,22 @@ sub get_relative_start_time {
return $self->dbh->selectrow_array("SELECT EXTRACT(EPOCH FROM now() - test_start_time) FROM test_results WHERE hash_id=?", undef, $hash_id);
}
sub add_result_entry {
my ( $self, $hash_id, $entry ) = @_;
my $nb_inserted = $self->dbh->do(
"INSERT INTO result_entries (hash_id, level, module, testcase, tag, timestamp, args) VALUES (?, ?, ?, ?, ?, ?, ?)",
undef,
$hash_id,
$entry->{level},
$entry->{module},
$entry->{testcase},
$entry->{tag},
$entry->{timestamp},
encode_json( $entry->{args} ),
);
return $nb_inserted;
}
no Moose;
__PACKAGE__->meta()->make_immutable();
......
......@@ -9,6 +9,7 @@ use DBI qw(:utils);
use JSON::PP;
use Scalar::Util qw( blessed );
use File::Slurp;
use Log::Any qw( $log );
use Zonemaster::LDNS;
......@@ -16,6 +17,7 @@ use Zonemaster::Engine;
use Zonemaster::Engine::Translator;
use Zonemaster::Backend::Config;
use Zonemaster::Engine::Profile;
use Zonemaster::Engine::Logger::Entry;
sub new {
my ( $class, $params ) = @_;
......@@ -82,6 +84,7 @@ sub run {
die "Must give the name of a domain to test.\n";
}
$domain = $self->to_idn( $domain );
my %numeric = Zonemaster::Engine::Logger::Entry->levels();
# used for progress indicator
my ( $previous_module, $previous_method ) = ( '', '' );
......@@ -91,6 +94,23 @@ sub run {
sub {
my ( $entry ) = @_;
if ( $entry->numeric_level >= $numeric{INFO} ) {
$log->info($entry->string);
eval {
$self->{_db}->add_result_entry( $test_id, {
timestamp => $entry->timestamp,
module => $entry->module,
testcase => $entry->testcase,
tag => $entry->tag,
level => $entry->numeric_level,
args => $entry->args // {},
});
};
if ($@) {
$log->error($@);
}
}
foreach my $trace ( reverse @{ $entry->trace } ) {
foreach my $module_method ( keys %{ $counter_for_progress_indicator{planned} } ) {
if ( index( $trace->[1], $module_method ) > -1 ) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment