| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Zonemaster::Backend::DB; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | our $VERSION = '1.2.0'; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 1428 | use Moose::Role; | 
|  | 2 |  |  |  |  | 8009 |  | 
|  | 2 |  |  |  |  | 7 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 9206 | use 5.14.2; | 
|  | 2 |  |  |  |  | 6 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 2 |  |  | 2 |  | 851 | use Data::Dumper; | 
|  | 2 |  |  |  |  | 9010 |  | 
|  | 2 |  |  |  |  | 849 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | requires 'add_api_user_to_db', 'user_exists_in_db', 'user_authorized', 'test_progress', 'test_results', | 
| 12 |  |  |  |  |  |  | 'create_new_batch_job', 'create_new_test', 'get_test_params', 'get_test_history', 'add_batch_job'; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub user_exists { | 
| 15 | 1 |  |  | 1 | 0 | 3 | my ( $self, $user ) = @_; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 1 | 50 |  |  |  | 5 | die "username not provided to the method user_exists\n" unless ( $user ); | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 1 |  |  |  |  | 4 | return $self->user_exists_in_db( $user ); | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub add_api_user { | 
| 23 | 1 |  |  | 1 | 0 | 3 | my ( $self, $username, $api_key ) = @_; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 1 | 50 | 33 |  |  | 7 | die "username or api_key not provided to the method add_api_user\n" | 
| 26 |  |  |  |  |  |  | unless ( $username && $api_key ); | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 1 | 50 |  |  |  | 5 | die "User already exists\n" if ( $self->user_exists( $username ) ); | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 1 |  |  |  |  | 7 | my $result = $self->add_api_user_to_db( $username, $api_key ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 1 | 50 |  |  |  | 6 | die "add_api_user_to_db not successfull" unless ( $result ); | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 1 |  |  |  |  | 11 | return $result; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | sub _get_allowed_id_field_name { | 
| 38 | 0 |  |  | 0 |  |  | my ( $self, $test_id ) = @_; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 0 |  |  |  |  |  | my $id_field; | 
| 41 | 0 | 0 |  |  |  |  | if (length($test_id) == 16) { | 
| 42 | 0 |  |  |  |  |  | $id_field = 'hash_id'; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  | else { | 
| 45 | 0 | 0 |  |  |  |  | if ($test_id <= Zonemaster::Backend::Config->force_hash_id_use_in_API_starting_from_id()) { | 
| 46 | 0 |  |  |  |  |  | $id_field = 'id'; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  | else { | 
| 49 | 0 |  |  |  |  |  | die "Querying test results with the [id] field is dissallowed by the current configuration values"; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | # Standatd SQL, can be here | 
| 55 |  |  |  |  |  |  | sub get_test_request { | 
| 56 | 0 |  |  | 0 | 0 |  | my ( $self ) = @_; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 0 |  |  |  |  |  | my $result_id; | 
| 59 | 0 |  |  |  |  |  | my $dbh = $self->dbh; | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 0 |  |  |  |  |  | my ( $id, $hash_id ); | 
| 63 | 0 |  |  |  |  |  | my $lock_on_queue = Zonemaster::Backend::Config->lock_on_queue(); | 
| 64 | 0 | 0 |  |  |  |  | if ( defined $lock_on_queue ) { | 
| 65 | 0 |  |  |  |  |  | ( $id, $hash_id ) = $dbh->selectrow_array( qq[ SELECT id, hash_id FROM test_results WHERE progress=0 AND queue=? ORDER BY priority DESC, id ASC LIMIT 1 ], undef, $lock_on_queue ); | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  | else { | 
| 68 | 0 |  |  |  |  |  | ( $id, $hash_id ) = $dbh->selectrow_array( q[ SELECT id, hash_id FROM test_results WHERE progress=0 ORDER BY priority DESC, id ASC LIMIT 1 ] ); | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 0 | 0 |  |  |  |  | if ($id) { | 
| 72 | 0 |  |  |  |  |  | $dbh->do( q[UPDATE test_results SET progress=1 WHERE id=?], undef, $id ); | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 | 0 |  |  |  |  | if ( $id > Zonemaster::Backend::Config->force_hash_id_use_in_API_starting_from_id() ) { | 
| 75 | 0 |  |  |  |  |  | $result_id = $hash_id; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | else { | 
| 78 | 0 |  |  |  |  |  | $result_id = $id; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 |  |  |  |  |  | return $result_id; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | # Standatd SQL, can be here | 
| 86 |  |  |  |  |  |  | sub get_batch_job_result { | 
| 87 | 0 |  |  | 0 | 0 |  | my ( $self, $batch_id ) = @_; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 0 |  |  |  |  |  | my $dbh = $self->dbh; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 0 |  |  |  |  |  | my %result; | 
| 92 | 0 |  |  |  |  |  | $result{nb_running} = 0; | 
| 93 | 0 |  |  |  |  |  | $result{nb_finished} = 0; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 0 |  |  |  |  |  | my $query = " | 
| 96 |  |  |  |  |  |  | SELECT hash_id, progress | 
| 97 |  |  |  |  |  |  | FROM test_results | 
| 98 |  |  |  |  |  |  | WHERE batch_id=?"; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 0 |  |  |  |  |  | my $sth1 = $dbh->prepare( $query ); | 
| 101 | 0 |  |  |  |  |  | $sth1->execute( $batch_id ); | 
| 102 | 0 |  |  |  |  |  | while ( my $h = $sth1->fetchrow_hashref ) { | 
| 103 | 0 | 0 |  |  |  |  | if ( $h->{progress} eq '100' ) { | 
| 104 | 0 |  |  |  |  |  | $result{nb_finished}++; | 
| 105 | 0 |  |  |  |  |  | push(@{$result{finished_test_ids}}, $h->{hash_id}); | 
|  | 0 |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  | else { | 
| 108 | 0 |  |  |  |  |  | $result{nb_running}++; | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 |  |  |  |  |  | return \%result; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 2 |  |  | 2 |  | 16 | no Moose::Role; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 19 |  | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | 1; |