1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jalle19\StatusManager\Instance; |
4
|
|
|
|
5
|
|
|
use Jalle19\tvheadend\model\InputStatus; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class InputErrorCumulative |
9
|
|
|
* @package Jalle19\StatusManager\Instance |
10
|
|
|
* @copyright Copyright © Sam Stenvall 2016- |
11
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0 |
12
|
|
|
*/ |
13
|
|
|
class InputErrorCumulative |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
const VALUE_TYPE_BER = 'ber'; |
17
|
|
|
const VALUE_TYPE_UNC = 'unc'; |
18
|
|
|
const VALUE_TYPE_TE = 'te'; |
19
|
|
|
const VALUE_TYPE_CC = 'cc'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \DateTime |
23
|
|
|
*/ |
24
|
|
|
private $_created; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $_counters; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initializes all value counters |
34
|
|
|
*/ |
35
|
|
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
$this->_created = new \DateTime(); |
38
|
|
|
|
39
|
|
|
foreach ([self::VALUE_TYPE_BER, self::VALUE_TYPE_UNC, self::VALUE_TYPE_TE, self::VALUE_TYPE_CC] as $type) |
40
|
|
|
$this->_counters[$type] = []; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return \DateTime |
46
|
|
|
*/ |
47
|
|
|
public function getCreated() |
48
|
|
|
{ |
49
|
|
|
return $this->_created; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param InputStatus $inputStatus |
55
|
|
|
*/ |
56
|
|
|
public function accumulate(InputStatus $inputStatus) |
57
|
|
|
{ |
58
|
|
|
$this->addValue(InputErrorCumulative::VALUE_TYPE_BER, $inputStatus->ber); |
59
|
|
|
$this->addValue(InputErrorCumulative::VALUE_TYPE_UNC, $inputStatus->unc); |
60
|
|
|
$this->addValue(InputErrorCumulative::VALUE_TYPE_TE, $inputStatus->te); |
61
|
|
|
$this->addValue(InputErrorCumulative::VALUE_TYPE_CC, $inputStatus->cc); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $type |
67
|
|
|
* |
68
|
|
|
* @return float |
69
|
|
|
*/ |
70
|
|
|
public function getAverageValue($type) |
71
|
|
|
{ |
72
|
|
|
$count = count($this->_counters[$type]); |
73
|
|
|
|
74
|
|
|
if ($count === 0) |
75
|
|
|
return 0; |
76
|
|
|
|
77
|
|
|
return array_sum($this->_counters[$type]) / $count; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $type |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function getLastValue($type) |
87
|
|
|
{ |
88
|
|
|
return end($this->_counters[$type]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $type |
94
|
|
|
* @param int $value |
95
|
|
|
*/ |
96
|
|
|
private function addValue($type, $value) |
97
|
|
|
{ |
98
|
|
|
$this->_counters[$type][] = $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|