Passed
Push — master ( 79b18a...ffcc8f )
by Nils
02:37
created

NetworkTrafficCollector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 52
rs 10
c 1
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A collect() 0 4 1
B calculateNetworkThroughput() 0 39 8
1
<?php
2
3
namespace Startwind\Inventorio\Collector\Network;
4
5
use Startwind\Inventorio\Collector\BasicCollector;
6
use Startwind\Inventorio\Exec\File;
7
use Startwind\Inventorio\Metrics\Memory\Memory;
8
9
class NetworkTrafficCollector extends BasicCollector
10
{
11
    protected string $identifier = "NetworkThroughput";
12
13
    private const MEMORY_KEY = 'net_traffic_history';
14
15
    public function collect(): array
16
    {
17
        return [
18
            'eth0' => $this->calculateNetworkThroughput('eth0')
19
        ];
20
    }
21
22
    function calculateNetworkThroughput($interface): float
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
    {
24
        $memory = Memory::getInstance();
25
26
        $file = new File();
27
28
        if (!$file->fileExists('/proc/net/dev')) return 0;
29
30
        $lines = $file->getContents('/proc/net/dev', true);
31
        foreach ($lines as $line) {
32
            if (strpos($line, $interface . ':') !== false) {
33
                $parts = preg_split('/\s+/', trim($line));
34
                $rx = (int)$parts[1];
35
                $tx = (int)$parts[9];
36
                $total = $rx + $tx;
37
                break;
38
            }
39
        }
40
41
        if (!isset($total)) return 0;
42
43
        $history = $memory->getData(self::MEMORY_KEY) ?? [];
44
        $lastTotal = end($history);
0 ignored issues
show
Bug introduced by
It seems like $history can also be of type double; however, parameter $array of end() does only seem to accept array|object, maybe add an additional type check? ( Ignorable by Annotation )

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

44
        $lastTotal = end(/** @scrutinizer ignore-type */ $history);
Loading history...
45
46
        if (!empty($history) && $total < $lastTotal) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $total does not seem to be defined for all execution paths leading up to this point.
Loading history...
47
            $memory->addData(self::MEMORY_KEY, $history);
48
            return 0;
49
        }
50
51
        if (!empty($history)) {
52
            $delta = $total - $lastTotal;
53
            $mb = round($delta / 1024 / 1024, 2);
54
        } else {
55
            $mb = 0;
56
        }
57
58
        $memory->addData(self::MEMORY_KEY, $total);
59
60
        return $mb;
61
    }
62
}