Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix](profile) fix /rest/v1/query_profile action. #15981

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Fix](profile) fix /rest/v1/query_profile action.
  • Loading branch information
wangxiangyu@360shuke.com committed Jan 17, 2023
commit 4bbd59c322095ffe0d68967694cd2c22dca2643f
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
Expand Down Expand Up @@ -85,7 +85,7 @@ public enum ProfileType {
LOAD,
}

public static final ArrayList<String> PROFILE_HEADERS = new ArrayList(
public static final List<String> PROFILE_HEADERS = Collections.unmodifiableList(
Arrays.asList(JOB_ID, QUERY_ID, USER, DEFAULT_DB, SQL_STATEMENT, QUERY_TYPE,
START_TIME, END_TIME, TOTAL_TIME, QUERY_STATE, TRACE_ID));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@
public class QueryProfileController extends BaseController {
private static final Logger LOG = LogManager.getLogger(QueryProfileController.class);

private static final String QUERY_ID = "query_id";
private static final String ID = "id";
private static final String DETAIL_COL = "Detail";

@RequestMapping(path = "/query_profile/{" + QUERY_ID + "}", method = RequestMethod.GET)
public Object profile(@PathVariable(value = QUERY_ID) String queryId) {
String profile = ProfileManager.getInstance().getProfile(queryId);
@RequestMapping(path = "/query_profile/{" + ID + "}", method = RequestMethod.GET)
public Object profile(@PathVariable(value = ID) String id) {
String profile = ProfileManager.getInstance().getProfile(id);
if (profile == null) {
return ResponseEntityBuilder.okWithCommonError("Query " + queryId + " does not exist");
return ResponseEntityBuilder.okWithCommonError("ID " + id + " does not exist");
}
profile = profile.replaceAll("\n", "</br>");
profile = profile.replaceAll(" ", "&nbsp;&nbsp;");
Expand All @@ -65,33 +66,51 @@ public Object query() {

private void addFinishedQueryInfo(Map<String, Object> result) {
List<List<String>> finishedQueries = ProfileManager.getInstance().getAllQueries();
List<String> columnHeaders = ProfileManager.PROFILE_HEADERS;
int queryIdIndex = 0; // the first column is 'Query ID' by default
List<String> columnHeaders = Lists.newLinkedList(ProfileManager.PROFILE_HEADERS);
int jobIdIndex = -1;
int queryIdIndex = -1;
int queryTypeIndex = -1;
for (int i = 0; i < columnHeaders.size(); ++i) {
if (columnHeaders.get(i).equals(ProfileManager.JOB_ID)) {
jobIdIndex = i;
continue;
}
if (columnHeaders.get(i).equals(ProfileManager.QUERY_ID)) {
queryIdIndex = i;
break;
continue;
}
if (columnHeaders.get(i).equals(ProfileManager.QUERY_TYPE)) {
queryTypeIndex = i;
continue;
}
}
// set href as the first column
columnHeaders.add(0, DETAIL_COL);

result.put("column_names", columnHeaders);
result.put("href_column", Lists.newArrayList(ProfileManager.QUERY_ID));
result.put("href_column", Lists.newArrayList(DETAIL_COL));
List<Map<String, Object>> list = Lists.newArrayList();
result.put("rows", list);

for (List<String> row : finishedQueries) {
String queryId = row.get(queryIdIndex);
List<String> realRow = Lists.newLinkedList(row);

String queryType = realRow.get(queryTypeIndex);
String id = "Query".equals(queryType) ? realRow.get(queryIdIndex) : realRow.get(jobIdIndex);

realRow.add(0, id);
Map<String, Object> rowMap = new HashMap<>();
for (int i = 0; i < row.size(); ++i) {
rowMap.put(columnHeaders.get(i), row.get(i));
for (int i = 0; i < realRow.size(); ++i) {
rowMap.put(columnHeaders.get(i), realRow.get(i));
}

// add hyper link
if (Strings.isNullOrEmpty(queryId)) {
if (Strings.isNullOrEmpty(id)) {
rowMap.put("__hrefPaths", Lists.newArrayList("/query_profile/-1"));
} else {
rowMap.put("__hrefPaths", Lists.newArrayList("/query_profile/" + queryId));
rowMap.put("__hrefPaths", Lists.newArrayList("/query_profile/" + id));
}

list.add(rowMap);
}
}
Expand Down