Skip to content
This repository was archived by the owner on Dec 11, 2022. It is now read-only.

Commit 8eb95a1

Browse files
committed
Solves #137
ORDER BY in query builder
1 parent 3bad7cc commit 8eb95a1

File tree

6 files changed

+82
-5
lines changed

6 files changed

+82
-5
lines changed

dist/module.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58477,6 +58477,8 @@ function () {
5847758477
this.groupBy = "";
5847858478
this.tmpValue = "";
5847958479
target.format = target.format || "time_series";
58480+
target.orderByCol = target.orderByCol || "1";
58481+
target.orderBySort = target.orderBySort || "1";
5848058482
target.timeColumn = target.timeColumn || "-- time --";
5848158483
target.timeColumnType = target.timeColumnType || "TIMESTAMP";
5848258484
target.metricColumn = target.metricColumn || "none";
@@ -58938,10 +58940,16 @@ function () {
5893858940
query += this.buildGroupClause();
5893958941

5894058942
if (!this.isWindow) {
58941-
query += "\nORDER BY 1";
58943+
var orderBy = "\nORDER BY 1";
5894258944

5894358945
if (this.hasMetricColumn()) {
58944-
query += ",2";
58946+
orderBy = this.target.orderByCol === "1" ? "\nORDER BY 1,2" : "\nORDER BY 2,1";
58947+
}
58948+
58949+
query += orderBy;
58950+
58951+
if (this.target.orderBySort === "2") {
58952+
query += " DESC";
5894558953
}
5894658954
} // query += '\nLIMIT 2';
5894758955

@@ -60260,6 +60268,20 @@ function (_super) {
6026060268
text: "Table",
6026160269
value: "table"
6026260270
}];
60271+
_this.orderByCols = [{
60272+
text: "Time",
60273+
value: "1"
60274+
}, {
60275+
text: "Metric",
60276+
value: "2"
60277+
}];
60278+
_this.orderBySorts = [{
60279+
text: "ASC",
60280+
value: "1"
60281+
}, {
60282+
text: "DESC",
60283+
value: "2"
60284+
}];
6026360285

6026460286
if (!_this.target.rawSql) {
6026560287
// special handling when in table panel

dist/module.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/partials/query.editor.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@
114114
</div>
115115
</div>
116116

117+
<!-- Start Order BY -->
118+
119+
<div class="gf-form">
120+
<label class="gf-form-label query-keyword width-8">ORDER BY</label>
121+
<div class="gf-form-select-wrapper">
122+
<select class="gf-form-input gf-size-auto" ng-model="ctrl.target.orderByCol" ng-options="f.value as f.text for f in ctrl.orderByCols"
123+
ng-change="ctrl.refresh()"></select>
124+
</div>
125+
<div class="gf-form-select-wrapper">
126+
<select class="gf-form-input gf-size-auto" ng-model="ctrl.target.orderBySort" ng-options="f.value as f.text for f in ctrl.orderBySorts"
127+
ng-change="ctrl.refresh()"></select>
128+
</div>
129+
<div class="gf-form gf-form--grow">
130+
<div class="gf-form-label gf-form-label--grow"></div>
131+
</div>
132+
</div>
133+
<!-- End Order BY. -->
134+
117135
<div class="gf-form-inline">
118136
<div class="gf-form">
119137
<label class="gf-form-label query-keyword">Format as</label>

src/bigquery_query.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export default class BigQueryQuery {
104104
this.tmpValue = "";
105105

106106
target.format = target.format || "time_series";
107+
target.orderByCol = target.orderByCol || "1";
108+
target.orderBySort = target.orderBySort || "1";
107109
target.timeColumn = target.timeColumn || "-- time --";
108110
target.timeColumnType = target.timeColumnType || "TIMESTAMP";
109111
target.metricColumn = target.metricColumn || "none";
@@ -477,9 +479,14 @@ export default class BigQueryQuery {
477479
query += this.buildWhereClause();
478480
query += this.buildGroupClause();
479481
if (!this.isWindow) {
480-
query += "\nORDER BY 1";
482+
let orderBy = "\nORDER BY 1";
481483
if (this.hasMetricColumn()) {
482-
query += ",2";
484+
orderBy =
485+
this.target.orderByCol === "1" ? "\nORDER BY 1,2" : "\nORDER BY 2,1";
486+
}
487+
query += orderBy;
488+
if (this.target.orderBySort === "2") {
489+
query += " DESC";
483490
}
484491
}
485492
// query += '\nLIMIT 2';

src/partials/query.editor.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@
114114
</div>
115115
</div>
116116

117+
<!-- Start Order BY -->
118+
119+
<div class="gf-form">
120+
<label class="gf-form-label query-keyword width-8">ORDER BY</label>
121+
<div class="gf-form-select-wrapper">
122+
<select class="gf-form-input gf-size-auto" ng-model="ctrl.target.orderByCol" ng-options="f.value as f.text for f in ctrl.orderByCols"
123+
ng-change="ctrl.refresh()"></select>
124+
</div>
125+
<div class="gf-form-select-wrapper">
126+
<select class="gf-form-input gf-size-auto" ng-model="ctrl.target.orderBySort" ng-options="f.value as f.text for f in ctrl.orderBySorts"
127+
ng-change="ctrl.refresh()"></select>
128+
</div>
129+
<div class="gf-form gf-form--grow">
130+
<div class="gf-form-label gf-form-label--grow"></div>
131+
</div>
132+
</div>
133+
<!-- End Order BY. -->
134+
117135
<div class="gf-form-inline">
118136
<div class="gf-form">
119137
<label class="gf-form-label query-keyword">Format as</label>

src/query_ctrl.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ WHERE
2121
export class BigQueryQueryCtrl extends QueryCtrl {
2222
public static templateUrl = "partials/query.editor.html";
2323
public formats: any[];
24+
public orderByCols: any[];
25+
public orderBySorts: any[];
2426
public queryModel: BigQueryQuery;
2527
public lastQueryMeta: QueryMeta;
2628
public lastQueryError: string;
@@ -36,6 +38,7 @@ export class BigQueryQueryCtrl extends QueryCtrl {
3638
public selectParts: SqlPart[][];
3739
public groupParts: SqlPart[];
3840
public whereParts: SqlPart[];
41+
public orderParts: SqlPart[];
3942
public groupAdd: any;
4043

4144
/** @ngInject */
@@ -57,6 +60,15 @@ export class BigQueryQueryCtrl extends QueryCtrl {
5760
{ text: "Time series", value: "time_series" },
5861
{ text: "Table", value: "table" }
5962
];
63+
this.orderByCols = [
64+
{ text: "Time", value: "1" },
65+
{ text: "Metric", value: "2" }
66+
];
67+
this.orderBySorts = [
68+
{ text: "ASC", value: "1" },
69+
{ text: "DESC", value: "2" }
70+
];
71+
6072
if (!this.target.rawSql) {
6173
// special handling when in table panel
6274
if (this.panelCtrl.panel.type === "table") {

0 commit comments

Comments
 (0)