Skip to content

Commit 0742f88

Browse files
authored
improve the config page (#36)
1 parent b62949f commit 0742f88

File tree

7 files changed

+58
-139
lines changed

7 files changed

+58
-139
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.2.0-dev",
44
"description": "Load data timestream in grafana",
55
"scripts": {
6-
"build": "grafana-toolkit plugin:build",
6+
"build": "rm -rf dist && grafana-toolkit plugin:build && mage build:backend",
77
"test": "grafana-toolkit plugin:test",
88
"dev": "rm -rf node_modules/@grafana/data/node_modules; grafana-toolkit plugin:dev",
99
"watch": "grafana-toolkit plugin:dev --watch"

pkg/timestream/executor_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,19 @@ func TestSavedConversions(t *testing.T) {
5555

5656
func TestGenerateTestData(t *testing.T) {
5757
t.Skip("Integration Test") // comment line to run this
58+
db := "SampleDB"
59+
tableName := "DevOps"
60+
table := db + "." + tableName
5861

5962
m := make(map[string]models.QueryModel)
63+
m["show-databases.json"] = models.QueryModel{
64+
RawQuery: "SHOW DATABASES",
65+
}
66+
67+
m["show-tables.json"] = models.QueryModel{
68+
RawQuery: "SHOW TABLES FROM " + db,
69+
}
70+
6071
m["select-consts.json"] = models.QueryModel{
6172
RawQuery: `SELECT
6273
1 as t_int32,
@@ -65,22 +76,22 @@ func TestGenerateTestData(t *testing.T) {
6576
}
6677

6778
m["describe-table.json"] = models.QueryModel{
68-
RawQuery: "DESCRIBE grafanaDB.grafanaTable",
79+
RawQuery: "DESCRIBE " + table,
6980
}
7081

7182
m["show-measures.json"] = models.QueryModel{
72-
RawQuery: "SHOW MEASURES FROM grafanaDB.grafanaTable",
83+
RawQuery: "SHOW MEASURES FROM " + table,
7384
}
7485

7586
m["select-star.json"] = models.QueryModel{
76-
RawQuery: `SELECT * FROM grafanaDB.grafanaTable LIMIT 10`,
87+
RawQuery: `SELECT * FROM ` + table + ` LIMIT 10`,
7788
}
7889

7990
m["single-timeseries.json"] = models.QueryModel{
8091
RawQuery: `SELECT region, cell, silo, availability_zone, microservice_name,
8192
instance_name, process_name, jdk_version,
8293
CREATE_TIME_SERIES(time, measure_value::double) AS gc_reclaimed
83-
FROM grafanaDB.grafanaTable
94+
FROM ` + table + `
8495
WHERE time > ago(2h)
8596
AND measure_name = 'gc_reclaimed'
8697
AND region = 'ap-northeast-1' AND cell = 'ap-northeast-1-cell-5' AND silo = 'ap-northeast-1-cell-5-silo-2'
@@ -101,7 +112,7 @@ func TestGenerateTestData(t *testing.T) {
101112
process_name,
102113
jdk_version,
103114
CREATE_TIME_SERIES(time, measure_value::double) AS gc_reclaimed
104-
FROM grafanaDB.grafanaTable
115+
FROM ` + table + `
105116
WHERE time > ago(1h)
106117
AND measure_name = 'gc_reclaimed'
107118
AND region = 'ap-northeast-1'

src/common/ConnectionConfig.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ export default class ConnectionConfig extends PureComponent<Props, State> {
4343
const { regions } = this.state;
4444
const { options } = this.props;
4545
const secureJsonData = (options.secureJsonData || {}) as AwsDataSourceSecureJsonData;
46-
let profile = options.jsonData.profile;
46+
const { jsonData } = options;
47+
let profile = jsonData.profile;
4748
if (profile === undefined) {
4849
profile = options.database;
4950
}
51+
let region = regions.find(v => v.value === jsonData.defaultRegion);
52+
if (!region && jsonData.defaultRegion) {
53+
region = { value: jsonData.defaultRegion, label: jsonData.defaultRegion };
54+
}
5055

5156
return (
5257
<>
@@ -209,10 +214,12 @@ export default class ConnectionConfig extends PureComponent<Props, State> {
209214
</InlineFormLabel>
210215
<Select
211216
className="width-30"
212-
value={regions.find(region => region.value === options.jsonData.defaultRegion)}
217+
value={region}
213218
options={regions}
214219
defaultValue={options.jsonData.defaultRegion}
215220
onChange={onUpdateDatasourceJsonDataOptionSelect(this.props, 'defaultRegion')}
221+
allowCustomValue={true}
222+
formatCreateLabel={t => `Region: ${t}`}
216223
/>
217224
</div>
218225
</div>

src/common/regions.ts

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,7 @@
11
import { SelectableValue } from '@grafana/data';
22

3-
export const standardRegions: Array<SelectableValue<string>> = [
4-
'ap-east-1',
5-
'ap-northeast-1',
6-
'ap-northeast-2',
7-
'ap-northeast-3',
8-
'ap-south-1',
9-
'ap-southeast-1',
10-
'ap-southeast-2',
11-
'ca-central-1',
12-
'cn-north-1',
13-
'cn-northwest-1',
14-
'eu-central-1',
15-
'eu-north-1',
16-
'eu-west-1',
17-
'eu-west-2',
18-
'eu-west-3',
19-
'me-south-1',
20-
'sa-east-1',
21-
'us-east-1',
22-
'us-east-2',
23-
'us-gov-east-1',
24-
'us-gov-west-1',
25-
'us-iso-east-1',
26-
'us-isob-east-1',
27-
'us-west-1',
28-
'us-west-2',
29-
].map(r => {
30-
return { value: r, label: r };
31-
});
3+
export const standardRegions: Array<SelectableValue<string>> = ['us-east-1', 'us-east-2', 'us-west-1', 'eu-west-2'].map(
4+
r => {
5+
return { value: r, label: r };
6+
}
7+
);

src/components/ConfigEditor.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export class ConfigEditor extends PureComponent<Props, State> {
2121
}
2222

2323
componentDidMount = async () => {
24-
console.log('componentDidMountX', this.props);
2524
const d = await getDataSourceSrv().get(this.props.options.name);
2625
const { options } = this.props;
2726
const ds = d as DataSource;
@@ -35,10 +34,16 @@ export class ConfigEditor extends PureComponent<Props, State> {
3534
};
3635

3736
// Try harder to get the state
38-
componentDidUpdate = async () => {
37+
componentDidUpdate = async (oldProps: Props) => {
3938
if (!this.state.schema) {
4039
console.log('no state... try again');
4140
this.componentDidMount();
41+
} else {
42+
const oldData = oldProps.options.jsonData;
43+
const { jsonData } = this.props.options;
44+
if (jsonData.authType !== oldData.authType || jsonData.defaultRegion !== jsonData.defaultRegion) {
45+
this.componentDidMount(); // update
46+
}
4247
}
4348
};
4449

@@ -115,21 +120,24 @@ export class ConfigEditor extends PureComponent<Props, State> {
115120
? { label: defaultMeasure, value: defaultMeasure }
116121
: { label: 'Select measure', value: '' };
117122

123+
// Reload the dropdowns when config changes
118124
return (
119-
<div>
125+
<div key={hashCode(JSON.stringify(this.props.options.jsonData))}>
120126
<br />
121127
<h3>Default Query Macros</h3>
122128
<div className="gf-form-inline">
123129
<div className="gf-form">
124130
<InlineFormLabel className={widthKey}>$__database</InlineFormLabel>
125131
<AsyncSelect
126132
className={widthVal}
133+
cacheOptions={false}
127134
loadOptions={schema.getDatabases}
128135
value={currentDatabase}
129136
onChange={this.onDatabaseChange}
130137
defaultOptions
131138
loadingMessage="..."
132139
allowCustomValue={true}
140+
formatCreateLabel={t => `DB: ${t}`}
133141
/>
134142
</div>
135143
</div>
@@ -139,12 +147,14 @@ export class ConfigEditor extends PureComponent<Props, State> {
139147
<InlineFormLabel className={widthKey}>$__table</InlineFormLabel>
140148
<AsyncSelect
141149
className={widthVal}
150+
cacheOptions={false}
142151
loadOptions={schema.getTables}
143152
value={currentTable}
144153
onChange={this.onTableChange}
145154
defaultOptions
146155
loadingMessage="..."
147156
allowCustomValue={true}
157+
formatCreateLabel={t => `Table: ${t}`}
148158
/>
149159
</div>
150160
</div>
@@ -155,6 +165,7 @@ export class ConfigEditor extends PureComponent<Props, State> {
155165
<InlineFormLabel className={widthKey}>$__measure</InlineFormLabel>
156166
<AsyncSelect
157167
className={widthVal}
168+
cacheOptions={false}
158169
loadOptions={schema.getMeasures}
159170
value={currentMeasure}
160171
onChange={this.onMeasureChange}
@@ -184,3 +195,10 @@ export class ConfigEditor extends PureComponent<Props, State> {
184195
);
185196
}
186197
}
198+
199+
function hashCode(s: string) {
200+
return s.split('').reduce((a, b) => {
201+
a = (a << 5) - a + b.charCodeAt(0);
202+
return a & a;
203+
}, 0);
204+
}

src/dashboards/sample.json

Lines changed: 5 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@
813813
"targets": [
814814
{
815815
"queryType": "raw",
816-
"rawQuery": "SELECT region, cell, silo, availability_zone, microservice_name,\n instance_type, os_version, instance_name\nFROM ${dbName}.${tableName}\nWHERE $__timeFilter\n AND region = 'us-east-1'\n AND measure_name = 'cpu_user'\nGROUP BY region, cell, silo, availability_zone, microservice_name,\n instance_type, os_version, instance_name\nORDER BY region, cell, silo, availability_zone, microservice_name,\n instance_type, os_version, instance_name",
816+
"rawQuery": "SELECT region, cell, silo, availability_zone, microservice_name,\n instance_type, os_version, instance_name\nFROM $__database.$__table\nWHERE $__timeFilter\n AND region = 'us-east-1'\n AND measure_name = 'cpu_user'\nGROUP BY region, cell, silo, availability_zone, microservice_name,\n instance_type, os_version, instance_name\nORDER BY region, cell, silo, availability_zone, microservice_name,\n instance_type, os_version, instance_name",
817817
"refId": "A"
818818
}
819819
],
@@ -886,7 +886,7 @@
886886
"targets": [
887887
{
888888
"queryType": "raw",
889-
"rawQuery": "SHOW MEASURES FROM ${dbName}.${tableName}",
889+
"rawQuery": "SHOW MEASURES FROM $__database.$__table",
890890
"refId": "A"
891891
}
892892
],
@@ -934,7 +934,7 @@
934934
"targets": [
935935
{
936936
"queryType": "raw",
937-
"rawQuery": "SELECT region, cell, silo, availability_zone,\n approx_distinct(microservice_name || instance_name) AS num_instances\nFROM ${dbName}.${tableName}\nWHERE $__timeFilter\n AND measure_name = 'cpu_user'\nGROUP BY region, cell, silo, availability_zone\nORDER BY region, cell, silo, availability_zone",
937+
"rawQuery": "SELECT region, cell, silo, availability_zone,\n approx_distinct(microservice_name || instance_name) AS num_instances\nFROM $__database.$__table\nWHERE $__timeFilter\n AND measure_name = 'cpu_user'\nGROUP BY region, cell, silo, availability_zone\nORDER BY region, cell, silo, availability_zone",
938938
"refId": "A"
939939
}
940940
],
@@ -947,99 +947,6 @@
947947
"refresh": "5m",
948948
"schemaVersion": 26,
949949
"style": "dark",
950-
"tags": [],
951-
"templating": {
952-
"list": [
953-
{
954-
"current": {
955-
"selected": false,
956-
"text": "grafanaDB",
957-
"value": "grafanaDB"
958-
},
959-
"hide": 2,
960-
"label": "Database Name",
961-
"name": "dbName",
962-
"options": [
963-
{
964-
"selected": true,
965-
"text": "grafanaDB",
966-
"value": "grafanaDB"
967-
}
968-
],
969-
"query": "grafanaDB",
970-
"skipUrlSync": false,
971-
"type": "textbox"
972-
},
973-
{
974-
"current": {
975-
"selected": false,
976-
"text": "grafanaTable",
977-
"value": "grafanaTable"
978-
},
979-
"hide": 2,
980-
"label": "Table name",
981-
"name": "tableName",
982-
"options": [
983-
{
984-
"selected": true,
985-
"text": "grafanaTable",
986-
"value": "grafanaTable"
987-
}
988-
],
989-
"query": "grafanaTable",
990-
"skipUrlSync": false,
991-
"type": "textbox"
992-
},
993-
{
994-
"allValue": null,
995-
"current": {
996-
"selected": false,
997-
"text": "us-east1",
998-
"value": "us-east1"
999-
},
1000-
"hide": 2,
1001-
"includeAll": false,
1002-
"label": "Region",
1003-
"multi": false,
1004-
"name": "region",
1005-
"options": [
1006-
{
1007-
"selected": true,
1008-
"text": "us-east1",
1009-
"value": "us-east1"
1010-
},
1011-
{
1012-
"selected": false,
1013-
"text": "us-east-2",
1014-
"value": "us-east-2"
1015-
},
1016-
{
1017-
"selected": false,
1018-
"text": "us-west-1",
1019-
"value": "us-west-1"
1020-
},
1021-
{
1022-
"selected": false,
1023-
"text": "us-west-2",
1024-
"value": "us-west-2"
1025-
},
1026-
{
1027-
"selected": false,
1028-
"text": "eu-west-1",
1029-
"value": "eu-west-1"
1030-
},
1031-
{
1032-
"selected": false,
1033-
"text": "ap-northeast-1",
1034-
"value": "ap-northeast-1"
1035-
}
1036-
],
1037-
"query": "us-east1,us-east-2,us-west-1,us-west-2,eu-west-1,ap-northeast-1",
1038-
"skipUrlSync": false,
1039-
"type": "custom"
1040-
}
1041-
]
1042-
},
1043950
"time": {
1044951
"from": "now-6h",
1045952
"to": "now"
@@ -1058,7 +965,7 @@
1058965
]
1059966
},
1060967
"timezone": "utc",
1061-
"title": "Sample Application Dashboard",
1062-
"uid": "eg1btqkgk",
968+
"title": "Sample (DevOps)",
969+
"uid": "G1btqkgkK",
1063970
"version": 1
1064971
}

src/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"alerting": true,
1111
"annotations": true,
1212
"includes": [
13-
{ "type": "dashboard", "name": "Sample Application", "path": "dashboards/sample.json" }
13+
{ "type": "dashboard", "name": "Sample (DevOps)", "path": "dashboards/sample.json" }
1414
],
1515
"info": {
1616
"description": "Managed timeseries database from amazon",

0 commit comments

Comments
 (0)