<commit_before>use MooseX::Declare;

=head1 NAME

Artemis::Cmd::Testrun - Backend functions for manipluation of testruns in the database

=head1 SYNOPSIS

This project offers backend functions for all projects that manipulate
testruns or preconditions in the database. This module handles the testrun part.

    use Artemis::Cmd::Testrun;

    my $bar = Artemis::Cmd::Testrun->new();
    $bar->add($testrun);
    ...

=head1 FUNCTIONS


=head2 add

Add a new testrun to database.

=cut

class Artemis::Cmd::Testrun
    extends Artemis::Cmd
{
        use Artemis::Model 'model';
        use DateTime;
        use Data::Dumper;
        use Artemis::Exception;

=head2 add

Add a new testrun. Hostname/hardwaredb_systems_id and owner/owner_user_id
allow to specify the associated value as id or string which will be converted
to the associated id. If both values are given the id is used and the string
is ignored. The function expects a hash reference with the following options:
-- required --
* hardwaredb_systems_id - int
or
* hostname - string
-- optional --
* notes - string
* shortname - string
* topic - string
* date - DateTime
* owner_user_id - int
or
* owner - string

@param hash ref - options for new testrun

@return success - testrun id
@return error   - undef

=cut

        method add($args) {

                my %args = %{$args}; # copy

                $args{notes}                 ||= '';
                $args{shortname}             ||=  '';

                $args{topic_name}              = $args->{topic}    || 'Misc';
                my $topic = model('TestrunDB')->resultset('Topic')->find_or_create({name => $args{topic_name}});
                
                $args{earliest}              ||= DateTime->now;
                $args{owner}                 ||= $ENV{USER};
                $args{owner_user_id}         ||= Artemis::Model::get_user_id_for_login(       $args->{owner}    );
                $args{hardwaredb_systems_id} ||= Artemis::Model::get_systems_id_for_hostname( $args->{hostname} );
                if (not $args{queue_id}) {
                        $args{queue}   ||= 'AdHoc';
                        my $queue_result = model('TestrunDB')->resultset('Queue')->search({name => $args{queue}}); 
                        return if not $queue_result;
                        $args{queue_id}  = $queue_id->first->id;
                }
                return model('TestrunDB')->resultset('Testrun')->add(\%args);
        }


=head2 update

Changes values of an existing testrun. Hostname/hardwaredb_systems_id and
owner/owner_user_id allow to specify the associated value as id or string
which will be converted to the associated id. If both values are given the id
is used and the string is ignored. The function expects a hash reference with
the following options (at least one should be given):

* hardwaredb_systems_id - int
or
* hostname  - string
* notes     - string
* shortname - string
* topic     - string
* date      - DateTime
* owner_user_id - int
or
* owner     - string

@param int      - testrun id
@param hash ref - options for new testrun

@return success - testrun id
@return error   - undef

=cut<commit_msg>Cmd: fixed wrong variable access<commit_after>use MooseX::Declare;

=head1 NAME

Artemis::Cmd::Testrun - Backend functions for manipluation of testruns in the database

=head1 SYNOPSIS

This project offers backend functions for all projects that manipulate
testruns or preconditions in the database. This module handles the testrun part.

    use Artemis::Cmd::Testrun;

    my $bar = Artemis::Cmd::Testrun->new();
    $bar->add($testrun);
    ...

=head1 FUNCTIONS


=head2 add

Add a new testrun to database.

=cut

class Artemis::Cmd::Testrun
    extends Artemis::Cmd
{
        use Artemis::Model 'model';
        use DateTime;
        use Data::Dumper;
        use Artemis::Exception;

=head2 add

Add a new testrun. Hostname/hardwaredb_systems_id and owner/owner_user_id
allow to specify the associated value as id or string which will be converted
to the associated id. If both values are given the id is used and the string
is ignored. The function expects a hash reference with the following options:
-- required --
* hardwaredb_systems_id - int
or
* hostname - string
-- optional --
* notes - string
* shortname - string
* topic - string
* date - DateTime
* owner_user_id - int
or
* owner - string

@param hash ref - options for new testrun

@return success - testrun id
@return error   - undef

=cut

        method add($args) {

                my %args = %{$args}; # copy

                $args{notes}                 ||= '';
                $args{shortname}             ||=  '';

                $args{topic_name}              = $args->{topic}    || 'Misc';
                my $topic = model('TestrunDB')->resultset('Topic')->find_or_create({name => $args{topic_name}});
                
                $args{earliest}              ||= DateTime->now;
                $args{owner}                 ||= $ENV{USER};
                $args{owner_user_id}         ||= Artemis::Model::get_user_id_for_login(       $args->{owner}    );
                $args{hardwaredb_systems_id} ||= Artemis::Model::get_systems_id_for_hostname( $args->{hostname} );
                if (not $args{queue_id}) {
                        $args{queue}   ||= 'AdHoc';
                        my $queue_result = model('TestrunDB')->resultset('Queue')->search({name => $args{queue}}); 
                        return if not $queue_result;
                        $args{queue_id}  = $queue_result->first->id;
                }
                return model('TestrunDB')->resultset('Testrun')->add(\%args);
        }


=head2 update

Changes values of an existing testrun. Hostname/hardwaredb_systems_id and
owner/owner_user_id allow to specify the associated value as id or string
which will be converted to the associated id. If both values are given the id
is used and the string is ignored. The function expects a hash reference with
the following options (at least one should be given):

* hardwaredb_systems_id - int
or
* hostname  - string
* notes     - string
* shortname - string
* topic     - string
* date      - DateTime
* owner_user_id - int
or
* owner     - string

@param int      - testrun id
@param hash ref - options for new testrun

@return success - testrun id
@return error   - undef

=cut
{'fraction_non_alphanumeric': 0.10373052101369432, 'fraction_numerical': 0.001888871399338895, 'mean_word_length': 2.3424513413992636, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '743143', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1807, 'n_words': 587}