@@ -65,127 +65,138 @@ func QueryMetrics(c *gin.Context) {
65
65
66
66
switch queryType {
67
67
case "mname" :
68
- rows , err := tx .Query (fmt .Sprintf ("SELECT DISTINCT name FROM %s" , sanitizedPodName ))
69
- if err != nil {
70
- log .Printf ("Error querying metric names: %v, SQL Error: %v" , err , err )
71
- c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to query metric names" })
68
+ queryMetricNames (c , tx , sanitizedPodName )
69
+ case "details" :
70
+ queryMetricDetailsByName (c , tx , sanitizedPodName , metricName )
71
+ case "metricsdetails" :
72
+ queryMetricDetails (c , appName )
73
+ }
74
+ }
75
+
76
+ func queryMetricNames (c * gin.Context , tx * sql.Tx , sanitizedPodName string ) {
77
+ rows , err := tx .Query (fmt .Sprintf ("SELECT DISTINCT name FROM %s" , sanitizedPodName ))
78
+ if err != nil {
79
+ log .Printf ("Error querying metric names: %v, SQL Error: %v" , err , err )
80
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to query metric names" })
81
+ return
82
+ }
83
+ defer rows .Close ()
84
+
85
+ var metricNames []string
86
+ for rows .Next () {
87
+ var metricName string
88
+ if err := rows .Scan (& metricName ); err != nil {
89
+ log .Printf ("Error scanning metric name: %v" , err )
90
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to scan metric name" })
72
91
return
73
92
}
74
- defer rows .Close ()
75
-
76
- var metricNames []string
77
- for rows .Next () {
78
- var metricName string
79
- if err := rows .Scan (& metricName ); err != nil {
80
- log .Printf ("Error scanning metric name: %v" , err )
81
- c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to scan metric name" })
82
- return
83
- }
84
- metricNames = append (metricNames , metricName )
85
- }
93
+ metricNames = append (metricNames , metricName )
94
+ }
86
95
87
- c .JSON (http .StatusOK , gin.H {"metricNames" : metricNames })
96
+ c .JSON (http .StatusOK , gin.H {"metricNames" : metricNames })
97
+ }
88
98
89
- case "details" :
90
- if metricName == "" {
91
- c .JSON (http .StatusBadRequest , gin.H {"error" : "Metric name required for details" })
92
- return
93
- }
94
- query := fmt . Sprintf ( `
99
+ func queryMetricDetailsByName ( c * gin. Context , tx * sql. Tx , sanitizedPodName , metricName string ) {
100
+ if metricName == "" {
101
+ c .JSON (http .StatusBadRequest , gin.H {"error" : "Metric name required for details" })
102
+ return
103
+ }
104
+ query := `
95
105
SELECT
96
106
m.currentTime,
97
107
m.name,
98
108
v.value,
99
109
v.measure,
100
110
v.id
101
- FROM %s m
102
- INNER JOIN %s_values v ON m.id = v.metric_id
111
+ FROM ? m
112
+ INNER JOIN ? v ON m.id = v.metric_id
103
113
WHERE m.name = ?
104
- ` , sanitizedPodName , sanitizedPodName )
105
- rows , err := tx .Query (query , metricName )
106
- if err != nil {
107
- log .Printf ("Error querying metric details: %v" , err )
108
- c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to query metric details" })
109
- return
110
- }
111
- defer rows .Close ()
114
+ `
115
+ rows , err := tx .Query (query , sanitizedPodName , fmt . Sprintf ( "%s_values" , sanitizedPodName ) , metricName )
116
+ if err != nil {
117
+ log .Printf ("Error querying metric details: %v" , err )
118
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to query metric details" })
119
+ return
120
+ }
121
+ defer rows .Close ()
112
122
113
- type MetricValue struct {
114
- Value string `json:"value"`
115
- Measure string `json:"measure"`
116
- Labels map [string ]string `json:"labels"`
117
- }
123
+ type MetricValue struct {
124
+ Value string `json:"value"`
125
+ Measure string `json:"measure"`
126
+ Labels map [string ]string `json:"labels"`
127
+ }
118
128
119
- type MetricDetails struct {
120
- Name string `json:"name"`
121
- Values []MetricValue `json:"values"`
122
- }
129
+ type MetricDetails struct {
130
+ Name string `json:"name"`
131
+ Values []MetricValue `json:"values"`
132
+ }
123
133
124
- detailsMap := make (map [string ]MetricDetails )
134
+ detailsMap := make (map [string ]MetricDetails )
125
135
126
- for rows .Next () {
127
- var currentTime time.Time
128
- var name , value , measure string
129
- var valueID int
130
- if err := rows .Scan (& currentTime , & name , & value , & measure , & valueID ); err != nil {
131
- log .Printf ("Error scanning metric details: %v" , err )
132
- c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to scan metric details" })
133
- return
134
- }
136
+ for rows .Next () {
137
+ var currentTime time.Time
138
+ var name , value , measure string
139
+ var valueID int
140
+ if err := rows .Scan (& currentTime , & name , & value , & measure , & valueID ); err != nil {
141
+ log .Printf ("Error scanning metric details: %v" , err )
142
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to scan metric details" })
143
+ return
144
+ }
135
145
136
- labelsQuery := fmt .Sprintf ("SELECT key, value FROM %s_labels WHERE value_id = ?" , sanitizedPodName )
137
- labelsRows , err := tx .Query (labelsQuery , valueID )
138
- if err != nil {
139
- log .Printf ("Error querying labels: %v" , err )
140
- c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to query labels" })
146
+ labelsQuery := "SELECT key, value FROM ? WHERE value_id = ?"
147
+ labelsRows , err := tx .Query (labelsQuery , fmt .Sprintf ("%s_labels" , sanitizedPodName ), valueID )
148
+ if err != nil {
149
+ log .Printf ("Error querying labels: %v" , err )
150
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to query labels" })
151
+ return
152
+ }
153
+ defer labelsRows .Close ()
154
+
155
+ labels := make (map [string ]string )
156
+ for labelsRows .Next () {
157
+ var labelKey , labelValue string
158
+ if err := labelsRows .Scan (& labelKey , & labelValue ); err != nil {
159
+ log .Printf ("Error scanning labels: %v" , err )
160
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to scan labels" })
141
161
return
142
162
}
143
- defer labelsRows .Close ()
144
-
145
- labels := make (map [string ]string )
146
- for labelsRows .Next () {
147
- var labelKey , labelValue string
148
- if err := labelsRows .Scan (& labelKey , & labelValue ); err != nil {
149
- log .Printf ("Error scanning labels: %v" , err )
150
- c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to scan labels" })
151
- return
152
- }
153
- labels [labelKey ] = labelValue
154
- }
163
+ labels [labelKey ] = labelValue
164
+ }
155
165
156
- timeKey := currentTime .Format (time .RFC3339 )
166
+ timeKey := currentTime .Format (time .RFC3339 )
157
167
158
- detail , exists := detailsMap [timeKey ]
159
- if ! exists {
160
- detail = MetricDetails {
161
- Name : name ,
162
- Values : []MetricValue {},
163
- }
168
+ detail , exists := detailsMap [timeKey ]
169
+ if ! exists {
170
+ detail = MetricDetails {
171
+ Name : name ,
172
+ Values : []MetricValue {},
164
173
}
174
+ }
165
175
166
- detail .Values = append (detail .Values , MetricValue {
167
- Value : value ,
168
- Measure : measure ,
169
- Labels : labels ,
170
- })
176
+ detail .Values = append (detail .Values , MetricValue {
177
+ Value : value ,
178
+ Measure : measure ,
179
+ Labels : labels ,
180
+ })
171
181
172
- detailsMap [timeKey ] = detail
173
- }
182
+ detailsMap [timeKey ] = detail
183
+ }
174
184
175
- c .JSON (http .StatusOK , gin.H {"details" : detailsMap })
185
+ c .JSON (http .StatusOK , gin.H {"details" : detailsMap })
186
+ }
176
187
177
- case "metricsdetails" :
178
- // Handle metricsdetails query type
179
- db , err := sql .Open ("sqlite" , strings .ReplaceAll (appName , "-" , "_" )+ ".db" )
180
- if err != nil {
181
- log .Printf ("Error opening database: %v" , err )
182
- c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to open database" })
183
- return
184
- }
185
- defer db .Close ()
188
+ func queryMetricDetails ( c * gin. Context , appName string ) {
189
+ // Handle metricsdetails query type
190
+ db , err := sql .Open ("sqlite" , strings .ReplaceAll (appName , "-" , "_" )+ ".db" )
191
+ if err != nil {
192
+ log .Printf ("Error opening database: %v" , err )
193
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to open database" })
194
+ return
195
+ }
196
+ defer db .Close ()
186
197
187
- // Get all relevant tables
188
- rows , err := db .Query (`
198
+ // Get all relevant tables
199
+ rows , err := db .Query (`
189
200
SELECT name
190
201
FROM sqlite_master
191
202
WHERE type='table'
@@ -194,55 +205,53 @@ func QueryMetrics(c *gin.Context) {
194
205
AND name NOT LIKE '%_time_load'
195
206
AND name != 'sqlite_sequence'
196
207
` )
197
- if err != nil {
198
- log .Printf ("Error querying tables: %v" , err )
199
- c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to query tables" })
200
- return
201
- }
202
- defer rows .Close ()
208
+ if err != nil {
209
+ log .Printf ("Error querying tables: %v" , err )
210
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : "Failed to query tables" })
211
+ return
212
+ }
213
+ defer rows .Close ()
203
214
204
- result := make (map [string ]map [string ]MetricInfo )
215
+ result := make (map [string ]map [string ]MetricInfo )
205
216
206
- // Process each table (pod)
207
- for rows .Next () {
208
- var tableName string
209
- if err := rows .Scan (& tableName ); err != nil {
210
- log .Printf ("Error scanning table name: %v" , err )
211
- continue
212
- }
217
+ // Process each table (pod)
218
+ for rows .Next () {
219
+ var tableName string
220
+ if err := rows .Scan (& tableName ); err != nil {
221
+ log .Printf ("Error scanning table name: %v" , err )
222
+ continue
223
+ }
213
224
214
- // Get metrics for this pod
215
- metricRows , err := db .Query (fmt .Sprintf (`
225
+ // Get metrics for this pod
226
+ metricRows , err := db .Query (fmt .Sprintf (`
216
227
SELECT DISTINCT name, help, type
217
228
FROM %s
218
229
GROUP BY name
219
230
` , tableName ))
220
- if err != nil {
221
- log .Printf ("Error querying metrics for table %s: %v" , tableName , err )
231
+ if err != nil {
232
+ log .Printf ("Error querying metrics for table %s: %v" , tableName , err )
233
+ continue
234
+ }
235
+ defer metricRows .Close ()
236
+
237
+ podMetrics := make (map [string ]MetricInfo )
238
+
239
+ // Process each metric
240
+ for metricRows .Next () {
241
+ var name , help , metricType string
242
+ if err := metricRows .Scan (& name , & help , & metricType ); err != nil {
243
+ log .Printf ("Error scanning metric info: %v" , err )
222
244
continue
223
245
}
224
- defer metricRows .Close ()
225
-
226
- podMetrics := make (map [string ]MetricInfo )
227
-
228
- // Process each metric
229
- for metricRows .Next () {
230
- var name , help , metricType string
231
- if err := metricRows .Scan (& name , & help , & metricType ); err != nil {
232
- log .Printf ("Error scanning metric info: %v" , err )
233
- continue
234
- }
235
-
236
- podMetrics [name ] = MetricInfo {
237
- Help : help ,
238
- Type : metricType ,
239
- }
240
- }
241
246
242
- result [tableName ] = podMetrics
247
+ podMetrics [name ] = MetricInfo {
248
+ Help : help ,
249
+ Type : metricType ,
250
+ }
243
251
}
244
252
245
- c .JSON (http .StatusOK , result )
246
- return
253
+ result [tableName ] = podMetrics
247
254
}
255
+
256
+ c .JSON (http .StatusOK , result )
248
257
}
0 commit comments