feature(web): 增加统一错误信息处理

This commit is contained in:
2023-07-06 13:15:49 +08:00
parent e47de5d5c6
commit 47bd555fbf
17 changed files with 301 additions and 274 deletions

View File

@@ -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<String, Object> 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<String, Object> getData() {
return data;
}
public void setData(Map<String, Object> 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<String, Object> 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<String, Object> data) {
this.data = data;
return this;
}
public AmisResponse build() {
return new AmisResponse(this);
}
}
}

View File

@@ -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<String, Object> 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.<String, Object>builder()
.put("detail", detail)
.build())
.build();
}
protected AmisResponse responseCrudData(Iterable<?> data) {
return AmisResponse.builder()
.status(SUCCESS_STATUS)
.message(SUCCESS_MESSAGE)
.data(MapUtil.<String, Object>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.<String, Object>builder()
.put("items", data)
.put("total", total)
.build())
.build();
}
protected MutableMap<String, Object> buildQueryMap(
Integer page,
Integer count,
String order,
String direction,
String searchFlinkJobId,
String searchAlias
) {
MutableMap<String, Object> 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);
}
}
}

View File

@@ -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<CloudServiceVO> serviceVOS = serviceVOS(CloudServiceVO.Service::getServiceId);
return responseCrudData(serviceVOS, serviceVOS.size());
return AmisResponse.responseCrudData(serviceVOS, serviceVOS.size());
}
@GetMapping("list_ip")
public AmisResponse listIp() {
ImmutableList<CloudServiceVO> 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.<String, Object>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);
}
}

View File

@@ -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());
}
}

View File

@@ -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<HudiInstant> 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<HudiInstant> 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");
}

View File

@@ -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.<String, String>builder()
return AmisResponse.responseDetail(lokiService.queryRange(MapUtil.<String, String>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.<String, String>builder()
return AmisResponse.responseDetail(lokiService.queryRange(MapUtil.<String, String>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.<String, String>builder()
return AmisResponse.responseDetail(lokiService.queryRange(MapUtil.<String, String>builder()
.put("app", "hudi-sync")
.put("app_id", applicationId)
.build())

View File

@@ -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<String, Object> 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<YarnApplication> applications = yarnService.jobList(cluster).select(app -> !isSearch || StrUtil.contains(app.getName(), text));
return Maps.immutable.ofAll(MapUtil.<String, Object>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<String> clusters, @RequestParam("search") String text) {
ImmutableList<ImmutableMap<String, Object>> 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<Long> unScheduledNormalTableCount = CompletableFuture.supplyAsync(() -> infoService.unScheduledNormalTableCount(version), ExecutorProvider.EXECUTORS);
CompletableFuture<Long> 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());
}
}

View File

@@ -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)));
}
}

View File

@@ -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));
}
}

View File

@@ -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)

View File

@@ -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;

View File

@@ -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<FlinkJob> flinkJobFuture = CompletableFuture.supplyAsync(() -> infoService.flinkJobDetail(flinkJobId), ExecutorProvider.EXECUTORS);
CompletableFuture<TableMeta> 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<CompactionMetrics> pageResponse = infoService.compactionMetrics(queryMap);
ImmutableList<CompactionMetricsVO> vos = Lists.immutable.ofAll(pageResponse.getData()).collect(CompactionMetricsVO::new);
return responseCrudData(vos, pageResponse.getTotal());
return AmisResponse.responseCrudData(vos, pageResponse.getTotal());
}
}

View File

@@ -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"));
}

View File

@@ -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));
}
}

View File

@@ -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<String, Object> 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<String, Object> getData() {
return data;
}
public void setData(Map<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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.<String, Object>builder()
.put("detail", detail)
.build())
.build();
}
public static AmisResponse responseCrudData(Iterable<?> data) {
return AmisResponse.builder()
.status(SUCCESS_STATUS)
.message(SUCCESS_MESSAGE)
.data(MapUtil.<String, Object>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.<String, Object>builder()
.put("items", data)
.put("total", total)
.build())
.build();
}
}

View File

@@ -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<String, Object> buildQueryMap(
Integer page,
Integer count,
String order,
String direction,
String searchFlinkJobId,
String searchAlias
) {
MutableMap<String, Object> 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);
}
}
}

View File

@@ -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();
}
}