Failed Conditions
Pull Request — 0.3 (#20)
by jean
11:24
created

StatCliDumper::showBar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 10
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
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
class StatCliDumper
15
{
16
    public function dump(array $stats, array $total, $zoom = false, SymfonyStyle $outputHelper)
17
    {
18
        $headers = ['class', '01 BEST', '02 BEST', '03 BEST', '01 BAD', '02 BAD', '03 BAD', 'TOTAL RUN', 'TOTAL WAIT'];
19
        $data = [];
20
        $globals = array_combine(array_keys($stats), array_column($stats, 'global'));
21
        $maxGlobal = max($globals);
22
23
        foreach ($stats as $class => $values) {
24
            $path = explode('\\', $class);
25
            $class = array_pop($path);
26
            $times = array_replace($values['best_times'], $values['bad_times']);
27
            foreach ($times as $i => &$time) {
28
                $time = (int) $time;
29
                $time = ($time > 100) ? '<error>'.$time.' ms</error>' : $time.' ms';
30
                if ($zoom) {
31
                    $time .= PHP_EOL.($i + 1).PHP_EOL;
32
                }
33
            }
34
            $global = $values['global'];
35
            if ($global === $maxGlobal) {
36
                $global = sprintf('<error>%d ms</error>', (int) $values['global']);
37
            } else {
38
                $global = ((int) $values['global']).' ms';
39
            }
40
            $times[] = $global;
41
            $times[] = ((int) $values['global_wait']).' ms';
42
            $data[] = array_merge([$values['position'].'. ('.$values['tendance'].') '.$class.' ('.number_format($values['count_iteration']).'x)'], $times);
43
        }
44
45
        $outputHelper->table($headers, $data);
46
47
        $waits = array_combine(array_keys($stats), array_column($stats, 'global_wait'));
48
        $maxWait = max($waits);
49
50
        foreach ($waits as $i => $ms)
51
        {
52
            $waits[$i] = (int) $waits[$i];
53
        }
54
55
        $reference = $maxWait / 100;
56
57
        foreach ($waits as $class => $ms) {
58
            if (0 !== $ms) {
59
                $countPercents = (int) ($ms / $reference);
60
61
                if ($countPercents < 1) {
62
                    $countPercents = 1;
63
                }
64
                echo $this->showBar($countPercents);
65
                echo $this->showBar($countPercents);
66
                echo $this->showBar($countPercents);
67
                echo $this->showBar($countPercents);
68
                echo $this->showBar($countPercents);
69
                echo $this->showBar($countPercents);
70
                echo $this->showBar($countPercents);
71
                echo $this->showBar($countPercents);
72
                echo $this->showBar($countPercents);
73
                echo $this->showBarClass($class, $countPercents);
74
                echo $this->showBar($countPercents);
75
                echo $this->showBar($countPercents);
76
                echo $this->showBar($countPercents);
77
                echo $this->showBar($countPercents);
78
                echo $this->showBar($countPercents);
79
                echo $this->showBar($countPercents);
80
                echo $this->showBar($countPercents);
81
                echo $this->showBar($countPercents);
82
                echo $this->showBar($countPercents);
83
                echo $this->showBar($countPercents);
84
            }
85
        }
86
    }
87
88
    public function showBar($countPercents)
89
    {
90
        $bar = str_repeat(' ',10);
91
        $bar .= str_repeat(' ', (100 - $countPercents)/2);
92
        $bar .= "\e[32m\e[42m".str_repeat('#', $countPercents)."\e[0m";
93
        $bar .= PHP_EOL;
94
95
        return $bar;
96
    }
97
98
    public function showBarClass($class, $countPercents)
99
    {
100
        $size = strlen($class);
101
        $bar = str_repeat(' ',10);
102
        $bar .= str_repeat(' ', (100 - $countPercents)/2);
103
        $bar .= "\e[42m".$class."\e[0m";
104
        if ($countPercents > $size) {
105
            $bar .= "\e[32m\e[42m".str_repeat('#', $countPercents - $size)."\e[0m";
106
        }
107
        $bar .= PHP_EOL;
108
109
        return $bar;
110
    }
111
}