Failed Conditions
Pull Request — 0.3 (#20)
by jean
06:00
created

StatsCollectorProcessNotifier::setEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ProcessBundle\ProcessNotifier;
6
7
use Darkilliant\ProcessBundle\State\ProcessState;
8
use Darkilliant\ProcessBundle\Step\StepInterface;
9
10
class StatsCollectorProcessNotifier extends ProgressBarProcessNotifier
11
{
12
    private $stats = [];
13
    private $resolvedStat = [];
14
    private $calculed = false;
15
    private $position = 0;
16
    private $enabled = false;
17
18
    public function onStartProcess(ProcessState $state, StepInterface $step)
19
    {
20
        if (!$this->enabled) {
21
            return;
22
        }
23
24
        if (!isset($this->stats[get_class($step)])) {
25
            $this->stats[get_class($step)] = [];
26
        }
27
28
        $this->stats[get_class($step)]['last_start'] = microtime(true) * 1000;
29
        if (!isset($this->stats[get_class($step)]['time'])) {
30
            $this->stats[get_class($step)]['time'] = [];
31
            $this->stats[get_class($step)]['real_time'] = [];
32
            $this->stats[get_class($step)]['position'] = ++$this->position;
33
            $this->stats[get_class($step)]['wait'] = [];
34
        } else {
35
            $this->stats[get_class($step)]['wait'][] = $this->stats[get_class($step)]['last_start'] - $this->stats[get_class($step)]['last_finish'];
36
        }
37
    }
38
39
    public function onStartIterableProcess(ProcessState $state, StepInterface $step)
40
    {
41
        return;
42
    }
43
44
    public function onUpdateIterableProcess(ProcessState $state, StepInterface $step)
45
    {
46
    }
47
48
    public function onEndProcess(ProcessState $state, StepInterface $step)
49
    {
50
    }
51
52
    public function onExecutedProcess(ProcessState $state, StepInterface $step)
53
    {
54
        if (!$this->enabled) {
55
            return;
56
        }
57
58
        $currentTime = (microtime(true) * 1000);
59
        $time = $currentTime - $this->stats[get_class($step)]['last_start'];
60
        $this->stats[get_class($step)]['time'][] = $time;
61
        $this->stats[get_class($step)]['last_finish'] = $currentTime;
62
    }
63
64
    public function getStats()
65
    {
66
        if (!$this->calculed) {
67
            $this->calcul();
68
            $this->calculed = true;
69
        }
70
71
        return $this->resolvedStat;
72
    }
73
74
    public function setEnabled($enabled)
75
    {
76
        $this->enabled = $enabled;
77
    }
78
79
    private function calcul()
80
    {
81
        foreach ($this->stats as $class => $stat) {
82
            $stat['global'] = array_sum($stat['time']);
83
84
            $moved = false;
85
            $last = null;
86
            foreach ($stat['time'] as $time) {
87
                $time = (int) $time;
88
                if (null !== $last && $last != $time) {
89
                    $moved = true;
90
                    break;
91
                }
92
                $last = $time;
93
            }
94
95
            $stat['tendance'] = (!$moved) ? '~~~' : '/\/';
96
            $stat['global_wait'] = array_sum($stat['wait']);
97
            asort($stat['time'], SORT_NUMERIC);
98
            $stat['best_times'] = array_slice($stat['time'], 0, 3, true);
99
            arsort($stat['time'], SORT_NUMERIC);
100
            $stat['bad_times'] = array_slice($stat['time'], 0, 3, true);
101
            $stat['potential_rate'] = (isset($stat['best_times'][0])) ? (int) (1000 / $stat['best_times'][0]) : '??';
102
            $stat['count_iteration'] = count($stat['time']);
103
            unset($stat['time']);
104
            unset($stat['wait']);
105
            $this->stats[$class] = $stat;
106
        }
107
108
        $this->resolvedStat = [
109
            'stats' => $this->stats,
110
            'total' => [
111
                'global' => array_sum(array_column($this->stats, 'global')),
112
                'best_times' => [0, 0, 0],
113
                'bad_times' => [0, 0, 0],
114
                'global_wait' => array_sum(array_column($this->stats, 'global_wait')),
115
                'position' => ' ',
116
                'potential_rate' => '??',
117
            ],
118
        ];
119
    }
120
}
121