Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 69 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ScheduleQueue | |
0.00% |
0 / 69 |
|
0.00% |
0 / 6 |
462 | |
0.00% |
0 / 1 |
| get | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
132 | |||
| insert | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| pop | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| bumpHold | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| adjustPriority | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| remove | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Scheduling |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Scheduling; |
| 16 | |
| 17 | /** |
| 18 | * Scheduler. |
| 19 | * |
| 20 | * @package phpOMS\Scheduling |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | final class ScheduleQueue |
| 26 | { |
| 27 | public array $queue = []; |
| 28 | |
| 29 | public function get(int $size = 1, int $type = PriorityMode::FIFO) : array |
| 30 | { |
| 31 | $jobs = []; |
| 32 | $keys = \array_keys($this->queue); |
| 33 | |
| 34 | switch ($type) { |
| 35 | case PriorityMode::FIFO: |
| 36 | for ($i = 0; $i < $size; ++$i) { |
| 37 | $jobs[$i] = $this->queue[$keys[$i]]; |
| 38 | } |
| 39 | |
| 40 | break; |
| 41 | case PriorityMode::LIFO: |
| 42 | for ($i = \count($this->queue) - $size - 1; $i < $size; ++$i) { |
| 43 | $jobs[$i] = $this->queue[$keys[$i]]; |
| 44 | } |
| 45 | |
| 46 | break; |
| 47 | case PriorityMode::PRIORITY: |
| 48 | $queue = $this->queue; |
| 49 | \uasort($queue, function (Job $a, Job $b) { |
| 50 | return $a->priority <=> $b->priority; |
| 51 | }); |
| 52 | |
| 53 | $jobs = \array_slice($queue, 0, $size, true); |
| 54 | |
| 55 | break; |
| 56 | case PriorityMode::VALUE: |
| 57 | $queue = $this->queue; |
| 58 | \uasort($queue, function (Job $a, Job $b) { |
| 59 | return $b->value <=> $a->value; |
| 60 | }); |
| 61 | |
| 62 | $jobs = \array_slice($queue, 0, $size, true); |
| 63 | |
| 64 | break; |
| 65 | case PriorityMode::COST: |
| 66 | $queue = $this->queue; |
| 67 | \uasort($queue, function (Job $a, Job $b) { |
| 68 | return $a->cost <=> $b->cost; |
| 69 | }); |
| 70 | |
| 71 | $jobs = \array_slice($queue, 0, $size, true); |
| 72 | |
| 73 | break; |
| 74 | case PriorityMode::PROFIT: |
| 75 | $queue = $this->queue; |
| 76 | \uasort($queue, function (Job $a, Job $b) { |
| 77 | return $b->getProfit() <=> $a->getProfit(); |
| 78 | }); |
| 79 | |
| 80 | $jobs = \array_slice($queue, 0, $size, true); |
| 81 | |
| 82 | break; |
| 83 | case PriorityMode::HOLD: |
| 84 | $queue = $this->queue; |
| 85 | \uasort($queue, function (Job $a, Job $b) { |
| 86 | return $b->onhold <=> $a->onhold; |
| 87 | }); |
| 88 | |
| 89 | $jobs = \array_slice($queue, 0, $size, true); |
| 90 | |
| 91 | break; |
| 92 | case PriorityMode::EARLIEST_DEADLINE: |
| 93 | $queue = $this->queue; |
| 94 | \uasort($queue, function (Job $a, Job $b) { |
| 95 | return $a->deadline->getTimestamp() <=> $b->deadline->getTimestamp(); |
| 96 | }); |
| 97 | |
| 98 | $jobs = \array_slice($queue, 0, $size, true); |
| 99 | |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | return $jobs; |
| 104 | } |
| 105 | |
| 106 | public function insert(int $id, Job $job) : void |
| 107 | { |
| 108 | $this->queue[$id] = $job; |
| 109 | } |
| 110 | |
| 111 | public function pop(int $size = 1, int $type = PriorityMode::FIFO) : array |
| 112 | { |
| 113 | $jobs = $this->get($size, $type); |
| 114 | foreach ($jobs as $id => $_) { |
| 115 | unset($this->queue[$id]); |
| 116 | } |
| 117 | |
| 118 | return $jobs; |
| 119 | } |
| 120 | |
| 121 | public function bumpHold(int $id = 0) : void |
| 122 | { |
| 123 | if ($id === 0) { |
| 124 | foreach ($this->queue as $job) { |
| 125 | ++$job->onhold; |
| 126 | } |
| 127 | } else { |
| 128 | ++$this->queue[$id]->onhold; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public function adjustPriority(int $id = 0, float $priority = 0.1) : void |
| 133 | { |
| 134 | if ($id === 0) { |
| 135 | foreach ($this->queue as $job) { |
| 136 | $job->priority += $priority; |
| 137 | } |
| 138 | } else { |
| 139 | $this->queue[$id]->priority += $priority; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | public function remove(string $id) : void |
| 144 | { |
| 145 | unset($this->queue[$id]); |
| 146 | } |
| 147 | } |