ProgressBar::advance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ProcessBundle\Console;
6
7
use Symfony\Component\Console\Helper\Helper;
8
use Symfony\Component\Console\Helper\ProgressBar as SymfonyProgressBar;
9
use Symfony\Component\Console\Output\NullOutput;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * @internal
14
 * Class ProgressBar
15
 */
16
class ProgressBar
17
{
18
    const INTERVAL_MONITORING = [1, 10, 20];
19
    /** @var OutputInterface */
20
    private $output;
21
    /** @var SymfonyProgressBar */
22
    private $progressBar;
23
24
    private $timelineMemory = [];
25
    private $intervalMemory = [-1, -1, -1];
26
    private $maxMemory;
27
28
    private $timelineItemPerSecond = [];
29
    private $intervalItemPerSecond = [-1, -1, -1];
30
    private $minItemPerSecond;
31
32
    private $lastTimeUpdated;
33
    private $timeElapsed = 0;
34
    private $previousItemCount = 0;
35
36 6
    public function setOutput($output)
37
    {
38 6
        $this->output = $output;
39 6
    }
40
41 5
    public function create($size, $title = 'Progression', $maxMemory = 30, $minItemPerSecond = 50)
42
    {
43 5
        $this->init();
44 5
        $this->maxMemory = $maxMemory;
45 5
        $this->minItemPerSecond = $minItemPerSecond;
46
47 5
        $this->progressBar =
48 5
            ($this->output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL)
49 1
            ? new SymfonyProgressBar(new NullOutput(), $size)
50 4
            : new SymfonyProgressBar($this->output, $size);
51 5
        SymfonyProgressBar::setPlaceholderFormatterDefinition('memory', [$this, 'renderRightbar']);
52 5
        $this->progressBar->setFormat(" \033[44;37m %title:-37s% \033[0m\n %current%/%max% %bar% %percent:3s%%\n 🏁  %remaining:-10s% %memory:37s%");
53 5
        $this->progressBar->setBarCharacter($done = "\033[32m=\033[0m");
54 5
        $this->progressBar->setEmptyBarCharacter($empty = "\033[31m \033[0m");
55 5
        $this->progressBar->setProgressCharacter($progress = "\033[32m>\033[0m");
56 5
        $this->progressBar->setBarWidth(400);
57 5
        $this->progressBar->setMessage($title, 'title');
58 5
        $this->progressBar->start();
59 5
    }
60
61 4
    public function renderRightbar()
62
    {
63 4
        return sprintf(
64 4
            'MEMORY %s / ITEMS %s',
65 4
            implode(' ', $this->intervalMemory),
66 4
            implode(' ', $this->intervalItemPerSecond)
67
        );
68
    }
69
70 4
    public function setProgress($progress)
71
    {
72 4
        if ($this->isUpdate()) {
73 2
            $this->updateMemory();
74 2
            $this->updateItemPerSecond();
75 2
            $this->progressBar->setProgress($progress);
76
        }
77 4
    }
78
79 1
    public function advance()
80
    {
81 1
        if ($this->isUpdate()) {
82 1
            $this->updateMemory();
83 1
            $this->updateItemPerSecond();
84 1
            $this->progressBar->advance();
85
        }
86 1
    }
87
88 1
    public function finish()
89
    {
90 1
        $this->progressBar->finish();
91 1
    }
92
93 5
    private function init()
94
    {
95 5
        $this->timelineMemory = [];
96 5
        $this->intervalMemory = [-1, -1, -1];
97
98 5
        $this->timelineItemPerSecond = [];
99 5
        $this->intervalItemPerSecond = [-1, -1, -1];
100
101 5
        $this->lastTimeUpdated = time();
102 5
        $this->previousItemCount = 0;
103 5
    }
104
105 4
    private function isUpdate()
106
    {
107 4
        if (null === $this->progressBar) {
108 1
            return;
109
        }
110
111 3
        $time = time();
112
113 3
        if ($time > $this->lastTimeUpdated) {
114 2
            $this->timeElapsed = $time - $this->lastTimeUpdated;
115 2
            $this->lastTimeUpdated = $time;
116
117 2
            return true;
118
        }
119
120 1
        return false;
121
    }
122
123 2
    private function updateMemory()
124
    {
125 2
        $time = time();
126
127 2
        if (count($this->timelineMemory) > self::INTERVAL_MONITORING[2]) {
128 1
            $this->timelineMemory = array_slice($this->timelineMemory, 1, null, true);
129
        }
130
131 2
        $this->timelineMemory[$time] = $now = memory_get_usage(true);
132
133 2
        $this->intervalMemory[0] = $this->formatMemory(
134 2
            $this->timelineMemory[$time - self::INTERVAL_MONITORING[0]] ?? $now,
135 2
            self::INTERVAL_MONITORING[0]
136
        );
137 2
        $this->intervalMemory[1] = $this->formatMemory(
138 2
            $this->timelineMemory[$time - self::INTERVAL_MONITORING[1]] ?? -1,
139 2
            self::INTERVAL_MONITORING[1]
140
        );
141 2
        $this->intervalMemory[2] = $this->formatMemory(
142 2
            $this->timelineMemory[$time - self::INTERVAL_MONITORING[2]] ?? -1,
143 2
            self::INTERVAL_MONITORING[2]
144
        );
145 2
    }
146
147 2
    private function updateItemPerSecond()
148
    {
149 2
        $time = time();
150
151 2
        if (count($this->timelineItemPerSecond) > self::INTERVAL_MONITORING[2]) {
152 1
            $this->timelineItemPerSecond = array_slice($this->timelineItemPerSecond, 1, null, true);
153
        }
154
155 2
        $this->timelineItemPerSecond[$time] = $now = ($this->progressBar->getProgress() - $this->previousItemCount) / $this->timeElapsed;
156 2
        $this->previousItemCount = $this->progressBar->getProgress();
157
158 2
        $this->intervalItemPerSecond[0] = $this->formatItemsCount(
159 2
            $this->timelineItemPerSecond[$time - self::INTERVAL_MONITORING[0]] ?? $now,
160 2
            self::INTERVAL_MONITORING[0]
161
        );
162 2
        $this->intervalItemPerSecond[1] = $this->formatItemsCount(
163 2
            $this->timelineItemPerSecond[$time - self::INTERVAL_MONITORING[1]] ?? -1,
164 2
            self::INTERVAL_MONITORING[1]
165
        );
166 2
        $this->intervalItemPerSecond[2] = $this->formatItemsCount(
167 2
            $this->timelineItemPerSecond[$time - self::INTERVAL_MONITORING[2]] ?? -1,
168 2
            self::INTERVAL_MONITORING[2]
169
        );
170 2
    }
171
172 2
    private function formatMemory($memory, $time)
173
    {
174 2
        $colors = (($memory / 1000) > $this->maxMemory * 1000) ? '41;37' : '44;37';
175 2
        $message = $time.'s = '.Helper::formatMemory($memory);
176
177 2
        return "\033[".$colors.'m '.$message." \033[0m";
178
    }
179
180 2
    private function formatItemsCount($itemsCount, $time)
181
    {
182 2
        $colors = ($itemsCount < $this->minItemPerSecond) ? '41;37' : '44;37';
183 2
        $message = $time.'s = '.$itemsCount;
184
185 2
        return "\033[".$colors.'m '.$message."/s \033[0m";
186
    }
187
}
188