From 47bd555fbfe042bd98dd1d8ec404bdf35366ba0f Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Thu, 6 Jul 2023 13:15:49 +0800 Subject: [PATCH] =?UTF-8?q?feature(web):=20=E5=A2=9E=E5=8A=A0=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configuration/entity/AmisResponse.java | 92 ---------- .../web/controller/BaseController.java | 104 ----------- .../web/controller/CloudController.java | 11 +- .../web/controller/FlinkController.java | 7 +- .../web/controller/HudiController.java | 11 +- .../service/web/controller/LogController.java | 9 +- .../web/controller/OverviewController.java | 54 ++---- .../web/controller/PulsarController.java | 5 +- .../web/controller/QueueController.java | 7 +- .../web/controller/RunningController.java | 7 +- .../web/controller/ScheduleController.java | 1 + .../web/controller/TableController.java | 11 +- .../web/controller/VersionController.java | 5 +- .../web/controller/YarnController.java | 15 +- .../web/controller/base/AmisResponse.java | 163 ++++++++++++++++++ .../web/controller/base/BaseController.java | 50 ++++++ .../controller/base/ErrorResponseAdvice.java | 23 +++ 17 files changed, 301 insertions(+), 274 deletions(-) delete mode 100644 service-configuration/src/main/java/com/lanyuanxiaoyao/service/configuration/entity/AmisResponse.java delete mode 100644 service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/BaseController.java create mode 100644 service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/base/AmisResponse.java create mode 100644 service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/base/BaseController.java create mode 100644 service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/base/ErrorResponseAdvice.java diff --git a/service-configuration/src/main/java/com/lanyuanxiaoyao/service/configuration/entity/AmisResponse.java b/service-configuration/src/main/java/com/lanyuanxiaoyao/service/configuration/entity/AmisResponse.java deleted file mode 100644 index 1356669..0000000 --- a/service-configuration/src/main/java/com/lanyuanxiaoyao/service/configuration/entity/AmisResponse.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.lanyuanxiaoyao.service.configuration.entity; - -import java.util.HashMap; -import java.util.Map; - -/** - * Amis 组件结构化返回值 - * - * @author lanyuanxiaoyao - * @date 2022-09-21 - */ -public class AmisResponse { - private Integer status; - private String message; - private Map data; - - public AmisResponse(Builder builder) { - this.status = builder.status; - this.message = builder.message; - this.data = builder.data; - } - - public static Builder builder() { - return new Builder(); - } - - public Integer getStatus() { - return status; - } - - public void setStatus(Integer status) { - this.status = status; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public Map getData() { - return data; - } - - public void setData(Map data) { - this.data = data; - } - - public AmisResponse withData(String key, Object value) { - data.put(key, value); - return this; - } - - @Override - public String toString() { - return "AmisResponse{" + - "status=" + status + - ", message='" + message + '\'' + - ", data=" + data + - '}'; - } - - public static final class Builder { - private Integer status = 0; - private String message = ""; - private Map data = new HashMap<>(); - - private Builder() { - } - - public Builder status(Integer status) { - this.status = status; - return this; - } - - public Builder message(String message) { - this.message = message; - return this; - } - - public Builder data(Map data) { - this.data = data; - return this; - } - - public AmisResponse build() { - return new AmisResponse(this); - } - } -} diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/BaseController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/BaseController.java deleted file mode 100644 index 8cd32ae..0000000 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/BaseController.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.lanyuanxiaoyao.service.web.controller; - -import cn.hutool.core.map.MapUtil; -import cn.hutool.core.util.StrUtil; -import com.lanyuanxiaoyao.service.configuration.Constants; -import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse; -import com.lanyuanxiaoyao.service.configuration.exception.TableNotFoundException; -import com.lanyuanxiaoyao.service.forest.service.InfoService; -import java.util.HashMap; -import java.util.Map; -import org.eclipse.collections.api.factory.Maps; -import org.eclipse.collections.api.map.MutableMap; - -/** - * 放一些 Controller 的辅助方法 - * - * @author lanyuanxiaoyao - * @date 2023-04-21 - */ -public class BaseController { - private static final int SUCCESS_STATUS = 0; - private static final String SUCCESS_MESSAGE = "OK"; - - protected AmisResponse responseData() { - return AmisResponse.builder() - .status(SUCCESS_STATUS) - .message(SUCCESS_MESSAGE) - .data(new HashMap<>()) - .build(); - } - - protected AmisResponse responseData(Map data) { - return AmisResponse.builder() - .status(SUCCESS_STATUS) - .message(SUCCESS_MESSAGE) - .data(data) - .build(); - } - - protected AmisResponse responseDetail(Object detail) { - return AmisResponse.builder() - .status(SUCCESS_STATUS) - .message(SUCCESS_MESSAGE) - .data(MapUtil.builder() - .put("detail", detail) - .build()) - .build(); - } - - protected AmisResponse responseCrudData(Iterable data) { - return AmisResponse.builder() - .status(SUCCESS_STATUS) - .message(SUCCESS_MESSAGE) - .data(MapUtil.builder() - .put("items", data) - .build()) - .build(); - } - - protected AmisResponse responseCrudData(Iterable data, Integer total) { - return responseCrudData(data, Integer.toUnsignedLong(total)); - } - - protected AmisResponse responseCrudData(Iterable data, Long total) { - return AmisResponse.builder() - .status(SUCCESS_STATUS) - .message(SUCCESS_MESSAGE) - .data(MapUtil.builder() - .put("items", data) - .put("total", total) - .build()) - .build(); - } - - protected MutableMap buildQueryMap( - Integer page, - Integer count, - String order, - String direction, - String searchFlinkJobId, - String searchAlias - ) { - MutableMap queryMap = Maps.mutable.empty(); - queryMap.put("page", page); - queryMap.put("count", count); - if (StrUtil.isNotBlank(searchFlinkJobId)) { - queryMap.put("flink_job_id", searchFlinkJobId); - } - if (StrUtil.isNotBlank(searchAlias)) { - queryMap.put("alias", searchAlias); - } - if (StrUtil.isNotBlank(order) && StrUtil.isNotBlank(direction)) { - queryMap.put("order", order); - queryMap.put("direction", direction); - } - return queryMap; - } - - protected void checkTableExists(InfoService infoService, Long flinkJobId, String alias) throws TableNotFoundException { - if (infoService.nonExistsTable(flinkJobId, alias)) { - throw new TableNotFoundException(Constants.SERVICE_NAME_INFO_QUERY, flinkJobId, alias); - } - } -} diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/CloudController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/CloudController.java index ed93d50..4d9a2bb 100644 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/CloudController.java +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/CloudController.java @@ -3,7 +3,8 @@ package com.lanyuanxiaoyao.service.web.controller; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; -import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse; +import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse; +import com.lanyuanxiaoyao.service.web.controller.base.BaseController; import com.lanyuanxiaoyao.service.web.entity.CloudServiceVO; import com.netflix.appinfo.InstanceInfo; import java.util.Map; @@ -82,13 +83,13 @@ public class CloudController extends BaseController { @GetMapping("list") public AmisResponse list() { ImmutableList serviceVOS = serviceVOS(CloudServiceVO.Service::getServiceId); - return responseCrudData(serviceVOS, serviceVOS.size()); + return AmisResponse.responseCrudData(serviceVOS, serviceVOS.size()); } @GetMapping("list_ip") public AmisResponse listIp() { ImmutableList serviceVOS = serviceVOS(CloudServiceVO.Service::getHost); - return responseCrudData(serviceVOS, serviceVOS.size()); + return AmisResponse.responseCrudData(serviceVOS, serviceVOS.size()); } @GetMapping("deploy_plan") @@ -121,7 +122,7 @@ public class CloudController extends BaseController { .put("tpl", "${" + key + "}") .build(); })); - return responseData( + return AmisResponse.responseData( MapUtil.builder() .put("type", "service") .put("data", MapUtil.builder() @@ -140,6 +141,6 @@ public class CloudController extends BaseController { @GetMapping("heart") public AmisResponse heart() { - return responseData().withData("status", true); + return AmisResponse.responseData().withData("status", true); } } diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/FlinkController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/FlinkController.java index 52b0274..ce09ddc 100644 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/FlinkController.java +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/FlinkController.java @@ -2,9 +2,10 @@ package com.lanyuanxiaoyao.service.web.controller; import cn.hutool.core.util.StrUtil; import com.lanyuanxiaoyao.service.configuration.ExecutorProvider; -import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse; +import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse; import com.lanyuanxiaoyao.service.configuration.entity.flink.FlinkCheckpoint; import com.lanyuanxiaoyao.service.forest.service.FlinkService; +import com.lanyuanxiaoyao.service.web.controller.base.BaseController; import com.lanyuanxiaoyao.service.web.entity.FlinkVertexVO; import java.util.concurrent.ExecutionException; import org.eclipse.collections.api.list.ImmutableList; @@ -35,7 +36,7 @@ public class FlinkController extends BaseController { @GetMapping("overview") public AmisResponse overview(@RequestParam("url") String url) throws ExecutionException, InterruptedException { - return responseDetail(flinkService.overview(url)); + return AmisResponse.responseDetail(flinkService.overview(url)); } @GetMapping("jobs") @@ -75,6 +76,6 @@ public class FlinkController extends BaseController { }) .toSortedListBy(FlinkVertexVO::getName) .toImmutable(); - return responseCrudData(vertexVOS, vertexVOS.size()); + return AmisResponse.responseCrudData(vertexVOS, vertexVOS.size()); } } diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/HudiController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/HudiController.java index 48e7f16..5554be2 100644 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/HudiController.java +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/HudiController.java @@ -2,10 +2,11 @@ package com.lanyuanxiaoyao.service.web.controller; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; -import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse; +import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse; import com.lanyuanxiaoyao.service.configuration.entity.PageResponse; import com.lanyuanxiaoyao.service.configuration.entity.hudi.HudiInstant; import com.lanyuanxiaoyao.service.forest.service.HudiService; +import com.lanyuanxiaoyao.service.web.controller.base.BaseController; import java.util.List; import java.util.function.Function; import org.eclipse.collections.api.factory.Maps; @@ -64,7 +65,7 @@ public class HudiController extends BaseController { queryMap.put("filter_state", filterState); } PageResponse response = hudiService.timelineList(queryMap); - return responseCrudData(response.getData(), response.getTotal()); + return AmisResponse.responseCrudData(response.getData(), response.getTotal()); } @GetMapping("/timeline/list_hdfs") @@ -98,7 +99,7 @@ public class HudiController extends BaseController { queryMap.put("filter_state", filterState); } PageResponse response = hudiService.timelineHdfsList(queryMap); - return responseCrudData(response.getData(), response.getTotal()); + return AmisResponse.responseCrudData(response.getData(), response.getTotal()); } @GetMapping("read_compaction_plan") @@ -109,9 +110,9 @@ public class HudiController extends BaseController { @RequestParam("instant") String instant ) throws Exception { if (StrUtil.isNotBlank(hdfs)) { - return responseDetail(hudiService.readCompactionPlanHdfs(hdfs, instant)); + return AmisResponse.responseDetail(hudiService.readCompactionPlanHdfs(hdfs, instant)); } else if (ObjectUtil.isNotNull(flinkJobId) && StrUtil.isNotBlank(alias)) { - return responseDetail(hudiService.readCompactionPlan(flinkJobId, alias, instant)); + return AmisResponse.responseDetail(hudiService.readCompactionPlan(flinkJobId, alias, instant)); } throw new Exception("Flink job id and alias or hdfs cannot be blank"); } diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/LogController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/LogController.java index 4efebca..2c6a395 100644 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/LogController.java +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/LogController.java @@ -1,9 +1,10 @@ package com.lanyuanxiaoyao.service.web.controller; import cn.hutool.core.map.MapUtil; -import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse; +import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse; import com.lanyuanxiaoyao.service.configuration.entity.loki.LokiLogLine; import com.lanyuanxiaoyao.service.forest.service.LokiService; +import com.lanyuanxiaoyao.service.web.controller.base.BaseController; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; @@ -37,7 +38,7 @@ public class LogController extends BaseController { @GetMapping("query_sync_log") public AmisResponse querySyncLog(@RequestParam("flink_job_id") Long flinkJobId) { - return responseDetail(lokiService.queryRange(MapUtil.builder() + return AmisResponse.responseDetail(lokiService.queryRange(MapUtil.builder() .put("app", "hudi-sync") .put("run_type", "sync") .put("flink_job_id", flinkJobId.toString()) @@ -50,7 +51,7 @@ public class LogController extends BaseController { @GetMapping("query_compaction_log") public AmisResponse queryCompactionLog(@RequestParam("flink_job_id") Long flinkJobId, @RequestParam("alias") String alias) { - return responseDetail(lokiService.queryRange(MapUtil.builder() + return AmisResponse.responseDetail(lokiService.queryRange(MapUtil.builder() .put("app", "hudi-sync") .put("run_type", "compaction") .put("flink_job_id", flinkJobId.toString()) @@ -64,7 +65,7 @@ public class LogController extends BaseController { @GetMapping("query_application_log") public AmisResponse queryApplicationLog(@RequestParam("application_id") String applicationId) { - return responseDetail(lokiService.queryRange(MapUtil.builder() + return AmisResponse.responseDetail(lokiService.queryRange(MapUtil.builder() .put("app", "hudi-sync") .put("app_id", applicationId) .build()) diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/OverviewController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/OverviewController.java index 2caa873..a53ba8b 100644 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/OverviewController.java +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/OverviewController.java @@ -1,9 +1,7 @@ package com.lanyuanxiaoyao.service.web.controller; -import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.StrUtil; import com.lanyuanxiaoyao.service.configuration.ExecutorProvider; -import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse; import com.lanyuanxiaoyao.service.configuration.entity.info.JobIdAndAlias; import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnApplication; import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnRootQueue; @@ -11,16 +9,15 @@ import com.lanyuanxiaoyao.service.forest.service.InfoService; import com.lanyuanxiaoyao.service.forest.service.QueueService; import com.lanyuanxiaoyao.service.forest.service.ScheduleService; import com.lanyuanxiaoyao.service.forest.service.YarnService; +import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse; +import com.lanyuanxiaoyao.service.web.controller.base.BaseController; import com.lanyuanxiaoyao.service.web.entity.JobIdAndAliasVO; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; -import org.eclipse.collections.api.factory.Lists; import org.eclipse.collections.api.factory.Maps; import org.eclipse.collections.api.list.ImmutableList; -import org.eclipse.collections.api.map.ImmutableMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; @@ -68,7 +65,7 @@ public class OverviewController extends BaseController { hiveCountFuture, hiveFocusCountFuture ).get(); - return responseData() + return AmisResponse.responseData() .withData("table_count", tableCountFuture.get()) .withData("table_focus_count", tableFocusCountFuture.get()) .withData("hudi_count", hudiCountFuture.get()) @@ -78,40 +75,21 @@ public class OverviewController extends BaseController { } @GetMapping("yarn-job") - private ImmutableMap yarnOverview(@RequestParam("cluster") String cluster, @RequestParam("search") String text) { + private AmisResponse yarnOverview(@RequestParam("cluster") String cluster, @RequestParam("search") String text) { boolean isSearch = StrUtil.isNotBlank(text); ImmutableList applications = yarnService.jobList(cluster).select(app -> !isSearch || StrUtil.contains(app.getName(), text)); - return Maps.immutable.ofAll(MapUtil.builder() - .put("name", cluster) - .put("total", applications.size()) - .put("running", applications.count(app -> StrUtil.equals(app.getState(), "RUNNING"))) - .put("scheduling", applications.count(app -> StrUtil.equals(app.getState(), "ACCEPTED"))) - .put("failure", applications.count(app -> StrUtil.equals(app.getState(), "FAILED"))) - .build()); - } - - @GetMapping("yarn") - public AmisResponse yarnOverview(@RequestParam("clusters") List clusters, @RequestParam("search") String text) { - ImmutableList> maps = Lists.immutable.ofAll(clusters) - .asParallel(ExecutorProvider.EXECUTORS, 1) - .collect(cluster -> yarnOverview(cluster, text)) - .toList() - .toImmutable(); - long total = maps.sumOfInt(m -> (int) m.get("total")); - long running = maps.sumOfInt(m -> (int) m.get("running")); - long scheduling = maps.sumOfInt(m -> (int) m.get("scheduling")); - long failure = maps.sumOfInt(m -> (int) m.get("failure")); - return responseData() - .withData("total", total) - .withData("running", running) - .withData("scheduling", scheduling) - .withData("failure", failure) - .withData("items", maps); + return AmisResponse.responseData() + .withData("name", cluster) + .withData("total", applications.size()) + .withData("running", applications.count(app -> StrUtil.equals(app.getState(), "RUNNING"))) + .withData("running", applications.count(app -> StrUtil.equals(app.getState(), "RUNNING"))) + .withData("scheduling", applications.count(app -> StrUtil.equals(app.getState(), "ACCEPTED"))) + .withData("failure", applications.count(app -> StrUtil.equals(app.getState(), "FAILED"))); } @GetMapping("yarn-cluster") public AmisResponse yarnClusterOverview(@RequestParam("cluster") String cluster, @RequestParam("queue") String queue) { - AmisResponse response = responseData(); + AmisResponse response = AmisResponse.responseData(); YarnRootQueue root = yarnService.cluster(cluster); response.withData("root", root); if (StrUtil.isNotBlank(queue)) { @@ -125,7 +103,7 @@ public class OverviewController extends BaseController { @GetMapping("queue") public AmisResponse queueOverview(@RequestParam("queue") String queue) { - return responseData() + return AmisResponse.responseData() .withData("size", queueService.size(queue)); } @@ -139,7 +117,7 @@ public class OverviewController extends BaseController { CompletableFuture unScheduledNormalTableCount = CompletableFuture.supplyAsync(() -> infoService.unScheduledNormalTableCount(version), ExecutorProvider.EXECUTORS); CompletableFuture unScheduledFocusTableCount = CompletableFuture.supplyAsync(() -> infoService.unScheduledFocusTableCount(version), ExecutorProvider.EXECUTORS); CompletableFuture.allOf(unReceiveNormalTableCount, unReceiveFocusCount, unScheduledNormalTableCount, unScheduledFocusTableCount).get(); - return responseData() + return AmisResponse.responseData() .withData("version", version) .withData("unReceive", Maps.immutable.of("normal", unReceiveNormalTableCount.get(), "focus", unReceiveFocusCount.get())) .withData("unSchedule", Maps.immutable.of("normal", unScheduledNormalTableCount.get(), "focus", unScheduledFocusTableCount.get())); @@ -159,11 +137,11 @@ public class OverviewController extends BaseController { } else { throw new Exception("Target not found " + target); } - return responseCrudData(jobIdAndAliases.collect(JobIdAndAliasVO::new)); + return AmisResponse.responseCrudData(jobIdAndAliases.collect(JobIdAndAliasVO::new)); } @GetMapping("schedule_jobs") public AmisResponse scheduleJobs() { - return responseCrudData(scheduleService.scheduleJobs()); + return AmisResponse.responseCrudData(scheduleService.scheduleJobs()); } } diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/PulsarController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/PulsarController.java index 60a5af0..9747f38 100644 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/PulsarController.java +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/PulsarController.java @@ -1,7 +1,8 @@ package com.lanyuanxiaoyao.service.web.controller; -import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse; +import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse; import com.lanyuanxiaoyao.service.forest.service.PulsarService; +import com.lanyuanxiaoyao.service.web.controller.base.BaseController; import com.lanyuanxiaoyao.service.web.entity.TopicVO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,6 +31,6 @@ public class PulsarController extends BaseController { @GetMapping("topic") public AmisResponse topic(@RequestParam("pulsar_url") String pulsarUrl, @RequestParam("topic") String topic) { - return responseDetail(new TopicVO(pulsarService.topic(pulsarService.name(pulsarUrl), topic))); + return AmisResponse.responseDetail(new TopicVO(pulsarService.topic(pulsarService.name(pulsarUrl), topic))); } } diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/QueueController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/QueueController.java index 0b34da1..29e097f 100644 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/QueueController.java +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/QueueController.java @@ -1,10 +1,9 @@ package com.lanyuanxiaoyao.service.web.controller; -import com.eshore.odcp.hudi.connector.entity.compaction.ScheduleJob; import com.lanyuanxiaoyao.micro.service.entity.queue.QueueItem; -import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse; +import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse; import com.lanyuanxiaoyao.service.forest.service.QueueService; -import java.util.Comparator; +import com.lanyuanxiaoyao.service.web.controller.base.BaseController; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; @@ -32,6 +31,6 @@ public class QueueController extends BaseController { @GetMapping("all") public AmisResponse all(@RequestParam("name") String name) { - return responseCrudData(queueService.all(name).toSortedList(QueueItem::compareTo)); + return AmisResponse.responseCrudData(queueService.all(name).toSortedList(QueueItem::compareTo)); } } diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/RunningController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/RunningController.java index e822a2c..11189d5 100644 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/RunningController.java +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/RunningController.java @@ -5,9 +5,10 @@ import com.eshore.odcp.hudi.connector.utils.NameHelper; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.lanyuanxiaoyao.service.configuration.ExecutorProvider; -import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse; +import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse; import com.lanyuanxiaoyao.service.configuration.entity.zookeeper.ZookeeperNode; import com.lanyuanxiaoyao.service.forest.service.ZookeeperService; +import com.lanyuanxiaoyao.service.web.controller.base.BaseController; import com.lanyuanxiaoyao.service.web.entity.ZookeeperNodeVO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,7 +39,7 @@ public class RunningController extends BaseController { @GetMapping("sync") public AmisResponse sync() { - return responseCrudData( + return AmisResponse.responseCrudData( zookeeperService.getChildren(NameHelper.ZK_SYNC_RUNNING_LOCK_PATH) .asParallel(ExecutorProvider.EXECUTORS, 1) .collect(this::parseRunMeta) @@ -50,7 +51,7 @@ public class RunningController extends BaseController { @GetMapping("compaction") public AmisResponse compaction() { - return responseCrudData( + return AmisResponse.responseCrudData( zookeeperService.getChildren(NameHelper.ZK_COMPACTION_RUNNING_LOCK_PATH) .asParallel(ExecutorProvider.EXECUTORS, 1) .collect(this::parseRunMeta) diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/ScheduleController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/ScheduleController.java index f8271ff..038299e 100644 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/ScheduleController.java +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/ScheduleController.java @@ -6,6 +6,7 @@ import com.lanyuanxiaoyao.service.configuration.entity.info.JobIdAndAlias; import com.lanyuanxiaoyao.service.configuration.exception.TableNotFoundException; import com.lanyuanxiaoyao.service.forest.service.InfoService; import com.lanyuanxiaoyao.service.forest.service.ScheduleService; +import com.lanyuanxiaoyao.service.web.controller.base.BaseController; import java.util.regex.Pattern; import org.eclipse.collections.api.factory.Lists; import org.eclipse.collections.api.list.ImmutableList; diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/TableController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/TableController.java index 9eaadc0..fcdeb9b 100644 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/TableController.java +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/TableController.java @@ -9,12 +9,13 @@ import com.eshore.odcp.hudi.connector.entity.TableMeta; import com.eshore.odcp.hudi.connector.utils.NameHelper; import com.fasterxml.jackson.databind.ObjectMapper; import com.lanyuanxiaoyao.service.configuration.ExecutorProvider; -import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse; +import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse; import com.lanyuanxiaoyao.service.configuration.entity.PageResponse; import com.lanyuanxiaoyao.service.configuration.entity.info.CompactionMetrics; import com.lanyuanxiaoyao.service.configuration.entity.info.JobIdAndAlias; import com.lanyuanxiaoyao.service.forest.service.InfoService; import com.lanyuanxiaoyao.service.forest.service.ZookeeperService; +import com.lanyuanxiaoyao.service.web.controller.base.BaseController; import com.lanyuanxiaoyao.service.web.entity.CompactionMetricsVO; import com.lanyuanxiaoyao.service.web.entity.FlinkJobVO; import com.lanyuanxiaoyao.service.web.entity.SyncStateVO; @@ -137,7 +138,7 @@ public class TableController extends BaseController { .reject(ObjectUtil::isNull) .toList() .toImmutable(); - return responseCrudData(tableVOS, tableVOS.size() == 0 ? 0 : total); + return AmisResponse.responseCrudData(tableVOS, tableVOS.size() == 0 ? 0 : total); } @GetMapping("list_metas") @@ -145,7 +146,7 @@ public class TableController extends BaseController { if (ObjectUtil.isNull(flinkJobId)) { throw new Exception("flink job id is null"); } - return responseCrudData(infoService.tableMetaList(flinkJobId).collect(meta -> new TableVO(null, meta, null, null, null, null, null))); + return AmisResponse.responseCrudData(infoService.tableMetaList(flinkJobId).collect(meta -> new TableVO(null, meta, null, null, null, null, null))); } @GetMapping("detail") @@ -156,7 +157,7 @@ public class TableController extends BaseController { CompletableFuture flinkJobFuture = CompletableFuture.supplyAsync(() -> infoService.flinkJobDetail(flinkJobId), ExecutorProvider.EXECUTORS); CompletableFuture tableMetaFuture = CompletableFuture.supplyAsync(() -> infoService.tableMetaDetail(flinkJobId, alias), ExecutorProvider.EXECUTORS); CompletableFuture.allOf(flinkJobFuture, tableMetaFuture).get(); - return responseData() + return AmisResponse.responseData() .withData("flinkJob", new FlinkJobVO(flinkJobFuture.get())) .withData("tableMeta", tableMetaFuture.get()); } @@ -180,6 +181,6 @@ public class TableController extends BaseController { } PageResponse pageResponse = infoService.compactionMetrics(queryMap); ImmutableList vos = Lists.immutable.ofAll(pageResponse.getData()).collect(CompactionMetricsVO::new); - return responseCrudData(vos, pageResponse.getTotal()); + return AmisResponse.responseCrudData(vos, pageResponse.getTotal()); } } diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/VersionController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/VersionController.java index 0c5f447..52669b7 100644 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/VersionController.java +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/VersionController.java @@ -5,10 +5,11 @@ import cn.hutool.core.util.StrUtil; import com.eshore.odcp.hudi.connector.entity.FlinkJob; import com.eshore.odcp.hudi.connector.entity.TableMeta; import com.lanyuanxiaoyao.service.configuration.ExecutorProvider; -import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse; +import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse; import com.lanyuanxiaoyao.service.configuration.entity.PageResponse; import com.lanyuanxiaoyao.service.configuration.entity.info.VersionUpdated; import com.lanyuanxiaoyao.service.forest.service.InfoService; +import com.lanyuanxiaoyao.service.web.controller.base.BaseController; import com.lanyuanxiaoyao.service.web.entity.VersionUpdateVO; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -85,7 +86,7 @@ public class VersionController extends BaseController { .reject(ObjectUtil::isNull) .toList() .toImmutable(); - return responseCrudData(vos, total) + return AmisResponse.responseCrudData(vos, total) .withData("scheduled", pageResponse.getMetadata().get("scheduled")) .withData("unScheduled", pageResponse.getMetadata().get("unScheduled")); } diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/YarnController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/YarnController.java index 676f5f5..60b8a03 100644 --- a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/YarnController.java +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/YarnController.java @@ -4,7 +4,7 @@ import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.lanyuanxiaoyao.service.configuration.ExecutorProvider; -import com.lanyuanxiaoyao.service.configuration.entity.AmisResponse; +import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse; import com.lanyuanxiaoyao.service.configuration.entity.flink.FlinkVertex; import com.lanyuanxiaoyao.service.configuration.entity.flink.FlinkVertexOverview; import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnApplication; @@ -12,6 +12,7 @@ import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnQueue; import com.lanyuanxiaoyao.service.configuration.entity.yarn.YarnRootQueue; import com.lanyuanxiaoyao.service.forest.service.FlinkService; import com.lanyuanxiaoyao.service.forest.service.YarnService; +import com.lanyuanxiaoyao.service.web.controller.base.BaseController; import com.lanyuanxiaoyao.service.web.entity.YarnApplicationVO; import com.lanyuanxiaoyao.service.web.entity.YarnClusterVO; import com.lanyuanxiaoyao.service.configuration.utils.ComparatorUtil; @@ -118,7 +119,7 @@ public class YarnController extends BaseController { }) .toList() .toImmutable(); - return responseCrudData(result, applications.size()) + return AmisResponse.responseCrudData(result, applications.size()) .withData("running", running) .withData("unRunning", unRunning); } @@ -132,11 +133,11 @@ public class YarnController extends BaseController { .toSortedList(ComparatorUtil.longComparator("startedTime", ComparatorUtil.DESC, SORT_MAP)) .getFirstOptional(); if (currentApp.isPresent()) { - return responseData() + return AmisResponse.responseData() .withData("hasCurrent", true) .withData("current", currentApp.get()); } else { - return responseData().withData("hasCurrent", false); + return AmisResponse.responseData().withData("hasCurrent", false); } } @@ -153,7 +154,7 @@ public class YarnController extends BaseController { }) .toList() .toImmutable(); - return responseCrudData(results, results.size()); + return AmisResponse.responseCrudData(results, results.size()); } @GetMapping("queue_names") @@ -164,7 +165,7 @@ public class YarnController extends BaseController { .collect(YarnQueue::getQueueName) .toList() .toImmutable(); - return responseData(MapUtil.of("queueNames", names)); + return AmisResponse.responseData(MapUtil.of("queueNames", names)); } @GetMapping("clusters") @@ -174,6 +175,6 @@ public class YarnController extends BaseController { .collect(yarnService::cluster) .toList() .toImmutable(); - return responseData(MapUtil.of("cluster", clusters)); + return AmisResponse.responseData(MapUtil.of("cluster", clusters)); } } diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/base/AmisResponse.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/base/AmisResponse.java new file mode 100644 index 0000000..eac9f48 --- /dev/null +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/base/AmisResponse.java @@ -0,0 +1,163 @@ +package com.lanyuanxiaoyao.service.web.controller.base; + +import cn.hutool.core.map.MapUtil; +import java.util.HashMap; +import java.util.Map; + +/** + * Amis 组件结构化返回值 + * + * @author lanyuanxiaoyao + * @date 2022-09-21 + */ +public class AmisResponse { + private Integer status; + private String message; + private Map data; + + public AmisResponse(Builder builder) { + this.status = builder.status; + this.message = builder.message; + this.data = builder.data; + } + + public static Builder builder() { + return new Builder(); + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Map getData() { + return data; + } + + public void setData(Map data) { + this.data = data; + } + + public AmisResponse withData(String key, Object value) { + data.put(key, value); + return this; + } + + @Override + public String toString() { + return "AmisResponse{" + + "status=" + status + + ", message='" + message + '\'' + + ", data=" + data + + '}'; + } + + public static final class Builder { + private Integer status = 0; + private String message = ""; + private Map data = null; + + private Builder() { + } + + public Builder status(Integer status) { + this.status = status; + return this; + } + + public Builder message(String message) { + this.message = message; + return this; + } + + public Builder data(Map data) { + this.data = data; + return this; + } + + public AmisResponse build() { + return new AmisResponse(this); + } + } + + private static final int SUCCESS_STATUS = 0; + private static final int ERROR_STATUS = 500; + private static final String SUCCESS_MESSAGE = "OK"; + private static final String ERROR_MESSAGE = "ERROR"; + + public static AmisResponse responseError() { + return AmisResponse.builder() + .status(ERROR_STATUS) + .message(ERROR_MESSAGE) + .build(); + } + + public static AmisResponse responseError(String message) { + return AmisResponse.builder() + .status(ERROR_STATUS) + .message(message) + .build(); + } + + public static AmisResponse responseData() { + return AmisResponse.builder() + .status(SUCCESS_STATUS) + .message(SUCCESS_MESSAGE) + .data(new HashMap<>()) + .build(); + } + + public static AmisResponse responseData(Map data) { + return AmisResponse.builder() + .status(SUCCESS_STATUS) + .message(SUCCESS_MESSAGE) + .data(data) + .build(); + } + + public static AmisResponse responseDetail(Object detail) { + return AmisResponse.builder() + .status(SUCCESS_STATUS) + .message(SUCCESS_MESSAGE) + .data(MapUtil.builder() + .put("detail", detail) + .build()) + .build(); + } + + public static AmisResponse responseCrudData(Iterable data) { + return AmisResponse.builder() + .status(SUCCESS_STATUS) + .message(SUCCESS_MESSAGE) + .data(MapUtil.builder() + .put("items", data) + .build()) + .build(); + } + + public static AmisResponse responseCrudData(Iterable data, Integer total) { + return responseCrudData(data, Integer.toUnsignedLong(total)); + } + + public static AmisResponse responseCrudData(Iterable data, Long total) { + return AmisResponse.builder() + .status(SUCCESS_STATUS) + .message(SUCCESS_MESSAGE) + .data(MapUtil.builder() + .put("items", data) + .put("total", total) + .build()) + .build(); + } +} diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/base/BaseController.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/base/BaseController.java new file mode 100644 index 0000000..60513de --- /dev/null +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/base/BaseController.java @@ -0,0 +1,50 @@ +package com.lanyuanxiaoyao.service.web.controller.base; + +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.StrUtil; +import com.lanyuanxiaoyao.service.configuration.Constants; +import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse; +import com.lanyuanxiaoyao.service.configuration.exception.TableNotFoundException; +import com.lanyuanxiaoyao.service.forest.service.InfoService; +import java.util.HashMap; +import java.util.Map; +import org.eclipse.collections.api.factory.Maps; +import org.eclipse.collections.api.map.MutableMap; + +/** + * 放一些 Controller 的辅助方法 + * + * @author lanyuanxiaoyao + * @date 2023-04-21 + */ +public class BaseController { + protected MutableMap buildQueryMap( + Integer page, + Integer count, + String order, + String direction, + String searchFlinkJobId, + String searchAlias + ) { + MutableMap queryMap = Maps.mutable.empty(); + queryMap.put("page", page); + queryMap.put("count", count); + if (StrUtil.isNotBlank(searchFlinkJobId)) { + queryMap.put("flink_job_id", searchFlinkJobId); + } + if (StrUtil.isNotBlank(searchAlias)) { + queryMap.put("alias", searchAlias); + } + if (StrUtil.isNotBlank(order) && StrUtil.isNotBlank(direction)) { + queryMap.put("order", order); + queryMap.put("direction", direction); + } + return queryMap; + } + + protected void checkTableExists(InfoService infoService, Long flinkJobId, String alias) throws TableNotFoundException { + if (infoService.nonExistsTable(flinkJobId, alias)) { + throw new TableNotFoundException(Constants.SERVICE_NAME_INFO_QUERY, flinkJobId, alias); + } + } +} diff --git a/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/base/ErrorResponseAdvice.java b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/base/ErrorResponseAdvice.java new file mode 100644 index 0000000..d8309c3 --- /dev/null +++ b/service-web/src/main/java/com/lanyuanxiaoyao/service/web/controller/base/ErrorResponseAdvice.java @@ -0,0 +1,23 @@ +package com.lanyuanxiaoyao.service.web.controller.base; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.http.HttpStatus; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * 全局返回结果类 + * + * @author lanyuanxiaoyao + * @date 2023-07-06 + */ +@RestControllerAdvice +public class ErrorResponseAdvice { + @ExceptionHandler(Exception.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public String exception(Exception exception) { + return exception.getMessage(); + } +}