Failed Conditions
Pull Request — 0.3 (#20)
by jean
03:16
created

StatCliDumper::generatePipe()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 28
ccs 13
cts 13
cp 1
crap 3
rs 9.472
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: darkilliant
5
 * Date: 6/24/18
6
 * Time: 11:14 AM.
7
 */
8
9
namespace Darkilliant\ProcessBundle\StatDumper;
10
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
13
class StatCliDumper
14
{
15
    const TABLE_HEADERS = ['class', '01 BEST', '02 BEST', '03 BEST', '01 BAD', '02 BAD', '03 BAD', 'TOTAL RUN', 'TOTAL WAIT'];
16
17 1
    public function dump(array $stats, array $total, $zoom = false, SymfonyStyle $outputHelper)
18
    {
19 1
        $data = [];
20 1
        $globals = array_combine(array_keys($stats), array_column($stats, 'global'));
21
22 1
        $maxGlobal = max($globals);
23
24 1
        foreach ($stats as $class => $values) {
25 1
            $class = $this->getShortClassName($class);
26
27 1
            $times = array_merge(
28 1
                array_pad($values['best_times'], 3, -1),
29 1
                array_pad($values['bad_times'], 3, -1)
30
            );
31 1
            foreach ($times as $i => &$time) {
32 1
                $time = (int) $time;
33 1
                if (-1 === $time) {
34 1
                    $time = '~ ms';
35
                } else {
36 1
                    $time = ($time > 100) ? '<error>'.$time.' ms</error>' : $time.' ms';
37 1
                    if ($zoom) {
38 1
                        $time .= PHP_EOL.($i + 1).PHP_EOL;
39
                    }
40
                }
41
            }
42 1
            $global = $values['global'];
43 1
            if ($global === $maxGlobal) {
44 1
                $global = sprintf('<error>%d ms</error>', (int) $values['global']);
45
            } else {
46 1
                $global = ((int) $values['global']).' ms';
47
            }
48 1
            $times[] = $global;
49 1
            $times[] = ((int) $values['global_wait']).' ms';
50 1
            $data[] = array_merge([$values['position'].'. ('.$values['tendance'].') '.$class.' ('.number_format($values['count_iteration']).'x)'], $times);
51
        }
52
53 1
        $outputHelper->table(self::TABLE_HEADERS, $data);
54
55 1
        $outputHelper->write($this->generatePipe($stats));
56 1
    }
57
58 1
    private function getShortClassName(string $fqcn): string
59
    {
60 1
        $path = explode('\\', $fqcn);
61
62 1
        return array_pop($path);
63
    }
64
65 1
    private function generatePipe($stats)
66
    {
67 1
        $waits = array_combine(array_keys($stats), array_column($stats, 'global_wait'));
68 1
        $maxWait = max($waits);
69
70
        // Convert all wait times to float
71
        $waits = array_map(function ($item) { return (int) $item; }, $waits);
72
        // Skip tasks not wait
73
        $waits = array_filter($waits, function ($item) { return 0 != $item; });
74
75 1
        $reference = $maxWait / 100;
76
77 1
        $pipe = [];
78
79 1
        foreach ($waits as $class => $ms) {
80 1
            $countPercents = (int) ($ms / $reference);
81
82 1
            if ($countPercents < 1) {
83 1
                $countPercents = 1;
84
            }
85
86
            // Create one part of pipe for represente speed of task
87 1
            $pipe = array_merge($pipe, array_pad([], 9, $this->generateBar($countPercents)));
88 1
            $pipe = array_merge($pipe, [$this->generateBarClass($class, $countPercents)]);
89 1
            $pipe = array_merge($pipe, array_pad([], 9, $this->generateBar($countPercents)));
90
        }
91
92 1
        return implode(PHP_EOL, $pipe);
93
    }
94
95 1
    private function generateBar(int $countPercents): string
96
    {
97 1
        $bar = str_repeat(' ', 10);
98 1
        $bar .= str_repeat(' ', (100 - $countPercents) / 2);
99 1
        $bar .= "\e[32m\e[42m".str_repeat('#', $countPercents)."\e[0m";
100
101 1
        return $bar;
102
    }
103
104 1
    private function generateBarClass(string $class, int $countPercents): string
105
    {
106 1
        $size = strlen($class);
107 1
        $bar = str_repeat(' ', 10);
108 1
        $bar .= str_repeat(' ', (100 - $countPercents) / 2);
109 1
        $bar .= "\e[42m".$class."\e[0m";
110 1
        if ($countPercents > $size) {
111 1
            $bar .= "\e[32m\e[42m".str_repeat('#', $countPercents - $size)."\e[0m";
112
        }
113
114 1
        return $bar;
115
    }
116
}
117