Skip to content

Commit 53f4cd4

Browse files
committed
Use YieldAll for database queries and add constants for metric names
1 parent d732e3f commit 53f4cd4

File tree

2 files changed

+211
-272
lines changed

2 files changed

+211
-272
lines changed

application/controllers/ChartsController.php

+66-66
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,18 @@ public function clusterAction(): void
4646
);
4747

4848
$metrics = new Metrics(Database::connection());
49-
$clusterMetrics = [];
5049

51-
$clusterMetrics = $metrics->getClusterUsage(
50+
$clusterMetrics = $metrics->getClusterMetrics(
5251
(new DateTime())->sub(new DateInterval('PT12H')),
53-
Metrics::$ClusterCpuUsage,
54-
Metrics::$ClusterMemoryUsage
52+
Metrics::CLUSTER_CPU_USAGE,
53+
Metrics::CLUSTER_MEMORY_USAGE
5554
);
5655

5756
$this->addContent(
5857
new LineChart(
5958
'chart-medium',
60-
implode(', ', $clusterMetrics[Metrics::$ClusterCpuUsage]),
61-
implode(', ', array_keys($clusterMetrics[Metrics::$ClusterCpuUsage])),
59+
implode(', ', $clusterMetrics[Metrics::CLUSTER_CPU_USAGE]),
60+
implode(', ', array_keys($clusterMetrics[Metrics::CLUSTER_CPU_USAGE])),
6261
'CPU Usage',
6362
'#00a8ff'
6463
)
@@ -67,23 +66,18 @@ public function clusterAction(): void
6766
$this->addContent(
6867
new LineChart(
6968
'chart-medium',
70-
implode(', ', $clusterMetrics[Metrics::$ClusterMemoryUsage]),
71-
implode(', ', array_keys($clusterMetrics[Metrics::$ClusterMemoryUsage])),
69+
implode(', ', $clusterMetrics[Metrics::CLUSTER_MEMORY_USAGE]),
70+
implode(', ', array_keys($clusterMetrics[Metrics::CLUSTER_MEMORY_USAGE])),
7271
'Memory Usage',
7372
'#8c7ae6'
7473
)
7574
);
7675

77-
// $numberOfRunningPods = $metrics->getNumberOfPodsByState('running');
78-
// $numberOfPendingPods = $metrics->getNumberOfPodsByState('pending');
79-
// $numberOfFailedPods = $metrics->getNumberOfPodsByState('failed');
80-
// $numberOfSucceededPods = $metrics->getNumberOfPodsByState('succeeded');
81-
8276
$pods = $metrics->getNumberOfPodsByState(
83-
Metrics::$PodStateRunning,
84-
Metrics::$PodStatePending,
85-
Metrics::$PodStateFailed,
86-
Metrics::$PodStateSucceeded
77+
Metrics::POD_STATE_RUNNING,
78+
Metrics::POD_STATE_PENDING,
79+
Metrics::POD_STATE_FAILED,
80+
Metrics::POD_STATE_SUCCEEDED
8781
);
8882

8983
$this->addContent(
@@ -92,35 +86,35 @@ public function clusterAction(): void
9286
implode(
9387
', ',
9488
[
95-
$pods[Metrics::$PodStateRunning],
96-
$pods[Metrics::$PodStatePending],
97-
$pods[Metrics::$PodStateFailed],
98-
$pods[Metrics::$PodStateSucceeded]
89+
$pods[Metrics::POD_STATE_RUNNING],
90+
$pods[Metrics::POD_STATE_PENDING],
91+
$pods[Metrics::POD_STATE_FAILED],
92+
$pods[Metrics::POD_STATE_SUCCEEDED]
9993
]
10094
),
10195
implode(
10296
', ',
10397
[
104-
$pods[Metrics::$PodStateRunning] . ' Running',
105-
$pods[Metrics::$PodStatePending] . ' Pending',
106-
$pods[Metrics::$PodStateFailed] . ' Failed',
107-
$pods[Metrics::$PodStateSucceeded] . ' Succeeded'
98+
$pods[Metrics::POD_STATE_RUNNING] . ' Running',
99+
$pods[Metrics::POD_STATE_PENDING] . ' Pending',
100+
$pods[Metrics::POD_STATE_FAILED] . ' Failed',
101+
$pods[Metrics::POD_STATE_SUCCEEDED] . ' Succeeded'
108102
]
109103
),
110104
'#007bff, #ffc107, #dc3545, #28a745'
111105
)
112106
);
113107

114-
$current = $metrics->getClusterUsage(
115-
(new DateTime())->sub(new DateInterval('PT1M')),
116-
Metrics::$ClusterCpuUsage,
117-
Metrics::$ClusterMemoryUsage
108+
$current = $metrics->getClusterMetrics(
109+
(new DateTime())->sub(new DateInterval('PT2M')),
110+
Metrics::CLUSTER_CPU_USAGE,
111+
Metrics::CLUSTER_MEMORY_USAGE
118112
);
119113

120114
$this->addContent(
121115
new DoughnutChartStates(
122116
'chart-small',
123-
$current[Metrics::$ClusterCpuUsage][array_key_last($current[Metrics::$ClusterCpuUsage])],
117+
$current[Metrics::CLUSTER_CPU_USAGE][array_key_last($current[Metrics::CLUSTER_CPU_USAGE])],
124118
'CPU Usage',
125119
'#28a745, #ffc107, #dc3545'
126120
)
@@ -129,7 +123,7 @@ public function clusterAction(): void
129123
$this->addContent(
130124
new DoughnutChartStates(
131125
'chart-small',
132-
$current[Metrics::$ClusterMemoryUsage][array_key_last($current[Metrics::$ClusterMemoryUsage])],
126+
$current[Metrics::CLUSTER_MEMORY_USAGE][array_key_last($current[Metrics::CLUSTER_MEMORY_USAGE])],
133127
'Memory Usage',
134128
'#28a745, #ffc107, #dc3545'
135129
)
@@ -149,19 +143,22 @@ public function nodeAction(): void
149143
);
150144

151145
$metrics = new Metrics(Database::connection());
152-
$nodeMetrics = [];
153146

154-
$metrics->getNodeNetworkBytes($nodeMetrics, 'received', $this->LAST_1_HOUR);
155-
$metrics->getNodeNetworkBytes($nodeMetrics, 'transmitted', $this->LAST_1_HOUR);
147+
$nodeNetworkMetrics = $metrics->getNodeNetworkBytes(
148+
(new DateTime())->sub(new DateInterval('PT1H')),
149+
Metrics::NODE_NETWORK_RECEIVED_BYTES,
150+
Metrics::NODE_NETWORK_TRANSMITTED_BYTES
151+
);
152+
156153

157-
foreach ($nodeMetrics as $node) {
154+
foreach ($nodeNetworkMetrics as $node) {
158155
$this->addContent(
159156
new LineChart(
160157
'chart-medium',
161-
implode(', ', $node['receivedBytes'])
158+
implode(', ', $node[Metrics::NODE_NETWORK_RECEIVED_BYTES])
162159
. '; '
163-
. implode(', ', $node['transmittedBytes']),
164-
implode(', ', array_keys($node['receivedBytes'])),
160+
. implode(', ', $node[Metrics::NODE_NETWORK_TRANSMITTED_BYTES]),
161+
implode(', ', array_keys($node[Metrics::NODE_NETWORK_RECEIVED_BYTES])),
165162
'Received Bytes; Transmitted Bytes',
166163
'#593684; #a3367f'
167164
)
@@ -182,23 +179,23 @@ public function podAction(): void
182179
);
183180

184181
$metrics = new Metrics(Database::connection());
185-
$podMetrics = [];
186-
187-
$metrics->getPodRequest($podMetrics, 'cpu');
188-
$metrics->getPodRequest($podMetrics, 'memory');
189182

190-
$metrics->getPodLimit($podMetrics, 'cpu');
191-
$metrics->getPodLimit($podMetrics, 'memory');
192-
193-
$metrics->getPodCpuCoreUsage($podMetrics);
194-
$metrics->getPodMemoryByteUsage($podMetrics);
183+
$podMetricsCurrent = $metrics->getPodMetricsCurrent(
184+
Metrics::POD_CPU_REQUEST,
185+
Metrics::POD_CPU_LIMIT,
186+
Metrics::POD_CPU_USAGE_CORES,
187+
Metrics::POD_MEMORY_REQUEST,
188+
Metrics::POD_MEMORY_LIMIT,
189+
Metrics::POD_MEMORY_USAGE_BYTES
190+
);
195191

196-
$metrics->getPodUsage($podMetrics, 'cpu', $this->LAST_1_HOUR);
197-
$metrics->getPodUsage($podMetrics, 'memory', $this->LAST_1_HOUR);
192+
$podMetricsPeriod = $metrics->getPodMetrics(
193+
(new DateTime())->sub(new DateInterval('PT1H')),
194+
Metrics::POD_CPU_USAGE,
195+
Metrics::POD_MEMORY_USAGE
196+
);
198197

199-
echo '<pre>';
200-
// print_r($podMetrics);
201-
// exit;
198+
$podMetrics = Metrics::mergeMetrics($podMetricsCurrent, $podMetricsPeriod);
202199

203200
$table = new HtmlElement('table', new Attributes(['class' => 'condition-table']));
204201
$table->addHtml(
@@ -224,40 +221,43 @@ public function podAction(): void
224221
$tr->addHtml(new HtmlElement('td', null, new Text($pod['name'])));
225222
$td = new HtmlElement('td', null);
226223

227-
if (isset($pod['cpuLimit']) && $pod['cpuRequest'] < $pod['cpuLimit']) {
224+
if (isset($pod[Metrics::POD_CPU_LIMIT]) && $pod[Metrics::POD_CPU_REQUEST] < $pod[Metrics::POD_CPU_LIMIT]) {
228225
$td->addHtml(
229226
new DoughnutChartRequestLimit(
230227
'chart-mini',
231-
$pod['cpuRequest'],
232-
$pod['cpuLimit'],
233-
$pod['cpuUsageCores']
228+
$pod[Metrics::POD_CPU_REQUEST],
229+
$pod[Metrics::POD_CPU_LIMIT],
230+
$pod[Metrics::POD_CPU_USAGE_CORES]
234231
)
235232
);
236233
}
237234

238235
$tr->addHtml($td);
239236
$td = new HtmlElement('td', null);
240237

241-
if (isset($pod['memoryLimit']) && $pod['memoryRequest'] < $pod['memoryLimit']) {
238+
if (
239+
isset($pod[Metrics::POD_MEMORY_LIMIT])
240+
&& $pod[Metrics::POD_MEMORY_REQUEST] < $pod[Metrics::POD_MEMORY_LIMIT]
241+
) {
242242
$td->addHtml(
243243
new DoughnutChartRequestLimit(
244244
'chart-mini',
245-
$pod['memoryRequest'],
246-
$pod['memoryLimit'],
247-
$pod['memoryUsageBytes']
245+
$pod[Metrics::POD_MEMORY_REQUEST],
246+
$pod[Metrics::POD_MEMORY_LIMIT],
247+
$pod[Metrics::POD_MEMORY_USAGE_BYTES]
248248
)
249249
);
250250
}
251251

252252
$tr->addHtml($td);
253253
$td = new HtmlElement('td', null);
254254

255-
if (isset($pod['cpu'])) {
255+
if (isset($pod[Metrics::POD_CPU_USAGE])) {
256256
$td->addHtml(
257257
new LineChartMinified(
258258
'chart-mini',
259-
implode(', ', $pod['cpu']),
260-
implode(', ', array_keys($pod['cpu'])),
259+
implode(', ', $pod[Metrics::POD_CPU_USAGE]),
260+
implode(', ', array_keys($pod[Metrics::POD_CPU_USAGE])),
261261
'#00a8ff'
262262
)
263263
);
@@ -266,12 +266,12 @@ public function podAction(): void
266266
$tr->addHtml($td);
267267
$td = new HtmlElement('td', null);
268268

269-
if (isset($pod['memory'])) {
269+
if (isset($pod[Metrics::POD_MEMORY_USAGE])) {
270270
$td->addHtml(
271271
new LineChartMinified(
272272
'chart-mini',
273-
implode(', ', $pod['memory']),
274-
implode(', ', array_keys($pod['memory'])),
273+
implode(', ', $pod[Metrics::POD_MEMORY_USAGE]),
274+
implode(', ', array_keys($pod[Metrics::POD_MEMORY_USAGE])),
275275
'#ffa800'
276276
)
277277
);

0 commit comments

Comments
 (0)