Skip to content

Commit f3bbffb

Browse files
author
Faiq Raza
committed
Merge branch 'sub-compartments' into 'master'
Sub compartments See merge request cloudnative/oci-grafana-plugin!16
2 parents 1d0a9e5 + cd735b1 commit f3bbffb

File tree

5 files changed

+52
-31
lines changed

5 files changed

+52
-31
lines changed

Gopkg.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

datasource.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"encoding/json"
66
"fmt"
77
"log"
8+
"regexp"
9+
"strings"
810
"time"
911

1012
"github.com/davecgh/go-spew/spew"
@@ -76,7 +78,7 @@ func (o *OCIDatasource) Query(ctx context.Context, tsdbReq *datasource.Datasourc
7678
var ts GrafanaCommonRequest
7779
json.Unmarshal([]byte(tsdbReq.Queries[0].ModelJson), &ts)
7880

79-
queryType := tsdbReq.Queries[0].RefId
81+
queryType := ts.QueryType
8082
if o.config == nil {
8183
configProvider, err := getConfigProvider(ts.Environment)
8284
if err != nil {
@@ -352,14 +354,18 @@ func (o *OCIDatasource) getCompartments(ctx context.Context, rootCompartment str
352354
for {
353355
res, err := o.identityClient.ListCompartments(ctx,
354356
identity.ListCompartmentsRequest{
355-
CompartmentId: &rootCompartment,
356-
Page: page,
357+
CompartmentId: &rootCompartment,
358+
Page: page,
359+
AccessLevel: identity.ListCompartmentsAccessLevelAny,
360+
CompartmentIdInSubtree: common.Bool(true),
357361
})
358362
if err != nil {
359363
return nil, errors.Wrap(err, fmt.Sprintf("this is what we were trying to get %s", rootCompartment))
360364
}
361365
for _, compartment := range res.Items {
362-
m[*(compartment.Name)] = *(compartment.Id)
366+
if compartment.LifecycleState == identity.CompartmentLifecycleStateActive {
367+
m[*(compartment.Name)] = *(compartment.Id)
368+
}
363369
}
364370
if res.OpcNextPage == nil {
365371
break
@@ -428,11 +434,25 @@ func (o *OCIDatasource) queryResponse(ctx context.Context, tsdbReq *datasource.D
428434
t := &datasource.TimeSeries{
429435
Name: *(item.Name),
430436
}
437+
var re = regexp.MustCompile(`(?m)\w+Name`)
431438
for k, v := range item.Dimensions {
432-
if k == "resourceId" {
439+
o.logger.Debug(k)
440+
if re.MatchString(k) {
433441
t.Name = fmt.Sprintf("%s, {%s}", t.Name, v)
434442
}
435443
}
444+
// if there isn't a human readable name fallback to resourceId
445+
if t.Name == *(item).Name {
446+
resourceID := item.Dimensions["resourceId"]
447+
id := resourceID[strings.LastIndex(resourceID, "."):]
448+
display := resourceID[0:strings.LastIndex(resourceID, ".")] + id[0:4] + "..." + id[len(id)-6:]
449+
t.Name = fmt.Sprintf("%s, {%s}", t.Name, display)
450+
}
451+
// if the namespace is loadbalancer toss on the Ad name to match the console
452+
if *(item.Namespace) == "oci_lbaas" {
453+
availabilityDomain := item.Dimensions["availabilityDomain"]
454+
t.Name = fmt.Sprintf("%s, %s", t.Name, availabilityDomain)
455+
}
436456
p := make([]*datasource.Point, 0, len(item.AggregatedDatapoints))
437457
for _, metric := range item.AggregatedDatapoints {
438458
point := &datasource.Point{

src/datasource.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _ from 'lodash'
2-
import { namespaces } from './constants'
2+
import { aggregations, namespaces } from './constants'
33
import retryOrThrow from './util/retry'
44

55
export default class OCIDatasource {
@@ -90,8 +90,7 @@ export default class OCIDatasource {
9090
testDatasource () {
9191
return this.doRequest({
9292
targets: [{
93-
queryType: 'query',
94-
refId: 'test',
93+
queryType: 'test',
9594
region: this.defaultRegion,
9695
tenancyOCID: this.tenancyOCID,
9796
compartment: '',
@@ -162,7 +161,6 @@ export default class OCIDatasource {
162161
tenancyOCID: this.tenancyOCID,
163162
region: this.templateSrv.replace(region),
164163
datasourceId: this.id,
165-
refId: 'search',
166164
namespace: this.templateSrv.replace(target.namespace)
167165
}]
168166
var options = {
@@ -198,8 +196,7 @@ export default class OCIDatasource {
198196
region: this.defaultRegion,
199197
tenancyOCID: this.tenancyOCID,
200198
queryType: 'compartments',
201-
datasourceId: this.id,
202-
refId: 'compartments'
199+
datasourceId: this.id
203200
}]
204201
var options = {
205202
range: range,
@@ -232,7 +229,6 @@ export default class OCIDatasource {
232229
tenancyOCID: this.tenancyOCID,
233230

234231
datasourceId: this.id,
235-
refId: 'dimensions',
236232
metric: this.templateSrv.replace(target.metric),
237233
namespace: this.templateSrv.replace(target.namespace)
238234
}]
@@ -257,8 +253,7 @@ export default class OCIDatasource {
257253
region: this.templateSrv.replace(region),
258254
tenancyOCID: this.tenancyOCID,
259255

260-
datasourceId: this.id,
261-
refId: 'namespaces'
256+
datasourceId: this.id
262257
}],
263258
range: this.timeSrv.timeRange()
264259
}).then((namespaces) => { return this.mapToTextValue(namespaces, 'namespaces') })
@@ -268,13 +263,16 @@ export default class OCIDatasource {
268263
return this.doRequest({ targets: [{
269264
environment: this.environment,
270265
queryType: 'regions',
271-
datasourceId: this.id,
272-
refId: 'regions'
266+
datasourceId: this.id
273267
}],
274268
range: this.timeSrv.timeRange()
275269
}).then((regions) => { return this.mapToTextValue(regions, 'regions') })
276270
}
277271

272+
getAggregations () {
273+
return this.q.when(aggregations)
274+
}
275+
278276
doRequest (options) {
279277
let _this = this
280278
return retryOrThrow(() => {

src/partials/query.editor.html

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
</div>
1616
<label class="gf-form-label query-keyword width-10">Compartment</label>
1717
<div class="gf-form-select-wrapper width-10">
18-
<gf-form-dropdown model="ctrl.target.compartment"
19-
allow-custom="true"
20-
lookup-text="true"
18+
<gf-form-dropdown
19+
model="ctrl.target.compartment"
2120
get-options="ctrl.getCompartments()"
2221
on-change="ctrl.onChangeInternal()">
2322
</gf-form-dropdown>
@@ -31,7 +30,7 @@
3130
</gf-form-dropdown>
3231
</div>
3332
<div class="gf-form">
34-
<label class="gf-form-label width-10">Resolution</label>
33+
<label class="gf-form-label width-10 query-keyword">Resolution</label>
3534
<input type="text" class="input-small gf-form-input width-5" ng-model="ctrl.target.resolution" ng-change="ctrl.onChangeInternal()" placeholder="1m"/>
3635
</div>
3736
</div>
@@ -51,14 +50,14 @@
5150
</div>
5251
</div>
5352
<div class="gf-form">
54-
<label class="gf-form-label width-10">Aggregation</label>
53+
<label class="gf-form-label width-10 query-keyword">Aggregation</label>
5554
<div class="gf-form-select-wrapper width-10">
56-
<select
57-
class="gf-form-input"
58-
ng-model="ctrl.target.aggregation"
59-
ng-options="o as o for o in ctrl.getAggregations()"
60-
ng-change="ctrl.onChangeInternal()">
61-
</select>
55+
<gf-form-dropdown
56+
model="ctrl.target.aggregation"
57+
get-options="ctrl.getAggregations()"
58+
on-change="ctrl.onChangeInternal()"
59+
allow-custom="true" >
60+
</gf-form-dropdown>
6261
</div>
6362
</div>
6463
<div class="gf-form">

src/query_ctrl.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { QueryCtrl } from 'app/plugins/sdk'
22
import './css/query-editor.css!'
3-
import { aggregations, windows } from './constants'
3+
import { windows } from './constants'
44
import _ from 'lodash'
55

66
export class OCIDatasourceQueryCtrl extends QueryCtrl {
@@ -76,7 +76,11 @@ export class OCIDatasourceQueryCtrl extends QueryCtrl {
7676
}
7777

7878
getAggregations () {
79-
return aggregations
79+
return this.datasource.getAggregations().then((aggs) => {
80+
return aggs.map((val) => {
81+
return { text: val, value: val }
82+
})
83+
})
8084
}
8185

8286
onChangeInternal () {

0 commit comments

Comments
 (0)