<commit_before>package Tapper::Cmd::Scenario;

use warnings;
use strict;
use 5.010;
use Moose;

use YAML::XS;
use Tapper::Model 'model';
use parent 'Tapper::Cmd';

use Tapper::Cmd::Testrun;


=head1 NAME

Tapper::Cmd::Scenario - Backend functions for manipulation of scenario in the database

=head1 SYNOPSIS

This project offers backend functions for all projects for manipulation the
database on a higher level than that offered by Tapper::Schema.

    use Tapper::Cmd::Scenario;

    my $bar = Tapper::Cmd::Scenario->new();
    $bar->add($scenario);
    ...

=head1 FUNCTIONS

=head2 create

Create a new scenario from one element of a test plan (actually a test
plan instance). If the new scenario belong to a test plan instance the
function expects the id of this instance as second parameter.

@param hash ref - test plan element
@param instance - test plan instance id

@return array   - scenario ids

=cut

sub create {
        my ($self, $scenario, $instance) = @_;
        $scenario->{instance_id} ||= $instance;
        return $self->add($scenario);
}

=head2 parse_interdep

Parse an interdep scenario and do everything needed to put it into the
database.

@param hash ref - config containing all relevant information
@param hash ref - options

@return success - 0
@return error   - die with error text

=cut

sub parse_interdep
{
        my ($self, $conf) = @_;

        # TODO
        # 1.) make atomic
        # 2.) check whether more than 2 testruns are local and prevent sync in this case

        my $scenario = model('TestrunDB')->resultset('Scenario')->new({type => 'interdep',
                                                                       options => $conf->{scenario_options} || $conf->{options},
                                                                       name => $conf->{scenario_name} || $conf->{name},
                                                                      });
        $scenario->insert;
        my $sc_id    = $scenario->id;
        my @all_ids;
        foreach my $plan (@{$conf->{scenario_description} || $conf->{description}}) {
                my $tr = Tapper::Cmd::Testrun->new();
                $plan->{scenario_id} ||= $sc_id;
                $plan->{status} = 'prepare';
                my @ids = $tr->create($plan, $conf->{instance_id});
                push @all_ids, @ids;
        }

        # when there is only on scenario element in the DB and the scheduler
        # picks it it will think all scenario elements are fitted and start it
        # therefore, we can only set them to 'schedule' when all testruns are in the DB
        foreach my $id (@all_ids) {
                my $testrun = model('TestrunDB')->resultset('Testrun')->find($id);
                $testrun->testrun_scheduling->status('schedule');<commit_msg>[FIX] TODO help string (one less)<commit_after>package Tapper::Cmd::Scenario;

use warnings;
use strict;
use 5.010;
use Moose;

use YAML::XS;
use Tapper::Model 'model';
use parent 'Tapper::Cmd';

use Tapper::Cmd::Testrun;


=head1 NAME

Tapper::Cmd::Scenario - Backend functions for manipulation of scenario in the database

=head1 SYNOPSIS

This project offers backend functions for all projects for manipulation the
database on a higher level than that offered by Tapper::Schema.

    use Tapper::Cmd::Scenario;

    my $bar = Tapper::Cmd::Scenario->new();
    $bar->add($scenario);
    ...

=head1 FUNCTIONS

=head2 create

Create a new scenario from one element of a test plan (actually a test
plan instance). If the new scenario belong to a test plan instance the
function expects the id of this instance as second parameter.

@param hash ref - test plan element
@param instance - test plan instance id

@return array   - scenario ids

=cut

sub create {
        my ($self, $scenario, $instance) = @_;
        $scenario->{instance_id} ||= $instance;
        return $self->add($scenario);
}

=head2 parse_interdep

Parse an interdep scenario and do everything needed to put it into the
database.

@param hash ref - config containing all relevant information
@param hash ref - options

@return success - 0
@return error   - die with error text

=cut

sub parse_interdep
{
        my ($self, $conf) = @_;

        # TODO
        # 2.) check whether more than 2 testruns are local and prevent sync in this case

        my $scenario = model('TestrunDB')->resultset('Scenario')->new({type => 'interdep',
                                                                       options => $conf->{scenario_options} || $conf->{options},
                                                                       name => $conf->{scenario_name} || $conf->{name},
                                                                      });
        $scenario->insert;
        my $sc_id    = $scenario->id;
        my @all_ids;
        foreach my $plan (@{$conf->{scenario_description} || $conf->{description}}) {
                my $tr = Tapper::Cmd::Testrun->new();
                $plan->{scenario_id} ||= $sc_id;
                $plan->{status} = 'prepare';
                my @ids = $tr->create($plan, $conf->{instance_id});
                push @all_ids, @ids;
        }

        # when there is only on scenario element in the DB and the scheduler
        # picks it it will think all scenario elements are fitted and start it
        # therefore, we can only set them to 'schedule' when all testruns are in the DB
        foreach my $id (@all_ids) {
                my $testrun = model('TestrunDB')->resultset('Testrun')->find($id);
                $testrun->testrun_scheduling->status('schedule');
{'fraction_non_alphanumeric': 0.11113099498926271, 'fraction_numerical': 0.004473872584108805, 'mean_word_length': 2.24376088218224, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5418458', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1494, 'n_words': 551}