5
5
use ipl \Sql \Select ;
6
6
use ipl \Sql \Connection ;
7
7
use PDO ;
8
+ use DateTimeInterface ;
8
9
9
10
class Metrics
10
11
{
@@ -21,7 +22,11 @@ public function getClusterUsage(string $resource, int $period): array
21
22
(new Select ())
22
23
->columns (['timestamp ' , 'value ' ])
23
24
->from ('prometheus_cluster_metric ' )
24
- ->where ('`group` = ? AND timestamp > UNIX_TIMESTAMP() * 1000 - ? ' , "$ resource.usage " , $ period )
25
+ ->where (
26
+ '`group` = ? AND timestamp > UNIX_TIMESTAMP() * 1000 - ? ' ,
27
+ "$ resource.usage " ,
28
+ $ period
29
+ )
25
30
);
26
31
27
32
foreach ($ dbData ->fetchAll (PDO ::FETCH_ASSOC ) as $ row ) {
@@ -55,7 +60,11 @@ public function getClusterUsageCurrent(string $resource): float|null
55
60
(new Select ())
56
61
->columns (['value ' ])
57
62
->from ('prometheus_cluster_metric ' )
58
- ->where ('`group` = ? AND timestamp > UNIX_TIMESTAMP() * 1000 - ? ' , "$ resource.usage " , 2 * 60 * 1000 )
63
+ ->where (
64
+ '`group` = ? AND timestamp > UNIX_TIMESTAMP() * 1000 - ? ' ,
65
+ "$ resource.usage " ,
66
+ 2 * 60 * 1000
67
+ )
59
68
->orderBy ('timestamp DESC ' )
60
69
->limit (1 )
61
70
);
@@ -70,7 +79,11 @@ public function getNodeNetworkBytes(array &$nodeMetrics, string $direction, int
70
79
->columns (['node.id ' , 'node.name ' , 'node_metric.timestamp ' , 'node_metric.value ' ])
71
80
->from ('prometheus_node_metric AS node_metric ' )
72
81
->join ('node ' , 'node_metric.node_id = node.id ' )
73
- ->where ('node_metric.group = ? AND node_metric.timestamp > UNIX_TIMESTAMP() * 1000 - ? ' , "network. $ direction.bytes " , $ period )
82
+ ->where (
83
+ 'node_metric.group = ? AND node_metric.timestamp > UNIX_TIMESTAMP() * 1000 - ? ' ,
84
+ "network. $ direction.bytes " ,
85
+ $ period
86
+ )
74
87
);
75
88
76
89
foreach ($ dbData ->fetchAll (PDO ::FETCH_ASSOC ) as $ row ) {
@@ -96,12 +109,13 @@ public function getPodRequest(array &$podMetrics, string $resource): void
96
109
->join (
97
110
[
98
111
'latest_metrics ' => (new Select ())
99
- ->columns (['pod_id ' , 'MAX(timestamp) AS latest_timestamp ' ])
100
- ->from ('prometheus_pod_metric ' )
101
- ->where ('`group` = ? ' , "$ resource.request " )
102
- ->groupBy ('pod_id ' )
112
+ ->columns (['pod_id ' , 'MAX(timestamp) AS latest_timestamp ' ])
113
+ ->from ('prometheus_pod_metric ' )
114
+ ->where ('`group` = ? ' , "$ resource.request " )
115
+ ->groupBy ('pod_id ' )
103
116
],
104
- 'pod_metric.pod_id = latest_metrics.pod_id AND pod_metric.timestamp = latest_metrics.latest_timestamp '
117
+ 'pod_metric.pod_id = latest_metrics.pod_id '
118
+ . ' AND pod_metric.timestamp = latest_metrics.latest_timestamp '
105
119
)
106
120
->where ('pod_metric.group = ? ' , "$ resource.request " )
107
121
);
@@ -123,12 +137,13 @@ public function getPodLimit(array &$podMetrics, string $resource): void
123
137
->join (
124
138
[
125
139
'latest_metrics ' => (new Select ())
126
- ->columns (['pod_id ' , 'MAX(timestamp) AS latest_timestamp ' ])
127
- ->from ('prometheus_pod_metric ' )
128
- ->where ('`group` = ? ' , "$ resource.limit " )
129
- ->groupBy ('pod_id ' )
140
+ ->columns (['pod_id ' , 'MAX(timestamp) AS latest_timestamp ' ])
141
+ ->from ('prometheus_pod_metric ' )
142
+ ->where ('`group` = ? ' , "$ resource.limit " )
143
+ ->groupBy ('pod_id ' )
130
144
],
131
- 'pod_metric.pod_id = latest_metrics.pod_id AND pod_metric.timestamp = latest_metrics.latest_timestamp '
145
+ 'pod_metric.pod_id = latest_metrics.pod_id '
146
+ . ' AND pod_metric.timestamp = latest_metrics.latest_timestamp '
132
147
)
133
148
->where ('pod_metric.group = ? ' , "$ resource.limit " )
134
149
);
@@ -150,12 +165,13 @@ public function getPodCpuCoreUsage(array &$podMetrics): void
150
165
->join (
151
166
[
152
167
'latest_metrics ' => (new Select ())
153
- ->columns (['pod_id ' , 'MAX(timestamp) AS latest_timestamp ' ])
154
- ->from ('prometheus_pod_metric ' )
155
- ->where ('`group` = ? ' , 'cpu.usage.cores ' )
156
- ->groupBy ('pod_id ' )
168
+ ->columns (['pod_id ' , 'MAX(timestamp) AS latest_timestamp ' ])
169
+ ->from ('prometheus_pod_metric ' )
170
+ ->where ('`group` = ? ' , 'cpu.usage.cores ' )
171
+ ->groupBy ('pod_id ' )
157
172
],
158
- 'pod_metric.pod_id = latest_metrics.pod_id AND pod_metric.timestamp = latest_metrics.latest_timestamp '
173
+ 'pod_metric.pod_id = latest_metrics.pod_id '
174
+ . ' AND pod_metric.timestamp = latest_metrics.latest_timestamp '
159
175
)
160
176
->where ('pod_metric.group = ? ' , 'cpu.usage.cores ' )
161
177
->where ('timestamp > UNIX_TIMESTAMP() * 1000 - ? ' , 2 * 60 * 1000 )
@@ -178,12 +194,13 @@ public function getPodMemoryByteUsage(array &$podMetrics): void
178
194
->join (
179
195
[
180
196
'latest_metrics ' => (new Select ())
181
- ->columns (['pod_id ' , 'MAX(timestamp) AS latest_timestamp ' ])
182
- ->from ('prometheus_pod_metric ' )
183
- ->where ('`group` = ? ' , 'memory.usage.bytes ' )
184
- ->groupBy ('pod_id ' )
197
+ ->columns (['pod_id ' , 'MAX(timestamp) AS latest_timestamp ' ])
198
+ ->from ('prometheus_pod_metric ' )
199
+ ->where ('`group` = ? ' , 'memory.usage.bytes ' )
200
+ ->groupBy ('pod_id ' )
185
201
],
186
- 'pod_metric.pod_id = latest_metrics.pod_id AND pod_metric.timestamp = latest_metrics.latest_timestamp '
202
+ 'pod_metric.pod_id = latest_metrics.pod_id '
203
+ . ' AND pod_metric.timestamp = latest_metrics.latest_timestamp '
187
204
)
188
205
->where ('pod_metric.group = ? ' , 'memory.usage.bytes ' )
189
206
->where ('timestamp > UNIX_TIMESTAMP() * 1000 - ? ' , 2 * 60 * 1000 )
@@ -203,7 +220,11 @@ public function getPodUsage(array &$podMetrics, string $resource, int $period):
203
220
->columns (['pod.id ' , 'pod.name ' , 'pod_metric.timestamp ' , 'pod_metric.value ' ])
204
221
->from ('prometheus_pod_metric AS pod_metric ' )
205
222
->join ('pod ' , 'pod_metric.pod_id = pod.id ' )
206
- ->where ('pod_metric.group = ? AND pod_metric.timestamp > UNIX_TIMESTAMP() * 1000 - ? ' , "$ resource.usage " , $ period )
223
+ ->where (
224
+ 'pod_metric.group = ? AND pod_metric.timestamp > UNIX_TIMESTAMP() * 1000 - ? ' ,
225
+ "$ resource.usage " ,
226
+ $ period
227
+ )
207
228
);
208
229
209
230
foreach ($ dbData ->fetchAll (PDO ::FETCH_ASSOC ) as $ row ) {
0 commit comments