| 1 | package P2::Scheduler; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use v5.18; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | P2::Scheduler - This module helps organizing monthly tests at component level. | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 VERSION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Version 0.04 | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =cut | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use P2::Scheduler; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | my $scheduler = P2::Scheduler->new( | ||||||||||||||||||||||||||||
| 23 | experiments => [][], # experiments ordered by priority (mandatory) | ||||||||||||||||||||||||||||
| 24 | weight => [3,2,1], # the weight of each priority, basically how many tests to run for a | ||||||||||||||||||||||||||||
| 25 | # given priority (defaults to [3,2,1]) | ||||||||||||||||||||||||||||
| 26 | hour_slots => [1,2,3,4,5], # hours of the day at which tests can run (mandatory) | ||||||||||||||||||||||||||||
| 27 | spare_days_per_month => 2, # number of business days to leave as spare at the end of the month | ||||||||||||||||||||||||||||
| 28 | starting_from => 6, # index of the day of the month to start from (defaults to 1) | ||||||||||||||||||||||||||||
| 29 | year => ... # mandatory | ||||||||||||||||||||||||||||
| 30 | num_week_days => 3 # number of business days to count starting from Monday, defaults to 5 | ||||||||||||||||||||||||||||
| 31 | pri_per_month' => [3,3,3,3,3,3,3,3,3,3,3,3] # an array for the minimum priority to take into consideration when scheduling | ||||||||||||||||||||||||||||
| 32 | # experiments for each month. Default to all | ||||||||||||||||||||||||||||
| 33 | # priorities for all months. This is meant to be | ||||||||||||||||||||||||||||
| 34 | # used in case we want to limit priority 2/3 | ||||||||||||||||||||||||||||
| 35 | # experiments only to specific months. | ||||||||||||||||||||||||||||
| 36 | selected_week_days => [3,4], # list of business days ids to consider: | ||||||||||||||||||||||||||||
| 37 | # 1 => Monday,..., 5 => Friday | ||||||||||||||||||||||||||||
| 38 | # for example the string '2,3,4' would set tests to run only | ||||||||||||||||||||||||||||
| 39 | # between Tuesdays and Thursdays | ||||||||||||||||||||||||||||
| 40 | fixed_intervals => 1 # if experiments times should be strictly scheduled in | ||||||||||||||||||||||||||||
| 41 | # fixed intervals or randomly allocated | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # getting the schedule for January... | ||||||||||||||||||||||||||||
| 46 | my @schedule = $scheduler->month_schedule(1); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # or for the whole year... | ||||||||||||||||||||||||||||
| 49 | my @schedule = $scheduler->year_schedule(); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Marco Masetti, C<< <marco.masetti at sky.uk> >> | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | perldoc P2::Scheduler | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Copyright 2020 Marco Masetti. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 70 | under the terms of the the BSD 3-Clause License. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 76 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | use POSIX qw(ceil); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | use UUID qw(uuid); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | 1 | use Date::Calc qw( | |||||||||||||||||||||||||||
| 80 | Days_in_Month | ||||||||||||||||||||||||||||
| 81 | Day_of_Week | ||||||||||||||||||||||||||||
| 82 | 1 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # experiments per priority | ||||||||||||||||||||||||||||
| 85 | 1 | has 'experiments' => (is => 'ro', isa => 'ArrayRef', required => 1); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # how many tests per priority per day | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | has 'weight' => (is => 'rw', isa => 'ArrayRef', default => sub {[3,2,1]}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # use this to set the hours in the testing slot | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | has 'hour_slots' => (is => 'rw', isa => 'ArrayRef', default => sub {[1,2,3,4,5]} ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 92 | 1 | has 'fixed_intervals' => ( is => 'rw', isa => 'Bool', default => 0 ); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | has 'year' => (is => 'rw', isa => 'Int', default => 2020); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # this to set to run P3 and P2 tests only on selected months | ||||||||||||||||||||||||||||
| 97 | # defaults to run all tests for all priorities all year round | ||||||||||||||||||||||||||||
| 98 | 1 | ▶ | has 'pri_per_month' => (is => 'rw', isa => 'ArrayRef', default => sub{ return [3,3,3,3,3,3,3,3,3,3,3,3]}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # this to leave some spare days at the end of the month to reschedule tests | ||||||||||||||||||||||||||||
| 101 | # defaults to 0 spare days | ||||||||||||||||||||||||||||
| 102 | 1 | has 'spare_days_per_month' => (is => 'rw', isa => 'Int', default => 0); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # this to start right not at the first business day of the month | ||||||||||||||||||||||||||||
| 105 | # usefull when you reschedule things in between... | ||||||||||||||||||||||||||||
| 106 | 1 | has 'starting_from' => (is => 'rw', isa => 'Int', default => 0); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # use this if you want to limit the number of testing days in a week, always starting | ||||||||||||||||||||||||||||
| 109 | # from Monday, defaults to 5 (Monday to Friday) | ||||||||||||||||||||||||||||
| 110 | 1 | has 'num_week_days' => (is => 'rw', isa => 'Int', default => 5); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # use this if you want to select the business days to run the tests | ||||||||||||||||||||||||||||
| 113 | # 1 => Monday,..., 5 => Friday | ||||||||||||||||||||||||||||
| 114 | 1 | ▶ | has 'selected_week_days' => (is => 'rw', isa => 'ArrayRef', default => sub{ return [1,2,3,4,5] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # the Kubernetes (or any other means to address the proper cloud) context... | ||||||||||||||||||||||||||||
| 117 | 1 | has 'context' => (is => 'rw', isa => 'Str'); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub BUILD { | ||||||||||||||||||||||||||||
| 120 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | $self->{tot_mins} = ($self->{hour_slots}->[-1] - $self->{hour_slots}->[0]) * 60; | |||||||||||||||||||||||||||
| 123 | 1 | my $tot_experiments = eval join '+', @{$self->{weight}}; | |||||||||||||||||||||||||||
| 124 | 1 | $self->{mins_interval} = $self->{tot_mins} / $tot_experiments; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub year_schedule { | ||||||||||||||||||||||||||||
| 128 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
year_schedule: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1 | my @year_schedule = (); | |||||||||||||||||||||||||||
| 131 | 1 | for (1..12) { | |||||||||||||||||||||||||||
| 132 | 12 | push @year_schedule, $self->month_schedule($_); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 1 | return @year_schedule; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub month_schedule { | ||||||||||||||||||||||||||||
| 139 | 13 | ▶ | my ( $self, $month ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
month_schedule: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 13 | my @schedule = (); | |||||||||||||||||||||||||||
| 142 | 13 | my @bd = $self->_get_business_days_of_month( $month ); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 13 | my @experiment_index=(0,0,0); | |||||||||||||||||||||||||||
| 145 | 13 | ▶ | for (my $day_index=0;$day_index<scalar(@bd) - $self->spare_days_per_month;$day_index++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 86 | ▶ | next unless ( $bd[$day_index] >= $self->starting_from ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 69 | my $exp_index=1; | |||||||||||||||||||||||||||
| 148 | 69 | ▶ | for (my $priority=0;$priority<$self->pri_per_month->[$month-1];$priority++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 207 | ▶ | next unless $self->experiments->[$priority]; # skip this priority if no experiments there... | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 207 | ▶ | for ( my $index=0;$index<$self->weight->[$priority];$index++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 414 | my $exp_mins = $self->{mins_interval} * $exp_index; | |||||||||||||||||||||||||||
| 152 | push @schedule, { | ||||||||||||||||||||||||||||
| 153 | year => $self->year, | ||||||||||||||||||||||||||||
| 154 | month => sprintf("%02d", $month), | ||||||||||||||||||||||||||||
| 155 | day => sprintf("%02d", $bd[$day_index]), | ||||||||||||||||||||||||||||
| 156 | experiment => $self->experiments->[$priority][$experiment_index[$priority]], | ||||||||||||||||||||||||||||
| 157 | priority => $priority+1, | ||||||||||||||||||||||||||||
| 158 | hour => $self->{fixed_intervals} ? sprintf("%02d", $self->{hour_slots}->[0] + int($exp_mins/60)) : | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sprintf("%02d", $self->hour_slots->[ int(rand(1000)) % scalar( @{$self->hour_slots} ) ]), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 414 | ▶ | min => $self->{fixed_intervals} ? sprintf("%02d", $exp_mins % 60) : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | sprintf("%02d", int(rand(100)) % 60), | ||||||||||||||||||||||||||||
| 162 | context => $self->context | ||||||||||||||||||||||||||||
| 163 | }; | ||||||||||||||||||||||||||||
| 164 | $experiment_index[$priority] = ($experiment_index[$priority] + 1) % | ||||||||||||||||||||||||||||
| 165 | 414 | scalar( @{$self->experiments->[$priority]} ); | |||||||||||||||||||||||||||
| 166 | 414 | $exp_index++; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 13 | return map{{ | |||||||||||||||||||||||||||
| 172 | exp_id => lc( $_->{experiment}.'-'.substr(uuid(), -8) ), | ||||||||||||||||||||||||||||
| 173 | context => $_->{context}, | ||||||||||||||||||||||||||||
| 174 | filepath => $_->{experiment}, | ||||||||||||||||||||||||||||
| 175 | schedule => "$_->{min} $_->{hour} $_->{day} $_->{month} *", | ||||||||||||||||||||||||||||
| 176 | year => $self->year, | ||||||||||||||||||||||||||||
| 177 | mins => $_->{min}, | ||||||||||||||||||||||||||||
| 178 | hour => $_->{hour}, | ||||||||||||||||||||||||||||
| 179 | dom => $_->{day}, | ||||||||||||||||||||||||||||
| 180 | month => $_->{month}, | ||||||||||||||||||||||||||||
| 181 | 414 | dow => '*' | |||||||||||||||||||||||||||
| 182 | }} @schedule; | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | sub _get_business_days_of_month { | ||||||||||||||||||||||||||||
| 186 | 14 | ▶ | my ( $self, $month ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_business_days_of_month: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 14 | my @business_days=(); | |||||||||||||||||||||||||||
| 189 | 14 | my $days = Days_in_Month($self->year, $month); | |||||||||||||||||||||||||||
| 190 | 14 | foreach my $day (1..$days) { | |||||||||||||||||||||||||||
| 191 | 428 | my $dow = Day_of_Week($self->year, $month, $day); | |||||||||||||||||||||||||||
| 192 | 428 | ▶ | if (( $dow <= $self->num_week_days) && ( grep /$dow/, @{$self->selected_week_days} )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$dow <= $self->num_week_days and grep /$dow/u, @{$self->selected_week_days;}
MC/DC50%1/2
$dow <= $self->num_week_days and grep /$dow/u, @{$self->selected_week_days;}
$dow <= $self->num_week_days
grep /$dow/u, @{$self->selected_week_days;}
Truth table66%2/3
A and B
A= $dow <= $self->num_week_daysB= grep /$dow/u, @{$self->selected_week_days;}
| |||||||||||||||||||||||||||||
| 193 | 120 | push @business_days, $day; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | 14 | return @business_days; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 1 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 200 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||