Skip to content

Commit ef4872c

Browse files
authored
Add a download button in the query UI (StarRocks#22153)
Signed-off-by: Astralidea <[email protected]>
1 parent 6d7cc91 commit ef4872c

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

fe/fe-core/src/main/java/com/starrocks/http/action/QueryAction.java

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void executeGet(BaseRequest request, BaseResponse response) {
7171
// Note: we do not show 'Query ID' column in web page
7272
private void addFinishedQueryInfo(StringBuilder buffer) {
7373
buffer.append("<h2>Finished Queries</h2>");
74+
buffer.append("<button style=\"float:right\" onclick=\"download_query()\">Download CSV</button>");
7475
buffer.append("<p>This table lists the latest 100 queries</p>");
7576

7677
List<List<String>> finishedQueries = ProfileManager.getInstance().getAllQueries();

fe/fe-core/src/main/java/com/starrocks/http/action/WebBaseAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public class WebBaseAction extends BaseAction {
8888
+ " <script type=\"text/javascript\" src=\"/static?res=jquery.js\"></script>"
8989
+ " <script type=\"text/javascript\" src=\"/static?res=jquery.dataTables.js\"></script>"
9090
+ " <script type=\"text/javascript\" src=\"/static?res=datatables_bootstrap.js\"></script>"
91-
91+
+ " <script type=\"text/javascript\" src=\"/static?res=starrocks.js\"></script>"
9292
+ " <script type=\"text/javascript\"> "
9393
+ " $(document).ready(function() { "
9494
+ " $('#table_id').dataTable({ "

webroot/static/starrocks.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
function download_query() {
19+
let tableThs = Array.from($('table th')).map(v=>{return v.textContent})
20+
let tableTds = Array.from($('table td')).map(v=>{return v.textContent})
21+
let combine = tableThs.concat(tableTds)
22+
let csv = ''
23+
let colWidth = 9
24+
let column = []
25+
combine.map((v,i)=>{
26+
column.push(v.trim())
27+
if((i+1)%colWidth === 0){
28+
csv += column.join(",")+'\n'
29+
column = []
30+
}
31+
})
32+
let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(csv);
33+
let link = document.createElement("a");
34+
let date = new Date().toISOString().replace(/\D/g, '').slice(0, -3)
35+
link.href = uri;
36+
link.download = "query_" + date + ".csv";
37+
document.body.appendChild(link);
38+
link.click();
39+
document.body.removeChild(link);
40+
}

0 commit comments

Comments
 (0)