Trunking::forget()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 6
nc 6
nop 1
dl 0
loc 8
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * Metrics trunking
4
 * User: moyo
5
 * Date: 28/12/2017
6
 * Time: 5:09 PM
7
 */
8
9
namespace Carno\Monitor\Chips;
10
11
use Carno\Monitor\Chips\Trunks\Histogram;
12
use Carno\Monitor\Chips\Trunks\Summary;
13
use Carno\Monitor\Chips\Trunks\Values;
14
use Carno\Monitor\Contracts\Labeled;
15
use Carno\Monitor\Contracts\Metrical;
16
use Closure;
17
18
trait Trunking
19
{
20
    use Histogram, Summary, Values;
21
22
    /**
23
     * @var array
24
     */
25
    private $metrics = [];
26
27
    /**
28
     * @var array
29
     */
30
    private $lived = [];
31
32
    /**
33
     * @return array
34
     */
35
    public function lived() : array
36
    {
37
        return $this->lived;
38
    }
39
40
41
    /**
42
     * @param string $identify
43
     * @param string $typed
44
     * @param string $named
45
     * @param string $grouped
46
     * @param string $description
47
     * @param array $labels
48
     * @param array $data
49
     */
50
    public function metrics(
51
        string $identify,
52
        string $typed,
53
        string $named,
54
        string $grouped,
55
        string $description,
56
        array $labels,
57
        array $data
58
    ) : void {
59
        $this->lived[$identify] = time();
60
        $this->metrics[$typed][$named][$grouped][$identify] = [$data, $labels, $description];
61
    }
62
63
    /**
64
     * @param string $identify
65
     * @param string $typed
66
     * @param string $named
67
     * @param string $grouped
68
     */
69
    public function remove(string $identify, string $typed, string $named, string $grouped) : void
70
    {
71
        unset($this->metrics[$typed][$named][$grouped][$identify]);
72
    }
73
74
    /**
75
     * @param string $identify
76
     */
77
    public function forget(string $identify) : void
78
    {
79
        unset($this->lived[$identify]);
80
        foreach ($this->metrics as $typed => $metrics) {
81
            foreach ($metrics as $named => $groups) {
82
                foreach ($groups as $grouped => $stack) {
83
                    foreach ($stack as $pid => $last) {
84
                        $pid === $identify && $this->remove($identify, $typed, $named, $grouped);
85
                    }
86
                }
87
            }
88
        }
89
    }
90
91
    /**
92
     * @param Closure $receiver
93
     */
94
    public function spouting(Closure $receiver) : void
95
    {
96
        foreach ($this->metrics as $typed => $metrics) {
97
            foreach ($metrics as $named => $groups) {
98
                foreach ($groups as $grouped => $stack) {
99
                    $stack && $receiver($typed, $named, $grouped, ...$this->expanding($typed, $stack));
100
                }
101
            }
102
        }
103
    }
104
105
    /**
106
     * @param string $typed
107
     * @param array $stack
108
     * @return array
109
     */
110
    private function expanding(string $typed, array $stack) : array
111
    {
112
        $data = [];
113
114
        foreach ($stack as $pid => $last) {
115
            // data,label,desc in last
116
            list($part, $labels, $description) = $last;
117
118
            // checking data is global
119
            if (isset($labels[Labeled::GLOBAL])) {
120
                unset($labels[Labeled::GLOBAL]);
121
                $data = $part;
122
                break;
123
            }
124
125
            // type switch
126
            switch ($typed) {
127
                case Metrical::COUNTER:
128
                case Metrical::GAUGE:
129
                    $this->trkValues($part, $data);
130
                    break;
131
                case Metrical::HISTOGRAM:
132
                    $this->trkHistogram($part, $data);
133
                    break;
134
                case Metrical::SUMMARY:
135
                    $this->trkSummary($part, $data);
136
                    break;
137
            }
138
        }
139
140
        return [$description ?? '', $labels ?? [], $data];
141
    }
142
}
143