Skip to content

Commit

Permalink
Fix $namespase and $metric variables
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimKomar committed Oct 3, 2019
1 parent ddd035b commit 954aa50
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 46 deletions.
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ fi

echo "building go binary"
GOOS=$GOOS go build -o ./dist/oci-plugin$POST

# GOOS=linux go build -o ./dist/oci-plugin_linux_amd64
# GOOS=windows GOARCH=amd64 go build -o ./dist/oci-plugin_windows_amd64.exe
# tar cvf plugin.tar ./dist

Binary file modified docs/images/Screen Shot 2019-01-11 at 3.19.58 PM.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion docs/linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ To [install the data source](https://grafana.com/plugins/oci-datasource/installa
grafana-cli plugins install oci-datasource
```

The plugin will be installed into your Grafana plugins directory, which by default is located at /var/lib/grafana/plugins. [Here is more information on the CLI tool](http://docs.grafana.org/plugins/installation/). Alternatively, you can manually download the .zip file and unpack it into your /grafana/plugins directory. To do so, change to the Grafana plugins directory: `cd /usr/local/var/lib/grafana/plugins`. Download the OCI Grafana Plugin: wget `https://grafana.com/api/plugins/oci-datasource/versions/1.0.0/download`. Create a directory and install the plugin: `mkdir oci && tar -C oci -xvf plugin.tar` and then remove the tarball: `rm plugin.tar`.
**NOTE** Today the latest version of the plugin is available only with the manual installation

The plugin will be installed into your Grafana plugins directory, which by default is located at /var/lib/grafana/plugins. [Here is more information on the CLI tool](http://docs.grafana.org/plugins/installation/).

### Manually installation
Alternatively, you can manually download the .tar file and unpack it into your /grafana/plugins directory. To do so, change to the Grafana plugins directory: `cd /usr/local/var/lib/grafana/plugins`. Download the OCI Grafana Plugin: wget `https://github.com/oracle/oci-grafana-plugin/releases/download/V1.0.4/plugin.tar`. Create a directory and install the plugin: `mkdir oci && tar -C oci -xvf plugin.tar` and then remove the tarball: `rm plugin.tar`.

To start the Grafana server, run: `sudo systemctl start grafana-server`.

Expand Down
6 changes: 5 additions & 1 deletion docs/linuxoci.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ To [install the data source](https://grafana.com/plugins/oci-datasource/installa
```
grafana-cli plugins install oci-datasource
```
**NOTE** Today the latest version of the plugin is available only with the manual installation

The plugin will be installed into your Grafana plugins directory, which by default is located at /var/lib/grafana/plugins. [Here is more information on the CLI tool](http://docs.grafana.org/plugins/installation/). Alternatively, you can manually download the .zip file and unpack it into your /grafana/plugins directory. To do so, change to the Grafana plugins directory: `cd /usr/local/var/lib/grafana/plugins`. Download the OCI Grafana Plugin: wget `https://grafana.com/api/plugins/oci-datasource/versions/1.0.0/download`. Create a directory and install the plugin: `mkdir oci && tar -C oci -xvf plugin.tar` and then remove the tarball: `rm plugin.tar`.
The plugin will be installed into your Grafana plugins directory, which by default is located at /var/lib/grafana/plugins. [Here is more information on the CLI tool](http://docs.grafana.org/plugins/installation/).

### Manually installation
Alternatively, you can manually download the .tar file and unpack it into your /grafana/plugins directory. To do so, change to the Grafana plugins directory: `cd /usr/local/var/lib/grafana/plugins`. Download the OCI Grafana Plugin: wget `https://github.com/oracle/oci-grafana-plugin/releases/download/V1.0.4/plugin.tar`. Create a directory and install the plugin: `mkdir oci && tar -C oci -xvf plugin.tar` and then remove the tarball: `rm plugin.tar`.

To start the Grafana server, run: `sudo systemctl start grafana-server`.

Expand Down
6 changes: 5 additions & 1 deletion docs/macos.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ To [install the data source](https://grafana.com/plugins/oci-datasource/installa
```
grafana-cli plugins install oci-datasource
```
**NOTE** Today the latest version of the plugin is available only with the manual installation

The plugin will be installed into your Grafana plugins directory, which by default is located at /var/lib/grafana/plugins. [Here is more information on the CLI tool](http://docs.grafana.org/plugins/installation/). Alternatively, you can manually download the .zip file and unpack it into your /grafana/plugins directory. To do so, change to the Grafana plugins directory: `cd /usr/local/var/lib/grafana/plugins`. Download the OCI Grafana Plugin: wget `https://grafana.com/api/plugins/oci-datasource/versions/1.0.0/download`. Create a directory and install the plugin: `mkdir oci && tar -C oci -xvf plugin.tar` and then remove the tarball: `rm plugin.tar`
The plugin will be installed into your Grafana plugins directory, which by default is located at /var/lib/grafana/plugins. [Here is more information on the CLI tool](http://docs.grafana.org/plugins/installation/).

### Manually installation
Alternatively, you can manually download the .tar file and unpack it into your /grafana/plugins directory. To do so, change to the Grafana plugins directory: `cd /usr/local/var/lib/grafana/plugins`. Download the OCI Grafana Plugin: wget `https://github.com/oracle/oci-grafana-plugin/releases/download/V1.0.4/plugin.tar`. Create a directory and install the plugin: `mkdir oci && tar -C oci -xvf plugin.tar` and then remove the tarball: `rm plugin.tar`

To start the Grafana server, run: `brew services start grafana`

Expand Down
12 changes: 6 additions & 6 deletions docs/using.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ The page will load a preview of valuables available for that variable. Scroll do

Repeat the process for the following OCI variables:

| Name | Query |
| ----------- | ---------------------------------- |
| region | `regions()` |
| compartment | `compartments()` |
| namespace | `namespaces()` |
| metric | `metrics($namespace,$compartment)` |
| Name | Query |
| ----------- | ------------------------------------------ |
| region | `regions()` |
| compartment | `compartments()` |
| namespace | `namespaces($region,$compartment)` |
| metric | `metrics($region,$compartment,$namespace)` |

The final list of variables should look like this:

Expand Down
79 changes: 42 additions & 37 deletions src/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { aggregations, namespaces } from './constants'
import retryOrThrow from './util/retry'

export default class OCIDatasource {
constructor (instanceSettings, $q, backendSrv, templateSrv, timeSrv) {
constructor(instanceSettings, $q, backendSrv, templateSrv, timeSrv) {
this.type = instanceSettings.type
this.url = instanceSettings.url
this.name = instanceSettings.name
Expand All @@ -21,7 +21,7 @@ export default class OCIDatasource {
this.timeSrv = timeSrv
}

query (options) {
query(options) {
var query = this.buildQueryParameters(options)
query.targets = query.targets.filter(t => !t.hide)
if (query.targets.length <= 0) {
Expand All @@ -47,7 +47,7 @@ export default class OCIDatasource {
})
}

buildQueryParameters (options) {
buildQueryParameters(options) {
// remove placeholder targets
options.targets = _.filter(options.targets, target => {
return target.metric !== 'select metric'
Expand Down Expand Up @@ -94,7 +94,7 @@ export default class OCIDatasource {
return options
}

testDatasource () {
testDatasource() {
return this.doRequest({
targets: [{
queryType: 'test',
Expand All @@ -115,7 +115,7 @@ export default class OCIDatasource {
}

// helps match the regex's from creating template variables in grafana
templateMeticSearch (varString) {
templateMeticSearch(varString) {
let compartmentQuery = varString.match(/^compartments\(\)/)
if (compartmentQuery) {
return this.getCompartments().catch(err => { throw new Error('Unable to make request ' + err) })
Expand All @@ -126,21 +126,24 @@ export default class OCIDatasource {
return this.getRegions().catch(err => { throw new Error('Unable to make request ' + err) })
}

let metricQuery = varString.match(/metrics\((\$?\w+)(,\s*\$\w+)*\)/)
let metricQuery = varString.match(/metrics\((\s*\$?\w+)(\s*,\s*\$\w+)(\s*,\s*\$\w+\s*)*\)/)
if (metricQuery) {
let target = {
namespace: this.templateSrv.replace(metricQuery[1]),
compartment: this.templateSrv.replace(metricQuery[2]).replace(',', '').trim()
region: this.templateSrv.replace(metricQuery[1].trim()),
compartment: this.templateSrv.replace(metricQuery[2].replace(',', '').trim()),
namespace: this.templateSrv.replace(metricQuery[3].replace(',', '').trim())
}
return this.metricFindQuery(target).catch(err => { throw new Error('Unable to make request ' + err) })
}

let namespaceQuery = varString.match(/namespaces\(\)/)
let namespaceQuery = varString.match(/namespaces\((\$?\w+)(,\s*\$\w+)*\)/)
if (namespaceQuery) {
let names = namespaces.map((reg) => {
return { row: reg, value: reg }
})
return this.q.when(names)

let target = {
region: this.templateSrv.replace(namespaceQuery[1]),
compartment: this.templateSrv.replace(namespaceQuery[2]).replace(',', '').trim()
}
return this.getNamespaces(target).catch(err => { throw new Error('Unable to make request(get namespaces) ' + err) })
}
throw new Error('Unable to parse templating string')
}
Expand All @@ -149,7 +152,7 @@ export default class OCIDatasource {
// and is the entrypoint for templating according to grafana -- since this wasn't docuemnted
// in grafana I was lead to believe that it did the former
// TODO: break the metric finding for the query editor out into a different function
metricFindQuery (target) {
metricFindQuery(target) {
if (typeof (target) === 'string') {
return this.templateMeticSearch(target)
}
Expand Down Expand Up @@ -184,7 +187,7 @@ export default class OCIDatasource {
})
}

mapToTextValue (result, searchField) {
mapToTextValue(result, searchField) {
var table = result.data.results[searchField].tables[0]
if (!table) {
return []
Expand All @@ -201,7 +204,7 @@ export default class OCIDatasource {
return m
}

getCompartments () {
getCompartments() {
var range = this.timeSrv.timeRange()
var targets = [{
environment: this.environment,
Expand All @@ -217,7 +220,7 @@ export default class OCIDatasource {
return this.doRequest(options).then((res) => { return this.mapToTextValue(res, 'compartment') })
}

getDimensions (target) {
getDimensions(target) {
var range = this.timeSrv.timeRange()
let region = target.region
if (target.namespace === 'select namespace') {
Expand Down Expand Up @@ -252,41 +255,43 @@ export default class OCIDatasource {
return this.doRequest(options).then((res) => { return this.mapToTextValue(res, 'dimensions') })
}

getNamespaces (target) {
getNamespaces(target) {
let region = target.region
if (region === 'select region') {
region = this.defaultRegion
}
return this.doRequest({ targets: [{
// commonRequestParameters
compartment: this.templateSrv.replace(target.compartment),
environment: this.environment,
queryType: 'namespaces',
region: this.templateSrv.replace(region),
tenancyOCID: this.tenancyOCID,
return this.doRequest({
targets: [{
// commonRequestParameters
compartment: this.templateSrv.replace(target.compartment),
environment: this.environment,
queryType: 'namespaces',
region: this.templateSrv.replace(region),
tenancyOCID: this.tenancyOCID,

datasourceId: this.id
}],
range: this.timeSrv.timeRange()
datasourceId: this.id
}],
range: this.timeSrv.timeRange()
}).then((namespaces) => { return this.mapToTextValue(namespaces, 'namespaces') })
}

getRegions () {
return this.doRequest({ targets: [{
environment: this.environment,
queryType: 'regions',
datasourceId: this.id
}],
range: this.timeSrv.timeRange()
getRegions() {
return this.doRequest({
targets: [{
environment: this.environment,
queryType: 'regions',
datasourceId: this.id
}],
range: this.timeSrv.timeRange()
}).then((regions) => { return this.mapToTextValue(regions, 'regions') })
}

getAggregations () {
getAggregations() {
return this.q.when(aggregations)
}

// retries all request to the backend grafana 10 times before failure
doRequest (options) {
doRequest(options) {
let _this = this
return retryOrThrow(() => {
return _this.backendSrv.datasourceRequest({
Expand Down

0 comments on commit 954aa50

Please sign in to comment.