Failed Conditions
Pull Request — 0.3 (#20)
by jean
12:27
created

onStartIterableProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 3
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
40
    public function onStartIterableProcess(ProcessState $state, StepInterface $step)
41
    {
42
        return;
43
    }
44
45
    public function onUpdateIterableProcess(ProcessState $state, StepInterface $step)
46
    {
47
    }
48
49
    public function onEndProcess(ProcessState $state, StepInterface $step)
50
    {
51
    }
52
53
    public function onExecutedProcess(ProcessState $state, StepInterface $step)
54
    {
55
        if (!$this->enabled) {
56
            return;
57
        }
58
59
        $currentTime = (microtime(true) * 1000);
60
        $time = $currentTime - $this->stats[get_class($step)]['last_start'];
61
        $this->stats[get_class($step)]['time'][] = $time;
62
        $this->stats[get_class($step)]['last_finish'] = $currentTime;
63
    }
64
65
    private function calcul()
66
    {
67
        foreach ($this->stats as $class => $stat) {
68
            $stat['global'] = array_sum($stat['time']);
69
            $stat['global_wait'] = array_sum($stat['wait']);
70
            asort($stat['time'], SORT_NUMERIC);
71
            $stat['best_times'] = array_slice($stat['time'], 0, 3);
72
            arsort($stat['time'], SORT_NUMERIC);
73
            $stat['bad_times'] = array_slice($stat['time'], 0, 3);
74
            $stat['potential_rate'] = (isset($stat['best_times'][0])) ? (int) (1000 / $stat['best_times'][0]) : '??';
75
            unset($stat['time']);
76
            unset($stat['wait']);
77
            $this->stats[$class] = $stat;
78
        }
79
80
        $this->resolvedStat = [
81
            'stats' => $this->stats,
82
            'total' => [
83
                'global' => array_sum(array_column($this->stats, 'global')),
84
                'best_times' => [0, 0, 0],
85
                'bad_times' => [0, 0, 0],
86
                'global_wait' => array_sum(array_column($this->stats, 'global_wait')),
87
                'position' => ' ',
88
                'potential_rate' => '??',
89
            ]
90
        ];
91
    }
92
93
94
    public function getStats()
95
    {
96
        if (!$this->calculed) {
97
            $this->calcul();
98
            $this->calculed = true;
99
        }
100
        return $this->resolvedStat;
101
    }
102
103
    public function setEnabled($enabled)
104
    {
105
        $this->enabled = $enabled;
106
    }
107
}