1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fieg\StatisticoBundle\Command; |
4
|
|
|
|
5
|
|
|
use Fieg\Statistico\Reader; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
class GraphCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Reader |
14
|
|
|
*/ |
15
|
|
|
private $reader; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Reader $reader |
19
|
|
|
*/ |
20
|
|
|
public function __construct(Reader $reader) |
21
|
|
|
{ |
22
|
|
|
$this->reader = $reader; |
23
|
|
|
parent::__construct(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function configure() |
27
|
|
|
{ |
28
|
|
|
$this |
29
|
|
|
->setName('statistico:graph') |
30
|
|
|
->addArgument('bucket') |
31
|
|
|
; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
35
|
|
|
{ |
36
|
|
|
while (true) { |
37
|
|
|
$from = new \DateTime('-1 minute'); |
38
|
|
|
$to = new \DateTime(); |
39
|
|
|
|
40
|
|
|
$buckets = $this->reader->getBuckets(); |
41
|
|
|
$bucketArgument = $input->getArgument('bucket'); |
42
|
|
|
|
43
|
|
|
$buckets = array_filter( |
44
|
|
|
$buckets, |
45
|
|
|
function ($bucket) use ($bucketArgument) { |
46
|
|
|
return preg_match('/'.preg_quote($bucketArgument, '/') . '/', $bucket); |
47
|
|
|
} |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$maxLen = 0; |
51
|
|
|
foreach ($buckets as $bucket) { |
52
|
|
|
if (strlen($bucket) > $maxLen) { |
53
|
|
|
$maxLen = strlen($bucket); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$out = []; |
58
|
|
|
|
59
|
|
|
foreach ($buckets as $bucket) { |
60
|
|
|
$counts = $this->reader->queryCounts($bucket, 'seconds', $from, $to); |
61
|
|
|
$rpm = array_sum($counts); |
62
|
|
|
|
63
|
|
|
$minX = $from->getTimestamp(); |
64
|
|
|
$maxX = $to->getTimestamp(); |
65
|
|
|
|
66
|
|
|
$data = []; |
67
|
|
|
|
68
|
|
|
for ($i = $minX; $i <= $maxX; $i++) { |
69
|
|
|
$data[$i] = (isset($counts[$i]) ? $counts[$i] : 0); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$out[] = sprintf("<info>%-".$maxLen."s</info> %s %3d rpm", $bucket, $this->drawGraph($data), $rpm); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->overwrite($output, implode($out, "\n")); |
76
|
|
|
|
77
|
|
|
usleep(500 * 1000); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function drawGraph($data) |
82
|
|
|
{ |
83
|
|
|
$chars = 60; |
84
|
|
|
|
85
|
|
|
if (count($data) > $chars) { |
86
|
|
|
$scaleX = floor(count($data) / $chars); |
87
|
|
|
|
88
|
|
|
$scaledData = []; |
89
|
|
|
|
90
|
|
|
for ($i = 0; $i < $chars; $i++) { |
91
|
|
|
$segment = array_slice($data, 1 + ($scaleX * $i) * -1, $scaleX); |
92
|
|
|
|
93
|
|
|
$total = 0; |
94
|
|
|
foreach ($segment as $value) { |
95
|
|
|
$total += $value; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$value = $total; |
99
|
|
|
|
100
|
|
|
$scaledData[] = round($value); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$data = array_reverse($scaledData); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$minY = min(array_values($data)); |
|
|
|
|
107
|
|
|
$maxY = max(array_values($data)); |
108
|
|
|
|
109
|
|
|
$graph = ''; |
110
|
|
|
|
111
|
|
|
foreach ($data as $time => $value) { |
112
|
|
|
if ($maxY > 0) { |
113
|
|
|
$percent = round($value * 100 / $maxY); |
114
|
|
|
} else { |
115
|
|
|
$percent = 0; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$char = '▁'; |
119
|
|
|
|
120
|
|
|
if ($percent > 75 && $percent <= 100) { |
121
|
|
|
$char = '▇'; |
122
|
|
|
} else if ($percent > 50 && $percent <= 75) { |
123
|
|
|
$char = '▅'; |
124
|
|
|
} else if ($percent > 25 && $percent <= 50) { |
125
|
|
|
$char = '▃'; |
126
|
|
|
} else if ($percent > 0 && $percent <= 25) { |
127
|
|
|
$char = '▂'; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$graph .= $char; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $graph; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected $lastMessageLength; |
137
|
|
|
protected $lastMessage; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Overwrites a previous message to the output. |
141
|
|
|
* |
142
|
|
|
* @param OutputInterface $output An Output instance |
143
|
|
|
* @param string $message The message |
144
|
|
|
*/ |
145
|
|
|
protected function overwrite(OutputInterface $output, $message) |
146
|
|
|
{ |
147
|
|
|
$length = $this->strlen($message); |
148
|
|
|
|
149
|
|
|
// append whitespace to match the last line's length |
150
|
|
|
if (null !== $this->lastMessageLength && $this->lastMessageLength > $length) { |
151
|
|
|
$message = str_pad($message, $this->lastMessageLength, "\x20", STR_PAD_RIGHT); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// clear previous lines |
155
|
|
|
if (null !== $this->lastMessage) { |
156
|
|
|
$count = mb_substr_count($this->lastMessage, "\n"); |
157
|
|
|
if ($count >= 1) { |
158
|
|
|
$output->write(chr(27) . '['.$count.'A'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// carriage return |
163
|
|
|
$output->write("\x0D"); |
164
|
|
|
$output->write($message); |
165
|
|
|
|
166
|
|
|
$this->lastMessageLength = $this->strlen($message); |
167
|
|
|
$this->lastMessage = $message; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Returns the length of a string, using mb_strlen if it is available. |
172
|
|
|
* |
173
|
|
|
* @param string $string The string to check its length |
174
|
|
|
* |
175
|
|
|
* @return integer The length of the string |
176
|
|
|
*/ |
177
|
|
|
protected function strlen($string) |
178
|
|
|
{ |
179
|
|
|
if (!function_exists('mb_strlen')) { |
180
|
|
|
return strlen($string); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if (false === $encoding = mb_detect_encoding($string)) { |
184
|
|
|
return strlen($string); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return mb_strlen($string, $encoding); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|