SwooleStatsCollector::collect()   B
last analyzed

Complexity

Conditions 7
Paths 48

Size

Total Lines 62
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 37
c 1
b 0
f 1
nc 48
nop 1
dl 0
loc 62
rs 8.3946

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Components\Prometheus\Collectors;
4
5
use Hhxsv5\LaravelS\Components\MetricCollector;
6
7
class SwooleStatsCollector extends MetricCollector
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SwooleStatsCollector
Loading history...
8
{
9
    public function collect(array $params = [])
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function collect()
Loading history...
10
    {
11
        /**@var \Swoole\Http\Server $swoole */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
12
        $swoole = app('swoole');
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

12
        $swoole = /** @scrutinizer ignore-call */ app('swoole');
Loading history...
13
        $stats = $swoole->stats();
14
        // Get worker_num/task_worker_num from setting for the old Swoole.
15
        $setting = $swoole->setting;
16
        if (!isset($stats['worker_num'])) {
17
            $stats['worker_num'] = $setting['worker_num'];
18
        }
19
        if (!isset($stats['task_worker_num'])) {
20
            $stats['task_worker_num'] = isset($setting['task_worker_num']) ? $setting['task_worker_num'] : 0;
21
        }
22
        $metrics = [
23
            [
24
                'name'  => 'swoole_cpu_num',
25
                'type'  => 'gauge',
26
                'value' => swoole_cpu_num(),
27
            ],
28
            [
29
                'name'  => 'swoole_start_time',
30
                'type'  => 'gauge',
31
                'value' => $stats['start_time'],
32
            ],
33
            [
34
                'name'  => 'swoole_connection_num',
35
                'type'  => 'gauge',
36
                'value' => $stats['connection_num'],
37
            ],
38
            [
39
                'name'  => 'swoole_request_count',
40
                'type'  => 'gauge',
41
                'value' => $stats['request_count'],
42
            ],
43
            [
44
                'name'  => 'swoole_worker_num',
45
                'type'  => 'gauge',
46
                'value' => $stats['worker_num'],
47
            ],
48
            [
49
                'name'  => 'swoole_idle_worker_num',
50
                'type'  => 'gauge',
51
                'value' => isset($stats['idle_worker_num']) ? $stats['idle_worker_num'] : 0,
52
            ],
53
            [
54
                'name'  => 'swoole_task_worker_num',
55
                'type'  => 'gauge',
56
                'value' => $stats['task_worker_num'],
57
            ],
58
            [
59
                'name'  => 'swoole_task_idle_worker_num',
60
                'type'  => 'gauge',
61
                'value' => isset($stats['task_idle_worker_num']) ? $stats['task_idle_worker_num'] : 0,
62
            ],
63
            [
64
                'name'  => 'swoole_tasking_num',
65
                'type'  => 'gauge',
66
                'value' => isset($stats['tasking_num']) ? $stats['tasking_num'] : 0,
67
            ],
68
        ];
69
        $key = implode($this->config['apcu_key_separator'], [$this->config['apcu_key_prefix'], 'swoole_stats', '', '']);
70
        apcu_store($key, $metrics, $this->config['apcu_key_max_age']);
71
    }
72
}