Skip to content

Commit abe35c0

Browse files
author
Faiq Raza
committed
Merge branch 'code-cleanup' into 'master'
Adds comments to tricky parts of the code See merge request cloudnative/oci-grafana-plugin!18
2 parents f3bbffb + b42b446 commit abe35c0

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/datasource.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export default class OCIDatasource {
5151

5252
var targets = _.map(options.targets, target => {
5353
let region = target.region
54+
// check to see if we have tags (dimensions) in the query and turn those into a string for MQL
5455
let t = []
5556
if (target.hasOwnProperty('tags')) {
5657
for (let i = 0; i < target.tags.length; i++) {
@@ -64,6 +65,7 @@ export default class OCIDatasource {
6465
if (target.region === 'select region') {
6566
region = this.defaultRegion
6667
}
68+
// If there's nothing there return a blank string otherwise return the dimensions encapsuled by these {}
6769
let dimension = (t.length === 0) ? '' : `{${t}}`
6870

6971
return {
@@ -78,6 +80,7 @@ export default class OCIDatasource {
7880
hide: target.hide,
7981
type: target.type || 'timeserie',
8082
datasourceId: this.id,
83+
// pass the MQL string we built here
8184
query: `${this.templateSrv.replace(target.metric, options.scopedVars || {})}[${target.window}]${dimension}.${target.aggregation}`
8285
}
8386
})
@@ -107,15 +110,16 @@ export default class OCIDatasource {
107110
})
108111
}
109112

113+
// helps match the regex's from creating template variables in grafana
110114
templateMeticSearch (varString) {
111115
let compartmentQuery = varString.match(/^compartments\(\)/)
112116
if (compartmentQuery) {
113-
return this.getCompartments()
117+
return this.getCompartments().catch(err => { throw new Error('Unable to make request ' + err) })
114118
}
115119

116120
let regionQuery = varString.match(/^regions\(\)/)
117121
if (regionQuery) {
118-
return this.getRegions()
122+
return this.getRegions().catch(err => { throw new Error('Unable to make request ' + err) })
119123
}
120124

121125
let metricQuery = varString.match(/metrics\((\$?\w+)(,\s*\$\w+)*\)/)
@@ -124,7 +128,7 @@ export default class OCIDatasource {
124128
namespace: this.templateSrv.replace(metricQuery[1]),
125129
compartment: this.templateSrv.replace(metricQuery[2]).replace(',', '').trim()
126130
}
127-
return this.metricFindQuery(target)
131+
return this.metricFindQuery(target).catch(err => { throw new Error('Unable to make request ' + err) })
128132
}
129133

130134
let namespaceQuery = varString.match(/namespaces\(\)/)
@@ -137,6 +141,10 @@ export default class OCIDatasource {
137141
throw new Error('Unable to parse templating string')
138142
}
139143

144+
// this function does 2 things - its the entrypoint for finding the metrics from the query editor
145+
// and is the entrypoint for templating according to grafana -- since this wasn't docuemnted
146+
// in grafana I was lead to believe that it did the former
147+
// TODO: break the metric finding for the query editor out into a different function
140148
metricFindQuery (target) {
141149
if (typeof (target) === 'string') {
142150
return this.templateMeticSearch(target)
@@ -273,6 +281,7 @@ export default class OCIDatasource {
273281
return this.q.when(aggregations)
274282
}
275283

284+
// retries all request to the backend grafana 10 times before failure
276285
doRequest (options) {
277286
let _this = this
278287
return retryOrThrow(() => {

src/query_ctrl.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class OCIDatasourceQueryCtrl extends QueryCtrl {
2828
value: '-- remove tag filter --'
2929
})
3030

31+
// rebuilds the tagSegments which are stored as tags when reloading the query editor
3132
for (let i = 0; i < this.target.tags.length; i++) {
3233
if (i > 0) {
3334
this.tagSegments.push(this.uiSegmentSrv.newCondition(','))
@@ -116,6 +117,7 @@ export class OCIDatasourceQueryCtrl extends QueryCtrl {
116117
return []
117118
}
118119

120+
// entrypoint in fetching the dimensions and their values for a particular metric
119121
getTagsOrValues (segment, index) {
120122
if (segment.type === 'operator') {
121123
return this.q.when([])
@@ -129,12 +131,15 @@ export class OCIDatasourceQueryCtrl extends QueryCtrl {
129131
const key = this.tagSegments[index - 2]
130132
const options = this.dimCache[key.value]
131133
const that = this
134+
// return all the values for the key
132135
const optSegments = options.map(v => that.uiSegmentSrv.newSegment({
133136
value: v
134137
}))
135138
return this.q.when(optSegments)
136139
}
137140

141+
// we fetch dimensions for a metric in pairs link this key=value
142+
// we save them in an object that looks like {key [list, of, values]}
138143
mapToSegment (dimensions) {
139144
const dimCache = {}
140145
const dims = dimensions.map((v) => {
@@ -154,6 +159,7 @@ export class OCIDatasourceQueryCtrl extends QueryCtrl {
154159
return dims
155160
}
156161

162+
// deals with adding or updating new values
157163
tagSegmentUpdated (segment, index) {
158164
this.tagSegments[index] = segment
159165

@@ -164,16 +170,19 @@ export class OCIDatasourceQueryCtrl extends QueryCtrl {
164170
this.tagSegments.push(this.uiSegmentSrv.newPlusButton())
165171
} else if (this.tagSegments.length > 2) {
166172
this.tagSegments.splice(Math.max(index - 1, 0), 1)
173+
// if the thing we removed was the only key value pair add + button so they can put in new metrics
167174
if (this.tagSegments[this.tagSegments.length - 1].type !== 'plus-button') {
168175
this.tagSegments.push(this.uiSegmentSrv.newPlusButton())
169176
}
170177
}
171178
} else {
179+
// handle adding in the first dimension pair
172180
if (segment.type === 'plus-button') {
173181
if (index > 2) {
174182
this.tagSegments.splice(index, 0, this.uiSegmentSrv.newCondition(','))
175183
}
176184
this.tagSegments.push(this.uiSegmentSrv.newOperator('='))
185+
// always push in the fake for the first value in case network request breaks
177186
this.tagSegments.push(this.uiSegmentSrv.newFake('select tag value', 'value', 'query-segment-value'))
178187
segment.type = 'key'
179188
segment.cssClass = 'query-segment-key'
@@ -187,6 +196,7 @@ export class OCIDatasourceQueryCtrl extends QueryCtrl {
187196
this.rebuildTargetTagConditions()
188197
}
189198

199+
// take the tagSegments and turn them into tags for the query that gets sent
190200
rebuildTargetTagConditions () {
191201
const tags = []
192202
let tagIndex = 0

0 commit comments

Comments
 (0)