xy720 commented on code in PR #18805:
URL: https://github.com/apache/doris/pull/18805#discussion_r1189507021


##########
fe/fe-core/src/main/java/org/apache/doris/httpv2/rest/QueryStatsAction.java:
##########
@@ -0,0 +1,292 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.httpv2.rest;
+
+import org.apache.doris.catalog.Env;
+import org.apache.doris.cluster.ClusterNamespace;
+import org.apache.doris.datasource.InternalCatalog;
+import org.apache.doris.httpv2.entity.ResponseEntityBuilder;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.system.SystemInfoService;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.BiFunction;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * This class is used to get query stats or clear query stats.
+ */
+@RestController
+public class QueryStatsAction extends RestBaseController {
+    private static final Logger LOG = 
LogManager.getLogger(QueryStatsAction.class);
+
+    @RequestMapping(path = "/api/query_stats/{catalog}", method = 
RequestMethod.GET)
+    protected Object getQueryStatsFromCatalog(@PathVariable("catalog") String 
catalog,
+            @RequestParam(name = "summary", required = false, defaultValue = 
"true") boolean summary,
+            @RequestParam(name = "pretty", required = false, defaultValue = 
"false") boolean pretty,
+            HttpServletRequest request, HttpServletResponse response) {
+        if (pretty && summary) {
+            return ResponseEntityBuilder.badRequest("pretty and summary can 
not be true at the same time");
+        }
+        executeCheckPassword(request, response);
+        checkGlobalAuth(ConnectContext.get().getCurrentUserIdentity(), 
PrivPredicate.ADMIN);
+        // use NS_KEY as catalog, but NS_KEY's default value is 
'default_cluster'.
+        if (catalog.equalsIgnoreCase(SystemInfoService.DEFAULT_CLUSTER)) {
+            catalog = InternalCatalog.INTERNAL_CATALOG_NAME;
+        }
+        try {
+            Map<String, Map> result = 
Env.getCurrentEnv().getQueryStats().getStats(catalog, summary);
+            if (pretty) {
+                return 
ResponseEntityBuilder.ok(getPrettyJson(result.get("detail"), 
QueryStatsType.DATABASE));
+            }
+            return ResponseEntityBuilder.ok(result);
+        } catch (Exception e) {
+            LOG.warn("get query stats from catalog {} failed", catalog, e);
+            return ResponseEntityBuilder.internalError(e.getMessage());
+        }
+    }
+
+    @RequestMapping(path = "/api/query_stats/{catalog}/{database}", method = 
RequestMethod.GET)
+    protected Object getQueryStatsFromDatabase(@PathVariable("catalog") String 
catalog,
+            @PathVariable("database") String database,
+            @RequestParam(name = "summary", required = false, defaultValue = 
"true") boolean summary,
+            @RequestParam(name = "pretty", required = false, defaultValue = 
"false") boolean pretty,
+            HttpServletRequest request, HttpServletResponse response) {
+        if (pretty && summary) {
+            return ResponseEntityBuilder.badRequest("pretty and summary can 
not be true at the same time");
+        }
+        executeCheckPassword(request, response);
+        checkDbAuth(ConnectContext.get().getCurrentUserIdentity(), database, 
PrivPredicate.SHOW);
+        // use NS_KEY as catalog, but NS_KEY's default value is 
'default_cluster'.
+        String clasterName = catalog;
+        if (catalog.equalsIgnoreCase(SystemInfoService.DEFAULT_CLUSTER)) {
+            catalog = InternalCatalog.INTERNAL_CATALOG_NAME;
+        }
+        database = ClusterNamespace.getFullName(clasterName, database);
+        try {
+            Map<String, Map> result = 
Env.getCurrentEnv().getQueryStats().getStats(catalog, database, summary);

Review Comment:
   May be we should unify the action and show stmt in later pr.
   The action should also get merged stat from each FE.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to