Skip to content

Commit 2f0008c

Browse files
AnsjAnsj
authored andcommitted
Add explain button for NLPchina#31
1 parent a460feb commit 2f0008c

File tree

4 files changed

+76
-15
lines changed

4 files changed

+76
-15
lines changed

src/_site/controllers.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce)
66
$scope.resultsColumns = [];
77
$scope.resultsRows = [];
88
$scope.loading = false;
9+
$scope.resultExplan = false;
10+
11+
912

1013
$scope.search = function() {
1114
// Reset results and error box
@@ -14,6 +17,7 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce)
1417
$scope.resultsRows = [];
1518
$scope.loading = true;
1619
$scope.$apply();
20+
$scope.resultExplan = false;
1721

1822
saveUrl()
1923

@@ -39,10 +43,42 @@ elasticsearchSqlApp.controller('MainController', function ($scope, $http, $sce)
3943
$scope.$apply()
4044
});
4145
}
46+
47+
$scope.explan = function() {
48+
// Reset results and error box
49+
$scope.error = "";
50+
$scope.resultsColumns = [];
51+
$scope.resultsRows = [];
52+
$scope.loading = true;
53+
$scope.$apply();
54+
$scope.resultExplan = true;
55+
56+
saveUrl()
57+
58+
var query = window.editor.getValue();
59+
$http.post($scope.url + "_sql/_explain", query)
60+
.success(function(data, status, headers, config) {
61+
$scope.resultExplan = true;
62+
window.explanResult.setValue(JSON.stringify(data, null, "\t"));
63+
})
64+
.error(function(data, status, headers, config) {
65+
$scope.resultExplan = false;
66+
if(data == "") {
67+
$scope.error = "Error occured! response is not avalible.";
68+
}
69+
else {
70+
$scope.error = JSON.stringify(data);
71+
}
72+
})
73+
.finally(function() {
74+
$scope.loading = false;
75+
$scope.$apply()
76+
});
77+
}
4278

43-
$scope.getSearchButtonContent = function(isLoading) {
79+
$scope.getButtonContent = function(isLoading , defName) {
4480
var loadingContent = "<span class=\"glyphicon glyphicon-refresh glyphicon-refresh-animate\"></span> Loading...";
45-
var returnValue = isLoading ? loadingContent : "Search";
81+
var returnValue = isLoading ? loadingContent : defName;
4682
return $sce.trustAsHtml(returnValue);
4783
}
4884

src/_site/editor.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
// Create the queryTextarea editor
22
window.onload = function() {
3-
window.editor = CodeMirror.fromTextArea(document.getElementById('queryTextarea'), {
4-
mode: 'text/x-mysql',
5-
indentWithTabs: true,
6-
smartIndent: true,
7-
lineNumbers: true,
8-
matchBrackets : true,
9-
autofocus: true,
10-
extraKeys: {
11-
"Ctrl-Space": "autocomplete",
12-
"Ctrl-Enter": angular.element($("#queryTextarea")).scope().search
13-
}
14-
});
3+
window.editor = CodeMirror.fromTextArea(document.getElementById('queryTextarea'), {
4+
mode : 'text/x-mysql',
5+
indentWithTabs : true,
6+
smartIndent : true,
7+
lineNumbers : true,
8+
matchBrackets : true,
9+
autofocus : true,
10+
extraKeys : {
11+
"Ctrl-Space" : "autocomplete",
12+
"Ctrl-Enter" : angular.element($("#queryTextarea")).scope().search
13+
}
14+
});
15+
16+
window.explanResult = CodeMirror.fromTextArea(document.getElementById('explanResult'), {
17+
mode : 'application/json',
18+
indentWithTabs : true,
19+
smartIndent : true,
20+
lineNumbers : true,
21+
matchBrackets : true
22+
});
1523
};

src/_site/index.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ <h1 class="page-header">SQL Query</h1>
101101
<!-- Query area -->
102102
<div class="search-area">
103103
<textarea id="queryTextarea">SELECT * FROM myindex</textarea>
104-
<button type="button" ng-click="search()" id="searchButton" class="btn btn-success search-button" ng-bind-html="getSearchButtonContent(loading)" ng-cloak></button>
104+
<button type="button" ng-click="search()" id="searchButton" class="btn btn-success search-button" ng-bind-html="getButtonContent(loading,'Search')" ng-cloak></button>
105+
<button type="button" ng-click="explan()" id="explanButton" class="btn btn-success explan-button" ng-bind-html="getButtonContent(loading,'Explan')" ng-cloak></button>
105106
</div>
106107

107108
<!-- Error box, displayed only on errors -->
@@ -132,6 +133,14 @@ <h2 class="sub-header">Results</h2>
132133
</div>
133134
</div>
134135

136+
<!-- explan area , displayed after explan -->
137+
<div class="fadein" ng-show="resultExplan" ng-cloak>
138+
<h2 class="sub-header">Results</h2>
139+
<div class="table-responsive">
140+
<textarea id="explanResult"></textarea>
141+
</div>
142+
</div>
143+
135144
</div>
136145
</div>
137146
</div>
@@ -149,6 +158,7 @@ <h2 class="sub-header">Results</h2>
149158
<script src="vendor/codemirror/lib/codemirror.js"></script>
150159
<link rel="stylesheet" href="vendor/codemirror/lib/codemirror.css">
151160
<script src="vendor/codemirror/mode/sql/sql.js"></script>
161+
<script src="vendor/codemirror/mode/javascript/javascript.js"></script>
152162
<link rel="stylesheet" href="vendor/codemirror/addon/hint/show-hint.css" />
153163
<script src="vendor/codemirror/addon/hint/show-hint.js"></script>
154164
<script src="vendor/codemirror/addon/hint/sql-hint.js"></script>

src/_site/style.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,19 @@ body {
109109
margin-bottom: 50px;
110110
}
111111

112+
112113
.search-button {
113114
float:right;
114115
margin-right: 20%;
115116
margin-top: 10px;
116117
}
117118

119+
.explan-button {
120+
float:right;
121+
margin-right: 25px;
122+
margin-top: 10px;
123+
}
124+
118125

119126
.keyboardSection {
120127
margin-top:35px;

0 commit comments

Comments
 (0)