Merge branch 'main' of http://www.pqmcc.com:3000/ClientApps/CN_Tool
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -5,6 +5,8 @@
|
|||||||
target/
|
target/
|
||||||
logs/
|
logs/
|
||||||
docs/
|
docs/
|
||||||
|
.codex-tmp/
|
||||||
|
.docs/
|
||||||
|
|
||||||
# Log file
|
# Log file
|
||||||
*.log
|
*.log
|
||||||
|
|||||||
@@ -0,0 +1,193 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.component;
|
||||||
|
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.bo.SteadyChecksquareValuePointBO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareHarmonicParityDetailVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareHarmonicParityRuleVO;
|
||||||
|
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendIndicatorDefinitionBO;
|
||||||
|
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 谐波偶次与局部奇次基线关系规则。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SteadyChecksquareHarmonicParityRuleComponent {
|
||||||
|
|
||||||
|
private static final DateTimeFormatter OUTPUT_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
private static final BigDecimal THRESHOLD_MULTIPLIER = new BigDecimal("2");
|
||||||
|
private static final BigDecimal EVEN_HARMONIC_DEADBAND_VALUE = new BigDecimal("0.1");
|
||||||
|
private static final int MIN_ODD_REFERENCE_COUNT = 2;
|
||||||
|
|
||||||
|
private final SteadyChecksquareInfluxQueryComponent influxQueryComponent;
|
||||||
|
|
||||||
|
public SteadyChecksquareHarmonicParityRuleVO check(String lineId, SteadyTrendIndicatorDefinitionBO indicator,
|
||||||
|
LocalDateTime startTime, LocalDateTime endTime,
|
||||||
|
int intervalMinutes) {
|
||||||
|
SteadyChecksquareHarmonicParityRuleVO result = new SteadyChecksquareHarmonicParityRuleVO();
|
||||||
|
if (!supportHarmonicParityRule(indicator)) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
for (String statType : indicator.getSupportStats()) {
|
||||||
|
for (String phase : indicator.getPhaseCodes()) {
|
||||||
|
Map<Integer, Map<LocalDateTime, BigDecimal>> valueMap = queryOrderValueMap(lineId, indicator, phase,
|
||||||
|
statType, startTime, endTime, intervalMinutes);
|
||||||
|
appendAbnormalDetails(result, phase, statType, indicator, valueMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.setAbnormalPointCount(result.getAbnormalDetails().size());
|
||||||
|
result.setAbnormal(result.getAbnormalPointCount() > 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean supportHarmonicParityRule(SteadyTrendIndicatorDefinitionBO indicator) {
|
||||||
|
return indicator != null && Boolean.TRUE.equals(indicator.getHarmonic())
|
||||||
|
&& indicator.getHarmonicOrderStart() != null && indicator.getHarmonicOrderEnd() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<Integer, Map<LocalDateTime, BigDecimal>> queryOrderValueMap(String lineId,
|
||||||
|
SteadyTrendIndicatorDefinitionBO indicator,
|
||||||
|
String phase, String statType,
|
||||||
|
LocalDateTime startTime,
|
||||||
|
LocalDateTime endTime,
|
||||||
|
int intervalMinutes) {
|
||||||
|
Map<Integer, Map<LocalDateTime, BigDecimal>> result = new LinkedHashMap<Integer, Map<LocalDateTime, BigDecimal>>();
|
||||||
|
List<SteadyTrendResolvedFieldBO> fields = new ArrayList<SteadyTrendResolvedFieldBO>();
|
||||||
|
for (int order = indicator.getHarmonicOrderStart(); order <= indicator.getHarmonicOrderEnd(); order++) {
|
||||||
|
fields.add(buildResolvedField(lineId, indicator, order, phase, statType));
|
||||||
|
}
|
||||||
|
Map<String, List<SteadyChecksquareValuePointBO>> fieldValueMap =
|
||||||
|
influxQueryComponent.queryValuePointMap(fields, startTime, endTime, intervalMinutes);
|
||||||
|
if (fieldValueMap == null) {
|
||||||
|
fieldValueMap = Collections.emptyMap();
|
||||||
|
}
|
||||||
|
for (int order = indicator.getHarmonicOrderStart(); order <= indicator.getHarmonicOrderEnd(); order++) {
|
||||||
|
result.put(order, toValueMap(fieldValueMap.get(indicator.getHarmonicFieldPrefix() + "_" + order)));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendAbnormalDetails(SteadyChecksquareHarmonicParityRuleVO result, String phase, String statType,
|
||||||
|
SteadyTrendIndicatorDefinitionBO indicator,
|
||||||
|
Map<Integer, Map<LocalDateTime, BigDecimal>> valueMap) {
|
||||||
|
for (int order = firstEvenOrder(indicator.getHarmonicOrderStart()); order <= indicator.getHarmonicOrderEnd(); order += 2) {
|
||||||
|
Map<LocalDateTime, BigDecimal> evenValues = valueMap.get(order);
|
||||||
|
if (evenValues == null || evenValues.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (Map.Entry<LocalDateTime, BigDecimal> entry : evenValues.entrySet()) {
|
||||||
|
appendAbnormalDetailIfNecessary(result, phase, statType, order, entry.getKey(), entry.getValue(), valueMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendAbnormalDetailIfNecessary(SteadyChecksquareHarmonicParityRuleVO result, String phase,
|
||||||
|
String statType, int evenOrder, LocalDateTime time,
|
||||||
|
BigDecimal evenValue,
|
||||||
|
Map<Integer, Map<LocalDateTime, BigDecimal>> valueMap) {
|
||||||
|
if (evenValue == null || evenValue.compareTo(EVEN_HARMONIC_DEADBAND_VALUE) <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List<Integer> oddOrders = buildOddReferenceOrders(evenOrder);
|
||||||
|
List<BigDecimal> oddValues = new ArrayList<BigDecimal>();
|
||||||
|
List<Integer> effectiveOddOrders = new ArrayList<Integer>();
|
||||||
|
for (Integer oddOrder : oddOrders) {
|
||||||
|
Map<LocalDateTime, BigDecimal> values = valueMap.get(oddOrder);
|
||||||
|
BigDecimal oddValue = values == null ? null : values.get(time);
|
||||||
|
if (oddValue != null) {
|
||||||
|
effectiveOddOrders.add(oddOrder);
|
||||||
|
oddValues.add(oddValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (oddValues.size() < MIN_ODD_REFERENCE_COUNT) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
BigDecimal median = calculateMedian(oddValues);
|
||||||
|
if (median == null || evenValue.compareTo(median.multiply(THRESHOLD_MULTIPLIER)) <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
result.getAbnormalDetails().add(buildDetail(time, phase, statType, evenOrder, evenValue,
|
||||||
|
effectiveOddOrders, oddValues, median));
|
||||||
|
}
|
||||||
|
|
||||||
|
private SteadyChecksquareHarmonicParityDetailVO buildDetail(LocalDateTime time, String phase, String statType,
|
||||||
|
Integer evenOrder, BigDecimal evenValue,
|
||||||
|
List<Integer> oddOrders, List<BigDecimal> oddValues,
|
||||||
|
BigDecimal median) {
|
||||||
|
SteadyChecksquareHarmonicParityDetailVO detail = new SteadyChecksquareHarmonicParityDetailVO();
|
||||||
|
detail.setTime(OUTPUT_TIME_FORMATTER.format(time));
|
||||||
|
detail.setPhase(phase);
|
||||||
|
detail.setStatType(statType);
|
||||||
|
detail.setEvenHarmonicOrder(evenOrder);
|
||||||
|
detail.setEvenValue(evenValue);
|
||||||
|
detail.setOddHarmonicOrders(new ArrayList<Integer>(oddOrders));
|
||||||
|
detail.setOddValues(new ArrayList<BigDecimal>(oddValues));
|
||||||
|
detail.setOddMedianValue(median);
|
||||||
|
detail.setThresholdMultiplier(THRESHOLD_MULTIPLIER);
|
||||||
|
return detail;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Integer> buildOddReferenceOrders(int evenOrder) {
|
||||||
|
List<Integer> result = new ArrayList<Integer>();
|
||||||
|
result.add(evenOrder - 3);
|
||||||
|
result.add(evenOrder - 1);
|
||||||
|
result.add(evenOrder + 1);
|
||||||
|
result.add(evenOrder + 3);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private BigDecimal calculateMedian(List<BigDecimal> values) {
|
||||||
|
if (values == null || values.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
List<BigDecimal> sorted = new ArrayList<BigDecimal>(values);
|
||||||
|
Collections.sort(sorted, Comparator.naturalOrder());
|
||||||
|
int middleIndex = sorted.size() / 2;
|
||||||
|
if (sorted.size() % 2 == 1) {
|
||||||
|
return sorted.get(middleIndex);
|
||||||
|
}
|
||||||
|
return sorted.get(middleIndex - 1).add(sorted.get(middleIndex)).divide(new BigDecimal("2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private int firstEvenOrder(int startOrder) {
|
||||||
|
return startOrder % 2 == 0 ? startOrder : startOrder + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<LocalDateTime, BigDecimal> toValueMap(List<SteadyChecksquareValuePointBO> points) {
|
||||||
|
Map<LocalDateTime, BigDecimal> result = new LinkedHashMap<LocalDateTime, BigDecimal>();
|
||||||
|
if (points == null || points.isEmpty()) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
for (SteadyChecksquareValuePointBO point : points) {
|
||||||
|
if (point != null && point.getTime() != null && point.getValue() != null) {
|
||||||
|
result.put(point.getTime(), point.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SteadyTrendResolvedFieldBO buildResolvedField(String lineId, SteadyTrendIndicatorDefinitionBO indicator,
|
||||||
|
Integer harmonicOrder, String phase, String statType) {
|
||||||
|
SteadyTrendResolvedFieldBO field = new SteadyTrendResolvedFieldBO();
|
||||||
|
field.setMeasurement(indicator.getTableName());
|
||||||
|
field.setField(indicator.getHarmonicFieldPrefix() + "_" + harmonicOrder);
|
||||||
|
field.setLineId(lineId);
|
||||||
|
field.setIndicatorCode(indicator.getIndicatorCode());
|
||||||
|
field.setIndicatorName(indicator.getName());
|
||||||
|
field.setPhase(phase);
|
||||||
|
field.setStatType(statType);
|
||||||
|
field.setUnit(indicator.getUnit());
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
import com.njcn.common.pojo.exception.BusinessException;
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.bo.SteadyChecksquareValuePointBO;
|
||||||
import com.njcn.gather.steady.datavie.config.SteadyInfluxDbProperties;
|
import com.njcn.gather.steady.datavie.config.SteadyInfluxDbProperties;
|
||||||
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO;
|
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@@ -14,6 +15,7 @@ import java.io.BufferedReader;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@@ -22,7 +24,11 @@ import java.time.LocalDateTime;
|
|||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,28 +41,121 @@ public class SteadyChecksquareInfluxQueryComponent {
|
|||||||
|
|
||||||
private static final DateTimeFormatter INFLUX_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
private static final DateTimeFormatter INFLUX_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||||
|
private static final ThreadLocal<Map<String, List<SteadyChecksquareValuePointBO>>> REQUEST_VALUE_CACHE =
|
||||||
|
new ThreadLocal<Map<String, List<SteadyChecksquareValuePointBO>>>();
|
||||||
|
|
||||||
private final SteadyInfluxDbProperties properties;
|
private final SteadyInfluxDbProperties properties;
|
||||||
|
|
||||||
|
public void enableRequestCache() {
|
||||||
|
REQUEST_VALUE_CACHE.set(new LinkedHashMap<String, List<SteadyChecksquareValuePointBO>>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearRequestCache() {
|
||||||
|
REQUEST_VALUE_CACHE.remove();
|
||||||
|
}
|
||||||
|
|
||||||
public Set<LocalDateTime> queryExistingSlots(SteadyTrendResolvedFieldBO field, LocalDateTime startTime,
|
public Set<LocalDateTime> queryExistingSlots(SteadyTrendResolvedFieldBO field, LocalDateTime startTime,
|
||||||
LocalDateTime endTime, int intervalMinutes) {
|
LocalDateTime endTime, int intervalMinutes) {
|
||||||
|
List<SteadyChecksquareValuePointBO> points = queryValuePoints(field, startTime, endTime, intervalMinutes);
|
||||||
|
Set<LocalDateTime> result = new HashSet<LocalDateTime>();
|
||||||
|
for (SteadyChecksquareValuePointBO point : points) {
|
||||||
|
if (point != null && point.getTime() != null) {
|
||||||
|
result.add(point.getTime());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SteadyChecksquareValuePointBO> queryValuePoints(SteadyTrendResolvedFieldBO field, LocalDateTime startTime,
|
||||||
|
LocalDateTime endTime, int intervalMinutes) {
|
||||||
validateConfig();
|
validateConfig();
|
||||||
String query = buildChecksquareQuery(field, startTime, endTime);
|
String query = buildValuePointQuery(field, startTime, endTime);
|
||||||
|
String cacheKey = buildCacheKey(query, intervalMinutes);
|
||||||
|
Map<String, List<SteadyChecksquareValuePointBO>> cache = REQUEST_VALUE_CACHE.get();
|
||||||
|
if (cache != null && cache.containsKey(cacheKey)) {
|
||||||
|
return new ArrayList<SteadyChecksquareValuePointBO>(cache.get(cacheKey));
|
||||||
|
}
|
||||||
long startMillis = System.currentTimeMillis();
|
long startMillis = System.currentTimeMillis();
|
||||||
log.info("数据校验 InfluxDB 查询开始,measurement={},field={},lineId={},phase={},statType={},query={}",
|
log.info("数据校验指标值 InfluxDB 查询开始,measurement={},field={},lineId={},phase={},statType={},query={}",
|
||||||
field.getMeasurement(), field.getField(), field.getLineId(), field.getPhase(), field.getStatType(), query);
|
field.getMeasurement(), field.getField(), field.getLineId(), field.getPhase(), field.getStatType(), query);
|
||||||
try {
|
try {
|
||||||
String body = executeQuery(query);
|
String body = executeQuery(query);
|
||||||
Set<LocalDateTime> slots = parseExistingSlots(body, intervalMinutes);
|
List<SteadyChecksquareValuePointBO> points = parseValuePoints(body, intervalMinutes);
|
||||||
log.info("数据校验 InfluxDB 查询结束,slotCount={},costMs={}", slots.size(), System.currentTimeMillis() - startMillis);
|
if (cache != null) {
|
||||||
return slots;
|
cache.put(cacheKey, new ArrayList<SteadyChecksquareValuePointBO>(points));
|
||||||
|
}
|
||||||
|
log.info("数据校验指标值 InfluxDB 查询结束,pointCount={},costMs={}", points.size(), System.currentTimeMillis() - startMillis);
|
||||||
|
return points;
|
||||||
} catch (RuntimeException ex) {
|
} catch (RuntimeException ex) {
|
||||||
log.warn("数据校验 InfluxDB 查询异常,costMs={},error={}", System.currentTimeMillis() - startMillis, ex.getMessage());
|
log.warn("数据校验指标值 InfluxDB 查询异常,costMs={},error={}", System.currentTimeMillis() - startMillis, ex.getMessage());
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<String, List<SteadyChecksquareValuePointBO>> queryValuePointMap(List<SteadyTrendResolvedFieldBO> fields,
|
||||||
|
LocalDateTime startTime,
|
||||||
|
LocalDateTime endTime,
|
||||||
|
int intervalMinutes) {
|
||||||
|
Map<String, List<SteadyChecksquareValuePointBO>> result =
|
||||||
|
new LinkedHashMap<String, List<SteadyChecksquareValuePointBO>>();
|
||||||
|
if (fields == null || fields.isEmpty()) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if (fields.size() == 1) {
|
||||||
|
SteadyTrendResolvedFieldBO field = fields.get(0);
|
||||||
|
result.put(field.getField(), queryValuePoints(field, startTime, endTime, intervalMinutes));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
validateConfig();
|
||||||
|
Map<String, List<SteadyChecksquareValuePointBO>> cache = REQUEST_VALUE_CACHE.get();
|
||||||
|
List<SteadyTrendResolvedFieldBO> missingFields = new ArrayList<SteadyTrendResolvedFieldBO>();
|
||||||
|
for (SteadyTrendResolvedFieldBO field : fields) {
|
||||||
|
String cacheKey = buildCacheKey(buildValuePointQuery(field, startTime, endTime), intervalMinutes);
|
||||||
|
if (cache != null && cache.containsKey(cacheKey)) {
|
||||||
|
result.put(field.getField(), new ArrayList<SteadyChecksquareValuePointBO>(cache.get(cacheKey)));
|
||||||
|
} else {
|
||||||
|
missingFields.add(field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!missingFields.isEmpty()) {
|
||||||
|
String query = buildBatchValuePointQuery(missingFields, startTime, endTime);
|
||||||
|
long startMillis = System.currentTimeMillis();
|
||||||
|
SteadyTrendResolvedFieldBO first = missingFields.get(0);
|
||||||
|
log.info("数据校验指标值 InfluxDB 批量查询开始,measurement={},fieldCount={},lineId={},phase={},statType={},query={}",
|
||||||
|
first.getMeasurement(), missingFields.size(), first.getLineId(), first.getPhase(), first.getStatType(), query);
|
||||||
|
try {
|
||||||
|
Map<String, List<SteadyChecksquareValuePointBO>> queried = parseBatchValuePoints(executeQuery(query), intervalMinutes);
|
||||||
|
for (SteadyTrendResolvedFieldBO field : missingFields) {
|
||||||
|
List<SteadyChecksquareValuePointBO> points = queried.get(field.getField());
|
||||||
|
if (points == null) {
|
||||||
|
points = new ArrayList<SteadyChecksquareValuePointBO>();
|
||||||
|
}
|
||||||
|
result.put(field.getField(), points);
|
||||||
|
if (cache != null) {
|
||||||
|
String cacheKey = buildCacheKey(buildValuePointQuery(field, startTime, endTime), intervalMinutes);
|
||||||
|
cache.put(cacheKey, new ArrayList<SteadyChecksquareValuePointBO>(points));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.info("数据校验指标值 InfluxDB 批量查询结束,fieldCount={},costMs={}",
|
||||||
|
missingFields.size(), System.currentTimeMillis() - startMillis);
|
||||||
|
} catch (RuntimeException ex) {
|
||||||
|
log.warn("数据校验指标值 InfluxDB 批量查询异常,fieldCount={},costMs={},error={}",
|
||||||
|
missingFields.size(), System.currentTimeMillis() - startMillis, ex.getMessage());
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public String buildChecksquareQuery(SteadyTrendResolvedFieldBO field, LocalDateTime startTime, LocalDateTime endTime) {
|
public String buildChecksquareQuery(SteadyTrendResolvedFieldBO field, LocalDateTime startTime, LocalDateTime endTime) {
|
||||||
|
return buildValuePointQuery(field, startTime, endTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildCacheKey(String query, int intervalMinutes) {
|
||||||
|
return query + "|intervalMinutes=" + intervalMinutes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String buildValuePointQuery(SteadyTrendResolvedFieldBO field, LocalDateTime startTime, LocalDateTime endTime) {
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
sql.append("SELECT \"").append(field.getField()).append("\" AS \"value\"");
|
sql.append("SELECT \"").append(field.getField()).append("\" AS \"value\"");
|
||||||
sql.append(" FROM \"").append(field.getMeasurement()).append("\"");
|
sql.append(" FROM \"").append(field.getMeasurement()).append("\"");
|
||||||
@@ -71,11 +170,33 @@ public class SteadyChecksquareInfluxQueryComponent {
|
|||||||
return sql.toString();
|
return sql.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<LocalDateTime> parseExistingSlots(String body, int intervalMinutes) {
|
public String buildBatchValuePointQuery(List<SteadyTrendResolvedFieldBO> fields, LocalDateTime startTime, LocalDateTime endTime) {
|
||||||
|
SteadyTrendResolvedFieldBO first = fields.get(0);
|
||||||
|
StringBuilder sql = new StringBuilder("SELECT ");
|
||||||
|
for (int i = 0; i < fields.size(); i++) {
|
||||||
|
SteadyTrendResolvedFieldBO field = fields.get(i);
|
||||||
|
if (i > 0) {
|
||||||
|
sql.append(", ");
|
||||||
|
}
|
||||||
|
sql.append("\"").append(field.getField()).append("\" AS \"").append(field.getField()).append("\"");
|
||||||
|
}
|
||||||
|
sql.append(" FROM \"").append(first.getMeasurement()).append("\"");
|
||||||
|
sql.append(" WHERE time >= '").append(INFLUX_TIME_FORMATTER.format(startTime)).append("'");
|
||||||
|
sql.append(" AND time <= '").append(INFLUX_TIME_FORMATTER.format(endTime)).append("'");
|
||||||
|
sql.append(" AND \"line_id\" = '").append(escapeTagValue(first.getLineId())).append("'");
|
||||||
|
sql.append(" AND \"phasic_type\" = '").append(escapeTagValue(first.getPhase())).append("'");
|
||||||
|
if (hasValueTypeTag(first.getMeasurement())) {
|
||||||
|
sql.append(" AND \"value_type\" = '").append(resolveValueType(first.getStatType())).append("'");
|
||||||
|
}
|
||||||
|
sql.append(" ORDER BY time ASC");
|
||||||
|
return sql.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<SteadyChecksquareValuePointBO> parseValuePoints(String body, int intervalMinutes) {
|
||||||
try {
|
try {
|
||||||
JsonNode root = OBJECT_MAPPER.readTree(body);
|
JsonNode root = OBJECT_MAPPER.readTree(body);
|
||||||
JsonNode values = root.path("results").path(0).path("series").path(0).path("values");
|
JsonNode values = root.path("results").path(0).path("series").path(0).path("values");
|
||||||
Set<LocalDateTime> result = new HashSet<LocalDateTime>();
|
List<SteadyChecksquareValuePointBO> result = new ArrayList<SteadyChecksquareValuePointBO>();
|
||||||
if (!values.isArray()) {
|
if (!values.isArray()) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -84,13 +205,64 @@ public class SteadyChecksquareInfluxQueryComponent {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
LocalDateTime time = parseInfluxTime(value.get(0).asText());
|
LocalDateTime time = parseInfluxTime(value.get(0).asText());
|
||||||
if (time != null) {
|
if (time == null) {
|
||||||
result.add(alignToPreviousSlot(time, intervalMinutes));
|
continue;
|
||||||
|
}
|
||||||
|
SteadyChecksquareValuePointBO point = new SteadyChecksquareValuePointBO();
|
||||||
|
point.setTime(alignToPreviousSlot(time, intervalMinutes));
|
||||||
|
point.setValue(new BigDecimal(value.get(1).asText()));
|
||||||
|
result.add(point);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw fail("InfluxDB 返回结果解析失败:" + ex.getMessage());
|
||||||
|
} catch (NumberFormatException ex) {
|
||||||
|
throw fail("InfluxDB 返回指标值格式不正确:" + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, List<SteadyChecksquareValuePointBO>> parseBatchValuePoints(String body, int intervalMinutes) {
|
||||||
|
try {
|
||||||
|
JsonNode root = OBJECT_MAPPER.readTree(body);
|
||||||
|
JsonNode series = root.path("results").path(0).path("series").path(0);
|
||||||
|
JsonNode columns = series.path("columns");
|
||||||
|
JsonNode values = series.path("values");
|
||||||
|
Map<Integer, String> columnMap = new LinkedHashMap<Integer, String>();
|
||||||
|
Map<String, List<SteadyChecksquareValuePointBO>> result =
|
||||||
|
new LinkedHashMap<String, List<SteadyChecksquareValuePointBO>>();
|
||||||
|
if (!columns.isArray() || !values.isArray()) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
for (int i = 1; i < columns.size(); i++) {
|
||||||
|
String fieldName = columns.get(i).asText();
|
||||||
|
columnMap.put(i, fieldName);
|
||||||
|
result.put(fieldName, new ArrayList<SteadyChecksquareValuePointBO>());
|
||||||
|
}
|
||||||
|
for (JsonNode row : values) {
|
||||||
|
if (row.size() < 2) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
LocalDateTime time = parseInfluxTime(row.get(0).asText());
|
||||||
|
if (time == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
LocalDateTime slot = alignToPreviousSlot(time, intervalMinutes);
|
||||||
|
for (Map.Entry<Integer, String> entry : columnMap.entrySet()) {
|
||||||
|
JsonNode value = row.get(entry.getKey());
|
||||||
|
if (value == null || value.isNull()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
SteadyChecksquareValuePointBO point = new SteadyChecksquareValuePointBO();
|
||||||
|
point.setTime(slot);
|
||||||
|
point.setValue(new BigDecimal(value.asText()));
|
||||||
|
result.get(entry.getValue()).add(point);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
throw fail("InfluxDB 返回结果解析失败:" + ex.getMessage());
|
throw fail("InfluxDB 返回结果解析失败:" + ex.getMessage());
|
||||||
|
} catch (NumberFormatException ex) {
|
||||||
|
throw fail("InfluxDB 返回指标值格式不正确:" + ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,145 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.component;
|
||||||
|
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.bo.SteadyChecksquareValuePointBO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareValueOrderDetailVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareValueOrderRuleVO;
|
||||||
|
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendIndicatorDefinitionBO;
|
||||||
|
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO;
|
||||||
|
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendSeriesFieldBO;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验指标值大小关系规则。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SteadyChecksquareValueOrderRuleComponent {
|
||||||
|
|
||||||
|
private static final DateTimeFormatter OUTPUT_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
private static final List<String> REQUIRED_STATS = Collections.unmodifiableList(Arrays.asList("MAX", "CP95", "AVG", "MIN"));
|
||||||
|
private static final int ABNORMAL_THRESHOLD = 1;
|
||||||
|
|
||||||
|
private final SteadyChecksquareInfluxQueryComponent influxQueryComponent;
|
||||||
|
|
||||||
|
public SteadyChecksquareValueOrderRuleVO check(String lineId, SteadyTrendIndicatorDefinitionBO indicator,
|
||||||
|
Integer harmonicOrder, LocalDateTime startTime,
|
||||||
|
LocalDateTime endTime, int intervalMinutes) {
|
||||||
|
SteadyChecksquareValueOrderRuleVO result = new SteadyChecksquareValueOrderRuleVO();
|
||||||
|
if (!supportValueOrderRule(indicator)) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
for (String phase : indicator.getPhaseCodes()) {
|
||||||
|
Map<String, Map<LocalDateTime, BigDecimal>> statValueMap = queryStatValueMap(lineId, indicator,
|
||||||
|
harmonicOrder, phase, startTime, endTime, intervalMinutes);
|
||||||
|
appendAbnormalDetails(result, phase, harmonicOrder, statValueMap);
|
||||||
|
}
|
||||||
|
result.setAbnormalPointCount(result.getAbnormalDetails().size());
|
||||||
|
result.setAbnormal(result.getAbnormalPointCount() > ABNORMAL_THRESHOLD);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean supportValueOrderRule(SteadyTrendIndicatorDefinitionBO indicator) {
|
||||||
|
return indicator != null && indicator.getSupportStats() != null && indicator.getSupportStats().containsAll(REQUIRED_STATS);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, Map<LocalDateTime, BigDecimal>> queryStatValueMap(String lineId,
|
||||||
|
SteadyTrendIndicatorDefinitionBO indicator,
|
||||||
|
Integer harmonicOrder, String phase,
|
||||||
|
LocalDateTime startTime, LocalDateTime endTime,
|
||||||
|
int intervalMinutes) {
|
||||||
|
Map<String, Map<LocalDateTime, BigDecimal>> result = new LinkedHashMap<String, Map<LocalDateTime, BigDecimal>>();
|
||||||
|
for (String statType : REQUIRED_STATS) {
|
||||||
|
SteadyTrendResolvedFieldBO field = buildResolvedField(lineId, indicator, harmonicOrder, phase, statType);
|
||||||
|
result.put(statType, toValueMap(influxQueryComponent.queryValuePoints(field, startTime, endTime, intervalMinutes)));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendAbnormalDetails(SteadyChecksquareValueOrderRuleVO result, String phase, Integer harmonicOrder,
|
||||||
|
Map<String, Map<LocalDateTime, BigDecimal>> statValueMap) {
|
||||||
|
Map<LocalDateTime, BigDecimal> maxValues = statValueMap.get("MAX");
|
||||||
|
Map<LocalDateTime, BigDecimal> cp95Values = statValueMap.get("CP95");
|
||||||
|
Map<LocalDateTime, BigDecimal> avgValues = statValueMap.get("AVG");
|
||||||
|
Map<LocalDateTime, BigDecimal> minValues = statValueMap.get("MIN");
|
||||||
|
if (maxValues == null || cp95Values == null || avgValues == null || minValues == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (Map.Entry<LocalDateTime, BigDecimal> entry : maxValues.entrySet()) {
|
||||||
|
LocalDateTime time = entry.getKey();
|
||||||
|
BigDecimal maxValue = entry.getValue();
|
||||||
|
BigDecimal cp95Value = cp95Values.get(time);
|
||||||
|
BigDecimal avgValue = avgValues.get(time);
|
||||||
|
BigDecimal minValue = minValues.get(time);
|
||||||
|
// 缺少任一统计值时由缺数校验负责,不重复计入大小关系异常。
|
||||||
|
if (maxValue == null || cp95Value == null || avgValue == null || minValue == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (maxValue.compareTo(cp95Value) >= 0 && cp95Value.compareTo(avgValue) >= 0 && avgValue.compareTo(minValue) >= 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
result.getAbnormalDetails().add(buildDetail(time, phase, harmonicOrder, maxValue, minValue, avgValue, cp95Value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SteadyChecksquareValueOrderDetailVO buildDetail(LocalDateTime time, String phase, Integer harmonicOrder,
|
||||||
|
BigDecimal maxValue, BigDecimal minValue,
|
||||||
|
BigDecimal avgValue, BigDecimal cp95Value) {
|
||||||
|
SteadyChecksquareValueOrderDetailVO detail = new SteadyChecksquareValueOrderDetailVO();
|
||||||
|
detail.setTime(OUTPUT_TIME_FORMATTER.format(time));
|
||||||
|
detail.setPhase(phase);
|
||||||
|
detail.setHarmonicOrder(harmonicOrder);
|
||||||
|
detail.setMaxValue(maxValue);
|
||||||
|
detail.setMinValue(minValue);
|
||||||
|
detail.setAvgValue(avgValue);
|
||||||
|
detail.setCp95Value(cp95Value);
|
||||||
|
return detail;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<LocalDateTime, BigDecimal> toValueMap(List<SteadyChecksquareValuePointBO> points) {
|
||||||
|
Map<LocalDateTime, BigDecimal> result = new LinkedHashMap<LocalDateTime, BigDecimal>();
|
||||||
|
if (points == null || points.isEmpty()) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
for (SteadyChecksquareValuePointBO point : points) {
|
||||||
|
if (point != null && point.getTime() != null && point.getValue() != null) {
|
||||||
|
result.put(point.getTime(), point.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SteadyTrendResolvedFieldBO buildResolvedField(String lineId, SteadyTrendIndicatorDefinitionBO indicator,
|
||||||
|
Integer harmonicOrder, String phase, String statType) {
|
||||||
|
SteadyTrendResolvedFieldBO field = new SteadyTrendResolvedFieldBO();
|
||||||
|
field.setMeasurement(indicator.getTableName());
|
||||||
|
field.setField(resolveField(indicator, harmonicOrder));
|
||||||
|
field.setLineId(lineId);
|
||||||
|
field.setIndicatorCode(indicator.getIndicatorCode());
|
||||||
|
field.setIndicatorName(indicator.getName());
|
||||||
|
field.setPhase(phase);
|
||||||
|
field.setStatType(statType);
|
||||||
|
field.setUnit(indicator.getUnit());
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveField(SteadyTrendIndicatorDefinitionBO indicator, Integer harmonicOrder) {
|
||||||
|
if (Boolean.TRUE.equals(indicator.getHarmonic())) {
|
||||||
|
return indicator.getHarmonicFieldPrefix() + "_" + harmonicOrder;
|
||||||
|
}
|
||||||
|
List<SteadyTrendSeriesFieldBO> fields = indicator.getSeriesFields();
|
||||||
|
if (fields == null || fields.isEmpty()) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return fields.get(0).getField();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,18 @@
|
|||||||
package com.njcn.gather.steady.checksquare.controller;
|
package com.njcn.gather.steady.checksquare.controller;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.njcn.common.pojo.annotation.OperateInfo;
|
import com.njcn.common.pojo.annotation.OperateInfo;
|
||||||
|
import com.njcn.common.pojo.constant.OperateType;
|
||||||
import com.njcn.common.pojo.enums.common.LogEnum;
|
import com.njcn.common.pojo.enums.common.LogEnum;
|
||||||
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
import com.njcn.common.pojo.response.HttpResult;
|
import com.njcn.common.pojo.response.HttpResult;
|
||||||
import com.njcn.common.utils.LogUtil;
|
import com.njcn.common.utils.LogUtil;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareHistoryQueryParam;
|
||||||
import com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareQueryParam;
|
import com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareQueryParam;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareCreateVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareItemDetailVO;
|
||||||
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareQueryVO;
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareQueryVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareTaskVO;
|
||||||
import com.njcn.gather.steady.checksquare.service.SteadyChecksquareService;
|
import com.njcn.gather.steady.checksquare.service.SteadyChecksquareService;
|
||||||
import com.njcn.web.controller.BaseController;
|
import com.njcn.web.controller.BaseController;
|
||||||
import com.njcn.web.utils.HttpResultUtil;
|
import com.njcn.web.utils.HttpResultUtil;
|
||||||
@@ -14,9 +20,12 @@ import io.swagger.annotations.Api;
|
|||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,12 +41,43 @@ public class SteadyChecksquareController extends BaseController {
|
|||||||
private final SteadyChecksquareService checksquareService;
|
private final SteadyChecksquareService checksquareService;
|
||||||
|
|
||||||
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||||
@ApiOperation("查询数据校验结果")
|
@ApiOperation("查询数据校验历史记录")
|
||||||
@PostMapping("/query")
|
@PostMapping("/query")
|
||||||
public HttpResult<SteadyChecksquareQueryVO> query(@RequestBody SteadyChecksquareQueryParam param) {
|
public HttpResult<Page<SteadyChecksquareTaskVO>> query(@RequestBody @Validated SteadyChecksquareHistoryQueryParam param) {
|
||||||
String methodDescribe = getMethodDescribe("query");
|
String methodDescribe = getMethodDescribe("query");
|
||||||
LogUtil.njcnDebug(log, "{},开始查询数据校验结果,param={}", methodDescribe, param);
|
LogUtil.njcnDebug(log, "{},开始查询数据校验历史记录,param={}", methodDescribe, param);
|
||||||
SteadyChecksquareQueryVO result = checksquareService.query(param);
|
Page<SteadyChecksquareTaskVO> result = checksquareService.query(param);
|
||||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
|
||||||
|
@ApiOperation("新增数据校验记录")
|
||||||
|
@PostMapping("/create")
|
||||||
|
public HttpResult<SteadyChecksquareCreateVO> create(@RequestBody @Validated SteadyChecksquareQueryParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("create");
|
||||||
|
LogUtil.njcnDebug(log, "{},开始新增数据校验记录,param={}", methodDescribe, param);
|
||||||
|
SteadyChecksquareCreateVO result = checksquareService.create(param);
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||||
|
@ApiOperation("查询数据校验任务详情")
|
||||||
|
@GetMapping("/detail")
|
||||||
|
public HttpResult<SteadyChecksquareQueryVO> detail(@RequestParam("taskId") String taskId) {
|
||||||
|
String methodDescribe = getMethodDescribe("detail");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, checksquareService.detail(taskId), methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||||
|
@ApiOperation("查询数据校验检测项明细")
|
||||||
|
@GetMapping("/item-detail")
|
||||||
|
public HttpResult<SteadyChecksquareItemDetailVO> itemDetail(@RequestParam("itemId") String itemId,
|
||||||
|
@RequestParam("detailType") String detailType,
|
||||||
|
@RequestParam(value = "statType", required = false) String statType,
|
||||||
|
@RequestParam(value = "pageNum", required = false) Integer pageNum,
|
||||||
|
@RequestParam(value = "pageSize", required = false) Integer pageSize) {
|
||||||
|
String methodDescribe = getMethodDescribe("itemDetail");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,
|
||||||
|
checksquareService.itemDetail(itemId, detailType, statType, pageNum, pageSize), methodDescribe);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareDetailPO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验明细 Mapper。
|
||||||
|
*/
|
||||||
|
public interface SteadyChecksquareDetailMapper extends BaseMapper<SteadyChecksquareDetailPO> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareItemPO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验检测项 Mapper。
|
||||||
|
*/
|
||||||
|
public interface SteadyChecksquareItemMapper extends BaseMapper<SteadyChecksquareItemPO> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareStatSummaryPO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验统计摘要 Mapper。
|
||||||
|
*/
|
||||||
|
public interface SteadyChecksquareStatSummaryMapper extends BaseMapper<SteadyChecksquareStatSummaryPO> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareTaskPO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验任务 Mapper。
|
||||||
|
*/
|
||||||
|
public interface SteadyChecksquareTaskMapper extends BaseMapper<SteadyChecksquareTaskPO> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.bo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验指标值时间点。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class SteadyChecksquareValuePointBO implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/** 对齐后的统计时间。 */
|
||||||
|
private LocalDateTime time;
|
||||||
|
|
||||||
|
/** 指标值。 */
|
||||||
|
private BigDecimal value;
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.constant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验常量。
|
||||||
|
*/
|
||||||
|
public final class SteadyChecksquareConst {
|
||||||
|
|
||||||
|
public static final int STATE_DELETED = 0;
|
||||||
|
public static final int STATE_ENABLED = 1;
|
||||||
|
|
||||||
|
public static final String TASK_STATUS_SUCCESS = "SUCCESS";
|
||||||
|
public static final String DETAIL_TYPE_SEGMENT = "SEGMENT";
|
||||||
|
public static final String DETAIL_TYPE_VALUE_ORDER = "VALUE_ORDER";
|
||||||
|
public static final String DETAIL_TYPE_HARMONIC_PARITY = "HARMONIC_PARITY";
|
||||||
|
|
||||||
|
private SteadyChecksquareConst() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.param;
|
||||||
|
|
||||||
|
import com.njcn.web.pojo.param.BaseParam;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验历史查询参数。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@ApiModel("数据校验历史查询参数")
|
||||||
|
public class SteadyChecksquareHistoryQueryParam extends BaseParam {
|
||||||
|
|
||||||
|
@ApiModelProperty("监测点 ID")
|
||||||
|
private String lineId;
|
||||||
|
|
||||||
|
@ApiModelProperty("指标编码")
|
||||||
|
private String indicatorCode;
|
||||||
|
|
||||||
|
@ApiModelProperty("检测开始时间,格式 yyyy-MM-dd HH:mm:ss")
|
||||||
|
private String timeStart;
|
||||||
|
|
||||||
|
@ApiModelProperty("检测结束时间,格式 yyyy-MM-dd HH:mm:ss")
|
||||||
|
private String timeEnd;
|
||||||
|
|
||||||
|
@ApiModelProperty("是否存在异常")
|
||||||
|
private Boolean hasAbnormal;
|
||||||
|
}
|
||||||
@@ -8,10 +8,10 @@ import java.io.Serializable;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据校验查询参数。
|
* 数据校验新增检测参数。
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@ApiModel("数据校验查询参数")
|
@ApiModel("数据校验新增检测参数")
|
||||||
public class SteadyChecksquareQueryParam implements Serializable {
|
public class SteadyChecksquareQueryParam implements Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
@@ -27,7 +27,4 @@ public class SteadyChecksquareQueryParam implements Serializable {
|
|||||||
|
|
||||||
@ApiModelProperty("结束时间,格式 yyyy-MM-dd HH:mm:ss")
|
@ApiModelProperty("结束时间,格式 yyyy-MM-dd HH:mm:ss")
|
||||||
private String timeEnd;
|
private String timeEnd;
|
||||||
|
|
||||||
@ApiModelProperty("谐波次数,谐波指标按请求次数查询")
|
|
||||||
private List<Integer> harmonicOrders;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.po;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验明细。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@TableName("steady_checksquare_detail")
|
||||||
|
public class SteadyChecksquareDetailPO implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@TableId("id")
|
||||||
|
private String id;
|
||||||
|
@TableField("item_id")
|
||||||
|
private String itemId;
|
||||||
|
@TableField("detail_type")
|
||||||
|
private String detailType;
|
||||||
|
@TableField("stat_type")
|
||||||
|
private String statType;
|
||||||
|
@TableField("start_time")
|
||||||
|
private LocalDateTime startTime;
|
||||||
|
@TableField("end_time")
|
||||||
|
private LocalDateTime endTime;
|
||||||
|
@TableField("point_time")
|
||||||
|
private LocalDateTime pointTime;
|
||||||
|
@TableField("segment_status")
|
||||||
|
private String segmentStatus;
|
||||||
|
@TableField("missing_point_count")
|
||||||
|
private Integer missingPointCount;
|
||||||
|
@TableField("duration_minutes")
|
||||||
|
private Integer durationMinutes;
|
||||||
|
@TableField("phase")
|
||||||
|
private String phase;
|
||||||
|
@TableField("harmonic_order")
|
||||||
|
private Integer harmonicOrder;
|
||||||
|
@TableField("max_value")
|
||||||
|
private BigDecimal maxValue;
|
||||||
|
@TableField("min_value")
|
||||||
|
private BigDecimal minValue;
|
||||||
|
@TableField("avg_value")
|
||||||
|
private BigDecimal avgValue;
|
||||||
|
@TableField("cp95_value")
|
||||||
|
private BigDecimal cp95Value;
|
||||||
|
@TableField("even_harmonic_order")
|
||||||
|
private Integer evenHarmonicOrder;
|
||||||
|
@TableField("even_value")
|
||||||
|
private BigDecimal evenValue;
|
||||||
|
@TableField("odd_harmonic_orders_json")
|
||||||
|
private String oddHarmonicOrdersJson;
|
||||||
|
@TableField("odd_values_json")
|
||||||
|
private String oddValuesJson;
|
||||||
|
@TableField("odd_median_value")
|
||||||
|
private BigDecimal oddMedianValue;
|
||||||
|
@TableField("threshold_multiplier")
|
||||||
|
private BigDecimal thresholdMultiplier;
|
||||||
|
@TableField("create_time")
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.po;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验检测项。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@TableName("steady_checksquare_item")
|
||||||
|
public class SteadyChecksquareItemPO implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@TableId("id")
|
||||||
|
private String id;
|
||||||
|
@TableField("task_id")
|
||||||
|
private String taskId;
|
||||||
|
@TableField("item_key")
|
||||||
|
private String itemKey;
|
||||||
|
@TableField("indicator_code")
|
||||||
|
private String indicatorCode;
|
||||||
|
@TableField("indicator_name")
|
||||||
|
private String indicatorName;
|
||||||
|
@TableField("harmonic_order")
|
||||||
|
private Integer harmonicOrder;
|
||||||
|
@TableField("interval_minutes")
|
||||||
|
private Integer intervalMinutes;
|
||||||
|
@TableField("has_data")
|
||||||
|
private Integer hasData;
|
||||||
|
@TableField("expected_point_count")
|
||||||
|
private Integer expectedPointCount;
|
||||||
|
@TableField("actual_point_count")
|
||||||
|
private Integer actualPointCount;
|
||||||
|
@TableField("missing_point_count")
|
||||||
|
private Integer missingPointCount;
|
||||||
|
@TableField("missing_rate")
|
||||||
|
private BigDecimal missingRate;
|
||||||
|
@TableField("missing_rate_text")
|
||||||
|
private String missingRateText;
|
||||||
|
@TableField("max_continuous_missing_minutes")
|
||||||
|
private Integer maxContinuousMissingMinutes;
|
||||||
|
@TableField("abnormal")
|
||||||
|
private Integer abnormal;
|
||||||
|
@TableField("abnormal_point_count")
|
||||||
|
private Integer abnormalPointCount;
|
||||||
|
@TableField("harmonic_parity_abnormal")
|
||||||
|
private Integer harmonicParityAbnormal;
|
||||||
|
@TableField("harmonic_parity_abnormal_point_count")
|
||||||
|
private Integer harmonicParityAbnormalPointCount;
|
||||||
|
@TableField("state")
|
||||||
|
private Integer state;
|
||||||
|
@TableField("create_time")
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
@TableField("update_time")
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.po;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验统计摘要。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@TableName("steady_checksquare_stat_summary")
|
||||||
|
public class SteadyChecksquareStatSummaryPO implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@TableId("id")
|
||||||
|
private String id;
|
||||||
|
@TableField("item_id")
|
||||||
|
private String itemId;
|
||||||
|
@TableField("stat_type")
|
||||||
|
private String statType;
|
||||||
|
@TableField("supported")
|
||||||
|
private Integer supported;
|
||||||
|
@TableField("has_data")
|
||||||
|
private Integer hasData;
|
||||||
|
@TableField("expected_point_count")
|
||||||
|
private Integer expectedPointCount;
|
||||||
|
@TableField("actual_point_count")
|
||||||
|
private Integer actualPointCount;
|
||||||
|
@TableField("missing_point_count")
|
||||||
|
private Integer missingPointCount;
|
||||||
|
@TableField("missing_rate")
|
||||||
|
private BigDecimal missingRate;
|
||||||
|
@TableField("missing_rate_text")
|
||||||
|
private String missingRateText;
|
||||||
|
@TableField("max_continuous_missing_minutes")
|
||||||
|
private Integer maxContinuousMissingMinutes;
|
||||||
|
@TableField("create_time")
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.po;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验任务。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@TableName("steady_checksquare_task")
|
||||||
|
public class SteadyChecksquareTaskPO implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@TableId("id")
|
||||||
|
private String id;
|
||||||
|
@TableField("task_no")
|
||||||
|
private String taskNo;
|
||||||
|
@TableField("line_id")
|
||||||
|
private String lineId;
|
||||||
|
@TableField("line_name")
|
||||||
|
private String lineName;
|
||||||
|
@TableField("time_start")
|
||||||
|
private LocalDateTime timeStart;
|
||||||
|
@TableField("time_end")
|
||||||
|
private LocalDateTime timeEnd;
|
||||||
|
@TableField("interval_minutes")
|
||||||
|
private Integer intervalMinutes;
|
||||||
|
@TableField("indicator_codes_json")
|
||||||
|
private String indicatorCodesJson;
|
||||||
|
@TableField("indicator_codes_text")
|
||||||
|
private String indicatorCodesText;
|
||||||
|
@TableField("task_status")
|
||||||
|
private String taskStatus;
|
||||||
|
@TableField("item_count")
|
||||||
|
private Integer itemCount;
|
||||||
|
@TableField("abnormal_item_count")
|
||||||
|
private Integer abnormalItemCount;
|
||||||
|
@TableField("max_missing_rate")
|
||||||
|
private BigDecimal maxMissingRate;
|
||||||
|
@TableField("result_message")
|
||||||
|
private String resultMessage;
|
||||||
|
@TableField("state")
|
||||||
|
private Integer state;
|
||||||
|
@TableField("create_by")
|
||||||
|
private String createBy;
|
||||||
|
@TableField("create_time")
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
@TableField("update_by")
|
||||||
|
private String updateBy;
|
||||||
|
@TableField("update_time")
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.vo;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增数据校验结果。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ApiModel("新增数据校验结果")
|
||||||
|
public class SteadyChecksquareCreateVO implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@ApiModelProperty("任务 ID")
|
||||||
|
private String taskId;
|
||||||
|
|
||||||
|
@ApiModelProperty("任务编号")
|
||||||
|
private String taskNo;
|
||||||
|
|
||||||
|
@ApiModelProperty("监测点 ID")
|
||||||
|
private String lineId;
|
||||||
|
|
||||||
|
@ApiModelProperty("监测点名称")
|
||||||
|
private String lineName;
|
||||||
|
|
||||||
|
@ApiModelProperty("开始时间")
|
||||||
|
private String timeStart;
|
||||||
|
|
||||||
|
@ApiModelProperty("结束时间")
|
||||||
|
private String timeEnd;
|
||||||
|
|
||||||
|
@ApiModelProperty("统计间隔,单位分钟")
|
||||||
|
private Integer intervalMinutes;
|
||||||
|
|
||||||
|
@ApiModelProperty("检测项数量")
|
||||||
|
private Integer itemCount;
|
||||||
|
|
||||||
|
@ApiModelProperty("异常检测项数量")
|
||||||
|
private Integer abnormalItemCount;
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.vo;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 谐波奇偶关系异常明细。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ApiModel("谐波奇偶关系异常明细")
|
||||||
|
public class SteadyChecksquareHarmonicParityDetailVO implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@ApiModelProperty("时间")
|
||||||
|
private String time;
|
||||||
|
|
||||||
|
@ApiModelProperty("相别")
|
||||||
|
private String phase;
|
||||||
|
|
||||||
|
@ApiModelProperty("统计类型")
|
||||||
|
private String statType;
|
||||||
|
|
||||||
|
@ApiModelProperty("偶次谐波次数")
|
||||||
|
private Integer evenHarmonicOrder;
|
||||||
|
|
||||||
|
@ApiModelProperty("偶次谐波值")
|
||||||
|
private BigDecimal evenValue;
|
||||||
|
|
||||||
|
@ApiModelProperty("参与比较的奇次谐波次数")
|
||||||
|
private List<Integer> oddHarmonicOrders = new ArrayList<Integer>();
|
||||||
|
|
||||||
|
@ApiModelProperty("参与比较的奇次谐波值")
|
||||||
|
private List<BigDecimal> oddValues = new ArrayList<BigDecimal>();
|
||||||
|
|
||||||
|
@ApiModelProperty("奇次谐波中位数")
|
||||||
|
private BigDecimal oddMedianValue;
|
||||||
|
|
||||||
|
@ApiModelProperty("异常阈值倍数")
|
||||||
|
private BigDecimal thresholdMultiplier;
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 谐波奇偶关系规则结果。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class SteadyChecksquareHarmonicParityRuleVO implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private Boolean abnormal = false;
|
||||||
|
|
||||||
|
private Integer abnormalPointCount = 0;
|
||||||
|
|
||||||
|
private List<SteadyChecksquareHarmonicParityDetailVO> abnormalDetails =
|
||||||
|
new ArrayList<SteadyChecksquareHarmonicParityDetailVO>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.vo;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验检测项明细。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ApiModel("数据校验检测项明细")
|
||||||
|
public class SteadyChecksquareItemDetailVO implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@ApiModelProperty("检测项 ID")
|
||||||
|
private String itemId;
|
||||||
|
|
||||||
|
@ApiModelProperty("明细类型")
|
||||||
|
private String detailType;
|
||||||
|
|
||||||
|
@ApiModelProperty("统计类型")
|
||||||
|
private String statType;
|
||||||
|
|
||||||
|
@ApiModelProperty("当前页码;未分页查询时为空")
|
||||||
|
private Integer pageNum;
|
||||||
|
|
||||||
|
@ApiModelProperty("每页条数;未分页查询时为空")
|
||||||
|
private Integer pageSize;
|
||||||
|
|
||||||
|
@ApiModelProperty("总记录数;未分页查询时为空")
|
||||||
|
private Long total;
|
||||||
|
|
||||||
|
@ApiModelProperty("缺失区间")
|
||||||
|
private List<SteadyChecksquareSegmentVO> segments = new ArrayList<SteadyChecksquareSegmentVO>();
|
||||||
|
|
||||||
|
@ApiModelProperty("大小关系异常明细")
|
||||||
|
private List<SteadyChecksquareValueOrderDetailVO> valueOrderDetails =
|
||||||
|
new ArrayList<SteadyChecksquareValueOrderDetailVO>();
|
||||||
|
|
||||||
|
@ApiModelProperty("谐波奇偶关系异常明细")
|
||||||
|
private List<SteadyChecksquareHarmonicParityDetailVO> harmonicParityDetails =
|
||||||
|
new ArrayList<SteadyChecksquareHarmonicParityDetailVO>();
|
||||||
|
}
|
||||||
@@ -18,6 +18,9 @@ public class SteadyChecksquareItemVO implements Serializable {
|
|||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@ApiModelProperty("检测项 ID")
|
||||||
|
private String itemId;
|
||||||
|
|
||||||
@ApiModelProperty("校验项唯一键")
|
@ApiModelProperty("校验项唯一键")
|
||||||
private String itemKey;
|
private String itemKey;
|
||||||
|
|
||||||
@@ -54,6 +57,25 @@ public class SteadyChecksquareItemVO implements Serializable {
|
|||||||
@ApiModelProperty("最大连续缺失时长,单位分钟")
|
@ApiModelProperty("最大连续缺失时长,单位分钟")
|
||||||
private Integer maxContinuousMissingMinutes;
|
private Integer maxContinuousMissingMinutes;
|
||||||
|
|
||||||
|
@ApiModelProperty("指标值大小关系是否异常")
|
||||||
|
private Boolean abnormal;
|
||||||
|
|
||||||
|
@ApiModelProperty("指标值大小关系异常累计值")
|
||||||
|
private Integer abnormalPointCount;
|
||||||
|
|
||||||
|
@ApiModelProperty("指标值大小关系异常明细")
|
||||||
|
private List<SteadyChecksquareValueOrderDetailVO> abnormalDetails = new ArrayList<SteadyChecksquareValueOrderDetailVO>();
|
||||||
|
|
||||||
|
@ApiModelProperty("谐波奇偶关系是否异常")
|
||||||
|
private Boolean harmonicParityAbnormal;
|
||||||
|
|
||||||
|
@ApiModelProperty("谐波奇偶关系异常累计值")
|
||||||
|
private Integer harmonicParityAbnormalPointCount;
|
||||||
|
|
||||||
|
@ApiModelProperty("谐波奇偶关系异常明细")
|
||||||
|
private List<SteadyChecksquareHarmonicParityDetailVO> harmonicParityAbnormalDetails =
|
||||||
|
new ArrayList<SteadyChecksquareHarmonicParityDetailVO>();
|
||||||
|
|
||||||
@ApiModelProperty("统计类型摘要")
|
@ApiModelProperty("统计类型摘要")
|
||||||
private List<SteadyChecksquareStatSummaryVO> statSummaries = new ArrayList<SteadyChecksquareStatSummaryVO>();
|
private List<SteadyChecksquareStatSummaryVO> statSummaries = new ArrayList<SteadyChecksquareStatSummaryVO>();
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ public class SteadyChecksquareQueryVO implements Serializable {
|
|||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@ApiModelProperty("任务 ID")
|
||||||
|
private String taskId;
|
||||||
|
|
||||||
|
@ApiModelProperty("任务编号")
|
||||||
|
private String taskNo;
|
||||||
|
|
||||||
@ApiModelProperty("监测点 ID")
|
@ApiModelProperty("监测点 ID")
|
||||||
private String lineId;
|
private String lineId;
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ public class SteadyChecksquareSegmentVO implements Serializable {
|
|||||||
@ApiModelProperty("状态,NORMAL/MISSING")
|
@ApiModelProperty("状态,NORMAL/MISSING")
|
||||||
private String status;
|
private String status;
|
||||||
|
|
||||||
|
@ApiModelProperty("谐波次数")
|
||||||
|
private Integer harmonicOrder;
|
||||||
|
|
||||||
@ApiModelProperty("缺失点数")
|
@ApiModelProperty("缺失点数")
|
||||||
private Integer missingPointCount;
|
private Integer missingPointCount;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.vo;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验历史任务。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ApiModel("数据校验历史任务")
|
||||||
|
public class SteadyChecksquareTaskVO implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@ApiModelProperty("任务 ID")
|
||||||
|
private String taskId;
|
||||||
|
|
||||||
|
@ApiModelProperty("任务编号")
|
||||||
|
private String taskNo;
|
||||||
|
|
||||||
|
@ApiModelProperty("监测点 ID")
|
||||||
|
private String lineId;
|
||||||
|
|
||||||
|
@ApiModelProperty("监测点名称")
|
||||||
|
private String lineName;
|
||||||
|
|
||||||
|
@ApiModelProperty("开始时间")
|
||||||
|
private String timeStart;
|
||||||
|
|
||||||
|
@ApiModelProperty("结束时间")
|
||||||
|
private String timeEnd;
|
||||||
|
|
||||||
|
@ApiModelProperty("统计间隔,单位分钟")
|
||||||
|
private Integer intervalMinutes;
|
||||||
|
|
||||||
|
@ApiModelProperty("任务状态")
|
||||||
|
private String taskStatus;
|
||||||
|
|
||||||
|
@ApiModelProperty("检测项数量")
|
||||||
|
private Integer itemCount;
|
||||||
|
|
||||||
|
@ApiModelProperty("异常检测项数量")
|
||||||
|
private Integer abnormalItemCount;
|
||||||
|
|
||||||
|
@ApiModelProperty("最大缺失率")
|
||||||
|
private BigDecimal maxMissingRate;
|
||||||
|
|
||||||
|
@ApiModelProperty("创建时间")
|
||||||
|
private String createTime;
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.vo;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验指标值大小关系异常明细。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ApiModel("数据校验指标值大小关系异常明细")
|
||||||
|
public class SteadyChecksquareValueOrderDetailVO implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@ApiModelProperty("时间")
|
||||||
|
private String time;
|
||||||
|
|
||||||
|
@ApiModelProperty("相别")
|
||||||
|
private String phase;
|
||||||
|
|
||||||
|
@ApiModelProperty("谐波次数")
|
||||||
|
private Integer harmonicOrder;
|
||||||
|
|
||||||
|
@ApiModelProperty("最大值")
|
||||||
|
private BigDecimal maxValue;
|
||||||
|
|
||||||
|
@ApiModelProperty("最小值")
|
||||||
|
private BigDecimal minValue;
|
||||||
|
|
||||||
|
@ApiModelProperty("平均值")
|
||||||
|
private BigDecimal avgValue;
|
||||||
|
|
||||||
|
@ApiModelProperty("CP95 值")
|
||||||
|
private BigDecimal cp95Value;
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.pojo.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验指标值大小关系规则结果。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class SteadyChecksquareValueOrderRuleVO implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private Boolean abnormal = false;
|
||||||
|
|
||||||
|
private Integer abnormalPointCount = 0;
|
||||||
|
|
||||||
|
private List<SteadyChecksquareValueOrderDetailVO> abnormalDetails = new ArrayList<SteadyChecksquareValueOrderDetailVO>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareDetailPO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验明细服务。
|
||||||
|
*/
|
||||||
|
public interface SteadyChecksquareDetailService extends IService<SteadyChecksquareDetailPO> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareItemPO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验检测项服务。
|
||||||
|
*/
|
||||||
|
public interface SteadyChecksquareItemService extends IService<SteadyChecksquareItemPO> {
|
||||||
|
}
|
||||||
@@ -1,12 +1,26 @@
|
|||||||
package com.njcn.gather.steady.checksquare.service;
|
package com.njcn.gather.steady.checksquare.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareHistoryQueryParam;
|
||||||
import com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareQueryParam;
|
import com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareQueryParam;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareCreateVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareItemDetailVO;
|
||||||
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareQueryVO;
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareQueryVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareTaskVO;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据校验服务。
|
* 数据校验服务。
|
||||||
*/
|
*/
|
||||||
public interface SteadyChecksquareService {
|
public interface SteadyChecksquareService {
|
||||||
|
|
||||||
SteadyChecksquareQueryVO query(SteadyChecksquareQueryParam param);
|
Page<SteadyChecksquareTaskVO> query(SteadyChecksquareHistoryQueryParam param);
|
||||||
|
|
||||||
|
SteadyChecksquareCreateVO create(SteadyChecksquareQueryParam param);
|
||||||
|
|
||||||
|
SteadyChecksquareQueryVO detail(String taskId);
|
||||||
|
|
||||||
|
SteadyChecksquareItemDetailVO itemDetail(String itemId, String detailType, String statType);
|
||||||
|
|
||||||
|
SteadyChecksquareItemDetailVO itemDetail(String itemId, String detailType, String statType,
|
||||||
|
Integer pageNum, Integer pageSize);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareStatSummaryPO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验统计摘要服务。
|
||||||
|
*/
|
||||||
|
public interface SteadyChecksquareStatSummaryService extends IService<SteadyChecksquareStatSummaryPO> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareTaskPO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验任务服务。
|
||||||
|
*/
|
||||||
|
public interface SteadyChecksquareTaskService extends IService<SteadyChecksquareTaskPO> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.njcn.gather.steady.checksquare.mapper.SteadyChecksquareDetailMapper;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareDetailPO;
|
||||||
|
import com.njcn.gather.steady.checksquare.service.SteadyChecksquareDetailService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验明细服务实现。
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class SteadyChecksquareDetailServiceImpl extends ServiceImpl<SteadyChecksquareDetailMapper, SteadyChecksquareDetailPO>
|
||||||
|
implements SteadyChecksquareDetailService {
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.njcn.gather.steady.checksquare.mapper.SteadyChecksquareItemMapper;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareItemPO;
|
||||||
|
import com.njcn.gather.steady.checksquare.service.SteadyChecksquareItemService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验检测项服务实现。
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class SteadyChecksquareItemServiceImpl extends ServiceImpl<SteadyChecksquareItemMapper, SteadyChecksquareItemPO>
|
||||||
|
implements SteadyChecksquareItemService {
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,16 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.njcn.gather.steady.checksquare.mapper.SteadyChecksquareStatSummaryMapper;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareStatSummaryPO;
|
||||||
|
import com.njcn.gather.steady.checksquare.service.SteadyChecksquareStatSummaryService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验统计摘要服务实现。
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class SteadyChecksquareStatSummaryServiceImpl
|
||||||
|
extends ServiceImpl<SteadyChecksquareStatSummaryMapper, SteadyChecksquareStatSummaryPO>
|
||||||
|
implements SteadyChecksquareStatSummaryService {
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.njcn.gather.steady.checksquare.mapper.SteadyChecksquareTaskMapper;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareTaskPO;
|
||||||
|
import com.njcn.gather.steady.checksquare.service.SteadyChecksquareTaskService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验任务服务实现。
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class SteadyChecksquareTaskServiceImpl extends ServiceImpl<SteadyChecksquareTaskMapper, SteadyChecksquareTaskPO>
|
||||||
|
implements SteadyChecksquareTaskService {
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.util;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验编号工具。
|
||||||
|
*/
|
||||||
|
public final class SteadyChecksquareIdUtil {
|
||||||
|
|
||||||
|
private static final DateTimeFormatter TASK_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
|
||||||
|
|
||||||
|
private SteadyChecksquareIdUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String uuid() {
|
||||||
|
return UUID.randomUUID().toString().replace("-", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String taskNo() {
|
||||||
|
return "CS" + LocalDateTime.now().format(TASK_FORMATTER);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
-- 稳态数据查看建议索引。
|
||||||
|
-- 本脚本不自动执行,请按数据库现状审阅后单独执行。
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_v_time_line_phase
|
||||||
|
ON data_v (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_i_time_line_phase
|
||||||
|
ON data_i (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_flicker_time_line_phase
|
||||||
|
ON data_flicker (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_fluc_time_line_phase
|
||||||
|
ON data_fluc (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_harmphasic_i_time_line_phase
|
||||||
|
ON data_harmphasic_i (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_harmphasic_v_time_line_phase
|
||||||
|
ON data_harmphasic_v (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_harmpower_p_time_line_phase
|
||||||
|
ON data_harmpower_p (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_harmpower_q_time_line_phase
|
||||||
|
ON data_harmpower_q (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_harmpower_s_time_line_phase
|
||||||
|
ON data_harmpower_s (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_harmrate_i_time_line_phase
|
||||||
|
ON data_harmrate_i (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_harmrate_v_time_line_phase
|
||||||
|
ON data_harmrate_v (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_inharm_i_time_line_phase
|
||||||
|
ON data_inharm_i (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
|
|
||||||
|
CREATE INDEX idx_data_plt_time_line_phase
|
||||||
|
ON data_plt (TIMEID, LINEID, PHASIC_TYPE);
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
-- 稳态模块菜单图标修正脚本。
|
||||||
|
-- 本脚本不自动执行,请按数据库现状审阅后单独执行。
|
||||||
|
|
||||||
|
UPDATE sys_function
|
||||||
|
SET Icon = 'DataAnalysis'
|
||||||
|
WHERE State = 1
|
||||||
|
AND Type = 0
|
||||||
|
AND (
|
||||||
|
Name = '稳态模块'
|
||||||
|
OR Code IN ('steady', 'steadyModule', 'steadyDataView')
|
||||||
|
OR Path IN ('/steady', '/steadyDataView', '/steady/data-view')
|
||||||
|
);
|
||||||
|
|
||||||
|
UPDATE sys_function
|
||||||
|
SET Icon = 'DataBoard'
|
||||||
|
WHERE State = 1
|
||||||
|
AND Type = 0
|
||||||
|
AND (
|
||||||
|
Name = '稳态数据'
|
||||||
|
OR Code IN ('steadyData', 'steadyDataDetail')
|
||||||
|
OR Path IN ('/steady/data', '/steady/data-view/detail', '/steadyDataView/index')
|
||||||
|
);
|
||||||
|
|
||||||
|
UPDATE sys_function
|
||||||
|
SET Icon = 'TrendCharts'
|
||||||
|
WHERE State = 1
|
||||||
|
AND Type = 0
|
||||||
|
AND (
|
||||||
|
Name = '稳态趋势'
|
||||||
|
OR Code IN ('steadyTrend', 'steadyDataTrend')
|
||||||
|
OR Path IN ('/steady/trend', '/steady/data-view/trend', '/steadyTrend/index')
|
||||||
|
);
|
||||||
|
|
||||||
|
UPDATE sys_function
|
||||||
|
SET Icon = 'CircleCheck'
|
||||||
|
WHERE State = 1
|
||||||
|
AND Type = 0
|
||||||
|
AND (
|
||||||
|
Name = '数据验证'
|
||||||
|
OR Code IN ('dataValidation', 'steadyDataValidation')
|
||||||
|
OR Path IN ('/steady/data-validation', '/steady/data-view/validation', '/dataValidation/index')
|
||||||
|
);
|
||||||
@@ -0,0 +1,147 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.component;
|
||||||
|
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.bo.SteadyChecksquareValuePointBO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareHarmonicParityDetailVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareHarmonicParityRuleVO;
|
||||||
|
import com.njcn.gather.steady.datavie.component.SteadyTrendIndicatorCatalog;
|
||||||
|
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendIndicatorDefinitionBO;
|
||||||
|
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 谐波奇偶关系规则测试。
|
||||||
|
*/
|
||||||
|
class SteadyChecksquareHarmonicParityRuleComponentTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldRecordAbnormalWhenEvenHarmonicExceedsOddMedianThreshold() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareHarmonicParityRuleComponent component = new SteadyChecksquareHarmonicParityRuleComponent(influxQueryComponent);
|
||||||
|
LocalDateTime time = LocalDateTime.of(2026, 5, 1, 0, 0);
|
||||||
|
when(influxQueryComponent.queryValuePointMap(any(),
|
||||||
|
any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenAnswer(invocation -> {
|
||||||
|
Map<String, List<SteadyChecksquareValuePointBO>> values = emptyBatchResult(invocation.getArgument(0));
|
||||||
|
putPoint(values, "v_3", time, "10");
|
||||||
|
putPoint(values, "v_4", time, "31");
|
||||||
|
putPoint(values, "v_5", time, "12");
|
||||||
|
putPoint(values, "v_7", time, "14");
|
||||||
|
return values;
|
||||||
|
});
|
||||||
|
SteadyTrendIndicatorDefinitionBO indicator = new SteadyTrendIndicatorCatalog().getIndicator("V_HARMONIC");
|
||||||
|
|
||||||
|
SteadyChecksquareHarmonicParityRuleVO result = component.check("line-001", indicator,
|
||||||
|
time, time, 1);
|
||||||
|
|
||||||
|
Assertions.assertEquals(Boolean.TRUE, result.getAbnormal());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(1), result.getAbnormalPointCount());
|
||||||
|
SteadyChecksquareHarmonicParityDetailVO detail = result.getAbnormalDetails().get(0);
|
||||||
|
Assertions.assertEquals("2026-05-01 00:00:00", detail.getTime());
|
||||||
|
Assertions.assertEquals("A", detail.getPhase());
|
||||||
|
Assertions.assertEquals("AVG", detail.getStatType());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(4), detail.getEvenHarmonicOrder());
|
||||||
|
Assertions.assertEquals(new BigDecimal("31"), detail.getEvenValue());
|
||||||
|
Assertions.assertEquals(Arrays.asList(3, 5, 7), detail.getOddHarmonicOrders());
|
||||||
|
Assertions.assertEquals(new BigDecimal("12"), detail.getOddMedianValue());
|
||||||
|
Assertions.assertEquals(new BigDecimal("2"), detail.getThresholdMultiplier());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldSkipWhenOddReferenceCountLessThanTwo() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareHarmonicParityRuleComponent component = new SteadyChecksquareHarmonicParityRuleComponent(influxQueryComponent);
|
||||||
|
LocalDateTime time = LocalDateTime.of(2026, 5, 1, 0, 0);
|
||||||
|
when(influxQueryComponent.queryValuePointMap(any(),
|
||||||
|
any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenAnswer(invocation -> {
|
||||||
|
Map<String, List<SteadyChecksquareValuePointBO>> values = emptyBatchResult(invocation.getArgument(0));
|
||||||
|
putPoint(values, "v_2", time, "50");
|
||||||
|
putPoint(values, "v_3", time, "10");
|
||||||
|
return values;
|
||||||
|
});
|
||||||
|
SteadyTrendIndicatorDefinitionBO indicator = new SteadyTrendIndicatorCatalog().getIndicator("V_HARMONIC");
|
||||||
|
|
||||||
|
SteadyChecksquareHarmonicParityRuleVO result = component.check("line-001", indicator,
|
||||||
|
time, time, 1);
|
||||||
|
|
||||||
|
Assertions.assertEquals(Boolean.FALSE, result.getAbnormal());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(0), result.getAbnormalPointCount());
|
||||||
|
Assertions.assertTrue(result.getAbnormalDetails().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldSkipEvenHarmonicWhenValueNotGreaterThanDeadband() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareHarmonicParityRuleComponent component = new SteadyChecksquareHarmonicParityRuleComponent(influxQueryComponent);
|
||||||
|
LocalDateTime time = LocalDateTime.of(2026, 5, 1, 0, 0);
|
||||||
|
when(influxQueryComponent.queryValuePointMap(any(),
|
||||||
|
any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenAnswer(invocation -> {
|
||||||
|
Map<String, List<SteadyChecksquareValuePointBO>> values = emptyBatchResult(invocation.getArgument(0));
|
||||||
|
putPoint(values, "v_3", time, "0.01");
|
||||||
|
putPoint(values, "v_4", time, "0.10");
|
||||||
|
putPoint(values, "v_5", time, "0.02");
|
||||||
|
return values;
|
||||||
|
});
|
||||||
|
SteadyTrendIndicatorDefinitionBO indicator = new SteadyTrendIndicatorCatalog().getIndicator("V_HARMONIC");
|
||||||
|
|
||||||
|
SteadyChecksquareHarmonicParityRuleVO result = component.check("line-001", indicator,
|
||||||
|
time, time, 1);
|
||||||
|
|
||||||
|
Assertions.assertEquals(Boolean.FALSE, result.getAbnormal());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(0), result.getAbnormalPointCount());
|
||||||
|
Assertions.assertTrue(result.getAbnormalDetails().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldSkipNonHarmonicIndicator() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareHarmonicParityRuleComponent component = new SteadyChecksquareHarmonicParityRuleComponent(influxQueryComponent);
|
||||||
|
SteadyTrendIndicatorDefinitionBO indicator = new SteadyTrendIndicatorCatalog().getIndicator("V_RMS");
|
||||||
|
|
||||||
|
SteadyChecksquareHarmonicParityRuleVO result = component.check("line-001", indicator,
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0),
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 1), 1);
|
||||||
|
|
||||||
|
Assertions.assertEquals(Boolean.FALSE, result.getAbnormal());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(0), result.getAbnormalPointCount());
|
||||||
|
Assertions.assertTrue(result.getAbnormalDetails().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
private SteadyChecksquareValuePointBO point(LocalDateTime time, String value) {
|
||||||
|
SteadyChecksquareValuePointBO point = new SteadyChecksquareValuePointBO();
|
||||||
|
point.setTime(time);
|
||||||
|
point.setValue(new BigDecimal(value));
|
||||||
|
return point;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, List<SteadyChecksquareValuePointBO>> emptyBatchResult(List<SteadyTrendResolvedFieldBO> fields) {
|
||||||
|
Map<String, List<SteadyChecksquareValuePointBO>> result =
|
||||||
|
new LinkedHashMap<String, List<SteadyChecksquareValuePointBO>>();
|
||||||
|
for (SteadyTrendResolvedFieldBO field : fields) {
|
||||||
|
result.put(field.getField(), Collections.<SteadyChecksquareValuePointBO>emptyList());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void putPoint(Map<String, List<SteadyChecksquareValuePointBO>> values, String field,
|
||||||
|
LocalDateTime time, String value) {
|
||||||
|
if (values.containsKey(field)) {
|
||||||
|
values.put(field, Collections.singletonList(point(time, value)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,10 +2,16 @@ package com.njcn.gather.steady.checksquare.component;
|
|||||||
|
|
||||||
import com.njcn.gather.steady.datavie.config.SteadyInfluxDbProperties;
|
import com.njcn.gather.steady.datavie.config.SteadyInfluxDbProperties;
|
||||||
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO;
|
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO;
|
||||||
|
import com.sun.net.httpserver.HttpServer;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据校验 InfluxQL 构造契约测试。
|
* 数据校验 InfluxQL 构造契约测试。
|
||||||
@@ -33,4 +39,115 @@ class SteadyChecksquareInfluxQueryComponentTest {
|
|||||||
Assertions.assertFalse(query.contains("quality_flag"));
|
Assertions.assertFalse(query.contains("quality_flag"));
|
||||||
Assertions.assertFalse(query.contains("GROUP BY time"));
|
Assertions.assertFalse(query.contains("GROUP BY time"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldBuildValuePointQueryWithStatTypeFilter() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent component = new SteadyChecksquareInfluxQueryComponent(new SteadyInfluxDbProperties());
|
||||||
|
SteadyTrendResolvedFieldBO field = new SteadyTrendResolvedFieldBO();
|
||||||
|
field.setMeasurement("data_v");
|
||||||
|
field.setField("rms");
|
||||||
|
field.setLineId("line-001");
|
||||||
|
field.setPhase("A");
|
||||||
|
field.setStatType("CP95");
|
||||||
|
|
||||||
|
String query = component.buildValuePointQuery(field,
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0, 0),
|
||||||
|
LocalDateTime.of(2026, 5, 1, 23, 59, 59));
|
||||||
|
|
||||||
|
Assertions.assertTrue(query.contains("SELECT \"rms\" AS \"value\""));
|
||||||
|
Assertions.assertTrue(query.contains("\"line_id\" = 'line-001'"));
|
||||||
|
Assertions.assertTrue(query.contains("\"phasic_type\" = 'A'"));
|
||||||
|
Assertions.assertTrue(query.contains("\"value_type\" = 'CP95'"));
|
||||||
|
Assertions.assertTrue(query.endsWith("ORDER BY time ASC"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReuseValuePointQueryWithinRequestCache() throws Exception {
|
||||||
|
AtomicInteger requestCount = new AtomicInteger();
|
||||||
|
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
|
||||||
|
server.createContext("/query", exchange -> {
|
||||||
|
requestCount.incrementAndGet();
|
||||||
|
byte[] body = ("{\"results\":[{\"series\":[{\"values\":["
|
||||||
|
+ "[\"2026-05-01T00:00:00Z\",1.23],"
|
||||||
|
+ "[\"2026-05-01T00:01:00Z\",2.34]"
|
||||||
|
+ "]}]}]}").getBytes(StandardCharsets.UTF_8);
|
||||||
|
exchange.sendResponseHeaders(200, body.length);
|
||||||
|
exchange.getResponseBody().write(body);
|
||||||
|
exchange.close();
|
||||||
|
});
|
||||||
|
server.start();
|
||||||
|
try {
|
||||||
|
SteadyInfluxDbProperties properties = new SteadyInfluxDbProperties();
|
||||||
|
properties.setUrl("http://127.0.0.1:" + server.getAddress().getPort());
|
||||||
|
properties.setDatabase("steady");
|
||||||
|
SteadyChecksquareInfluxQueryComponent component = new SteadyChecksquareInfluxQueryComponent(properties);
|
||||||
|
SteadyTrendResolvedFieldBO field = new SteadyTrendResolvedFieldBO();
|
||||||
|
field.setMeasurement("data_v");
|
||||||
|
field.setField("rms");
|
||||||
|
field.setLineId("line-001");
|
||||||
|
field.setPhase("A");
|
||||||
|
field.setStatType("AVG");
|
||||||
|
|
||||||
|
component.enableRequestCache();
|
||||||
|
component.queryExistingSlots(field,
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0, 0),
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 1, 0), 1);
|
||||||
|
component.queryValuePoints(field,
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0, 0),
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 1, 0), 1);
|
||||||
|
component.clearRequestCache();
|
||||||
|
|
||||||
|
Assertions.assertEquals(1, requestCount.get());
|
||||||
|
} finally {
|
||||||
|
server.stop(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldQueryMultipleValueFieldsOnce() throws Exception {
|
||||||
|
AtomicInteger requestCount = new AtomicInteger();
|
||||||
|
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
|
||||||
|
server.createContext("/query", exchange -> {
|
||||||
|
requestCount.incrementAndGet();
|
||||||
|
byte[] body = ("{\"results\":[{\"series\":[{\"columns\":[\"time\",\"h_2\",\"h_3\"],\"values\":["
|
||||||
|
+ "[\"2026-05-01T00:00:00Z\",1.23,2.34],"
|
||||||
|
+ "[\"2026-05-01T00:01:00Z\",3.45,null]"
|
||||||
|
+ "]}]}]}").getBytes(StandardCharsets.UTF_8);
|
||||||
|
exchange.sendResponseHeaders(200, body.length);
|
||||||
|
exchange.getResponseBody().write(body);
|
||||||
|
exchange.close();
|
||||||
|
});
|
||||||
|
server.start();
|
||||||
|
try {
|
||||||
|
SteadyInfluxDbProperties properties = new SteadyInfluxDbProperties();
|
||||||
|
properties.setUrl("http://127.0.0.1:" + server.getAddress().getPort());
|
||||||
|
properties.setDatabase("steady");
|
||||||
|
SteadyChecksquareInfluxQueryComponent component = new SteadyChecksquareInfluxQueryComponent(properties);
|
||||||
|
SteadyTrendResolvedFieldBO h2 = buildField("h_2");
|
||||||
|
SteadyTrendResolvedFieldBO h3 = buildField("h_3");
|
||||||
|
|
||||||
|
component.enableRequestCache();
|
||||||
|
Map<String, java.util.List<com.njcn.gather.steady.checksquare.pojo.bo.SteadyChecksquareValuePointBO>> result =
|
||||||
|
component.queryValuePointMap(Arrays.asList(h2, h3),
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0, 0),
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 1, 0), 1);
|
||||||
|
component.clearRequestCache();
|
||||||
|
|
||||||
|
Assertions.assertEquals(1, requestCount.get());
|
||||||
|
Assertions.assertEquals(2, result.get("h_2").size());
|
||||||
|
Assertions.assertEquals(1, result.get("h_3").size());
|
||||||
|
} finally {
|
||||||
|
server.stop(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SteadyTrendResolvedFieldBO buildField(String fieldName) {
|
||||||
|
SteadyTrendResolvedFieldBO field = new SteadyTrendResolvedFieldBO();
|
||||||
|
field.setMeasurement("data_harmonic");
|
||||||
|
field.setField(fieldName);
|
||||||
|
field.setLineId("line-001");
|
||||||
|
field.setPhase("A");
|
||||||
|
field.setStatType("AVG");
|
||||||
|
return field;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,219 @@
|
|||||||
|
package com.njcn.gather.steady.checksquare.component;
|
||||||
|
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.bo.SteadyChecksquareValuePointBO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareValueOrderRuleVO;
|
||||||
|
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendIndicatorDefinitionBO;
|
||||||
|
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendSeriesFieldBO;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据校验指标值大小关系规则测试。
|
||||||
|
*/
|
||||||
|
class SteadyChecksquareValueOrderRuleComponentTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldMarkIndicatorAbnormalWhenInvalidPointCountGreaterThanOne() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareValueOrderRuleComponent component = new SteadyChecksquareValueOrderRuleComponent(influxQueryComponent);
|
||||||
|
LocalDateTime firstTime = LocalDateTime.of(2026, 5, 1, 0, 0);
|
||||||
|
LocalDateTime secondTime = LocalDateTime.of(2026, 5, 1, 0, 1);
|
||||||
|
when(influxQueryComponent.queryValuePoints(any(), any(LocalDateTime.class), any(LocalDateTime.class), eq(1)))
|
||||||
|
.thenAnswer(invocation -> {
|
||||||
|
String statType = invocation.getArgument(0, com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO.class).getStatType();
|
||||||
|
if ("MAX".equals(statType)) {
|
||||||
|
return Arrays.asList(point(firstTime, "8"), point(secondTime, "9"));
|
||||||
|
}
|
||||||
|
if ("CP95".equals(statType)) {
|
||||||
|
return Arrays.asList(point(firstTime, "9"), point(secondTime, "10"));
|
||||||
|
}
|
||||||
|
if ("AVG".equals(statType)) {
|
||||||
|
return Arrays.asList(point(firstTime, "7"), point(secondTime, "8"));
|
||||||
|
}
|
||||||
|
if ("MIN".equals(statType)) {
|
||||||
|
return Arrays.asList(point(firstTime, "1"), point(secondTime, "9"));
|
||||||
|
}
|
||||||
|
return Collections.emptyList();
|
||||||
|
});
|
||||||
|
|
||||||
|
SteadyChecksquareValueOrderRuleVO result = component.check("line-001", indicator(), null,
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0), LocalDateTime.of(2026, 5, 1, 0, 2), 1);
|
||||||
|
|
||||||
|
Assertions.assertEquals(Integer.valueOf(2), result.getAbnormalPointCount());
|
||||||
|
Assertions.assertEquals(Boolean.TRUE, result.getAbnormal());
|
||||||
|
Assertions.assertEquals(2, result.getAbnormalDetails().size());
|
||||||
|
Assertions.assertEquals("2026-05-01 00:00:00", result.getAbnormalDetails().get(0).getTime());
|
||||||
|
Assertions.assertEquals("A", result.getAbnormalDetails().get(0).getPhase());
|
||||||
|
Assertions.assertEquals(new BigDecimal("8"), result.getAbnormalDetails().get(0).getMaxValue());
|
||||||
|
Assertions.assertEquals(new BigDecimal("1"), result.getAbnormalDetails().get(0).getMinValue());
|
||||||
|
Assertions.assertEquals(new BigDecimal("7"), result.getAbnormalDetails().get(0).getAvgValue());
|
||||||
|
Assertions.assertEquals(new BigDecimal("9"), result.getAbnormalDetails().get(0).getCp95Value());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldTreatEqualAdjacentStatValuesAsNormal() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareValueOrderRuleComponent component = new SteadyChecksquareValueOrderRuleComponent(influxQueryComponent);
|
||||||
|
LocalDateTime time = LocalDateTime.of(2026, 5, 1, 0, 0);
|
||||||
|
when(influxQueryComponent.queryValuePoints(any(), any(LocalDateTime.class), any(LocalDateTime.class), eq(1)))
|
||||||
|
.thenAnswer(invocation -> {
|
||||||
|
String statType = invocation.getArgument(0, com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO.class).getStatType();
|
||||||
|
if ("MAX".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "10"));
|
||||||
|
}
|
||||||
|
if ("CP95".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "10"));
|
||||||
|
}
|
||||||
|
if ("AVG".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "8"));
|
||||||
|
}
|
||||||
|
if ("MIN".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "8"));
|
||||||
|
}
|
||||||
|
return Collections.emptyList();
|
||||||
|
});
|
||||||
|
|
||||||
|
SteadyChecksquareValueOrderRuleVO result = component.check("line-001", indicator(), null,
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0), LocalDateTime.of(2026, 5, 1, 0, 1), 1);
|
||||||
|
|
||||||
|
Assertions.assertEquals(Integer.valueOf(0), result.getAbnormalPointCount());
|
||||||
|
Assertions.assertEquals(Boolean.FALSE, result.getAbnormal());
|
||||||
|
Assertions.assertTrue(result.getAbnormalDetails().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldNotMarkIndicatorAbnormalWhenOnlyOneInvalidPointExists() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareValueOrderRuleComponent component = new SteadyChecksquareValueOrderRuleComponent(influxQueryComponent);
|
||||||
|
LocalDateTime time = LocalDateTime.of(2026, 5, 1, 0, 0);
|
||||||
|
when(influxQueryComponent.queryValuePoints(any(), any(LocalDateTime.class), any(LocalDateTime.class), eq(1)))
|
||||||
|
.thenAnswer(invocation -> {
|
||||||
|
String statType = invocation.getArgument(0, com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO.class).getStatType();
|
||||||
|
if ("MAX".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "8"));
|
||||||
|
}
|
||||||
|
if ("CP95".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "10"));
|
||||||
|
}
|
||||||
|
if ("AVG".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "8"));
|
||||||
|
}
|
||||||
|
if ("MIN".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "1"));
|
||||||
|
}
|
||||||
|
return Collections.emptyList();
|
||||||
|
});
|
||||||
|
|
||||||
|
SteadyChecksquareValueOrderRuleVO result = component.check("line-001", indicator(), null,
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0), LocalDateTime.of(2026, 5, 1, 0, 1), 1);
|
||||||
|
|
||||||
|
Assertions.assertEquals(Integer.valueOf(1), result.getAbnormalPointCount());
|
||||||
|
Assertions.assertEquals(Boolean.FALSE, result.getAbnormal());
|
||||||
|
Assertions.assertEquals(1, result.getAbnormalDetails().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldFillHarmonicOrderInAbnormalDetailForHarmonicIndicator() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareValueOrderRuleComponent component = new SteadyChecksquareValueOrderRuleComponent(influxQueryComponent);
|
||||||
|
LocalDateTime time = LocalDateTime.of(2026, 5, 1, 0, 0);
|
||||||
|
when(influxQueryComponent.queryValuePoints(any(), any(LocalDateTime.class), any(LocalDateTime.class), eq(1)))
|
||||||
|
.thenAnswer(invocation -> {
|
||||||
|
String statType = invocation.getArgument(0, com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO.class).getStatType();
|
||||||
|
if ("MAX".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "8"));
|
||||||
|
}
|
||||||
|
if ("CP95".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "10"));
|
||||||
|
}
|
||||||
|
if ("AVG".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "8"));
|
||||||
|
}
|
||||||
|
if ("MIN".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "1"));
|
||||||
|
}
|
||||||
|
return Collections.emptyList();
|
||||||
|
});
|
||||||
|
SteadyTrendIndicatorDefinitionBO indicator = indicator();
|
||||||
|
indicator.setHarmonic(true);
|
||||||
|
indicator.setHarmonicFieldPrefix("v");
|
||||||
|
|
||||||
|
SteadyChecksquareValueOrderRuleVO result = component.check("line-001", indicator, 5,
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0), LocalDateTime.of(2026, 5, 1, 0, 1), 1);
|
||||||
|
|
||||||
|
Assertions.assertEquals(1, result.getAbnormalDetails().size());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(5), result.getAbnormalDetails().get(0).getHarmonicOrder());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldSkipPointWhenAnyRequiredStatValueMissing() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareValueOrderRuleComponent component = new SteadyChecksquareValueOrderRuleComponent(influxQueryComponent);
|
||||||
|
LocalDateTime time = LocalDateTime.of(2026, 5, 1, 0, 0);
|
||||||
|
when(influxQueryComponent.queryValuePoints(any(), any(LocalDateTime.class), any(LocalDateTime.class), eq(1)))
|
||||||
|
.thenAnswer(invocation -> {
|
||||||
|
String statType = invocation.getArgument(0, com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO.class).getStatType();
|
||||||
|
if ("MAX".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "10"));
|
||||||
|
}
|
||||||
|
if ("CP95".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "11"));
|
||||||
|
}
|
||||||
|
if ("MIN".equals(statType)) {
|
||||||
|
return Collections.singletonList(point(time, "1"));
|
||||||
|
}
|
||||||
|
return Collections.emptyList();
|
||||||
|
});
|
||||||
|
|
||||||
|
SteadyChecksquareValueOrderRuleVO result = component.check("line-001", indicator(), null,
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0), LocalDateTime.of(2026, 5, 1, 0, 1), 1);
|
||||||
|
|
||||||
|
Assertions.assertEquals(Integer.valueOf(0), result.getAbnormalPointCount());
|
||||||
|
Assertions.assertEquals(Boolean.FALSE, result.getAbnormal());
|
||||||
|
Assertions.assertTrue(result.getAbnormalDetails().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldSkipIndicatorWhenNotAllFourStatsSupported() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareValueOrderRuleComponent component = new SteadyChecksquareValueOrderRuleComponent(influxQueryComponent);
|
||||||
|
SteadyTrendIndicatorDefinitionBO indicator = indicator();
|
||||||
|
indicator.setSupportStats(Collections.singletonList("AVG"));
|
||||||
|
|
||||||
|
SteadyChecksquareValueOrderRuleVO result = component.check("line-001", indicator, null,
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0), LocalDateTime.of(2026, 5, 1, 0, 1), 1);
|
||||||
|
|
||||||
|
Assertions.assertEquals(Integer.valueOf(0), result.getAbnormalPointCount());
|
||||||
|
Assertions.assertEquals(Boolean.FALSE, result.getAbnormal());
|
||||||
|
Assertions.assertTrue(result.getAbnormalDetails().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
private SteadyTrendIndicatorDefinitionBO indicator() {
|
||||||
|
SteadyTrendIndicatorDefinitionBO indicator = new SteadyTrendIndicatorDefinitionBO();
|
||||||
|
indicator.setIndicatorCode("V_RMS");
|
||||||
|
indicator.setName("相电压有效值");
|
||||||
|
indicator.setTableName("data_v");
|
||||||
|
indicator.setPhaseCodes(Collections.singletonList("A"));
|
||||||
|
indicator.setSeriesFields(Collections.singletonList(new SteadyTrendSeriesFieldBO("rms", "相电压有效值")));
|
||||||
|
indicator.setSupportStats(Arrays.asList("AVG", "MAX", "MIN", "CP95"));
|
||||||
|
indicator.setUnit("V");
|
||||||
|
return indicator;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SteadyChecksquareValuePointBO point(LocalDateTime time, String value) {
|
||||||
|
SteadyChecksquareValuePointBO point = new SteadyChecksquareValuePointBO();
|
||||||
|
point.setTime(time);
|
||||||
|
point.setValue(new BigDecimal(value));
|
||||||
|
return point;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package com.njcn.gather.steady.checksquare.controller;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
@@ -17,8 +18,21 @@ class SteadyChecksquareControllerTest {
|
|||||||
RequestMapping requestMapping = SteadyChecksquareController.class.getAnnotation(RequestMapping.class);
|
RequestMapping requestMapping = SteadyChecksquareController.class.getAnnotation(RequestMapping.class);
|
||||||
Assertions.assertArrayEquals(new String[]{"/steady/data-view/checksquare"}, requestMapping.value());
|
Assertions.assertArrayEquals(new String[]{"/steady/data-view/checksquare"}, requestMapping.value());
|
||||||
|
|
||||||
Method method = SteadyChecksquareController.class.getDeclaredMethod("query", com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareQueryParam.class);
|
Method queryMethod = SteadyChecksquareController.class.getDeclaredMethod("query", com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareHistoryQueryParam.class);
|
||||||
PostMapping postMapping = method.getAnnotation(PostMapping.class);
|
PostMapping queryMapping = queryMethod.getAnnotation(PostMapping.class);
|
||||||
Assertions.assertArrayEquals(new String[]{"/query"}, postMapping.value());
|
Assertions.assertArrayEquals(new String[]{"/query"}, queryMapping.value());
|
||||||
|
|
||||||
|
Method createMethod = SteadyChecksquareController.class.getDeclaredMethod("create", com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareQueryParam.class);
|
||||||
|
PostMapping createMapping = createMethod.getAnnotation(PostMapping.class);
|
||||||
|
Assertions.assertArrayEquals(new String[]{"/create"}, createMapping.value());
|
||||||
|
|
||||||
|
Method detailMethod = SteadyChecksquareController.class.getDeclaredMethod("detail", String.class);
|
||||||
|
GetMapping detailMapping = detailMethod.getAnnotation(GetMapping.class);
|
||||||
|
Assertions.assertArrayEquals(new String[]{"/detail"}, detailMapping.value());
|
||||||
|
|
||||||
|
Method itemDetailMethod = SteadyChecksquareController.class.getDeclaredMethod("itemDetail",
|
||||||
|
String.class, String.class, String.class, Integer.class, Integer.class);
|
||||||
|
GetMapping itemDetailMapping = itemDetailMethod.getAnnotation(GetMapping.class);
|
||||||
|
Assertions.assertArrayEquals(new String[]{"/item-detail"}, itemDetailMapping.value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class SteadyChecksquareQueryParamTest {
|
|||||||
Assertions.assertNull(field("qualityFlag"));
|
Assertions.assertNull(field("qualityFlag"));
|
||||||
Assertions.assertNull(field("statTypes"));
|
Assertions.assertNull(field("statTypes"));
|
||||||
Assertions.assertNull(field("phases"));
|
Assertions.assertNull(field("phases"));
|
||||||
Assertions.assertNotNull(field("harmonicOrders"));
|
Assertions.assertNull(field("harmonicOrders"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Field field(String name) {
|
private Field field(String name) {
|
||||||
|
|||||||
@@ -1,26 +1,54 @@
|
|||||||
package com.njcn.gather.steady.checksquare.service.impl;
|
package com.njcn.gather.steady.checksquare.service.impl;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.njcn.gather.steady.checksquare.component.SteadyChecksquareCalculator;
|
import com.njcn.gather.steady.checksquare.component.SteadyChecksquareCalculator;
|
||||||
|
import com.njcn.gather.steady.checksquare.component.SteadyChecksquareHarmonicParityRuleComponent;
|
||||||
import com.njcn.gather.steady.checksquare.component.SteadyChecksquareInfluxQueryComponent;
|
import com.njcn.gather.steady.checksquare.component.SteadyChecksquareInfluxQueryComponent;
|
||||||
|
import com.njcn.gather.steady.checksquare.component.SteadyChecksquareValueOrderRuleComponent;
|
||||||
import com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareQueryParam;
|
import com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareQueryParam;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareDetailPO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareItemPO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareStatSummaryPO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.po.SteadyChecksquareTaskPO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareHarmonicParityDetailVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareHarmonicParityRuleVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareItemDetailVO;
|
||||||
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareItemVO;
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareItemVO;
|
||||||
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareQueryVO;
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareQueryVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareStatSummaryVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareValueOrderDetailVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareValueOrderRuleVO;
|
||||||
|
import com.njcn.gather.steady.checksquare.service.SteadyChecksquareDetailService;
|
||||||
|
import com.njcn.gather.steady.checksquare.service.SteadyChecksquareItemService;
|
||||||
|
import com.njcn.gather.steady.checksquare.service.SteadyChecksquareStatSummaryService;
|
||||||
|
import com.njcn.gather.steady.checksquare.service.SteadyChecksquareTaskService;
|
||||||
import com.njcn.gather.steady.datavie.component.SteadyTrendIndicatorCatalog;
|
import com.njcn.gather.steady.datavie.component.SteadyTrendIndicatorCatalog;
|
||||||
|
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendResolvedFieldBO;
|
||||||
import com.njcn.gather.tool.adddata.component.AddDataTimeSlotCalculator;
|
import com.njcn.gather.tool.adddata.component.AddDataTimeSlotCalculator;
|
||||||
import com.njcn.gather.tool.addledger.pojo.vo.AddLedgerLinePathVO;
|
import com.njcn.gather.tool.addledger.pojo.vo.AddLedgerLinePathVO;
|
||||||
import com.njcn.gather.tool.addledger.service.AddLedgerService;
|
import com.njcn.gather.tool.addledger.service.AddLedgerService;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,12 +56,28 @@ import static org.mockito.Mockito.when;
|
|||||||
*/
|
*/
|
||||||
class SteadyChecksquareServiceImplTest {
|
class SteadyChecksquareServiceImplTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldNotOpenTransactionAroundCreateCalculation() throws Exception {
|
||||||
|
Method createMethod = SteadyChecksquareServiceImpl.class.getMethod("create", SteadyChecksquareQueryParam.class);
|
||||||
|
|
||||||
|
Assertions.assertNull(createMethod.getAnnotation(Transactional.class));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldUseFixedFlickerIntervalsPerIndicator() {
|
void shouldUseFixedFlickerIntervalsPerIndicator() {
|
||||||
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareValueOrderRuleComponent valueOrderRuleComponent = mock(SteadyChecksquareValueOrderRuleComponent.class);
|
||||||
|
SteadyChecksquareHarmonicParityRuleComponent harmonicParityRuleComponent = mock(SteadyChecksquareHarmonicParityRuleComponent.class);
|
||||||
AddLedgerService addLedgerService = mock(AddLedgerService.class);
|
AddLedgerService addLedgerService = mock(AddLedgerService.class);
|
||||||
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
||||||
influxQueryComponent, new SteadyChecksquareCalculator(), new AddDataTimeSlotCalculator(), addLedgerService);
|
influxQueryComponent, new SteadyChecksquareCalculator(), valueOrderRuleComponent, harmonicParityRuleComponent,
|
||||||
|
new AddDataTimeSlotCalculator(), addLedgerService, mock(SteadyChecksquareTaskService.class),
|
||||||
|
mock(SteadyChecksquareItemService.class), mock(SteadyChecksquareStatSummaryService.class),
|
||||||
|
mock(SteadyChecksquareDetailService.class), new ObjectMapper());
|
||||||
|
when(valueOrderRuleComponent.check(any(), any(), any(), any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenReturn(emptyRuleResult());
|
||||||
|
when(harmonicParityRuleComponent.check(any(), any(), any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenReturn(emptyHarmonicParityRuleResult());
|
||||||
AddLedgerLinePathVO linePath = new AddLedgerLinePathVO();
|
AddLedgerLinePathVO linePath = new AddLedgerLinePathVO();
|
||||||
linePath.setLineId("line-001");
|
linePath.setLineId("line-001");
|
||||||
linePath.setLineName("进线一");
|
linePath.setLineName("进线一");
|
||||||
@@ -54,7 +98,7 @@ class SteadyChecksquareServiceImplTest {
|
|||||||
param.setTimeStart("2026-05-01 00:00:00");
|
param.setTimeStart("2026-05-01 00:00:00");
|
||||||
param.setTimeEnd("2026-05-01 02:00:00");
|
param.setTimeEnd("2026-05-01 02:00:00");
|
||||||
|
|
||||||
SteadyChecksquareQueryVO result = service.query(param);
|
SteadyChecksquareQueryVO result = calculate(service, param);
|
||||||
|
|
||||||
Assertions.assertEquals(Integer.valueOf(1), result.getIntervalMinutes());
|
Assertions.assertEquals(Integer.valueOf(1), result.getIntervalMinutes());
|
||||||
Assertions.assertEquals(3, result.getItems().size());
|
Assertions.assertEquals(3, result.getItems().size());
|
||||||
@@ -64,11 +108,20 @@ class SteadyChecksquareServiceImplTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldOnlyQueryRequestedHarmonicOrders() {
|
void shouldAggregateAllHarmonicOrdersIntoIndicatorItem() {
|
||||||
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareValueOrderRuleComponent valueOrderRuleComponent = mock(SteadyChecksquareValueOrderRuleComponent.class);
|
||||||
|
SteadyChecksquareHarmonicParityRuleComponent harmonicParityRuleComponent = mock(SteadyChecksquareHarmonicParityRuleComponent.class);
|
||||||
AddLedgerService addLedgerService = mock(AddLedgerService.class);
|
AddLedgerService addLedgerService = mock(AddLedgerService.class);
|
||||||
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
||||||
influxQueryComponent, new SteadyChecksquareCalculator(), new AddDataTimeSlotCalculator(), addLedgerService);
|
influxQueryComponent, new SteadyChecksquareCalculator(), valueOrderRuleComponent, harmonicParityRuleComponent,
|
||||||
|
new AddDataTimeSlotCalculator(), addLedgerService, mock(SteadyChecksquareTaskService.class),
|
||||||
|
mock(SteadyChecksquareItemService.class), mock(SteadyChecksquareStatSummaryService.class),
|
||||||
|
mock(SteadyChecksquareDetailService.class), new ObjectMapper());
|
||||||
|
when(valueOrderRuleComponent.check(any(), any(), any(), any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenReturn(emptyRuleResult());
|
||||||
|
when(harmonicParityRuleComponent.check(any(), any(), any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenReturn(emptyHarmonicParityRuleResult());
|
||||||
AddLedgerLinePathVO linePath = new AddLedgerLinePathVO();
|
AddLedgerLinePathVO linePath = new AddLedgerLinePathVO();
|
||||||
linePath.setLineId("line-001");
|
linePath.setLineId("line-001");
|
||||||
linePath.setLineName("进线一");
|
linePath.setLineName("进线一");
|
||||||
@@ -82,22 +135,32 @@ class SteadyChecksquareServiceImplTest {
|
|||||||
SteadyChecksquareQueryParam param = new SteadyChecksquareQueryParam();
|
SteadyChecksquareQueryParam param = new SteadyChecksquareQueryParam();
|
||||||
param.setLineId("line-001");
|
param.setLineId("line-001");
|
||||||
param.setIndicatorCodes(Collections.singletonList("V_HARMONIC"));
|
param.setIndicatorCodes(Collections.singletonList("V_HARMONIC"));
|
||||||
param.setHarmonicOrders(Collections.singletonList(5));
|
|
||||||
param.setTimeStart("2026-05-01 00:00:00");
|
param.setTimeStart("2026-05-01 00:00:00");
|
||||||
param.setTimeEnd("2026-05-01 00:01:00");
|
param.setTimeEnd("2026-05-01 00:01:00");
|
||||||
|
|
||||||
SteadyChecksquareQueryVO result = service.query(param);
|
SteadyChecksquareQueryVO result = calculate(service, param);
|
||||||
|
|
||||||
Assertions.assertEquals(1, result.getItems().size());
|
Assertions.assertEquals(1, result.getItems().size());
|
||||||
Assertions.assertEquals(Integer.valueOf(5), result.getItems().get(0).getHarmonicOrder());
|
Assertions.assertEquals("line-001|V_HARMONIC", result.getItems().get(0).getItemKey());
|
||||||
|
Assertions.assertNull(result.getItems().get(0).getHarmonicOrder());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(2), result.getItems().get(0).getStatDetails().get(0).getSegments().get(0).getHarmonicOrder());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldKeepRequestedHarmonicOrdersDistinctAndOrdered() {
|
void shouldAverageHarmonicOrderResultsAndMarkAbnormalWhenAnyOrderAbnormal() {
|
||||||
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareValueOrderRuleComponent valueOrderRuleComponent = mock(SteadyChecksquareValueOrderRuleComponent.class);
|
||||||
|
SteadyChecksquareHarmonicParityRuleComponent harmonicParityRuleComponent = mock(SteadyChecksquareHarmonicParityRuleComponent.class);
|
||||||
AddLedgerService addLedgerService = mock(AddLedgerService.class);
|
AddLedgerService addLedgerService = mock(AddLedgerService.class);
|
||||||
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
||||||
influxQueryComponent, new SteadyChecksquareCalculator(), new AddDataTimeSlotCalculator(), addLedgerService);
|
influxQueryComponent, new SteadyChecksquareCalculator(), valueOrderRuleComponent, harmonicParityRuleComponent,
|
||||||
|
new AddDataTimeSlotCalculator(), addLedgerService, mock(SteadyChecksquareTaskService.class),
|
||||||
|
mock(SteadyChecksquareItemService.class), mock(SteadyChecksquareStatSummaryService.class),
|
||||||
|
mock(SteadyChecksquareDetailService.class), new ObjectMapper());
|
||||||
|
when(valueOrderRuleComponent.check(any(), any(), any(), any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenReturn(emptyRuleResult());
|
||||||
|
when(harmonicParityRuleComponent.check(any(), any(), any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenReturn(emptyHarmonicParityRuleResult());
|
||||||
AddLedgerLinePathVO linePath = new AddLedgerLinePathVO();
|
AddLedgerLinePathVO linePath = new AddLedgerLinePathVO();
|
||||||
linePath.setLineId("line-001");
|
linePath.setLineId("line-001");
|
||||||
linePath.setLineName("进线一");
|
linePath.setLineName("进线一");
|
||||||
@@ -105,21 +168,328 @@ class SteadyChecksquareServiceImplTest {
|
|||||||
when(addLedgerService.listLinePathByLineIds(eq(Collections.singletonList("line-001"))))
|
when(addLedgerService.listLinePathByLineIds(eq(Collections.singletonList("line-001"))))
|
||||||
.thenReturn(Collections.singletonMap("line-001", linePath));
|
.thenReturn(Collections.singletonMap("line-001", linePath));
|
||||||
when(influxQueryComponent.queryExistingSlots(any(), any(LocalDateTime.class), any(LocalDateTime.class), eq(1)))
|
when(influxQueryComponent.queryExistingSlots(any(), any(LocalDateTime.class), any(LocalDateTime.class), eq(1)))
|
||||||
.thenReturn(new HashSet<LocalDateTime>());
|
.thenReturn(new HashSet<LocalDateTime>(Collections.singletonList(
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0))));
|
||||||
|
SteadyChecksquareValueOrderRuleVO normalRuleResult = new SteadyChecksquareValueOrderRuleVO();
|
||||||
|
SteadyChecksquareValueOrderRuleVO abnormalRuleResult = new SteadyChecksquareValueOrderRuleVO();
|
||||||
|
SteadyChecksquareValueOrderDetailVO abnormalDetail = new SteadyChecksquareValueOrderDetailVO();
|
||||||
|
abnormalDetail.setTime("2026-05-01 00:00:00");
|
||||||
|
abnormalDetail.setPhase("A");
|
||||||
|
abnormalDetail.setHarmonicOrder(2);
|
||||||
|
abnormalRuleResult.setAbnormal(true);
|
||||||
|
abnormalRuleResult.setAbnormalPointCount(4);
|
||||||
|
abnormalRuleResult.setAbnormalDetails(Collections.singletonList(abnormalDetail));
|
||||||
|
when(valueOrderRuleComponent.check(any(), any(), any(), any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenReturn(normalRuleResult);
|
||||||
|
when(valueOrderRuleComponent.check(any(), any(), eq(2), any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenReturn(abnormalRuleResult);
|
||||||
|
when(valueOrderRuleComponent.check(any(), any(), eq(3), any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenReturn(normalRuleResult);
|
||||||
|
|
||||||
SteadyChecksquareQueryParam param = new SteadyChecksquareQueryParam();
|
SteadyChecksquareQueryParam param = new SteadyChecksquareQueryParam();
|
||||||
param.setLineId("line-001");
|
param.setLineId("line-001");
|
||||||
param.setIndicatorCodes(Collections.singletonList("V_HARMONIC"));
|
param.setIndicatorCodes(Collections.singletonList("V_HARMONIC"));
|
||||||
param.setHarmonicOrders(Arrays.asList(7, 5, 7));
|
|
||||||
param.setTimeStart("2026-05-01 00:00:00");
|
param.setTimeStart("2026-05-01 00:00:00");
|
||||||
param.setTimeEnd("2026-05-01 00:01:00");
|
param.setTimeEnd("2026-05-01 00:01:00");
|
||||||
|
|
||||||
SteadyChecksquareQueryVO result = service.query(param);
|
SteadyChecksquareQueryVO result = calculate(service, param);
|
||||||
|
|
||||||
List<SteadyChecksquareItemVO> items = result.getItems();
|
List<SteadyChecksquareItemVO> items = result.getItems();
|
||||||
Assertions.assertEquals(2, items.size());
|
Assertions.assertEquals(1, items.size());
|
||||||
Assertions.assertEquals(Integer.valueOf(7), items.get(0).getHarmonicOrder());
|
Assertions.assertEquals(Boolean.TRUE, items.get(0).getAbnormal());
|
||||||
Assertions.assertEquals(Integer.valueOf(5), items.get(1).getHarmonicOrder());
|
Assertions.assertEquals(Integer.valueOf(1), items.get(0).getAbnormalPointCount());
|
||||||
|
Assertions.assertEquals(1, items.get(0).getAbnormalDetails().size());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(2), items.get(0).getAbnormalDetails().get(0).getHarmonicOrder());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(8), items.get(0).getExpectedPointCount());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(4), items.get(0).getActualPointCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldAssembleValueOrderRuleResultIntoItem() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareValueOrderRuleComponent valueOrderRuleComponent = mock(SteadyChecksquareValueOrderRuleComponent.class);
|
||||||
|
SteadyChecksquareHarmonicParityRuleComponent harmonicParityRuleComponent = mock(SteadyChecksquareHarmonicParityRuleComponent.class);
|
||||||
|
AddLedgerService addLedgerService = mock(AddLedgerService.class);
|
||||||
|
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
||||||
|
influxQueryComponent, new SteadyChecksquareCalculator(), valueOrderRuleComponent, harmonicParityRuleComponent,
|
||||||
|
new AddDataTimeSlotCalculator(), addLedgerService, mock(SteadyChecksquareTaskService.class),
|
||||||
|
mock(SteadyChecksquareItemService.class), mock(SteadyChecksquareStatSummaryService.class),
|
||||||
|
mock(SteadyChecksquareDetailService.class), new ObjectMapper());
|
||||||
|
AddLedgerLinePathVO linePath = new AddLedgerLinePathVO();
|
||||||
|
linePath.setLineId("line-001");
|
||||||
|
linePath.setLineName("进线一");
|
||||||
|
linePath.setLineInterval(1);
|
||||||
|
when(addLedgerService.listLinePathByLineIds(eq(Collections.singletonList("line-001"))))
|
||||||
|
.thenReturn(Collections.singletonMap("line-001", linePath));
|
||||||
|
when(influxQueryComponent.queryExistingSlots(any(), any(LocalDateTime.class), any(LocalDateTime.class), eq(1)))
|
||||||
|
.thenReturn(new HashSet<LocalDateTime>(Collections.singletonList(
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0))));
|
||||||
|
SteadyChecksquareValueOrderRuleVO ruleResult = new SteadyChecksquareValueOrderRuleVO();
|
||||||
|
SteadyChecksquareValueOrderDetailVO detail = new SteadyChecksquareValueOrderDetailVO();
|
||||||
|
detail.setTime("2026-05-01 00:00:00");
|
||||||
|
detail.setPhase("A");
|
||||||
|
ruleResult.setAbnormalPointCount(2);
|
||||||
|
ruleResult.setAbnormal(true);
|
||||||
|
ruleResult.setAbnormalDetails(Collections.singletonList(detail));
|
||||||
|
when(valueOrderRuleComponent.check(any(), any(), any(), any(LocalDateTime.class), any(LocalDateTime.class), eq(1)))
|
||||||
|
.thenReturn(ruleResult);
|
||||||
|
|
||||||
|
SteadyChecksquareQueryParam param = new SteadyChecksquareQueryParam();
|
||||||
|
param.setLineId("line-001");
|
||||||
|
param.setIndicatorCodes(Collections.singletonList("V_RMS"));
|
||||||
|
param.setTimeStart("2026-05-01 00:00:00");
|
||||||
|
param.setTimeEnd("2026-05-01 00:01:00");
|
||||||
|
|
||||||
|
SteadyChecksquareQueryVO result = calculate(service, param);
|
||||||
|
|
||||||
|
SteadyChecksquareItemVO item = result.getItems().get(0);
|
||||||
|
Assertions.assertEquals(Boolean.TRUE, item.getAbnormal());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(2), item.getAbnormalPointCount());
|
||||||
|
Assertions.assertEquals(1, item.getAbnormalDetails().size());
|
||||||
|
Assertions.assertEquals("A", item.getAbnormalDetails().get(0).getPhase());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldPrefetchNormalIndicatorFieldsByMeasurementPhaseAndStat() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareValueOrderRuleComponent valueOrderRuleComponent = mock(SteadyChecksquareValueOrderRuleComponent.class);
|
||||||
|
SteadyChecksquareHarmonicParityRuleComponent harmonicParityRuleComponent = mock(SteadyChecksquareHarmonicParityRuleComponent.class);
|
||||||
|
AddLedgerService addLedgerService = mock(AddLedgerService.class);
|
||||||
|
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
||||||
|
influxQueryComponent, new SteadyChecksquareCalculator(), valueOrderRuleComponent, harmonicParityRuleComponent,
|
||||||
|
new AddDataTimeSlotCalculator(), addLedgerService, mock(SteadyChecksquareTaskService.class),
|
||||||
|
mock(SteadyChecksquareItemService.class), mock(SteadyChecksquareStatSummaryService.class),
|
||||||
|
mock(SteadyChecksquareDetailService.class), new ObjectMapper());
|
||||||
|
when(valueOrderRuleComponent.check(any(), any(), any(), any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenReturn(emptyRuleResult());
|
||||||
|
when(harmonicParityRuleComponent.check(any(), any(), any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenReturn(emptyHarmonicParityRuleResult());
|
||||||
|
AddLedgerLinePathVO linePath = new AddLedgerLinePathVO();
|
||||||
|
linePath.setLineId("line-001");
|
||||||
|
linePath.setLineName("进线一");
|
||||||
|
linePath.setLineInterval(1);
|
||||||
|
when(addLedgerService.listLinePathByLineIds(eq(Collections.singletonList("line-001"))))
|
||||||
|
.thenReturn(Collections.singletonMap("line-001", linePath));
|
||||||
|
when(influxQueryComponent.queryExistingSlots(any(), any(LocalDateTime.class), any(LocalDateTime.class), eq(1)))
|
||||||
|
.thenReturn(new HashSet<LocalDateTime>(Collections.singletonList(
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0))));
|
||||||
|
|
||||||
|
SteadyChecksquareQueryParam param = new SteadyChecksquareQueryParam();
|
||||||
|
param.setLineId("line-001");
|
||||||
|
param.setIndicatorCodes(Arrays.asList("V_RMS", "V_LINE_RMS"));
|
||||||
|
param.setTimeStart("2026-05-01 00:00:00");
|
||||||
|
param.setTimeEnd("2026-05-01 00:01:00");
|
||||||
|
|
||||||
|
calculate(service, param);
|
||||||
|
|
||||||
|
ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
|
||||||
|
verify(influxQueryComponent, times(12)).queryValuePointMap(captor.capture(),
|
||||||
|
any(LocalDateTime.class), any(LocalDateTime.class), eq(1));
|
||||||
|
boolean foundBatch = false;
|
||||||
|
for (List fields : captor.getAllValues()) {
|
||||||
|
if (fields.size() == 2) {
|
||||||
|
List<String> fieldNames = new ArrayList<String>();
|
||||||
|
for (Object field : fields) {
|
||||||
|
fieldNames.add(((SteadyTrendResolvedFieldBO) field).getField());
|
||||||
|
}
|
||||||
|
foundBatch = fieldNames.contains("rms") && fieldNames.contains("rms_lvr");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Assertions.assertTrue(foundBatch);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldAssembleHarmonicParityRuleResultIntoAggregateItem() {
|
||||||
|
SteadyChecksquareInfluxQueryComponent influxQueryComponent = mock(SteadyChecksquareInfluxQueryComponent.class);
|
||||||
|
SteadyChecksquareValueOrderRuleComponent valueOrderRuleComponent = mock(SteadyChecksquareValueOrderRuleComponent.class);
|
||||||
|
SteadyChecksquareHarmonicParityRuleComponent harmonicParityRuleComponent = mock(SteadyChecksquareHarmonicParityRuleComponent.class);
|
||||||
|
AddLedgerService addLedgerService = mock(AddLedgerService.class);
|
||||||
|
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
||||||
|
influxQueryComponent, new SteadyChecksquareCalculator(), valueOrderRuleComponent, harmonicParityRuleComponent,
|
||||||
|
new AddDataTimeSlotCalculator(), addLedgerService, mock(SteadyChecksquareTaskService.class),
|
||||||
|
mock(SteadyChecksquareItemService.class), mock(SteadyChecksquareStatSummaryService.class),
|
||||||
|
mock(SteadyChecksquareDetailService.class), new ObjectMapper());
|
||||||
|
when(valueOrderRuleComponent.check(any(), any(), any(), any(LocalDateTime.class), any(LocalDateTime.class), anyInt()))
|
||||||
|
.thenReturn(emptyRuleResult());
|
||||||
|
AddLedgerLinePathVO linePath = new AddLedgerLinePathVO();
|
||||||
|
linePath.setLineId("line-001");
|
||||||
|
linePath.setLineName("进线一");
|
||||||
|
linePath.setLineInterval(1);
|
||||||
|
when(addLedgerService.listLinePathByLineIds(eq(Collections.singletonList("line-001"))))
|
||||||
|
.thenReturn(Collections.singletonMap("line-001", linePath));
|
||||||
|
when(influxQueryComponent.queryExistingSlots(any(), any(LocalDateTime.class), any(LocalDateTime.class), eq(1)))
|
||||||
|
.thenReturn(new HashSet<LocalDateTime>(Collections.singletonList(
|
||||||
|
LocalDateTime.of(2026, 5, 1, 0, 0))));
|
||||||
|
SteadyChecksquareHarmonicParityRuleVO ruleResult = new SteadyChecksquareHarmonicParityRuleVO();
|
||||||
|
SteadyChecksquareHarmonicParityDetailVO detail = new SteadyChecksquareHarmonicParityDetailVO();
|
||||||
|
detail.setTime("2026-05-01 00:00:00");
|
||||||
|
detail.setPhase("A");
|
||||||
|
detail.setStatType("AVG");
|
||||||
|
detail.setEvenHarmonicOrder(4);
|
||||||
|
ruleResult.setAbnormal(true);
|
||||||
|
ruleResult.setAbnormalPointCount(1);
|
||||||
|
ruleResult.setAbnormalDetails(Collections.singletonList(detail));
|
||||||
|
when(harmonicParityRuleComponent.check(any(), any(), any(LocalDateTime.class), any(LocalDateTime.class), eq(1)))
|
||||||
|
.thenReturn(ruleResult);
|
||||||
|
|
||||||
|
SteadyChecksquareQueryParam param = new SteadyChecksquareQueryParam();
|
||||||
|
param.setLineId("line-001");
|
||||||
|
param.setIndicatorCodes(Collections.singletonList("V_HARMONIC"));
|
||||||
|
param.setTimeStart("2026-05-01 00:00:00");
|
||||||
|
param.setTimeEnd("2026-05-01 00:01:00");
|
||||||
|
|
||||||
|
SteadyChecksquareQueryVO result = calculate(service, param);
|
||||||
|
|
||||||
|
SteadyChecksquareItemVO item = result.getItems().get(0);
|
||||||
|
Assertions.assertNull(item.getHarmonicOrder());
|
||||||
|
Assertions.assertEquals(Boolean.TRUE, item.getHarmonicParityAbnormal());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(1), item.getHarmonicParityAbnormalPointCount());
|
||||||
|
Assertions.assertEquals("AVG", item.getHarmonicParityAbnormalDetails().get(0).getStatType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldRejectUnsupportedItemDetailType() {
|
||||||
|
SteadyChecksquareItemService itemService = mock(SteadyChecksquareItemService.class);
|
||||||
|
SteadyChecksquareItemPO item = new SteadyChecksquareItemPO();
|
||||||
|
item.setId("item-001");
|
||||||
|
item.setState(1);
|
||||||
|
when(itemService.getById("item-001")).thenReturn(item);
|
||||||
|
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
||||||
|
mock(SteadyChecksquareInfluxQueryComponent.class), new SteadyChecksquareCalculator(),
|
||||||
|
mock(SteadyChecksquareValueOrderRuleComponent.class), mock(SteadyChecksquareHarmonicParityRuleComponent.class),
|
||||||
|
new AddDataTimeSlotCalculator(), mock(AddLedgerService.class), mock(SteadyChecksquareTaskService.class),
|
||||||
|
itemService, mock(SteadyChecksquareStatSummaryService.class),
|
||||||
|
mock(SteadyChecksquareDetailService.class), new ObjectMapper());
|
||||||
|
|
||||||
|
Assertions.assertThrows(RuntimeException.class, () -> service.itemDetail("item-001", "UNKNOWN", null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldLoadDetailSummariesInSingleBatch() {
|
||||||
|
SteadyChecksquareTaskService taskService = mock(SteadyChecksquareTaskService.class);
|
||||||
|
SteadyChecksquareItemService itemService = mock(SteadyChecksquareItemService.class);
|
||||||
|
SteadyChecksquareStatSummaryService statSummaryService = mock(SteadyChecksquareStatSummaryService.class);
|
||||||
|
LambdaQueryChainWrapper<SteadyChecksquareItemPO> itemQuery = mock(LambdaQueryChainWrapper.class);
|
||||||
|
LambdaQueryChainWrapper<SteadyChecksquareStatSummaryPO> summaryQuery = mock(LambdaQueryChainWrapper.class);
|
||||||
|
SteadyChecksquareTaskPO task = new SteadyChecksquareTaskPO();
|
||||||
|
task.setId("task-001");
|
||||||
|
task.setState(1);
|
||||||
|
task.setLineId("line-001");
|
||||||
|
task.setLineName("进线一");
|
||||||
|
task.setTimeStart(LocalDateTime.of(2026, 5, 1, 0, 0));
|
||||||
|
task.setTimeEnd(LocalDateTime.of(2026, 5, 1, 0, 1));
|
||||||
|
task.setIntervalMinutes(1);
|
||||||
|
SteadyChecksquareItemPO item1 = buildItemPO("item-001", "V_RMS");
|
||||||
|
SteadyChecksquareItemPO item2 = buildItemPO("item-002", "FREQ");
|
||||||
|
SteadyChecksquareStatSummaryPO summary1 = buildSummaryPO("item-001", "AVG");
|
||||||
|
SteadyChecksquareStatSummaryPO summary2 = buildSummaryPO("item-002", "AVG");
|
||||||
|
when(taskService.getById("task-001")).thenReturn(task);
|
||||||
|
when(itemService.lambdaQuery()).thenReturn(itemQuery);
|
||||||
|
when(itemQuery.eq(any(), any())).thenReturn(itemQuery);
|
||||||
|
when(itemQuery.list()).thenReturn(Arrays.asList(item1, item2));
|
||||||
|
when(statSummaryService.lambdaQuery()).thenReturn(summaryQuery);
|
||||||
|
when(summaryQuery.in(any(), any(List.class))).thenReturn(summaryQuery);
|
||||||
|
when(summaryQuery.list()).thenReturn(Arrays.asList(summary1, summary2));
|
||||||
|
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
||||||
|
mock(SteadyChecksquareInfluxQueryComponent.class), new SteadyChecksquareCalculator(),
|
||||||
|
mock(SteadyChecksquareValueOrderRuleComponent.class), mock(SteadyChecksquareHarmonicParityRuleComponent.class),
|
||||||
|
new AddDataTimeSlotCalculator(), mock(AddLedgerService.class), taskService,
|
||||||
|
itemService, statSummaryService, mock(SteadyChecksquareDetailService.class), new ObjectMapper());
|
||||||
|
|
||||||
|
SteadyChecksquareQueryVO result = service.detail("task-001");
|
||||||
|
|
||||||
|
Assertions.assertEquals(2, result.getItems().size());
|
||||||
|
Assertions.assertEquals(1, result.getItems().get(0).getStatSummaries().size());
|
||||||
|
Assertions.assertEquals(1, result.getItems().get(1).getStatSummaries().size());
|
||||||
|
verify(statSummaryService, times(1)).lambdaQuery();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldPageItemDetailWhenPageArgumentsPresent() {
|
||||||
|
SteadyChecksquareItemService itemService = mock(SteadyChecksquareItemService.class);
|
||||||
|
SteadyChecksquareDetailService detailService = mock(SteadyChecksquareDetailService.class);
|
||||||
|
SteadyChecksquareItemPO item = new SteadyChecksquareItemPO();
|
||||||
|
item.setId("item-001");
|
||||||
|
item.setState(1);
|
||||||
|
SteadyChecksquareDetailPO detail = new SteadyChecksquareDetailPO();
|
||||||
|
detail.setItemId("item-001");
|
||||||
|
detail.setDetailType("VALUE_ORDER");
|
||||||
|
detail.setPointTime(LocalDateTime.of(2026, 5, 1, 0, 0));
|
||||||
|
detail.setPhase("A");
|
||||||
|
Page<SteadyChecksquareDetailPO> page = new Page<SteadyChecksquareDetailPO>(2, 1);
|
||||||
|
page.setTotal(1);
|
||||||
|
page.setRecords(Collections.singletonList(detail));
|
||||||
|
when(itemService.getById("item-001")).thenReturn(item);
|
||||||
|
when(detailService.page(any(Page.class), any())).thenReturn(page);
|
||||||
|
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
||||||
|
mock(SteadyChecksquareInfluxQueryComponent.class), new SteadyChecksquareCalculator(),
|
||||||
|
mock(SteadyChecksquareValueOrderRuleComponent.class), mock(SteadyChecksquareHarmonicParityRuleComponent.class),
|
||||||
|
new AddDataTimeSlotCalculator(), mock(AddLedgerService.class), mock(SteadyChecksquareTaskService.class),
|
||||||
|
itemService, mock(SteadyChecksquareStatSummaryService.class),
|
||||||
|
detailService, new ObjectMapper());
|
||||||
|
|
||||||
|
SteadyChecksquareItemDetailVO result = service.itemDetail("item-001", "VALUE_ORDER", null, 2, 1);
|
||||||
|
|
||||||
|
Assertions.assertEquals(Integer.valueOf(2), result.getPageNum());
|
||||||
|
Assertions.assertEquals(Integer.valueOf(1), result.getPageSize());
|
||||||
|
Assertions.assertEquals(Long.valueOf(1L), result.getTotal());
|
||||||
|
Assertions.assertEquals(1, result.getValueOrderDetails().size());
|
||||||
|
verify(detailService).page(any(Page.class), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldSaveChecksquareResultsInBatch() {
|
||||||
|
SteadyChecksquareTaskService taskService = mock(SteadyChecksquareTaskService.class);
|
||||||
|
SteadyChecksquareItemService itemService = mock(SteadyChecksquareItemService.class);
|
||||||
|
SteadyChecksquareStatSummaryService statSummaryService = mock(SteadyChecksquareStatSummaryService.class);
|
||||||
|
SteadyChecksquareDetailService detailService = mock(SteadyChecksquareDetailService.class);
|
||||||
|
SteadyChecksquareServiceImpl service = new SteadyChecksquareServiceImpl(new SteadyTrendIndicatorCatalog(),
|
||||||
|
mock(SteadyChecksquareInfluxQueryComponent.class), new SteadyChecksquareCalculator(),
|
||||||
|
mock(SteadyChecksquareValueOrderRuleComponent.class), mock(SteadyChecksquareHarmonicParityRuleComponent.class),
|
||||||
|
new AddDataTimeSlotCalculator(), mock(AddLedgerService.class), taskService,
|
||||||
|
itemService, statSummaryService, detailService, new ObjectMapper());
|
||||||
|
SteadyChecksquareQueryParam param = new SteadyChecksquareQueryParam();
|
||||||
|
param.setIndicatorCodes(Collections.singletonList("V_RMS"));
|
||||||
|
SteadyChecksquareQueryVO result = new SteadyChecksquareQueryVO();
|
||||||
|
result.setLineId("line-001");
|
||||||
|
result.setLineName("进线一");
|
||||||
|
result.setTimeStart("2026-05-01 00:00:00");
|
||||||
|
result.setTimeEnd("2026-05-01 00:01:00");
|
||||||
|
result.setIntervalMinutes(1);
|
||||||
|
SteadyChecksquareItemVO item = new SteadyChecksquareItemVO();
|
||||||
|
item.setItemKey("line-001|V_RMS");
|
||||||
|
item.setIndicatorCode("V_RMS");
|
||||||
|
item.setIndicatorName("相电压有效值");
|
||||||
|
item.setIntervalMinutes(1);
|
||||||
|
item.setHasData(true);
|
||||||
|
item.setExpectedPointCount(2);
|
||||||
|
item.setActualPointCount(2);
|
||||||
|
item.setMissingPointCount(0);
|
||||||
|
item.setMissingRate(BigDecimal.ZERO.setScale(6));
|
||||||
|
item.setMissingRateText("0.00%");
|
||||||
|
item.setMaxContinuousMissingMinutes(0);
|
||||||
|
item.setAbnormal(false);
|
||||||
|
item.setAbnormalPointCount(0);
|
||||||
|
item.setHarmonicParityAbnormal(false);
|
||||||
|
item.setHarmonicParityAbnormalPointCount(0);
|
||||||
|
SteadyChecksquareStatSummaryVO summary = new SteadyChecksquareStatSummaryVO();
|
||||||
|
summary.setStatType("AVG");
|
||||||
|
summary.setSupported(true);
|
||||||
|
summary.setHasData(true);
|
||||||
|
summary.setExpectedPointCount(2);
|
||||||
|
summary.setActualPointCount(2);
|
||||||
|
summary.setMissingPointCount(0);
|
||||||
|
summary.setMissingRate(BigDecimal.ZERO.setScale(6));
|
||||||
|
summary.setMissingRateText("0.00%");
|
||||||
|
summary.setMaxContinuousMissingMinutes(0);
|
||||||
|
item.getStatSummaries().add(summary);
|
||||||
|
result.getItems().add(item);
|
||||||
|
|
||||||
|
saveResult(service, param, result);
|
||||||
|
|
||||||
|
verify(taskService).save(any());
|
||||||
|
verify(itemService).saveBatch(any());
|
||||||
|
verify(statSummaryService).saveBatch(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertItemInterval(SteadyChecksquareItemVO item, String indicatorCode, int intervalMinutes, int expectedPointCount) {
|
private void assertItemInterval(SteadyChecksquareItemVO item, String indicatorCode, int intervalMinutes, int expectedPointCount) {
|
||||||
@@ -127,4 +497,66 @@ class SteadyChecksquareServiceImplTest {
|
|||||||
Assertions.assertEquals(Integer.valueOf(intervalMinutes), item.getIntervalMinutes());
|
Assertions.assertEquals(Integer.valueOf(intervalMinutes), item.getIntervalMinutes());
|
||||||
Assertions.assertEquals(Integer.valueOf(expectedPointCount), item.getExpectedPointCount());
|
Assertions.assertEquals(Integer.valueOf(expectedPointCount), item.getExpectedPointCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SteadyChecksquareQueryVO calculate(SteadyChecksquareServiceImpl service, SteadyChecksquareQueryParam param) {
|
||||||
|
try {
|
||||||
|
Method method = SteadyChecksquareServiceImpl.class.getDeclaredMethod("calculate", SteadyChecksquareQueryParam.class);
|
||||||
|
method.setAccessible(true);
|
||||||
|
return (SteadyChecksquareQueryVO) method.invoke(service, param);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new RuntimeException(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveResult(SteadyChecksquareServiceImpl service, SteadyChecksquareQueryParam param, SteadyChecksquareQueryVO result) {
|
||||||
|
try {
|
||||||
|
Method method = SteadyChecksquareServiceImpl.class.getDeclaredMethod("saveResult",
|
||||||
|
SteadyChecksquareQueryParam.class, SteadyChecksquareQueryVO.class);
|
||||||
|
method.setAccessible(true);
|
||||||
|
method.invoke(service, param, result);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new RuntimeException(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SteadyChecksquareItemPO buildItemPO(String itemId, String indicatorCode) {
|
||||||
|
SteadyChecksquareItemPO item = new SteadyChecksquareItemPO();
|
||||||
|
item.setId(itemId);
|
||||||
|
item.setIndicatorCode(indicatorCode);
|
||||||
|
item.setIndicatorName(indicatorCode);
|
||||||
|
item.setState(1);
|
||||||
|
item.setHasData(1);
|
||||||
|
item.setExpectedPointCount(1);
|
||||||
|
item.setActualPointCount(1);
|
||||||
|
item.setMissingPointCount(0);
|
||||||
|
item.setMissingRate(BigDecimal.ZERO.setScale(6));
|
||||||
|
item.setMaxContinuousMissingMinutes(0);
|
||||||
|
item.setAbnormal(0);
|
||||||
|
item.setAbnormalPointCount(0);
|
||||||
|
item.setHarmonicParityAbnormal(0);
|
||||||
|
item.setHarmonicParityAbnormalPointCount(0);
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SteadyChecksquareStatSummaryPO buildSummaryPO(String itemId, String statType) {
|
||||||
|
SteadyChecksquareStatSummaryPO summary = new SteadyChecksquareStatSummaryPO();
|
||||||
|
summary.setItemId(itemId);
|
||||||
|
summary.setStatType(statType);
|
||||||
|
summary.setSupported(1);
|
||||||
|
summary.setHasData(1);
|
||||||
|
summary.setExpectedPointCount(1);
|
||||||
|
summary.setActualPointCount(1);
|
||||||
|
summary.setMissingPointCount(0);
|
||||||
|
summary.setMissingRate(BigDecimal.ZERO.setScale(6));
|
||||||
|
summary.setMaxContinuousMissingMinutes(0);
|
||||||
|
return summary;
|
||||||
|
}
|
||||||
|
|
||||||
|
private SteadyChecksquareValueOrderRuleVO emptyRuleResult() {
|
||||||
|
return new SteadyChecksquareValueOrderRuleVO();
|
||||||
|
}
|
||||||
|
|
||||||
|
private SteadyChecksquareHarmonicParityRuleVO emptyHarmonicParityRuleResult() {
|
||||||
|
return new SteadyChecksquareHarmonicParityRuleVO();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,100 @@
|
|||||||
|
|
||||||
## 模块定位
|
## 模块定位
|
||||||
|
|
||||||
`dbms` 是 `system-ops` 下的数据库监控模块,当前先提供数据库监控菜单对应的后端基础入口。
|
`dbms` 是 `system-ops` 下的数据库运维模块,当前支持 Oracle、MySQL 两类数据库运维能力,其中 Oracle 支持 `DATA_PUMP`、`JDBC_EXPORT`,MySQL 当前支持 `JDBC_EXPORT`。
|
||||||
|
|
||||||
## 当前接口
|
## 当前接口
|
||||||
|
|
||||||
- `GET /database/overview`
|
- `GET /database/overview`
|
||||||
- 查询数据库监控基础信息。
|
- 查询数据库运维概览信息。
|
||||||
|
- `POST /database/connections/list`
|
||||||
|
- 查询数据库连接配置。
|
||||||
|
- `POST /database/connections/add`
|
||||||
|
- 新增 Oracle 数据库连接配置。
|
||||||
|
- `POST /database/connections/update`
|
||||||
|
- 修改 Oracle 数据库连接配置。
|
||||||
|
- `POST /database/connections/delete`
|
||||||
|
- 删除数据库连接配置。
|
||||||
|
- `POST /database/connections/test`
|
||||||
|
- 测试 Oracle 数据库连接。
|
||||||
|
- `POST /database/connections/tables`
|
||||||
|
- 查询 Oracle 表列表。
|
||||||
|
- `POST /database/backups/create`
|
||||||
|
- 创建备份任务,默认使用 `DATA_PUMP`,可选 `JDBC_EXPORT`。
|
||||||
|
- `POST /database/backups/tasks/list`
|
||||||
|
- 查询备份任务列表。
|
||||||
|
- `GET /database/backups/tasks/status`
|
||||||
|
- 查询任务状态。
|
||||||
|
- `POST /database/backups/files/list`
|
||||||
|
- 查询备份文件记录。
|
||||||
|
- `POST /database/restores/create`
|
||||||
|
- 创建恢复任务。
|
||||||
|
- `GET /database/restores/tasks/status`
|
||||||
|
- 查询恢复任务状态。
|
||||||
|
- `POST /database/delete/backup-file`
|
||||||
|
- 删除备份文件,要求 `confirmText=确认删除`。
|
||||||
|
- `POST /database/delete/task`
|
||||||
|
- 删除任务记录,要求 `confirmText=确认删除`。
|
||||||
|
|
||||||
|
## 数据脚本
|
||||||
|
|
||||||
|
- `src/main/resources/sql/system-ops/system-ops-init.sql`
|
||||||
|
- 系统运维菜单初始化脚本。
|
||||||
|
- `src/main/resources/sql/system-ops/dbms-database-ops-init.sql`
|
||||||
|
- 数据库运维连接、任务、备份文件和恢复记录表结构。
|
||||||
|
|
||||||
|
## 配置项
|
||||||
|
|
||||||
|
建议通过环境配置覆盖:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
dbms:
|
||||||
|
backup:
|
||||||
|
storage-path: D:/dbms-backup
|
||||||
|
default-max-file-size-mb: 512
|
||||||
|
mysql-fetch-size: 1000
|
||||||
|
tools:
|
||||||
|
expdp-path:
|
||||||
|
impdp-path:
|
||||||
|
```
|
||||||
|
|
||||||
|
说明:
|
||||||
|
|
||||||
|
- `backup.storage-path`
|
||||||
|
- `JDBC_EXPORT` 生成的 CSV 和元数据 JSON 的受管根目录。
|
||||||
|
- `backup.default-max-file-size-mb`
|
||||||
|
- MySQL `JDBC_EXPORT` 默认分片大小,前端可通过 `maxFileSizeMb` 覆盖,默认 512MB。
|
||||||
|
- `backup.mysql-fetch-size`
|
||||||
|
- MySQL `JDBC_EXPORT` 流式读取批量大小,默认 1000。
|
||||||
|
- `tools.expdp-path`、`tools.impdp-path`
|
||||||
|
- Oracle Data Pump 工具路径;为空时尝试走系统 `PATH`。
|
||||||
|
|
||||||
|
## 当前行为
|
||||||
|
|
||||||
|
- 当前能力矩阵如下:
|
||||||
|
|
||||||
|
| 数据库类型 | 连接测试 | 表列表 | JDBC_EXPORT | DATA_PUMP |
|
||||||
|
| --- | --- | --- | --- | --- |
|
||||||
|
| ORACLE | 支持 | 支持 | 支持 | 支持 |
|
||||||
|
| MYSQL | 支持 | 支持 | 支持 | 不支持 |
|
||||||
|
- 备份和恢复只允许基于已保存且连接可用的连接配置发起。
|
||||||
|
- 新增连接前的测试接口仍可传 `temporaryPassword` 做临时连通性测试。
|
||||||
|
- 备份任务异步执行,只有实际文件生成成功后才会写入 `dbms_backup_file` 记录。
|
||||||
|
- `JDBC_EXPORT` 当前会生成两类文件:
|
||||||
|
- MySQL `JDBC_EXPORT` 会按任务号创建独立备份目录,每张表独立 CSV,默认按 512MB 分片:
|
||||||
|
- 数据分片文件:`<table>_part001_yyyyMMdd_<taskNo>.csv`
|
||||||
|
- 元数据文件:`mysql_jdbc_export_metadata_yyyyMMdd_<taskNo>.json`
|
||||||
|
- 备份任务支持停止和重新开始:
|
||||||
|
- `POST /database/backups/tasks/stop`
|
||||||
|
- `POST /database/backups/tasks/restart`
|
||||||
|
- `JDBC_EXPORT` 恢复依赖元数据文件,不再允许缺少元数据直接发起恢复。
|
||||||
|
- 删除备份文件时,会校验目标路径必须位于受管备份目录下,避免误删非备份文件。
|
||||||
|
|
||||||
## 当前限制
|
## 当前限制
|
||||||
|
|
||||||
- 当前只完成后端基础入口,不包含真实数据库连接状态、容量或性能指标探测逻辑。
|
- `DATA_PUMP` 仍依赖部署机器可执行 `expdp`、`impdp`,并且 Oracle 侧已准备好 `directory` 对象和权限。
|
||||||
|
- 当前代码要求 `DATA_PUMP` 连接配置里补齐可管理的 `directoryPath`,否则虽然 Oracle 端可能已导出成功,后端无法安全管理文件记录与删除。
|
||||||
|
- `JDBC_EXPORT` 恢复当前仅覆盖表数据,不承诺恢复索引、约束、触发器、序列、存储过程、权限等数据库对象。
|
||||||
|
- `TIME_RANGE` 模式当前只在 `JDBC_EXPORT` 场景真正参与查询过滤;`DATA_PUMP` 尚未接入 Oracle `QUERY` 参数。
|
||||||
|
- MySQL `JDBC_EXPORT` 已实现按大小分片;Oracle `JDBC_EXPORT` 仍沿用原单文件导出路径。
|
||||||
|
- 本轮仅完成代码路径和文档收口,未执行 `mvn` 编译、测试或真实库联调。
|
||||||
|
|||||||
@@ -26,5 +26,19 @@
|
|||||||
<artifactId>spingboot2.3.12</artifactId>
|
<artifactId>spingboot2.3.12</artifactId>
|
||||||
<version>2.3.12</version>
|
<version>2.3.12</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.njcn</groupId>
|
||||||
|
<artifactId>mybatis-plus</artifactId>
|
||||||
|
<version>0.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-tx</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.oracle</groupId>
|
||||||
|
<artifactId>ojdbc6</artifactId>
|
||||||
|
<version>11.2.0.3</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -0,0 +1,111 @@
|
|||||||
|
package com.njcn.gather.systemops.database.component;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.njcn.gather.systemops.database.config.DbmsProperties;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oracle Data Pump 命令执行组件。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DataPumpCommandExecutor {
|
||||||
|
|
||||||
|
private final DbmsProperties dbmsProperties;
|
||||||
|
|
||||||
|
public CommandResult expdp(DatabaseConnection connection, String password, String directoryName,
|
||||||
|
String dumpFileName, String logFileName, List<String> tableNames) {
|
||||||
|
List<String> command = new ArrayList<>();
|
||||||
|
command.add(resolveTool(dbmsProperties.getTools().getExpdpPath(), "expdp"));
|
||||||
|
fillCommonArgs(command, connection, password, directoryName, dumpFileName, logFileName);
|
||||||
|
if (tableNames != null && !tableNames.isEmpty()) {
|
||||||
|
command.add("tables=" + connection.getSchemaName() + "." + String.join("," + connection.getSchemaName() + ".", tableNames));
|
||||||
|
}
|
||||||
|
return execute(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandResult impdp(DatabaseConnection connection, String password, String directoryName,
|
||||||
|
String dumpFileName, String logFileName, String tableExistsAction) {
|
||||||
|
List<String> command = new ArrayList<>();
|
||||||
|
command.add(resolveTool(dbmsProperties.getTools().getImpdpPath(), "impdp"));
|
||||||
|
fillCommonArgs(command, connection, password, directoryName, dumpFileName, logFileName);
|
||||||
|
if (StrUtil.isNotBlank(tableExistsAction)) {
|
||||||
|
command.add("table_exists_action=" + tableExistsAction);
|
||||||
|
}
|
||||||
|
return execute(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillCommonArgs(List<String> command, DatabaseConnection connection, String password,
|
||||||
|
String directoryName, String dumpFileName, String logFileName) {
|
||||||
|
command.add(connection.getUsername() + "/" + password + "@" + buildConnectIdentifier(connection));
|
||||||
|
command.add("directory=" + directoryName);
|
||||||
|
command.add("dumpfile=" + dumpFileName);
|
||||||
|
command.add("logfile=" + logFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildConnectIdentifier(DatabaseConnection connection) {
|
||||||
|
if (StrUtil.isNotBlank(connection.getServiceName())) {
|
||||||
|
return "//" + connection.getHost() + ":" + connection.getPort() + "/" + connection.getServiceName();
|
||||||
|
}
|
||||||
|
return connection.getHost() + ":" + connection.getPort() + ":" + connection.getSid();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveTool(String configuredPath, String defaultName) {
|
||||||
|
return StrUtil.blankToDefault(configuredPath, defaultName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CommandResult execute(List<String> command) {
|
||||||
|
CommandResult result = new CommandResult();
|
||||||
|
result.setCommand(maskPassword(command));
|
||||||
|
try {
|
||||||
|
Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
|
||||||
|
StringBuilder output = new StringBuilder();
|
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.defaultCharset()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
output.append(line).append(System.lineSeparator());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int exitCode = process.waitFor();
|
||||||
|
result.setExitCode(exitCode);
|
||||||
|
result.setOutput(output.toString());
|
||||||
|
result.setSuccess(exitCode == 0);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
result.setExitCode(-1);
|
||||||
|
result.setOutput(exception.getMessage());
|
||||||
|
result.setSuccess(false);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String maskPassword(List<String> command) {
|
||||||
|
if (command.size() < 2) {
|
||||||
|
return String.join(" ", command);
|
||||||
|
}
|
||||||
|
List<String> masked = new ArrayList<>(command);
|
||||||
|
String credential = masked.get(1);
|
||||||
|
int slashIndex = credential.indexOf('/');
|
||||||
|
int atIndex = credential.indexOf('@');
|
||||||
|
if (slashIndex > 0 && atIndex > slashIndex) {
|
||||||
|
masked.set(1, credential.substring(0, slashIndex + 1) + "******" + credential.substring(atIndex));
|
||||||
|
}
|
||||||
|
return String.join(" ", masked);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class CommandResult {
|
||||||
|
private Boolean success;
|
||||||
|
private Integer exitCode;
|
||||||
|
private String command;
|
||||||
|
private String output;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.njcn.gather.systemops.database.component;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库连接密码处理组件。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class DatabasePasswordComponent {
|
||||||
|
|
||||||
|
public String encrypt(String plainText) {
|
||||||
|
if (StrUtil.isBlank(plainText)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return plainText;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 优先使用本次请求传入的临时密码;否则复用已保存的数据库密码。
|
||||||
|
*/
|
||||||
|
public String resolveRuntimePassword(String passwordCipher, String temporaryPassword) {
|
||||||
|
if (StrUtil.isNotBlank(temporaryPassword)) {
|
||||||
|
return temporaryPassword;
|
||||||
|
}
|
||||||
|
return StrUtil.isBlank(passwordCipher) ? null : passwordCipher;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,541 @@
|
|||||||
|
package com.njcn.gather.systemops.database.component;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.njcn.gather.systemops.database.constant.DatabaseOpsConst;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseBackupParam;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseFileNameUtil;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.ResultSetMetaData;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JDBC 表数据导出与恢复组件。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class JdbcExportComponent {
|
||||||
|
|
||||||
|
private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("^[A-Za-z][A-Za-z0-9_#$]*$");
|
||||||
|
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
public void exportCsv(Connection jdbcConnection, String ownerName, DatabaseBackupParam.CreateParam param,
|
||||||
|
Path dataFilePath, Path metadataFilePath) throws Exception {
|
||||||
|
Files.createDirectories(dataFilePath.getParent());
|
||||||
|
if (metadataFilePath.getParent() != null) {
|
||||||
|
Files.createDirectories(metadataFilePath.getParent());
|
||||||
|
}
|
||||||
|
List<TableMetadata> metadataList = new ArrayList<>();
|
||||||
|
try (BufferedWriter writer = Files.newBufferedWriter(dataFilePath, StandardCharsets.UTF_8)) {
|
||||||
|
for (String tableName : param.getTargetNames()) {
|
||||||
|
metadataList.add(exportTable(jdbcConnection, ownerName, tableName, param, writer));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try (BufferedWriter metadataWriter = Files.newBufferedWriter(metadataFilePath, StandardCharsets.UTF_8)) {
|
||||||
|
objectMapper.writeValue(metadataWriter, metadataList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void importCsv(Connection jdbcConnection, Path dataFilePath, Path metadataFilePath, String dbType,
|
||||||
|
String restoreMode, String targetOwnerName) throws Exception {
|
||||||
|
String metadataText = new String(Files.readAllBytes(metadataFilePath), StandardCharsets.UTF_8);
|
||||||
|
if (metadataText.trim().startsWith("{")) {
|
||||||
|
importCsvV2(jdbcConnection, metadataFilePath, dbType, restoreMode, targetOwnerName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List<TableMetadata> metadataList = Arrays.asList(objectMapper.readValue(metadataFilePath.toFile(), TableMetadata[].class));
|
||||||
|
Map<String, TableMetadata> metadataMap = new LinkedHashMap<>();
|
||||||
|
for (TableMetadata metadata : metadataList) {
|
||||||
|
metadataMap.put(metadata.getFullTableName(), metadata);
|
||||||
|
}
|
||||||
|
jdbcConnection.setAutoCommit(false);
|
||||||
|
try (BufferedReader reader = Files.newBufferedReader(dataFilePath, StandardCharsets.UTF_8)) {
|
||||||
|
try {
|
||||||
|
String line;
|
||||||
|
TableMetadata currentMetadata = null;
|
||||||
|
List<String> currentColumns = null;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
if (line.startsWith("-- TABLE ")) {
|
||||||
|
currentMetadata = metadataMap.get(line.substring("-- TABLE ".length()).trim());
|
||||||
|
if (currentMetadata == null) {
|
||||||
|
throw new IllegalArgumentException("未找到表元数据:" + line);
|
||||||
|
}
|
||||||
|
currentColumns = null;
|
||||||
|
prepareTargetTable(jdbcConnection, currentMetadata, dbType, restoreMode, targetOwnerName);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (currentMetadata == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (currentColumns == null) {
|
||||||
|
currentColumns = parseCsvLine(line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
List<String> values = parseCsvLine(line);
|
||||||
|
insertRow(jdbcConnection, currentMetadata, currentColumns, values, dbType, restoreMode, targetOwnerName);
|
||||||
|
}
|
||||||
|
jdbcConnection.commit();
|
||||||
|
} catch (Exception exception) {
|
||||||
|
jdbcConnection.rollback();
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExportManifest exportMysqlCsvV2(Connection jdbcConnection, String databaseName, String taskNo,
|
||||||
|
DatabaseBackupParam.CreateParam param, Path backupDirectory,
|
||||||
|
Path metadataFilePath, int fetchSize, long maxPartBytes,
|
||||||
|
BooleanSupplier cancelled) throws Exception {
|
||||||
|
Files.createDirectories(backupDirectory);
|
||||||
|
ExportManifest manifest = new ExportManifest();
|
||||||
|
manifest.setVersion(2);
|
||||||
|
manifest.setDbType("MYSQL");
|
||||||
|
manifest.setBackupStrategy("JDBC_EXPORT");
|
||||||
|
manifest.setTaskNo(taskNo);
|
||||||
|
manifest.setDatabaseName(databaseName);
|
||||||
|
List<TableExportMetadata> tableMetadataList = new ArrayList<>();
|
||||||
|
manifest.setTables(tableMetadataList);
|
||||||
|
for (String tableName : param.getTargetNames()) {
|
||||||
|
checkCancelled(cancelled, backupDirectory);
|
||||||
|
tableMetadataList.add(exportMysqlTableV2(jdbcConnection, tableName, param, backupDirectory, taskNo,
|
||||||
|
fetchSize, maxPartBytes, cancelled));
|
||||||
|
}
|
||||||
|
try (BufferedWriter metadataWriter = Files.newBufferedWriter(metadataFilePath, StandardCharsets.UTF_8)) {
|
||||||
|
objectMapper.writeValue(metadataWriter, manifest);
|
||||||
|
}
|
||||||
|
return manifest;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TableMetadata exportTable(Connection connection, String ownerName, String tableName,
|
||||||
|
DatabaseBackupParam.CreateParam param, BufferedWriter writer) throws Exception {
|
||||||
|
String normalizedOwner = normalizeOwner(ownerName);
|
||||||
|
String normalizedTable = normalizeMysqlIdentifier(tableName);
|
||||||
|
String fullTableName = buildFullTableName(normalizedOwner, normalizedTable);
|
||||||
|
String querySql = buildQuerySql(fullTableName, param);
|
||||||
|
TableMetadata metadata = new TableMetadata();
|
||||||
|
metadata.setOwnerName(normalizedOwner);
|
||||||
|
metadata.setTableName(normalizedTable);
|
||||||
|
metadata.setFullTableName(fullTableName);
|
||||||
|
metadata.setTimeColumn(StrUtil.isBlank(param.getTimeColumn()) ? null : normalizeMysqlIdentifier(param.getTimeColumn()));
|
||||||
|
metadata.setStartTime(param.getStartTime() == null ? null : param.getStartTime().format(DATE_TIME_FORMATTER));
|
||||||
|
metadata.setEndTime(param.getEndTime() == null ? null : param.getEndTime().format(DATE_TIME_FORMATTER));
|
||||||
|
writer.write("-- TABLE " + fullTableName);
|
||||||
|
writer.newLine();
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(querySql)) {
|
||||||
|
fillQueryParams(statement, param);
|
||||||
|
try (ResultSet resultSet = statement.executeQuery()) {
|
||||||
|
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
|
||||||
|
int columnCount = resultSetMetaData.getColumnCount();
|
||||||
|
List<String> columnNames = new ArrayList<>();
|
||||||
|
List<String> columnTypes = new ArrayList<>();
|
||||||
|
for (int i = 1; i <= columnCount; i++) {
|
||||||
|
String columnName = resultSetMetaData.getColumnName(i);
|
||||||
|
columnNames.add(normalizeMysqlIdentifier(columnName));
|
||||||
|
columnTypes.add(resultSetMetaData.getColumnTypeName(i));
|
||||||
|
if (i > 1) {
|
||||||
|
writer.write(",");
|
||||||
|
}
|
||||||
|
writer.write(escape(columnName));
|
||||||
|
}
|
||||||
|
writer.newLine();
|
||||||
|
long rowCount = 0L;
|
||||||
|
while (resultSet.next()) {
|
||||||
|
for (int i = 1; i <= columnCount; i++) {
|
||||||
|
if (i > 1) {
|
||||||
|
writer.write(",");
|
||||||
|
}
|
||||||
|
writer.write(escape(resultSet.getString(i)));
|
||||||
|
}
|
||||||
|
writer.newLine();
|
||||||
|
rowCount++;
|
||||||
|
}
|
||||||
|
metadata.setColumnNames(columnNames);
|
||||||
|
metadata.setColumnTypes(columnTypes);
|
||||||
|
metadata.setRowCount(rowCount);
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private TableExportMetadata exportMysqlTableV2(Connection connection, String tableName,
|
||||||
|
DatabaseBackupParam.CreateParam param, Path backupDirectory,
|
||||||
|
String taskNo, int fetchSize, long maxPartBytes,
|
||||||
|
BooleanSupplier cancelled) throws Exception {
|
||||||
|
String normalizedTable = normalizeIdentifier(tableName);
|
||||||
|
String querySql = buildQuerySql(normalizedTable, param);
|
||||||
|
TableExportMetadata metadata = new TableExportMetadata();
|
||||||
|
metadata.setTableName(normalizedTable);
|
||||||
|
metadata.setFullTableName(normalizedTable);
|
||||||
|
metadata.setTimeColumn(StrUtil.isBlank(param.getTimeColumn()) ? null : normalizeIdentifier(param.getTimeColumn()));
|
||||||
|
metadata.setStartTime(param.getStartTime() == null ? null : param.getStartTime().format(DATE_TIME_FORMATTER));
|
||||||
|
metadata.setEndTime(param.getEndTime() == null ? null : param.getEndTime().format(DATE_TIME_FORMATTER));
|
||||||
|
metadata.setColumns(new ArrayList<>());
|
||||||
|
metadata.setParts(new ArrayList<>());
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(querySql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
|
||||||
|
statement.setFetchSize(fetchSize);
|
||||||
|
fillQueryParams(statement, param);
|
||||||
|
try (ResultSet resultSet = statement.executeQuery()) {
|
||||||
|
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
|
||||||
|
int columnCount = resultSetMetaData.getColumnCount();
|
||||||
|
List<String> columnNames = new ArrayList<>();
|
||||||
|
for (int i = 1; i <= columnCount; i++) {
|
||||||
|
String columnName = resultSetMetaData.getColumnName(i);
|
||||||
|
columnNames.add(columnName);
|
||||||
|
ColumnMetadata columnMetadata = new ColumnMetadata();
|
||||||
|
columnMetadata.setName(columnName);
|
||||||
|
columnMetadata.setType(resultSetMetaData.getColumnTypeName(i));
|
||||||
|
metadata.getColumns().add(columnMetadata);
|
||||||
|
}
|
||||||
|
PartWriter partWriter = openPartWriter(backupDirectory, normalizedTable, taskNo,
|
||||||
|
metadata.getParts().size() + 1, columnNames);
|
||||||
|
metadata.getParts().add(partWriter.getPart());
|
||||||
|
long totalRows = 0L;
|
||||||
|
try {
|
||||||
|
while (resultSet.next()) {
|
||||||
|
checkCancelled(cancelled, backupDirectory);
|
||||||
|
if (partWriter.shouldRotate(maxPartBytes)) {
|
||||||
|
partWriter.close();
|
||||||
|
partWriter = openPartWriter(backupDirectory, normalizedTable, taskNo,
|
||||||
|
metadata.getParts().size() + 1, columnNames);
|
||||||
|
metadata.getParts().add(partWriter.getPart());
|
||||||
|
}
|
||||||
|
partWriter.writeRow(resultSet, columnCount);
|
||||||
|
totalRows++;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
partWriter.close();
|
||||||
|
}
|
||||||
|
metadata.setRowCount(totalRows);
|
||||||
|
return metadata;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildQuerySql(String fullTableName, DatabaseBackupParam.CreateParam param) {
|
||||||
|
StringBuilder sql = new StringBuilder("SELECT * FROM ").append(fullTableName);
|
||||||
|
if (param.getStartTime() != null && param.getEndTime() != null && StrUtil.isNotBlank(param.getTimeColumn())) {
|
||||||
|
sql.append(" WHERE ").append(normalizeIdentifier(param.getTimeColumn())).append(" BETWEEN ? AND ?");
|
||||||
|
}
|
||||||
|
return sql.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillQueryParams(PreparedStatement statement, DatabaseBackupParam.CreateParam param) throws Exception {
|
||||||
|
if (param.getStartTime() != null && param.getEndTime() != null && StrUtil.isNotBlank(param.getTimeColumn())) {
|
||||||
|
statement.setString(1, param.getStartTime().format(DATE_TIME_FORMATTER));
|
||||||
|
statement.setString(2, param.getEndTime().format(DATE_TIME_FORMATTER));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void prepareTargetTable(Connection connection, TableMetadata metadata, String dbType, String restoreMode,
|
||||||
|
String targetOwnerName) throws Exception {
|
||||||
|
if (!"TRUNCATE".equalsIgnoreCase(restoreMode)
|
||||||
|
&& !("REPLACE".equalsIgnoreCase(restoreMode) && !isMysql(dbType))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String fullTargetName = buildTargetTableName(metadata, targetOwnerName);
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
statement.execute("TRUNCATE TABLE " + fullTargetName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertRow(Connection connection, TableMetadata metadata, List<String> columns,
|
||||||
|
List<String> values, String dbType, String restoreMode, String targetOwnerName) throws Exception {
|
||||||
|
String fullTargetName = buildTargetTableName(metadata, targetOwnerName);
|
||||||
|
StringBuilder placeholders = new StringBuilder();
|
||||||
|
for (int i = 0; i < columns.size(); i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
placeholders.append(",");
|
||||||
|
}
|
||||||
|
placeholders.append("?");
|
||||||
|
}
|
||||||
|
String sql = buildInsertSql(dbType, restoreMode, fullTargetName, columns, placeholders.toString());
|
||||||
|
try (PreparedStatement statement = connection.prepareStatement(sql)) {
|
||||||
|
for (int i = 0; i < columns.size(); i++) {
|
||||||
|
statement.setString(i + 1, i < values.size() ? values.get(i) : null);
|
||||||
|
}
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildInsertSql(String dbType, String restoreMode, String fullTargetName, List<String> columns,
|
||||||
|
String placeholders) {
|
||||||
|
String command = "INSERT INTO";
|
||||||
|
if (isMysql(dbType) && "SKIP".equalsIgnoreCase(restoreMode)) {
|
||||||
|
// MySQL 跳过重复主键行,避免普通恢复因历史数据重复而整体失败。
|
||||||
|
command = "INSERT IGNORE INTO";
|
||||||
|
} else if (isMysql(dbType) && "REPLACE".equalsIgnoreCase(restoreMode)) {
|
||||||
|
command = "REPLACE INTO";
|
||||||
|
}
|
||||||
|
return command + " " + fullTargetName + " (" + String.join(",", columns) + ") VALUES (" + placeholders + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void importCsvV2(Connection jdbcConnection, Path metadataFilePath, String dbType, String restoreMode,
|
||||||
|
String targetOwnerName) throws Exception {
|
||||||
|
ExportManifest manifest = objectMapper.readValue(metadataFilePath.toFile(), ExportManifest.class);
|
||||||
|
jdbcConnection.setAutoCommit(false);
|
||||||
|
try {
|
||||||
|
for (TableExportMetadata tableMetadata : manifest.getTables()) {
|
||||||
|
prepareTargetTable(jdbcConnection, toLegacyMetadata(tableMetadata), dbType, restoreMode, targetOwnerName);
|
||||||
|
for (FilePartMetadata part : tableMetadata.getParts()) {
|
||||||
|
importPart(jdbcConnection, metadataFilePath.getParent(), tableMetadata, part, dbType, restoreMode, targetOwnerName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jdbcConnection.commit();
|
||||||
|
} catch (Exception exception) {
|
||||||
|
jdbcConnection.rollback();
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void importPart(Connection jdbcConnection, Path backupDirectory, TableExportMetadata tableMetadata,
|
||||||
|
FilePartMetadata part, String dbType, String restoreMode, String targetOwnerName) throws Exception {
|
||||||
|
Path partPath = backupDirectory.resolve(part.getFileName()).normalize();
|
||||||
|
if (!partPath.startsWith(backupDirectory.normalize())) {
|
||||||
|
throw new IllegalArgumentException("备份分片路径不在元数据目录内:" + part.getFileName());
|
||||||
|
}
|
||||||
|
try (BufferedReader reader = Files.newBufferedReader(partPath, StandardCharsets.UTF_8)) {
|
||||||
|
List<String> columns = null;
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
if (line.startsWith("-- TABLE ")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (columns == null) {
|
||||||
|
columns = parseCsvLine(line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
List<String> values = parseCsvLine(line);
|
||||||
|
insertRow(jdbcConnection, toLegacyMetadata(tableMetadata), columns, values, dbType, restoreMode, targetOwnerName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isMysql(String dbType) {
|
||||||
|
return DatabaseOpsConst.DB_TYPE_MYSQL.equalsIgnoreCase(dbType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private PartWriter openPartWriter(Path backupDirectory, String tableName, String taskNo, int partIndex,
|
||||||
|
List<String> columnNames) throws IOException {
|
||||||
|
String rawName = tableName.toLowerCase(Locale.ROOT) + "_part" + String.format("%03d", partIndex) + ".csv";
|
||||||
|
String fileName = DatabaseFileNameUtil.appendTodayWithTask(rawName, taskNo);
|
||||||
|
Path filePath = backupDirectory.resolve(fileName).normalize();
|
||||||
|
BufferedWriter writer = Files.newBufferedWriter(filePath, StandardCharsets.UTF_8);
|
||||||
|
writer.write("-- TABLE " + tableName);
|
||||||
|
writer.newLine();
|
||||||
|
for (int i = 0; i < columnNames.size(); i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
writer.write(",");
|
||||||
|
}
|
||||||
|
writer.write(escape(columnNames.get(i)));
|
||||||
|
}
|
||||||
|
writer.newLine();
|
||||||
|
FilePartMetadata part = new FilePartMetadata();
|
||||||
|
part.setFileName(fileName);
|
||||||
|
part.setFilePath(filePath.toString());
|
||||||
|
part.setRowCount(0L);
|
||||||
|
part.setFileSize(0L);
|
||||||
|
return new PartWriter(writer, filePath, part);
|
||||||
|
}
|
||||||
|
|
||||||
|
private TableMetadata toLegacyMetadata(TableExportMetadata metadata) {
|
||||||
|
TableMetadata legacy = new TableMetadata();
|
||||||
|
legacy.setOwnerName(null);
|
||||||
|
legacy.setTableName(metadata.getTableName());
|
||||||
|
legacy.setFullTableName(metadata.getFullTableName());
|
||||||
|
legacy.setTimeColumn(metadata.getTimeColumn());
|
||||||
|
legacy.setStartTime(metadata.getStartTime());
|
||||||
|
legacy.setEndTime(metadata.getEndTime());
|
||||||
|
legacy.setRowCount(metadata.getRowCount());
|
||||||
|
List<String> columnNames = new ArrayList<>();
|
||||||
|
List<String> columnTypes = new ArrayList<>();
|
||||||
|
for (ColumnMetadata column : metadata.getColumns()) {
|
||||||
|
columnNames.add(column.getName());
|
||||||
|
columnTypes.add(column.getType());
|
||||||
|
}
|
||||||
|
legacy.setColumnNames(columnNames);
|
||||||
|
legacy.setColumnTypes(columnTypes);
|
||||||
|
return legacy;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkCancelled(BooleanSupplier cancelled, Path backupDirectory) {
|
||||||
|
if (cancelled != null && cancelled.getAsBoolean()) {
|
||||||
|
throw new IllegalStateException("备份任务已停止,已生成文件保留在:" + backupDirectory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildTargetTableName(TableMetadata metadata, String targetOwnerName) {
|
||||||
|
String owner = normalizeOwner(StrUtil.blankToDefault(targetOwnerName, metadata.getOwnerName()));
|
||||||
|
return buildFullTableName(owner, metadata.getTableName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildFullTableName(String ownerName, String tableName) {
|
||||||
|
if (StrUtil.isBlank(ownerName)) {
|
||||||
|
return tableName;
|
||||||
|
}
|
||||||
|
return ownerName + "." + tableName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String normalizeOwner(String ownerName) {
|
||||||
|
if (StrUtil.isBlank(ownerName)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return normalizeIdentifier(ownerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> parseCsvLine(String line) {
|
||||||
|
List<String> result = new ArrayList<>();
|
||||||
|
StringBuilder current = new StringBuilder();
|
||||||
|
boolean quoted = false;
|
||||||
|
for (int i = 0; i < line.length(); i++) {
|
||||||
|
char currentChar = line.charAt(i);
|
||||||
|
if (currentChar == '"') {
|
||||||
|
if (quoted && i + 1 < line.length() && line.charAt(i + 1) == '"') {
|
||||||
|
current.append('"');
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
quoted = !quoted;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (currentChar == ',' && !quoted) {
|
||||||
|
result.add(current.toString());
|
||||||
|
current.setLength(0);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
current.append(currentChar);
|
||||||
|
}
|
||||||
|
result.add(current.toString());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String escape(String value) {
|
||||||
|
if (value == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return "\"" + value.replace("\"", "\"\"") + "\"";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String normalizeIdentifier(String value) {
|
||||||
|
if (value == null || !IDENTIFIER_PATTERN.matcher(value).matches()) {
|
||||||
|
throw new IllegalArgumentException("数据库对象名称格式不正确:" + value);
|
||||||
|
}
|
||||||
|
return value.trim().toUpperCase(Locale.ROOT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String normalizeMysqlIdentifier(String value) {
|
||||||
|
if (value == null || !IDENTIFIER_PATTERN.matcher(value).matches()) {
|
||||||
|
throw new IllegalArgumentException("数据库对象名称格式不正确:" + value);
|
||||||
|
}
|
||||||
|
return value.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class TableMetadata {
|
||||||
|
private String ownerName;
|
||||||
|
private String tableName;
|
||||||
|
private String fullTableName;
|
||||||
|
private List<String> columnNames;
|
||||||
|
private List<String> columnTypes;
|
||||||
|
private String timeColumn;
|
||||||
|
private String startTime;
|
||||||
|
private String endTime;
|
||||||
|
private Long rowCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class ExportManifest {
|
||||||
|
private Integer version;
|
||||||
|
private String dbType;
|
||||||
|
private String backupStrategy;
|
||||||
|
private String taskNo;
|
||||||
|
private String databaseName;
|
||||||
|
private List<TableExportMetadata> tables;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class TableExportMetadata {
|
||||||
|
private String tableName;
|
||||||
|
private String fullTableName;
|
||||||
|
private String timeColumn;
|
||||||
|
private String startTime;
|
||||||
|
private String endTime;
|
||||||
|
private List<ColumnMetadata> columns;
|
||||||
|
private Long rowCount;
|
||||||
|
private List<FilePartMetadata> parts;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class ColumnMetadata {
|
||||||
|
private String name;
|
||||||
|
private String type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class FilePartMetadata {
|
||||||
|
private String fileName;
|
||||||
|
private String filePath;
|
||||||
|
private Long rowCount;
|
||||||
|
private Long fileSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class PartWriter {
|
||||||
|
private final BufferedWriter writer;
|
||||||
|
private final Path filePath;
|
||||||
|
private final FilePartMetadata part;
|
||||||
|
|
||||||
|
private PartWriter(BufferedWriter writer, Path filePath, FilePartMetadata part) {
|
||||||
|
this.writer = writer;
|
||||||
|
this.filePath = filePath;
|
||||||
|
this.part = part;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FilePartMetadata getPart() {
|
||||||
|
return part;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean shouldRotate(long maxPartBytes) throws IOException {
|
||||||
|
writer.flush();
|
||||||
|
return part.getRowCount() > 0 && Files.size(filePath) >= maxPartBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeRow(ResultSet resultSet, int columnCount) throws Exception {
|
||||||
|
for (int i = 1; i <= columnCount; i++) {
|
||||||
|
if (i > 1) {
|
||||||
|
writer.write(",");
|
||||||
|
}
|
||||||
|
writer.write(escape(resultSet.getString(i)));
|
||||||
|
}
|
||||||
|
writer.newLine();
|
||||||
|
part.setRowCount(part.getRowCount() + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void close() throws IOException {
|
||||||
|
writer.close();
|
||||||
|
part.setFileSize(Files.exists(filePath) ? Files.size(filePath) : 0L);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
package com.njcn.gather.systemops.database.component;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.njcn.gather.systemops.database.constant.DatabaseOpsConst;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTableVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTestResultVO;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oracle JDBC 连接与元数据探测组件。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class OracleJdbcComponent {
|
||||||
|
|
||||||
|
public DatabaseTestResultVO test(DatabaseConnection connection, String password) {
|
||||||
|
DatabaseTestResultVO result = new DatabaseTestResultVO();
|
||||||
|
try (Connection ignored = openConnection(connection, password)) {
|
||||||
|
result.setSuccess(true);
|
||||||
|
result.setMessage("连接成功");
|
||||||
|
} catch (Exception exception) {
|
||||||
|
result.setSuccess(false);
|
||||||
|
result.setMessage(exception.getMessage());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<DatabaseTableVO> listTables(DatabaseConnection connection, String password, String schemaName) throws Exception {
|
||||||
|
String owner = StrUtil.blankToDefault(schemaName, connection.getSchemaName());
|
||||||
|
if (StrUtil.isBlank(owner)) {
|
||||||
|
owner = connection.getUsername();
|
||||||
|
}
|
||||||
|
owner = owner.trim().toUpperCase(Locale.ROOT);
|
||||||
|
String sql = "SELECT t.owner, t.table_name, t.num_rows, o.last_ddl_time, c.comments "
|
||||||
|
+ "FROM all_tables t "
|
||||||
|
+ "LEFT JOIN all_tab_comments c "
|
||||||
|
+ "ON t.owner = c.owner AND t.table_name = c.table_name "
|
||||||
|
+ "LEFT JOIN all_objects o "
|
||||||
|
+ "ON t.owner = o.owner AND t.table_name = o.object_name AND o.object_type = 'TABLE' "
|
||||||
|
+ "WHERE t.owner = ? ORDER BY t.table_name";
|
||||||
|
try (Connection jdbcConnection = openConnection(connection, password);
|
||||||
|
PreparedStatement statement = jdbcConnection.prepareStatement(sql)) {
|
||||||
|
statement.setString(1, owner);
|
||||||
|
try (ResultSet resultSet = statement.executeQuery()) {
|
||||||
|
List<DatabaseTableVO> result = new ArrayList<>();
|
||||||
|
while (resultSet.next()) {
|
||||||
|
DatabaseTableVO table = new DatabaseTableVO();
|
||||||
|
table.setOwner(resultSet.getString("owner"));
|
||||||
|
table.setTableName(resultSet.getString("table_name"));
|
||||||
|
table.setEngine(DatabaseOpsConst.DB_TYPE_ORACLE);
|
||||||
|
table.setTableRows(getLongValue(resultSet, "num_rows"));
|
||||||
|
Timestamp updateTime = resultSet.getTimestamp("last_ddl_time");
|
||||||
|
table.setUpdateTime(updateTime == null ? null : updateTime.toLocalDateTime());
|
||||||
|
table.setComments(resultSet.getString("comments"));
|
||||||
|
result.add(table);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Long getLongValue(ResultSet resultSet, String columnName) throws Exception {
|
||||||
|
long value = resultSet.getLong(columnName);
|
||||||
|
return resultSet.wasNull() ? null : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Connection openConnection(DatabaseConnection connection, String password) throws Exception {
|
||||||
|
if (StrUtil.isBlank(password)) {
|
||||||
|
throw new IllegalArgumentException("数据库密码不能为空");
|
||||||
|
}
|
||||||
|
return DriverManager.getConnection(buildJdbcUrl(connection), connection.getUsername(), password);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String buildJdbcUrl(DatabaseConnection connection) {
|
||||||
|
if (DatabaseOpsConst.CONNECT_TYPE_SID.equalsIgnoreCase(connection.getConnectType())) {
|
||||||
|
return "jdbc:oracle:thin:@" + connection.getHost() + ":" + connection.getPort() + ":" + connection.getSid();
|
||||||
|
}
|
||||||
|
return "jdbc:oracle:thin:@//" + connection.getHost() + ":" + connection.getPort() + "/" + connection.getServiceName();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.njcn.gather.systemops.database.config;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库运维后台任务线程池。
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Configuration
|
||||||
|
public class DbmsExecutorConfig {
|
||||||
|
|
||||||
|
@Bean(name = "dbmsTaskExecutorService", destroyMethod = "shutdown")
|
||||||
|
public ExecutorService dbmsTaskExecutorService() {
|
||||||
|
AtomicInteger threadIndex = new AtomicInteger(1);
|
||||||
|
return new ThreadPoolExecutor(
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
30,
|
||||||
|
TimeUnit.SECONDS,
|
||||||
|
new LinkedBlockingQueue<Runnable>(8),
|
||||||
|
runnable -> {
|
||||||
|
Thread thread = new Thread(runnable);
|
||||||
|
thread.setName("dbms-task-" + threadIndex.getAndIncrement());
|
||||||
|
return thread;
|
||||||
|
},
|
||||||
|
(runnable, executor) -> log.warn("数据库运维任务线程池已满,拒绝新的任务")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.njcn.gather.systemops.database.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库运维配置。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "dbms")
|
||||||
|
public class DbmsProperties {
|
||||||
|
|
||||||
|
private Backup backup = new Backup();
|
||||||
|
private Tools tools = new Tools();
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Backup {
|
||||||
|
private String storagePath = "D:/dbms-backup";
|
||||||
|
private Integer defaultMaxFileSizeMb = 512;
|
||||||
|
private Integer mysqlFetchSize = 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Tools {
|
||||||
|
private String expdpPath;
|
||||||
|
private String impdpPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.njcn.gather.systemops.database.constant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库运维常量。
|
||||||
|
*/
|
||||||
|
public final class DatabaseOpsConst {
|
||||||
|
|
||||||
|
public static final String DB_TYPE_ORACLE = "ORACLE";
|
||||||
|
public static final String DB_TYPE_MYSQL = "MYSQL";
|
||||||
|
public static final String CONNECT_TYPE_SERVICE_NAME = "SERVICE_NAME";
|
||||||
|
public static final String CONNECT_TYPE_SID = "SID";
|
||||||
|
public static final String CONFIRM_DELETE = "确认删除";
|
||||||
|
public static final String CONFIRM_OVERWRITE = "确认覆盖";
|
||||||
|
public static final int STATE_DELETED = 0;
|
||||||
|
public static final int STATE_ENABLED = 1;
|
||||||
|
public static final int SAVE_PASSWORD_YES = 1;
|
||||||
|
public static final int SAVE_PASSWORD_NO = 0;
|
||||||
|
|
||||||
|
private DatabaseOpsConst() {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package com.njcn.gather.systemops.database.controller;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.njcn.common.pojo.annotation.OperateInfo;
|
||||||
|
import com.njcn.common.pojo.constant.OperateType;
|
||||||
|
import com.njcn.common.pojo.enums.common.LogEnum;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.response.HttpResult;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseBackupParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseBackupFileVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTaskCreateVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTaskVO;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseBackupFileService;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseOperationTaskService;
|
||||||
|
import com.njcn.web.controller.BaseController;
|
||||||
|
import com.njcn.web.utils.HttpResultUtil;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库备份接口。
|
||||||
|
*/
|
||||||
|
@Api(tags = "数据库备份")
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/database/backups")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DatabaseBackupController extends BaseController {
|
||||||
|
|
||||||
|
private final DatabaseOperationTaskService databaseOperationTaskService;
|
||||||
|
private final DatabaseBackupFileService databaseBackupFileService;
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
|
||||||
|
@ApiOperation("创建备份任务")
|
||||||
|
@PostMapping("/create")
|
||||||
|
public HttpResult<DatabaseTaskCreateVO> create(@RequestBody @Validated DatabaseBackupParam.CreateParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("create");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, databaseOperationTaskService.createBackupTask(param), methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||||
|
@ApiOperation("查询备份任务")
|
||||||
|
@PostMapping("/tasks/list")
|
||||||
|
public HttpResult<Page<DatabaseTaskVO>> listTasks(@RequestBody @Validated DatabaseBackupParam.TaskQueryParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("listTasks");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, databaseOperationTaskService.listBackupTasks(param), methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||||
|
@ApiOperation("查询任务状态")
|
||||||
|
@GetMapping("/tasks/status")
|
||||||
|
public HttpResult<DatabaseTaskVO> status(@RequestParam("taskId") String taskId) {
|
||||||
|
String methodDescribe = getMethodDescribe("status");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, databaseOperationTaskService.getStatus(taskId), methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
|
||||||
|
@ApiOperation("停止备份任务")
|
||||||
|
@PostMapping("/tasks/stop")
|
||||||
|
public HttpResult<Boolean> stop(@RequestBody @Validated DatabaseBackupParam.StopParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("stop");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, databaseOperationTaskService.stopBackupTask(param), methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
|
||||||
|
@ApiOperation("重新开始备份任务")
|
||||||
|
@PostMapping("/tasks/restart")
|
||||||
|
public HttpResult<DatabaseTaskCreateVO> restart(@RequestBody @Validated DatabaseBackupParam.RestartParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("restart");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, databaseOperationTaskService.restartBackupTask(param), methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||||
|
@ApiOperation("查询备份文件")
|
||||||
|
@PostMapping("/files/list")
|
||||||
|
public HttpResult<Page<DatabaseBackupFileVO>> listFiles(@RequestBody @Validated DatabaseBackupParam.FileQueryParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("listFiles");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, databaseBackupFileService.listFiles(param), methodDescribe);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package com.njcn.gather.systemops.database.controller;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.njcn.common.pojo.annotation.OperateInfo;
|
||||||
|
import com.njcn.common.pojo.constant.OperateType;
|
||||||
|
import com.njcn.common.pojo.enums.common.LogEnum;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.response.HttpResult;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseConnectionParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseConnectionVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTableVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTestResultVO;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseConnectionService;
|
||||||
|
import com.njcn.web.controller.BaseController;
|
||||||
|
import com.njcn.web.utils.HttpResultUtil;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库连接配置接口。
|
||||||
|
*/
|
||||||
|
@Api(tags = "数据库连接配置")
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/database/connections")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DatabaseConnectionController extends BaseController {
|
||||||
|
|
||||||
|
private final DatabaseConnectionService databaseConnectionService;
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||||
|
@ApiOperation("查询数据库连接配置")
|
||||||
|
@PostMapping("/list")
|
||||||
|
public HttpResult<Page<DatabaseConnectionVO>> list(@RequestBody @Validated DatabaseConnectionParam.QueryParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("list");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, databaseConnectionService.listConnections(param), methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
|
||||||
|
@ApiOperation("新增数据库连接配置")
|
||||||
|
@PostMapping("/add")
|
||||||
|
public HttpResult<Boolean> add(@RequestBody @Validated DatabaseConnectionParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("add");
|
||||||
|
boolean result = databaseConnectionService.addConnection(param);
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL, result, methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
|
||||||
|
@ApiOperation("修改数据库连接配置")
|
||||||
|
@PostMapping("/update")
|
||||||
|
public HttpResult<Boolean> update(@RequestBody @Validated DatabaseConnectionParam.UpdateParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("update");
|
||||||
|
boolean result = databaseConnectionService.updateConnection(param);
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL, result, methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
|
||||||
|
@ApiOperation("删除数据库连接配置")
|
||||||
|
@PostMapping("/delete")
|
||||||
|
public HttpResult<Boolean> delete(@RequestBody @Validated DatabaseConnectionParam.DeleteParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("delete");
|
||||||
|
boolean result = databaseConnectionService.deleteConnection(param);
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL, result, methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||||
|
@ApiOperation("测试数据库连接")
|
||||||
|
@PostMapping("/test")
|
||||||
|
public HttpResult<DatabaseTestResultVO> test(@RequestBody @Validated DatabaseConnectionParam.TestParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("test");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, databaseConnectionService.testConnection(param), methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||||
|
@ApiOperation("查询 Oracle 表列表")
|
||||||
|
@PostMapping("/tables")
|
||||||
|
public HttpResult<List<DatabaseTableVO>> tables(@RequestBody @Validated DatabaseConnectionParam.TablesParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("tables");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, databaseConnectionService.listTables(param), methodDescribe);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package com.njcn.gather.systemops.database.controller;
|
||||||
|
|
||||||
|
import com.njcn.common.pojo.annotation.OperateInfo;
|
||||||
|
import com.njcn.common.pojo.constant.OperateType;
|
||||||
|
import com.njcn.common.pojo.enums.common.LogEnum;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.response.HttpResult;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseDeleteParam;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseBackupFileService;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseOperationTaskService;
|
||||||
|
import com.njcn.web.controller.BaseController;
|
||||||
|
import com.njcn.web.utils.HttpResultUtil;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库运维删除接口。
|
||||||
|
*/
|
||||||
|
@Api(tags = "数据库运维删除")
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/database/delete")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DatabaseDeleteController extends BaseController {
|
||||||
|
|
||||||
|
private final DatabaseBackupFileService databaseBackupFileService;
|
||||||
|
private final DatabaseOperationTaskService databaseOperationTaskService;
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
|
||||||
|
@ApiOperation("删除备份文件")
|
||||||
|
@PostMapping("/backup-file")
|
||||||
|
public HttpResult<Boolean> deleteBackupFile(@RequestBody @Validated DatabaseDeleteParam.BackupFileParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("deleteBackupFile");
|
||||||
|
boolean result = databaseBackupFileService.deleteBackupFile(param.getBackupFileId(), param.getConfirmText());
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL, result, methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
|
||||||
|
@ApiOperation("删除任务记录")
|
||||||
|
@PostMapping("/task")
|
||||||
|
public HttpResult<Boolean> deleteTask(@RequestBody @Validated DatabaseDeleteParam.TaskParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("deleteTask");
|
||||||
|
boolean result = databaseOperationTaskService.deleteTask(param.getTaskId(), param.getConfirmText());
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL, result, methodDescribe);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package com.njcn.gather.systemops.database.controller;
|
||||||
|
|
||||||
|
import com.njcn.common.pojo.annotation.OperateInfo;
|
||||||
|
import com.njcn.common.pojo.constant.OperateType;
|
||||||
|
import com.njcn.common.pojo.enums.common.LogEnum;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.response.HttpResult;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseRestoreParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTaskCreateVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTaskVO;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseOperationTaskService;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseRestoreService;
|
||||||
|
import com.njcn.web.controller.BaseController;
|
||||||
|
import com.njcn.web.utils.HttpResultUtil;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库恢复接口。
|
||||||
|
*/
|
||||||
|
@Api(tags = "数据库恢复")
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/database/restores")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DatabaseRestoreController extends BaseController {
|
||||||
|
|
||||||
|
private final DatabaseRestoreService databaseRestoreService;
|
||||||
|
private final DatabaseOperationTaskService databaseOperationTaskService;
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
|
||||||
|
@ApiOperation("创建恢复任务")
|
||||||
|
@PostMapping("/create")
|
||||||
|
public HttpResult<DatabaseTaskCreateVO> create(@RequestBody @Validated DatabaseRestoreParam.CreateParam param) {
|
||||||
|
String methodDescribe = getMethodDescribe("create");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, databaseRestoreService.createRestoreTask(param), methodDescribe);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||||
|
@ApiOperation("查询恢复任务状态")
|
||||||
|
@GetMapping("/tasks/status")
|
||||||
|
public HttpResult<DatabaseTaskVO> status(@RequestParam("taskId") String taskId) {
|
||||||
|
String methodDescribe = getMethodDescribe("status");
|
||||||
|
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, databaseOperationTaskService.getStatus(taskId), methodDescribe);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.systemops.database.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseBackupFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库备份文件 Mapper。
|
||||||
|
*/
|
||||||
|
public interface DatabaseBackupFileMapper extends BaseMapper<DatabaseBackupFile> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.systemops.database.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库连接配置 Mapper。
|
||||||
|
*/
|
||||||
|
public interface DatabaseConnectionMapper extends BaseMapper<DatabaseConnection> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.systemops.database.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseOperationTask;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库运维任务 Mapper。
|
||||||
|
*/
|
||||||
|
public interface DatabaseOperationTaskMapper extends BaseMapper<DatabaseOperationTask> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.systemops.database.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseRestoreRecord;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库恢复记录 Mapper。
|
||||||
|
*/
|
||||||
|
public interface DatabaseRestoreRecordMapper extends BaseMapper<DatabaseRestoreRecord> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 备份模式。
|
||||||
|
*/
|
||||||
|
public enum BackupModeEnum {
|
||||||
|
FULL_TABLE,
|
||||||
|
TIME_RANGE,
|
||||||
|
SIZE_SPLIT
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 备份策略。
|
||||||
|
*/
|
||||||
|
public enum BackupStrategyEnum {
|
||||||
|
DATA_PUMP,
|
||||||
|
JDBC_EXPORT
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 备份文件格式。
|
||||||
|
*/
|
||||||
|
public enum FileFormatEnum {
|
||||||
|
DMP,
|
||||||
|
SQL,
|
||||||
|
CSV
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库运维操作类型。
|
||||||
|
*/
|
||||||
|
public enum OperationTypeEnum {
|
||||||
|
BACKUP,
|
||||||
|
RESTORE,
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 恢复模式。
|
||||||
|
*/
|
||||||
|
public enum RestoreModeEnum {
|
||||||
|
SKIP,
|
||||||
|
APPEND,
|
||||||
|
TRUNCATE,
|
||||||
|
REPLACE
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运维任务状态。
|
||||||
|
*/
|
||||||
|
public enum TaskStatusEnum {
|
||||||
|
WAITING,
|
||||||
|
RUNNING,
|
||||||
|
SUCCESS,
|
||||||
|
FAIL,
|
||||||
|
CANCELLED
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.param;
|
||||||
|
|
||||||
|
import com.njcn.web.pojo.param.BaseParam;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库备份参数。
|
||||||
|
*/
|
||||||
|
public class DatabaseBackupParam {
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("创建备份任务参数")
|
||||||
|
public static class CreateParam {
|
||||||
|
@ApiModelProperty("连接 ID")
|
||||||
|
@NotBlank(message = "连接 ID 不能为空")
|
||||||
|
private String connectionId;
|
||||||
|
@ApiModelProperty("备份策略:DATA_PUMP、JDBC_EXPORT,默认 DATA_PUMP")
|
||||||
|
private String backupStrategy;
|
||||||
|
@ApiModelProperty("Schema")
|
||||||
|
private String schemaName;
|
||||||
|
@ApiModelProperty("表名列表")
|
||||||
|
private List<String> targetNames;
|
||||||
|
@ApiModelProperty("备份模式:FULL_TABLE、TIME_RANGE、SIZE_SPLIT")
|
||||||
|
private String backupMode;
|
||||||
|
@ApiModelProperty("时间字段")
|
||||||
|
private String timeColumn;
|
||||||
|
@ApiModelProperty("开始时间")
|
||||||
|
private LocalDateTime startTime;
|
||||||
|
@ApiModelProperty("结束时间")
|
||||||
|
private LocalDateTime endTime;
|
||||||
|
@ApiModelProperty("最大文件大小 MB")
|
||||||
|
private Integer maxFileSizeMb;
|
||||||
|
@ApiModelProperty("Oracle Directory 名称")
|
||||||
|
private String directoryName;
|
||||||
|
@ApiModelProperty("临时密码,不保存密码时传入")
|
||||||
|
private String temporaryPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@ApiModel("备份任务查询参数")
|
||||||
|
public static class TaskQueryParam extends BaseParam {
|
||||||
|
@ApiModelProperty("连接 ID")
|
||||||
|
private String connectionId;
|
||||||
|
@ApiModelProperty("任务状态")
|
||||||
|
private String taskStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@ApiModel("备份文件查询参数")
|
||||||
|
public static class FileQueryParam extends BaseParam {
|
||||||
|
@ApiModelProperty("连接 ID")
|
||||||
|
private String connectionId;
|
||||||
|
@ApiModelProperty("任务 ID")
|
||||||
|
private String taskId;
|
||||||
|
@ApiModelProperty("备份策略")
|
||||||
|
private String backupStrategy;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("停止备份任务参数")
|
||||||
|
public static class StopParam {
|
||||||
|
@ApiModelProperty("备份任务 ID")
|
||||||
|
@NotBlank(message = "备份任务 ID 不能为空")
|
||||||
|
private String taskId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("重新开始备份任务参数")
|
||||||
|
public static class RestartParam {
|
||||||
|
@ApiModelProperty("备份任务 ID")
|
||||||
|
@NotBlank(message = "备份任务 ID 不能为空")
|
||||||
|
private String taskId;
|
||||||
|
@ApiModelProperty("临时密码,原连接未保存密码时传入")
|
||||||
|
private String temporaryPassword;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.param;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAlias;
|
||||||
|
import com.njcn.web.pojo.param.BaseParam;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库连接配置参数。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ApiModel("数据库连接配置参数")
|
||||||
|
public class DatabaseConnectionParam {
|
||||||
|
|
||||||
|
@ApiModelProperty("连接名称")
|
||||||
|
@NotBlank(message = "连接名称不能为空")
|
||||||
|
private String connectionName;
|
||||||
|
|
||||||
|
@ApiModelProperty("数据库类型:ORACLE、MYSQL")
|
||||||
|
private String dbType;
|
||||||
|
|
||||||
|
@ApiModelProperty("数据库主机地址")
|
||||||
|
@NotBlank(message = "数据库主机地址不能为空")
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
@ApiModelProperty("数据库端口")
|
||||||
|
@NotNull(message = "数据库端口不能为空")
|
||||||
|
private Integer port;
|
||||||
|
|
||||||
|
@ApiModelProperty("连接类型:SERVICE_NAME、SID")
|
||||||
|
private String connectType;
|
||||||
|
|
||||||
|
@ApiModelProperty("服务名")
|
||||||
|
private String serviceName;
|
||||||
|
|
||||||
|
@ApiModelProperty("SID")
|
||||||
|
private String sid;
|
||||||
|
|
||||||
|
@ApiModelProperty("数据库名,MySQL 使用")
|
||||||
|
private String databaseName;
|
||||||
|
|
||||||
|
@ApiModelProperty("Schema")
|
||||||
|
private String schemaName;
|
||||||
|
|
||||||
|
@ApiModelProperty("用户名")
|
||||||
|
@NotBlank(message = "用户名不能为空")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@ApiModelProperty("密码")
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
@ApiModelProperty("是否保存密码:0-否,1-是")
|
||||||
|
private Integer savePassword;
|
||||||
|
|
||||||
|
@ApiModelProperty("Oracle Directory 名称")
|
||||||
|
private String directoryName;
|
||||||
|
|
||||||
|
@ApiModelProperty("Oracle Directory 物理路径")
|
||||||
|
private String directoryPath;
|
||||||
|
|
||||||
|
@ApiModelProperty("扩展配置 JSON")
|
||||||
|
private String extraConfigJson;
|
||||||
|
|
||||||
|
@ApiModelProperty("备注")
|
||||||
|
private String remark;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@ApiModel("数据库连接更新参数")
|
||||||
|
public static class UpdateParam extends DatabaseConnectionParam {
|
||||||
|
@ApiModelProperty("连接 ID")
|
||||||
|
@NotBlank(message = "连接 ID 不能为空")
|
||||||
|
private String id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@ApiModel("数据库连接查询参数")
|
||||||
|
public static class QueryParam extends BaseParam {
|
||||||
|
@ApiModelProperty("连接名称")
|
||||||
|
private String connectionName;
|
||||||
|
@ApiModelProperty("数据库类型")
|
||||||
|
private String dbType;
|
||||||
|
@ApiModelProperty("Schema")
|
||||||
|
private String schemaName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("数据库连接删除参数")
|
||||||
|
public static class DeleteParam {
|
||||||
|
@ApiModelProperty("连接 ID")
|
||||||
|
@NotBlank(message = "连接 ID 不能为空")
|
||||||
|
private String id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("数据库连接测试参数")
|
||||||
|
public static class TestParam {
|
||||||
|
@ApiModelProperty("连接 ID,已有连接测试时传入")
|
||||||
|
private String connectionId;
|
||||||
|
@ApiModelProperty("临时连接参数,新增前测试时传入")
|
||||||
|
private DatabaseConnectionParam connection;
|
||||||
|
@ApiModelProperty("临时密码,测试时允许只传该字段而不写入 connection.password")
|
||||||
|
private String temporaryPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("数据库表查询参数")
|
||||||
|
public static class TablesParam {
|
||||||
|
@ApiModelProperty("连接 ID")
|
||||||
|
@NotBlank(message = "连接 ID 不能为空")
|
||||||
|
private String connectionId;
|
||||||
|
@ApiModelProperty("临时密码,不保存密码时传入")
|
||||||
|
private String temporaryPassword;
|
||||||
|
@ApiModelProperty("兼容前端传入的运行时密码;为空时复用数据库 password_cipher")
|
||||||
|
private String password;
|
||||||
|
@JsonAlias("password_cipher")
|
||||||
|
@ApiModelProperty("兼容前端传入的已保存密码")
|
||||||
|
private String passwordCipher;
|
||||||
|
@ApiModelProperty("兼容前端传入的临时连接参数")
|
||||||
|
private DatabaseConnectionParam connection;
|
||||||
|
@ApiModelProperty("Schema 或数据库名,不传则使用连接默认值")
|
||||||
|
private String schemaName;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.param;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库运维删除参数。
|
||||||
|
*/
|
||||||
|
public class DatabaseDeleteParam {
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("删除备份文件参数")
|
||||||
|
public static class BackupFileParam {
|
||||||
|
@ApiModelProperty("备份文件 ID")
|
||||||
|
@NotBlank(message = "备份文件 ID 不能为空")
|
||||||
|
private String backupFileId;
|
||||||
|
@ApiModelProperty("确认文案")
|
||||||
|
private String confirmText;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("删除任务参数")
|
||||||
|
public static class TaskParam {
|
||||||
|
@ApiModelProperty("任务 ID")
|
||||||
|
@NotBlank(message = "任务 ID 不能为空")
|
||||||
|
private String taskId;
|
||||||
|
@ApiModelProperty("确认文案")
|
||||||
|
private String confirmText;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.param;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库恢复参数。
|
||||||
|
*/
|
||||||
|
public class DatabaseRestoreParam {
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@ApiModel("创建恢复任务参数")
|
||||||
|
public static class CreateParam {
|
||||||
|
@ApiModelProperty("目标连接 ID")
|
||||||
|
@NotBlank(message = "连接 ID 不能为空")
|
||||||
|
private String connectionId;
|
||||||
|
@ApiModelProperty("备份文件 ID")
|
||||||
|
@NotBlank(message = "备份文件 ID 不能为空")
|
||||||
|
private String backupFileId;
|
||||||
|
@ApiModelProperty("恢复模式:SKIP、APPEND、TRUNCATE、REPLACE")
|
||||||
|
private String restoreMode;
|
||||||
|
@ApiModelProperty("目标 Schema")
|
||||||
|
private String targetSchemaName;
|
||||||
|
@ApiModelProperty("临时密码,不保存密码时传入")
|
||||||
|
private String temporaryPassword;
|
||||||
|
@ApiModelProperty("覆盖确认文案")
|
||||||
|
private String overwriteConfirmText;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.po;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库备份文件记录。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@TableName("dbms_backup_file")
|
||||||
|
public class DatabaseBackupFile implements Serializable {
|
||||||
|
private static final long serialVersionUID = 3119981982091873277L;
|
||||||
|
|
||||||
|
@TableId("id")
|
||||||
|
private String id;
|
||||||
|
@TableField("task_id")
|
||||||
|
private String taskId;
|
||||||
|
@TableField("connection_id")
|
||||||
|
private String connectionId;
|
||||||
|
@TableField("db_type")
|
||||||
|
private String dbType;
|
||||||
|
@TableField("backup_strategy")
|
||||||
|
private String backupStrategy;
|
||||||
|
@TableField("file_format")
|
||||||
|
private String fileFormat;
|
||||||
|
@TableField("schema_name")
|
||||||
|
private String schemaName;
|
||||||
|
@TableField("target_names_json")
|
||||||
|
private String targetNamesJson;
|
||||||
|
@TableField("backup_mode")
|
||||||
|
private String backupMode;
|
||||||
|
@TableField("backup_start_time")
|
||||||
|
private LocalDateTime backupStartTime;
|
||||||
|
@TableField("backup_end_time")
|
||||||
|
private LocalDateTime backupEndTime;
|
||||||
|
@TableField("time_column")
|
||||||
|
private String timeColumn;
|
||||||
|
@TableField("directory_name")
|
||||||
|
private String directoryName;
|
||||||
|
@TableField("dump_file_name")
|
||||||
|
private String dumpFileName;
|
||||||
|
@TableField("log_file_name")
|
||||||
|
private String logFileName;
|
||||||
|
@TableField("file_name")
|
||||||
|
private String fileName;
|
||||||
|
@TableField("file_path")
|
||||||
|
private String filePath;
|
||||||
|
@TableField("log_file_path")
|
||||||
|
private String logFilePath;
|
||||||
|
@TableField("metadata_file_path")
|
||||||
|
private String metadataFilePath;
|
||||||
|
@TableField("file_size")
|
||||||
|
private Long fileSize;
|
||||||
|
@TableField("checksum")
|
||||||
|
private String checksum;
|
||||||
|
@TableField("state")
|
||||||
|
private Integer state;
|
||||||
|
@TableField("create_by")
|
||||||
|
private String createBy;
|
||||||
|
@TableField("create_time")
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
@TableField("update_by")
|
||||||
|
private String updateBy;
|
||||||
|
@TableField("update_time")
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.po;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库连接配置。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@TableName("dbms_connection")
|
||||||
|
public class DatabaseConnection implements Serializable {
|
||||||
|
private static final long serialVersionUID = -5821519248914313778L;
|
||||||
|
|
||||||
|
@TableId("id")
|
||||||
|
private String id;
|
||||||
|
@TableField("connection_name")
|
||||||
|
private String connectionName;
|
||||||
|
@TableField("db_type")
|
||||||
|
private String dbType;
|
||||||
|
@TableField("host")
|
||||||
|
private String host;
|
||||||
|
@TableField("port")
|
||||||
|
private Integer port;
|
||||||
|
@TableField("connect_type")
|
||||||
|
private String connectType;
|
||||||
|
@TableField("service_name")
|
||||||
|
private String serviceName;
|
||||||
|
@TableField("sid")
|
||||||
|
private String sid;
|
||||||
|
@TableField("database_name")
|
||||||
|
private String databaseName;
|
||||||
|
@TableField("schema_name")
|
||||||
|
private String schemaName;
|
||||||
|
@TableField("username")
|
||||||
|
private String username;
|
||||||
|
@TableField("password_cipher")
|
||||||
|
private String passwordCipher;
|
||||||
|
@TableField("save_password")
|
||||||
|
private Integer savePassword;
|
||||||
|
@TableField("directory_name")
|
||||||
|
private String directoryName;
|
||||||
|
@TableField("directory_path")
|
||||||
|
private String directoryPath;
|
||||||
|
@TableField("extra_config_json")
|
||||||
|
private String extraConfigJson;
|
||||||
|
@TableField("remark")
|
||||||
|
private String remark;
|
||||||
|
@TableField("last_test_status")
|
||||||
|
private String lastTestStatus;
|
||||||
|
@TableField("last_test_message")
|
||||||
|
private String lastTestMessage;
|
||||||
|
@TableField("last_test_time")
|
||||||
|
private LocalDateTime lastTestTime;
|
||||||
|
@TableField("state")
|
||||||
|
private Integer state;
|
||||||
|
@TableField("create_by")
|
||||||
|
private String createBy;
|
||||||
|
@TableField("create_time")
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
@TableField("update_by")
|
||||||
|
private String updateBy;
|
||||||
|
@TableField("update_time")
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.po;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库运维任务。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@TableName("dbms_operation_task")
|
||||||
|
public class DatabaseOperationTask implements Serializable {
|
||||||
|
private static final long serialVersionUID = 1831235987236858769L;
|
||||||
|
|
||||||
|
@TableId("id")
|
||||||
|
private String id;
|
||||||
|
@TableField("task_no")
|
||||||
|
private String taskNo;
|
||||||
|
@TableField("connection_id")
|
||||||
|
private String connectionId;
|
||||||
|
@TableField("db_type")
|
||||||
|
private String dbType;
|
||||||
|
@TableField("operation_type")
|
||||||
|
private String operationType;
|
||||||
|
@TableField("backup_strategy")
|
||||||
|
private String backupStrategy;
|
||||||
|
@TableField("task_status")
|
||||||
|
private String taskStatus;
|
||||||
|
@TableField("schema_name")
|
||||||
|
private String schemaName;
|
||||||
|
@TableField("target_names_json")
|
||||||
|
private String targetNamesJson;
|
||||||
|
@TableField("request_param_json")
|
||||||
|
private String requestParamJson;
|
||||||
|
@TableField("result_message")
|
||||||
|
private String resultMessage;
|
||||||
|
@TableField("progress_percent")
|
||||||
|
private BigDecimal progressPercent;
|
||||||
|
@TableField("started_at")
|
||||||
|
private LocalDateTime startedAt;
|
||||||
|
@TableField("finished_at")
|
||||||
|
private LocalDateTime finishedAt;
|
||||||
|
@TableField("state")
|
||||||
|
private Integer state;
|
||||||
|
@TableField("create_by")
|
||||||
|
private String createBy;
|
||||||
|
@TableField("create_time")
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
@TableField("update_by")
|
||||||
|
private String updateBy;
|
||||||
|
@TableField("update_time")
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.po;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库恢复记录。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@TableName("dbms_restore_record")
|
||||||
|
public class DatabaseRestoreRecord implements Serializable {
|
||||||
|
private static final long serialVersionUID = -5638979151924581277L;
|
||||||
|
|
||||||
|
@TableId("id")
|
||||||
|
private String id;
|
||||||
|
@TableField("task_id")
|
||||||
|
private String taskId;
|
||||||
|
@TableField("backup_file_id")
|
||||||
|
private String backupFileId;
|
||||||
|
@TableField("connection_id")
|
||||||
|
private String connectionId;
|
||||||
|
@TableField("db_type")
|
||||||
|
private String dbType;
|
||||||
|
@TableField("restore_mode")
|
||||||
|
private String restoreMode;
|
||||||
|
@TableField("target_schema_name")
|
||||||
|
private String targetSchemaName;
|
||||||
|
@TableField("target_names_json")
|
||||||
|
private String targetNamesJson;
|
||||||
|
@TableField("table_exists_action")
|
||||||
|
private String tableExistsAction;
|
||||||
|
@TableField("overwrite_confirmed")
|
||||||
|
private Integer overwriteConfirmed;
|
||||||
|
@TableField("result_message")
|
||||||
|
private String resultMessage;
|
||||||
|
@TableField("state")
|
||||||
|
private Integer state;
|
||||||
|
@TableField("create_by")
|
||||||
|
private String createBy;
|
||||||
|
@TableField("create_time")
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
@TableField("update_by")
|
||||||
|
private String updateBy;
|
||||||
|
@TableField("update_time")
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库备份文件响应。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class DatabaseBackupFileVO {
|
||||||
|
private String id;
|
||||||
|
private String taskId;
|
||||||
|
private String connectionId;
|
||||||
|
private String dbType;
|
||||||
|
private String backupStrategy;
|
||||||
|
private String fileFormat;
|
||||||
|
private String schemaName;
|
||||||
|
private String targetNamesJson;
|
||||||
|
private String backupMode;
|
||||||
|
private String fileName;
|
||||||
|
private String filePath;
|
||||||
|
private String logFileName;
|
||||||
|
private String logFilePath;
|
||||||
|
private Long fileSize;
|
||||||
|
private String checksum;
|
||||||
|
private Integer state;
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库连接配置响应。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class DatabaseConnectionVO {
|
||||||
|
private String id;
|
||||||
|
private String connectionName;
|
||||||
|
private String dbType;
|
||||||
|
private String host;
|
||||||
|
private Integer port;
|
||||||
|
private String connectType;
|
||||||
|
private String serviceName;
|
||||||
|
private String sid;
|
||||||
|
private String databaseName;
|
||||||
|
private String schemaName;
|
||||||
|
private String username;
|
||||||
|
private Integer savePassword;
|
||||||
|
private String directoryName;
|
||||||
|
private String directoryPath;
|
||||||
|
private String extraConfigJson;
|
||||||
|
private String remark;
|
||||||
|
private String lastTestStatus;
|
||||||
|
private String lastTestMessage;
|
||||||
|
private LocalDateTime lastTestTime;
|
||||||
|
private Integer state;
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库表信息。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class DatabaseTableVO {
|
||||||
|
private String owner;
|
||||||
|
private String tableName;
|
||||||
|
private Long autoIncrementValue = 0L;
|
||||||
|
private Long autoIncrement = 0L;
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
private Long dataLength;
|
||||||
|
private String engine;
|
||||||
|
private Long tableRows;
|
||||||
|
private String comments;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运维任务创建结果。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class DatabaseTaskCreateVO {
|
||||||
|
private String taskId;
|
||||||
|
private String taskNo;
|
||||||
|
private String taskStatus;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库运维任务响应。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class DatabaseTaskVO {
|
||||||
|
private String id;
|
||||||
|
private String taskNo;
|
||||||
|
private String connectionId;
|
||||||
|
private String dbType;
|
||||||
|
private String operationType;
|
||||||
|
private String backupStrategy;
|
||||||
|
private String taskStatus;
|
||||||
|
private String schemaName;
|
||||||
|
private String targetNamesJson;
|
||||||
|
private String resultMessage;
|
||||||
|
private BigDecimal progressPercent;
|
||||||
|
private LocalDateTime startedAt;
|
||||||
|
private LocalDateTime finishedAt;
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.njcn.gather.systemops.database.pojo.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库连接测试结果。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class DatabaseTestResultVO {
|
||||||
|
private Boolean success;
|
||||||
|
private String message;
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.njcn.gather.systemops.database.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseBackupParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseBackupFile;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseBackupFileVO;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库备份文件服务。
|
||||||
|
*/
|
||||||
|
public interface DatabaseBackupFileService extends IService<DatabaseBackupFile> {
|
||||||
|
|
||||||
|
Page<DatabaseBackupFileVO> listFiles(DatabaseBackupParam.FileQueryParam param);
|
||||||
|
|
||||||
|
boolean deleteBackupFile(String backupFileId, String confirmText);
|
||||||
|
|
||||||
|
void validateBackupFileReadable(DatabaseBackupFile backupFile);
|
||||||
|
|
||||||
|
Path resolveManagedPath(DatabaseBackupFile backupFile, String filePath);
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.njcn.gather.systemops.database.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseConnectionParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseConnectionVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTableVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTestResultVO;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库连接配置服务。
|
||||||
|
*/
|
||||||
|
public interface DatabaseConnectionService extends IService<DatabaseConnection> {
|
||||||
|
|
||||||
|
Page<DatabaseConnectionVO> listConnections(DatabaseConnectionParam.QueryParam queryParam);
|
||||||
|
|
||||||
|
boolean addConnection(DatabaseConnectionParam param);
|
||||||
|
|
||||||
|
boolean updateConnection(DatabaseConnectionParam.UpdateParam param);
|
||||||
|
|
||||||
|
boolean deleteConnection(DatabaseConnectionParam.DeleteParam param);
|
||||||
|
|
||||||
|
DatabaseTestResultVO testConnection(DatabaseConnectionParam.TestParam param);
|
||||||
|
|
||||||
|
List<DatabaseTableVO> listTables(DatabaseConnectionParam.TablesParam param);
|
||||||
|
|
||||||
|
DatabaseConnection requireEnabled(String connectionId);
|
||||||
|
|
||||||
|
String resolvePassword(DatabaseConnection connection, String temporaryPassword);
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.njcn.gather.systemops.database.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseBackupParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseOperationTask;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTaskCreateVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTaskVO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库运维任务服务。
|
||||||
|
*/
|
||||||
|
public interface DatabaseOperationTaskService extends IService<DatabaseOperationTask> {
|
||||||
|
|
||||||
|
DatabaseTaskCreateVO createBackupTask(DatabaseBackupParam.CreateParam param);
|
||||||
|
|
||||||
|
Page<DatabaseTaskVO> listBackupTasks(DatabaseBackupParam.TaskQueryParam param);
|
||||||
|
|
||||||
|
DatabaseTaskVO getStatus(String taskId);
|
||||||
|
|
||||||
|
boolean stopBackupTask(DatabaseBackupParam.StopParam param);
|
||||||
|
|
||||||
|
DatabaseTaskCreateVO restartBackupTask(DatabaseBackupParam.RestartParam param);
|
||||||
|
|
||||||
|
boolean deleteTask(String taskId, String confirmText);
|
||||||
|
|
||||||
|
boolean existsRunningTask(String connectionId);
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.njcn.gather.systemops.database.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseRestoreParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseRestoreRecord;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTaskCreateVO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库恢复服务。
|
||||||
|
*/
|
||||||
|
public interface DatabaseRestoreService extends IService<DatabaseRestoreRecord> {
|
||||||
|
|
||||||
|
DatabaseTaskCreateVO createRestoreTask(DatabaseRestoreParam.CreateParam param);
|
||||||
|
}
|
||||||
@@ -0,0 +1,179 @@
|
|||||||
|
package com.njcn.gather.systemops.database.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
|
import com.njcn.gather.systemops.database.config.DbmsProperties;
|
||||||
|
import com.njcn.gather.systemops.database.constant.DatabaseOpsConst;
|
||||||
|
import com.njcn.gather.systemops.database.mapper.DatabaseBackupFileMapper;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseBackupParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseBackupFile;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseBackupFileVO;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseBackupFileService;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseChecksumUtil;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabasePathUtil;
|
||||||
|
import com.njcn.web.factory.PageFactory;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库备份文件服务实现。
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DatabaseBackupFileServiceImpl extends ServiceImpl<DatabaseBackupFileMapper, DatabaseBackupFile> implements DatabaseBackupFileService {
|
||||||
|
|
||||||
|
private final DbmsProperties dbmsProperties;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<DatabaseBackupFileVO> listFiles(DatabaseBackupParam.FileQueryParam param) {
|
||||||
|
DatabaseBackupParam.FileQueryParam query = param == null ? new DatabaseBackupParam.FileQueryParam() : param;
|
||||||
|
LambdaQueryWrapper<DatabaseBackupFile> wrapper = new LambdaQueryWrapper<>();
|
||||||
|
wrapper.eq(DatabaseBackupFile::getState, DatabaseOpsConst.STATE_ENABLED)
|
||||||
|
.eq(StrUtil.isNotBlank(query.getConnectionId()), DatabaseBackupFile::getConnectionId, query.getConnectionId())
|
||||||
|
.eq(StrUtil.isNotBlank(query.getTaskId()), DatabaseBackupFile::getTaskId, query.getTaskId())
|
||||||
|
.eq(StrUtil.isNotBlank(query.getBackupStrategy()), DatabaseBackupFile::getBackupStrategy, query.getBackupStrategy())
|
||||||
|
.orderByDesc(DatabaseBackupFile::getCreateTime);
|
||||||
|
Page<DatabaseBackupFile> page = this.page(new Page<>(PageFactory.getPageNum(query), PageFactory.getPageSize(query)), wrapper);
|
||||||
|
Page<DatabaseBackupFileVO> result = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
|
||||||
|
result.setRecords(page.getRecords().stream().map(this::toVO).collect(Collectors.toList()));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public boolean deleteBackupFile(String backupFileId, String confirmText) {
|
||||||
|
if (!DatabaseOpsConst.CONFIRM_DELETE.equals(confirmText)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "确认文案不正确");
|
||||||
|
}
|
||||||
|
DatabaseBackupFile file = this.lambdaQuery()
|
||||||
|
.eq(DatabaseBackupFile::getId, backupFileId)
|
||||||
|
.eq(DatabaseBackupFile::getState, DatabaseOpsConst.STATE_ENABLED)
|
||||||
|
.one();
|
||||||
|
if (file == null) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份文件不存在或已删除");
|
||||||
|
}
|
||||||
|
deletePhysicalPath(file, file.getFilePath());
|
||||||
|
deletePhysicalPath(file, file.getLogFilePath());
|
||||||
|
deletePhysicalPath(file, file.getMetadataFilePath());
|
||||||
|
file.setState(DatabaseOpsConst.STATE_DELETED);
|
||||||
|
file.setUpdateTime(LocalDateTime.now());
|
||||||
|
return this.updateById(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validateBackupFileReadable(DatabaseBackupFile backupFile) {
|
||||||
|
validateReadablePath(backupFile, backupFile.getFilePath(), "备份文件", false, true);
|
||||||
|
validateReadablePath(backupFile, backupFile.getMetadataFilePath(), "备份元数据文件",
|
||||||
|
StrUtil.isBlank(backupFile.getMetadataFilePath()), false);
|
||||||
|
if (StrUtil.isBlank(backupFile.getChecksum())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份文件缺少校验值");
|
||||||
|
}
|
||||||
|
Path checksumPath = resolveChecksumPath(backupFile);
|
||||||
|
String actualChecksum = DatabaseChecksumUtil.sha256(checksumPath);
|
||||||
|
if (!backupFile.getChecksum().equalsIgnoreCase(actualChecksum)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份文件校验失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Path resolveManagedPath(DatabaseBackupFile backupFile, String filePath) {
|
||||||
|
if (StrUtil.isBlank(filePath)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Path path = DatabasePathUtil.normalize(filePath);
|
||||||
|
if (path == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Path storageRoot = DatabasePathUtil.normalize(dbmsProperties.getBackup().getStoragePath());
|
||||||
|
if (DatabasePathUtil.isUnder(path, storageRoot)) {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
Path primaryFilePath = DatabasePathUtil.normalize(backupFile.getFilePath());
|
||||||
|
if (primaryFilePath != null) {
|
||||||
|
Path allowedRoot = Files.isDirectory(primaryFilePath) ? primaryFilePath : primaryFilePath.getParent();
|
||||||
|
if (allowedRoot != null && DatabasePathUtil.isUnder(path, allowedRoot)) {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "文件路径不在允许的备份目录内");
|
||||||
|
}
|
||||||
|
|
||||||
|
private Path resolveChecksumPath(DatabaseBackupFile backupFile) {
|
||||||
|
Path metadataPath = resolveManagedPath(backupFile, backupFile.getMetadataFilePath());
|
||||||
|
if (metadataPath != null && Files.exists(metadataPath) && !Files.isDirectory(metadataPath)) {
|
||||||
|
return metadataPath;
|
||||||
|
}
|
||||||
|
Path filePath = resolveManagedPath(backupFile, backupFile.getFilePath());
|
||||||
|
if (filePath == null || !Files.exists(filePath) || Files.isDirectory(filePath)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份校验文件不存在");
|
||||||
|
}
|
||||||
|
return filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deletePhysicalPath(DatabaseBackupFile backupFile, String filePath) {
|
||||||
|
if (StrUtil.isBlank(filePath)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Path path = resolveManagedPath(backupFile, filePath);
|
||||||
|
if (path == null || !Files.exists(path)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (Files.isDirectory(path)) {
|
||||||
|
try (Stream<Path> paths = Files.walk(path)) {
|
||||||
|
paths.sorted(Comparator.reverseOrder()).forEach(this::deleteSinglePath);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Files.delete(path);
|
||||||
|
}
|
||||||
|
} catch (BusinessException exception) {
|
||||||
|
throw exception;
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "删除物理文件失败:" + exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteSinglePath(Path path) {
|
||||||
|
try {
|
||||||
|
Files.deleteIfExists(path);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "删除物理文件失败:" + exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateReadablePath(DatabaseBackupFile backupFile, String filePath, String fileType,
|
||||||
|
boolean allowBlank, boolean allowDirectory) {
|
||||||
|
if (StrUtil.isBlank(filePath)) {
|
||||||
|
if (allowBlank) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, fileType + "路径不能为空");
|
||||||
|
}
|
||||||
|
Path path = resolveManagedPath(backupFile, filePath);
|
||||||
|
if (path == null || !Files.exists(path)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, fileType + "不存在");
|
||||||
|
}
|
||||||
|
if (Files.isDirectory(path) && !allowDirectory) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, fileType + "不能是目录");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseBackupFileVO toVO(DatabaseBackupFile file) {
|
||||||
|
DatabaseBackupFileVO vo = new DatabaseBackupFileVO();
|
||||||
|
BeanUtil.copyProperties(file, vo);
|
||||||
|
return vo;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,287 @@
|
|||||||
|
package com.njcn.gather.systemops.database.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
|
import com.njcn.gather.systemops.database.component.DatabasePasswordComponent;
|
||||||
|
import com.njcn.gather.systemops.database.constant.DatabaseOpsConst;
|
||||||
|
import com.njcn.gather.systemops.database.mapper.DatabaseConnectionMapper;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseConnectionParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseConnectionVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTableVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTestResultVO;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseConnectionService;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseOperationTaskService;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseConnectionOperator;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseOperatorRegistry;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseOpsIdUtil;
|
||||||
|
import com.njcn.web.factory.PageFactory;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库连接配置服务实现。
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DatabaseConnectionServiceImpl extends ServiceImpl<DatabaseConnectionMapper, DatabaseConnection> implements DatabaseConnectionService {
|
||||||
|
|
||||||
|
private final DatabasePasswordComponent databasePasswordComponent;
|
||||||
|
private final DatabaseOperatorRegistry databaseOperatorRegistry;
|
||||||
|
private final ObjectProvider<DatabaseOperationTaskService> databaseOperationTaskServiceProvider;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<DatabaseConnectionVO> listConnections(DatabaseConnectionParam.QueryParam queryParam) {
|
||||||
|
DatabaseConnectionParam.QueryParam query = queryParam == null ? new DatabaseConnectionParam.QueryParam() : queryParam;
|
||||||
|
LambdaQueryWrapper<DatabaseConnection> wrapper = new LambdaQueryWrapper<>();
|
||||||
|
wrapper.eq(DatabaseConnection::getState, DatabaseOpsConst.STATE_ENABLED)
|
||||||
|
.like(StrUtil.isNotBlank(query.getConnectionName()), DatabaseConnection::getConnectionName, query.getConnectionName())
|
||||||
|
.eq(StrUtil.isNotBlank(query.getDbType()), DatabaseConnection::getDbType, query.getDbType())
|
||||||
|
.like(StrUtil.isNotBlank(query.getSchemaName()), DatabaseConnection::getSchemaName, query.getSchemaName())
|
||||||
|
.orderByDesc(DatabaseConnection::getUpdateTime);
|
||||||
|
Page<DatabaseConnection> page = this.page(new Page<>(PageFactory.getPageNum(query), PageFactory.getPageSize(query)), wrapper);
|
||||||
|
Page<DatabaseConnectionVO> result = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
|
||||||
|
result.setRecords(page.getRecords().stream().map(this::toVO).collect(Collectors.toList()));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public boolean addConnection(DatabaseConnectionParam param) {
|
||||||
|
DatabaseConnection connection = new DatabaseConnection();
|
||||||
|
fillConnection(connection, param, true);
|
||||||
|
checkConnectionNameUnique(connection.getConnectionName());
|
||||||
|
connection.setId(DatabaseOpsIdUtil.uuid());
|
||||||
|
connection.setState(DatabaseOpsConst.STATE_ENABLED);
|
||||||
|
connection.setCreateTime(LocalDateTime.now());
|
||||||
|
connection.setUpdateTime(LocalDateTime.now());
|
||||||
|
return this.save(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public boolean updateConnection(DatabaseConnectionParam.UpdateParam param) {
|
||||||
|
DatabaseConnection connection = requireEnabled(param.getId());
|
||||||
|
fillConnection(connection, param, false);
|
||||||
|
connection.setUpdateTime(LocalDateTime.now());
|
||||||
|
return this.updateById(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public boolean deleteConnection(DatabaseConnectionParam.DeleteParam param) {
|
||||||
|
requireEnabled(param.getId());
|
||||||
|
if (databaseOperationTaskServiceProvider.getObject().existsRunningTask(param.getId())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "存在运行中的任务,不能删除连接");
|
||||||
|
}
|
||||||
|
return this.lambdaUpdate()
|
||||||
|
.set(DatabaseConnection::getState, DatabaseOpsConst.STATE_DELETED)
|
||||||
|
.set(DatabaseConnection::getUpdateTime, LocalDateTime.now())
|
||||||
|
.eq(DatabaseConnection::getId, param.getId())
|
||||||
|
.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public DatabaseTestResultVO testConnection(DatabaseConnectionParam.TestParam param) {
|
||||||
|
DatabaseConnection connection = resolveTestConnection(param);
|
||||||
|
DatabaseConnectionOperator operator = databaseOperatorRegistry.getConnectionOperator(connection.getDbType());
|
||||||
|
DatabaseTestResultVO result = operator.test(connection, resolvePassword(connection, param.getTemporaryPassword()));
|
||||||
|
if (StrUtil.isNotBlank(connection.getId())) {
|
||||||
|
updateLastTestResult(connection.getId(), result);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<DatabaseTableVO> listTables(DatabaseConnectionParam.TablesParam param) {
|
||||||
|
DatabaseConnection connection = requireEnabled(param.getConnectionId());
|
||||||
|
try {
|
||||||
|
DatabaseConnectionOperator operator = databaseOperatorRegistry.getConnectionOperator(connection.getDbType());
|
||||||
|
String password = resolveTablesPassword(connection, param);
|
||||||
|
return operator.listTables(connection, password,
|
||||||
|
resolveSchemaOrDatabase(param, connection));
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DatabaseConnection requireEnabled(String connectionId) {
|
||||||
|
if (StrUtil.isBlank(connectionId)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "连接 ID 不能为空");
|
||||||
|
}
|
||||||
|
DatabaseConnection connection = this.lambdaQuery()
|
||||||
|
.eq(DatabaseConnection::getId, connectionId)
|
||||||
|
.eq(DatabaseConnection::getState, DatabaseOpsConst.STATE_ENABLED)
|
||||||
|
.one();
|
||||||
|
if (connection == null) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "数据库连接不存在或已删除");
|
||||||
|
}
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String resolvePassword(DatabaseConnection connection, String temporaryPassword) {
|
||||||
|
try {
|
||||||
|
return databasePasswordComponent.resolveRuntimePassword(connection.getPasswordCipher(), temporaryPassword);
|
||||||
|
} catch (IllegalArgumentException exception) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseConnection resolveTestConnection(DatabaseConnectionParam.TestParam param) {
|
||||||
|
if (StrUtil.isNotBlank(param.getConnectionId())) {
|
||||||
|
DatabaseConnection savedConnection = requireEnabled(param.getConnectionId());
|
||||||
|
if (param.getConnection() == null) {
|
||||||
|
return savedConnection;
|
||||||
|
}
|
||||||
|
DatabaseConnection connection = new DatabaseConnection();
|
||||||
|
fillConnection(connection, param.getConnection(), true, true);
|
||||||
|
connection.setId(savedConnection.getId());
|
||||||
|
if (StrUtil.isBlank(param.getConnection().getPassword())) {
|
||||||
|
// 已有连接测试编辑后参数时,未传密码则复用库里保存的密码。
|
||||||
|
connection.setPasswordCipher(savedConnection.getPasswordCipher());
|
||||||
|
}
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
if (param.getConnection() == null) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "连接测试参数不能为空");
|
||||||
|
}
|
||||||
|
DatabaseConnection connection = new DatabaseConnection();
|
||||||
|
fillConnection(connection, param.getConnection(), true, StrUtil.isNotBlank(param.getTemporaryPassword()));
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateLastTestResult(String connectionId, DatabaseTestResultVO result) {
|
||||||
|
this.lambdaUpdate()
|
||||||
|
.set(DatabaseConnection::getLastTestStatus, Boolean.TRUE.equals(result.getSuccess()) ? "SUCCESS" : "FAIL")
|
||||||
|
.set(DatabaseConnection::getLastTestMessage, result.getMessage())
|
||||||
|
.set(DatabaseConnection::getLastTestTime, LocalDateTime.now())
|
||||||
|
.eq(DatabaseConnection::getId, connectionId)
|
||||||
|
.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveTablesPassword(DatabaseConnection connection, DatabaseConnectionParam.TablesParam param) {
|
||||||
|
if (StrUtil.isNotBlank(param.getTemporaryPassword())) {
|
||||||
|
return param.getTemporaryPassword();
|
||||||
|
}
|
||||||
|
if (StrUtil.isNotBlank(param.getPassword())) {
|
||||||
|
return param.getPassword();
|
||||||
|
}
|
||||||
|
if (param.getConnection() != null && StrUtil.isNotBlank(param.getConnection().getPassword())) {
|
||||||
|
return param.getConnection().getPassword();
|
||||||
|
}
|
||||||
|
if (StrUtil.isNotBlank(param.getPasswordCipher())) {
|
||||||
|
return databasePasswordComponent.resolveRuntimePassword(param.getPasswordCipher(), null);
|
||||||
|
}
|
||||||
|
return resolvePassword(connection, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillConnection(DatabaseConnection connection, DatabaseConnectionParam param, boolean create) {
|
||||||
|
fillConnection(connection, param, create, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillConnection(DatabaseConnection connection, DatabaseConnectionParam param, boolean create,
|
||||||
|
boolean allowTemporaryPasswordOnly) {
|
||||||
|
String dbType = resolveDbType(param.getDbType());
|
||||||
|
validateConnectionParam(param, dbType);
|
||||||
|
connection.setConnectionName(param.getConnectionName().trim());
|
||||||
|
connection.setDbType(dbType);
|
||||||
|
connection.setHost(param.getHost().trim());
|
||||||
|
connection.setPort(param.getPort());
|
||||||
|
connection.setConnectType(DatabaseOpsConst.DB_TYPE_ORACLE.equals(dbType) ? resolveConnectType(param.getConnectType()) : null);
|
||||||
|
connection.setServiceName(DatabaseOpsConst.DB_TYPE_ORACLE.equals(dbType) ? trimToNull(param.getServiceName()) : null);
|
||||||
|
connection.setSid(DatabaseOpsConst.DB_TYPE_ORACLE.equals(dbType) ? trimToNull(param.getSid()) : null);
|
||||||
|
connection.setDatabaseName(DatabaseOpsConst.DB_TYPE_MYSQL.equals(dbType) ? trimToNull(param.getDatabaseName()) : null);
|
||||||
|
connection.setSchemaName(trimToNull(param.getSchemaName()));
|
||||||
|
connection.setUsername(param.getUsername().trim());
|
||||||
|
connection.setSavePassword(param.getSavePassword() == null ? DatabaseOpsConst.SAVE_PASSWORD_YES : param.getSavePassword());
|
||||||
|
if (connection.getSavePassword() != DatabaseOpsConst.SAVE_PASSWORD_YES
|
||||||
|
&& connection.getSavePassword() != DatabaseOpsConst.SAVE_PASSWORD_NO) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "savePassword 只能是 0 或 1");
|
||||||
|
}
|
||||||
|
if (DatabaseOpsConst.SAVE_PASSWORD_YES == connection.getSavePassword() && StrUtil.isNotBlank(param.getPassword())) {
|
||||||
|
connection.setPasswordCipher(databasePasswordComponent.encrypt(param.getPassword()));
|
||||||
|
}
|
||||||
|
if (DatabaseOpsConst.SAVE_PASSWORD_NO == connection.getSavePassword()) {
|
||||||
|
connection.setPasswordCipher(null);
|
||||||
|
} else if (create && StrUtil.isBlank(param.getPassword()) && !allowTemporaryPasswordOnly) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "保存密码时密码不能为空");
|
||||||
|
}
|
||||||
|
connection.setDirectoryName(DatabaseOpsConst.DB_TYPE_ORACLE.equals(dbType) ? trimToNull(param.getDirectoryName()) : null);
|
||||||
|
connection.setDirectoryPath(DatabaseOpsConst.DB_TYPE_ORACLE.equals(dbType) ? trimToNull(param.getDirectoryPath()) : null);
|
||||||
|
connection.setExtraConfigJson(trimToNull(param.getExtraConfigJson()));
|
||||||
|
connection.setRemark(param.getRemark());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateConnectionParam(DatabaseConnectionParam param, String dbType) {
|
||||||
|
if (DatabaseOpsConst.DB_TYPE_ORACLE.equals(dbType)) {
|
||||||
|
String connectType = resolveConnectType(param.getConnectType());
|
||||||
|
if (DatabaseOpsConst.CONNECT_TYPE_SERVICE_NAME.equals(connectType) && StrUtil.isBlank(param.getServiceName())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "SERVICE_NAME 连接方式下服务名不能为空");
|
||||||
|
}
|
||||||
|
if (DatabaseOpsConst.CONNECT_TYPE_SID.equals(connectType) && StrUtil.isBlank(param.getSid())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "SID 连接方式下 SID 不能为空");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (StrUtil.isBlank(param.getDatabaseName())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "MYSQL 数据库名不能为空");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增连接时,连接名称在有效记录中必须唯一。
|
||||||
|
*/
|
||||||
|
private void checkConnectionNameUnique(String connectionName) {
|
||||||
|
long count = this.lambdaQuery()
|
||||||
|
.eq(DatabaseConnection::getConnectionName, connectionName)
|
||||||
|
.eq(DatabaseConnection::getState, DatabaseOpsConst.STATE_ENABLED)
|
||||||
|
.count();
|
||||||
|
if (count > 0) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "连接名称已存在");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveConnectType(String connectType) {
|
||||||
|
return StrUtil.blankToDefault(connectType, DatabaseOpsConst.CONNECT_TYPE_SERVICE_NAME).trim().toUpperCase(Locale.ROOT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveDbType(String dbType) {
|
||||||
|
String resolved = StrUtil.blankToDefault(dbType, DatabaseOpsConst.DB_TYPE_ORACLE).trim().toUpperCase(Locale.ROOT);
|
||||||
|
if (!DatabaseOpsConst.DB_TYPE_ORACLE.equals(resolved) && !DatabaseOpsConst.DB_TYPE_MYSQL.equals(resolved)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "不支持的数据库类型:" + dbType);
|
||||||
|
}
|
||||||
|
return resolved;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveSchemaOrDatabase(DatabaseConnectionParam.TablesParam param, DatabaseConnection connection) {
|
||||||
|
if (DatabaseOpsConst.DB_TYPE_MYSQL.equals(connection.getDbType())) {
|
||||||
|
return StrUtil.blankToDefault(param.getSchemaName(), connection.getDatabaseName());
|
||||||
|
}
|
||||||
|
return param.getSchemaName();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String trimToNull(String value) {
|
||||||
|
return StrUtil.isBlank(value) ? null : value.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseConnectionVO toVO(DatabaseConnection connection) {
|
||||||
|
DatabaseConnectionVO vo = new DatabaseConnectionVO();
|
||||||
|
BeanUtil.copyProperties(connection, vo);
|
||||||
|
return vo;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,333 @@
|
|||||||
|
package com.njcn.gather.systemops.database.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
|
import com.njcn.gather.systemops.database.constant.DatabaseOpsConst;
|
||||||
|
import com.njcn.gather.systemops.database.mapper.DatabaseOperationTaskMapper;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.BackupModeEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.BackupStrategyEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.OperationTypeEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.TaskStatusEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseBackupParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseBackupFile;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseOperationTask;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTaskCreateVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTaskVO;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseBackupFileService;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseConnectionService;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseOperationTaskService;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseBackupOperator;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseOperatorRegistry;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseOpsIdUtil;
|
||||||
|
import com.njcn.web.factory.PageFactory;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库运维任务服务实现。
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DatabaseOperationTaskServiceImpl extends ServiceImpl<DatabaseOperationTaskMapper, DatabaseOperationTask> implements DatabaseOperationTaskService {
|
||||||
|
|
||||||
|
private final DatabaseConnectionService databaseConnectionService;
|
||||||
|
private final DatabaseBackupFileService databaseBackupFileService;
|
||||||
|
private final DatabaseOperatorRegistry databaseOperatorRegistry;
|
||||||
|
private final ObjectMapper objectMapper;
|
||||||
|
@Resource(name = "dbmsTaskExecutorService")
|
||||||
|
private ExecutorService dbmsTaskExecutorService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public DatabaseTaskCreateVO createBackupTask(DatabaseBackupParam.CreateParam param) {
|
||||||
|
DatabaseConnection connection = databaseConnectionService.requireEnabled(param.getConnectionId());
|
||||||
|
validateBackupParam(param, connection);
|
||||||
|
if (existsRunningTask(connection.getId())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "当前连接存在运行中的任务");
|
||||||
|
}
|
||||||
|
DatabaseOperationTask task = buildBackupTask(param, connection);
|
||||||
|
this.save(task);
|
||||||
|
dbmsTaskExecutorService.submit(() -> executeBackupTask(task.getId(), param));
|
||||||
|
return toCreateVO(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<DatabaseTaskVO> listBackupTasks(DatabaseBackupParam.TaskQueryParam param) {
|
||||||
|
DatabaseBackupParam.TaskQueryParam query = param == null ? new DatabaseBackupParam.TaskQueryParam() : param;
|
||||||
|
LambdaQueryWrapper<DatabaseOperationTask> wrapper = new LambdaQueryWrapper<>();
|
||||||
|
wrapper.eq(DatabaseOperationTask::getState, DatabaseOpsConst.STATE_ENABLED)
|
||||||
|
.eq(DatabaseOperationTask::getOperationType, OperationTypeEnum.BACKUP.name())
|
||||||
|
.eq(StrUtil.isNotBlank(query.getConnectionId()), DatabaseOperationTask::getConnectionId, query.getConnectionId())
|
||||||
|
.eq(StrUtil.isNotBlank(query.getTaskStatus()), DatabaseOperationTask::getTaskStatus, query.getTaskStatus())
|
||||||
|
.orderByDesc(DatabaseOperationTask::getCreateTime);
|
||||||
|
Page<DatabaseOperationTask> page = this.page(new Page<>(PageFactory.getPageNum(query), PageFactory.getPageSize(query)), wrapper);
|
||||||
|
Page<DatabaseTaskVO> result = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
|
||||||
|
result.setRecords(page.getRecords().stream().map(this::toVO).collect(Collectors.toList()));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DatabaseTaskVO getStatus(String taskId) {
|
||||||
|
return toVO(requireEnabledTask(taskId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public boolean stopBackupTask(DatabaseBackupParam.StopParam param) {
|
||||||
|
DatabaseOperationTask task = requireEnabledTask(param.getTaskId());
|
||||||
|
if (!OperationTypeEnum.BACKUP.name().equals(task.getOperationType())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "仅支持停止备份任务");
|
||||||
|
}
|
||||||
|
if (!TaskStatusEnum.WAITING.name().equals(task.getTaskStatus())
|
||||||
|
&& !TaskStatusEnum.RUNNING.name().equals(task.getTaskStatus())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "仅等待中或运行中的任务允许停止");
|
||||||
|
}
|
||||||
|
task.setTaskStatus(TaskStatusEnum.CANCELLED.name());
|
||||||
|
task.setResultMessage("用户请求停止备份任务");
|
||||||
|
task.setFinishedAt(LocalDateTime.now());
|
||||||
|
task.setUpdateTime(LocalDateTime.now());
|
||||||
|
return this.updateById(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public DatabaseTaskCreateVO restartBackupTask(DatabaseBackupParam.RestartParam param) {
|
||||||
|
DatabaseOperationTask sourceTask = requireEnabledTask(param.getTaskId());
|
||||||
|
if (!OperationTypeEnum.BACKUP.name().equals(sourceTask.getOperationType())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "仅支持重新开始备份任务");
|
||||||
|
}
|
||||||
|
if (!TaskStatusEnum.FAIL.name().equals(sourceTask.getTaskStatus())
|
||||||
|
&& !TaskStatusEnum.CANCELLED.name().equals(sourceTask.getTaskStatus())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "仅失败或已取消的任务允许重新开始");
|
||||||
|
}
|
||||||
|
DatabaseBackupParam.CreateParam createParam = readCreateParam(sourceTask.getRequestParamJson());
|
||||||
|
createParam.setTemporaryPassword(param.getTemporaryPassword());
|
||||||
|
return createBackupTask(createParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public boolean deleteTask(String taskId, String confirmText) {
|
||||||
|
if (!DatabaseOpsConst.CONFIRM_DELETE.equals(confirmText)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "确认文案不正确");
|
||||||
|
}
|
||||||
|
DatabaseOperationTask task = requireEnabledTask(taskId);
|
||||||
|
if (TaskStatusEnum.RUNNING.name().equals(task.getTaskStatus()) || TaskStatusEnum.WAITING.name().equals(task.getTaskStatus())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "运行中的任务不能删除");
|
||||||
|
}
|
||||||
|
task.setState(DatabaseOpsConst.STATE_DELETED);
|
||||||
|
task.setUpdateTime(LocalDateTime.now());
|
||||||
|
return this.updateById(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean existsRunningTask(String connectionId) {
|
||||||
|
return this.lambdaQuery()
|
||||||
|
.eq(DatabaseOperationTask::getConnectionId, connectionId)
|
||||||
|
.eq(DatabaseOperationTask::getState, DatabaseOpsConst.STATE_ENABLED)
|
||||||
|
.in(DatabaseOperationTask::getTaskStatus, Arrays.asList(TaskStatusEnum.WAITING.name(), TaskStatusEnum.RUNNING.name()))
|
||||||
|
.count() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void executeBackupTask(String taskId, DatabaseBackupParam.CreateParam param) {
|
||||||
|
DatabaseOperationTask task = this.getById(taskId);
|
||||||
|
try {
|
||||||
|
if (task == null || TaskStatusEnum.CANCELLED.name().equals(task.getTaskStatus())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
markRunning(task);
|
||||||
|
DatabaseConnection connection = databaseConnectionService.requireEnabled(task.getConnectionId());
|
||||||
|
connection.setSchemaName(task.getSchemaName());
|
||||||
|
String password = databaseConnectionService.resolvePassword(connection, param.getTemporaryPassword());
|
||||||
|
DatabaseBackupOperator operator = databaseOperatorRegistry.getBackupOperator(connection.getDbType(), task.getBackupStrategy());
|
||||||
|
DatabaseBackupFile backupFile = operator.executeBackup(task, connection, password, param);
|
||||||
|
task = this.getById(taskId);
|
||||||
|
if (task == null || TaskStatusEnum.CANCELLED.name().equals(task.getTaskStatus())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
databaseBackupFileService.save(backupFile);
|
||||||
|
markSuccess(task, "备份任务执行成功");
|
||||||
|
} catch (Exception exception) {
|
||||||
|
log.error("数据库备份任务失败,taskId={}", taskId, exception);
|
||||||
|
task = this.getById(taskId);
|
||||||
|
if (task != null && TaskStatusEnum.CANCELLED.name().equals(task.getTaskStatus())) {
|
||||||
|
markCancelled(task, exception.getMessage());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
markFail(task, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseOperationTask buildBackupTask(DatabaseBackupParam.CreateParam param, DatabaseConnection connection) {
|
||||||
|
DatabaseOperationTask task = new DatabaseOperationTask();
|
||||||
|
task.setId(DatabaseOpsIdUtil.uuid());
|
||||||
|
task.setTaskNo(DatabaseOpsIdUtil.taskNo("DBMSB"));
|
||||||
|
task.setConnectionId(connection.getId());
|
||||||
|
task.setDbType(connection.getDbType());
|
||||||
|
task.setOperationType(OperationTypeEnum.BACKUP.name());
|
||||||
|
task.setBackupStrategy(resolveBackupStrategy(param.getBackupStrategy(), connection.getDbType()));
|
||||||
|
task.setTaskStatus(TaskStatusEnum.WAITING.name());
|
||||||
|
task.setSchemaName(resolveSchemaName(param, connection));
|
||||||
|
task.setTargetNamesJson(writeJson(param.getTargetNames()));
|
||||||
|
task.setRequestParamJson(writeJsonWithoutPassword(param));
|
||||||
|
task.setProgressPercent(BigDecimal.ZERO);
|
||||||
|
task.setState(DatabaseOpsConst.STATE_ENABLED);
|
||||||
|
task.setCreateTime(LocalDateTime.now());
|
||||||
|
task.setUpdateTime(LocalDateTime.now());
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateBackupParam(DatabaseBackupParam.CreateParam param, DatabaseConnection connection) {
|
||||||
|
if (param.getTargetNames() == null || param.getTargetNames().isEmpty()) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份表不能为空");
|
||||||
|
}
|
||||||
|
if (DatabaseOpsConst.DB_TYPE_ORACLE.equals(connection.getDbType())
|
||||||
|
&& StrUtil.isBlank(StrUtil.blankToDefault(param.getSchemaName(), connection.getSchemaName()))) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份 Schema 不能为空");
|
||||||
|
}
|
||||||
|
if (DatabaseOpsConst.DB_TYPE_MYSQL.equals(connection.getDbType()) && StrUtil.isBlank(connection.getDatabaseName())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "MYSQL 数据库名不能为空");
|
||||||
|
}
|
||||||
|
String backupMode = StrUtil.blankToDefault(param.getBackupMode(), BackupModeEnum.FULL_TABLE.name()).toUpperCase(Locale.ROOT);
|
||||||
|
if (BackupModeEnum.TIME_RANGE.name().equals(backupMode)
|
||||||
|
&& (param.getStartTime() == null || param.getEndTime() == null)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "按时间备份必须传入开始时间和结束时间");
|
||||||
|
}
|
||||||
|
if (BackupModeEnum.TIME_RANGE.name().equals(backupMode)
|
||||||
|
&& param.getStartTime() != null && param.getEndTime() != null
|
||||||
|
&& param.getStartTime().isAfter(param.getEndTime())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "开始时间不能晚于结束时间");
|
||||||
|
}
|
||||||
|
if (BackupModeEnum.SIZE_SPLIT.name().equals(backupMode)
|
||||||
|
&& (param.getMaxFileSizeMb() == null || param.getMaxFileSizeMb() <= 0)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "按大小分片必须传入大于 0 的文件大小");
|
||||||
|
}
|
||||||
|
if (BackupModeEnum.TIME_RANGE.name().equals(backupMode) && StrUtil.isBlank(param.getTimeColumn())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "按时间备份必须传入时间字段");
|
||||||
|
}
|
||||||
|
resolveBackupStrategy(param.getBackupStrategy(), connection.getDbType());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveBackupStrategy(String backupStrategy, String dbType) {
|
||||||
|
String value = StrUtil.blankToDefault(backupStrategy,
|
||||||
|
DatabaseOpsConst.DB_TYPE_ORACLE.equals(dbType) ? BackupStrategyEnum.DATA_PUMP.name() : BackupStrategyEnum.JDBC_EXPORT.name())
|
||||||
|
.trim()
|
||||||
|
.toUpperCase(Locale.ROOT);
|
||||||
|
try {
|
||||||
|
BackupStrategyEnum strategyEnum = BackupStrategyEnum.valueOf(value);
|
||||||
|
if (DatabaseOpsConst.DB_TYPE_MYSQL.equals(dbType) && BackupStrategyEnum.DATA_PUMP == strategyEnum) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "MYSQL 不支持 DATA_PUMP");
|
||||||
|
}
|
||||||
|
return strategyEnum.name();
|
||||||
|
} catch (BusinessException exception) {
|
||||||
|
throw exception;
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "不支持的备份策略:" + backupStrategy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveSchemaName(DatabaseBackupParam.CreateParam param, DatabaseConnection connection) {
|
||||||
|
if (DatabaseOpsConst.DB_TYPE_MYSQL.equals(connection.getDbType())) {
|
||||||
|
return StrUtil.blankToDefault(param.getSchemaName(), connection.getDatabaseName());
|
||||||
|
}
|
||||||
|
return StrUtil.blankToDefault(param.getSchemaName(), connection.getSchemaName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void markRunning(DatabaseOperationTask task) {
|
||||||
|
task.setTaskStatus(TaskStatusEnum.RUNNING.name());
|
||||||
|
task.setStartedAt(LocalDateTime.now());
|
||||||
|
task.setUpdateTime(LocalDateTime.now());
|
||||||
|
this.updateById(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void markSuccess(DatabaseOperationTask task, String message) {
|
||||||
|
task.setTaskStatus(TaskStatusEnum.SUCCESS.name());
|
||||||
|
task.setResultMessage(message);
|
||||||
|
task.setProgressPercent(new BigDecimal("100.00"));
|
||||||
|
task.setFinishedAt(LocalDateTime.now());
|
||||||
|
task.setUpdateTime(LocalDateTime.now());
|
||||||
|
this.updateById(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void markFail(DatabaseOperationTask task, String message) {
|
||||||
|
if (task == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
task.setTaskStatus(TaskStatusEnum.FAIL.name());
|
||||||
|
task.setResultMessage(message);
|
||||||
|
task.setFinishedAt(LocalDateTime.now());
|
||||||
|
task.setUpdateTime(LocalDateTime.now());
|
||||||
|
this.updateById(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void markCancelled(DatabaseOperationTask task, String message) {
|
||||||
|
task.setTaskStatus(TaskStatusEnum.CANCELLED.name());
|
||||||
|
task.setResultMessage(StrUtil.blankToDefault(message, "备份任务已停止"));
|
||||||
|
task.setFinishedAt(LocalDateTime.now());
|
||||||
|
task.setUpdateTime(LocalDateTime.now());
|
||||||
|
this.updateById(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseOperationTask requireEnabledTask(String taskId) {
|
||||||
|
DatabaseOperationTask task = this.getById(taskId);
|
||||||
|
if (task == null || !Integer.valueOf(DatabaseOpsConst.STATE_ENABLED).equals(task.getState())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "任务不存在或已删除");
|
||||||
|
}
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String writeJson(Object value) {
|
||||||
|
try {
|
||||||
|
return objectMapper.writeValueAsString(value);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.JSON_CONVERT_EXCEPTION, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String writeJsonWithoutPassword(DatabaseBackupParam.CreateParam param) {
|
||||||
|
DatabaseBackupParam.CreateParam copy = new DatabaseBackupParam.CreateParam();
|
||||||
|
BeanUtil.copyProperties(param, copy);
|
||||||
|
copy.setTemporaryPassword(null);
|
||||||
|
return writeJson(copy);
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseBackupParam.CreateParam readCreateParam(String requestParamJson) {
|
||||||
|
try {
|
||||||
|
return objectMapper.readValue(requestParamJson, DatabaseBackupParam.CreateParam.class);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.JSON_CONVERT_EXCEPTION, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseTaskCreateVO toCreateVO(DatabaseOperationTask task) {
|
||||||
|
DatabaseTaskCreateVO vo = new DatabaseTaskCreateVO();
|
||||||
|
vo.setTaskId(task.getId());
|
||||||
|
vo.setTaskNo(task.getTaskNo());
|
||||||
|
vo.setTaskStatus(task.getTaskStatus());
|
||||||
|
return vo;
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseTaskVO toVO(DatabaseOperationTask task) {
|
||||||
|
DatabaseTaskVO vo = new DatabaseTaskVO();
|
||||||
|
BeanUtil.copyProperties(task, vo);
|
||||||
|
return vo;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,226 @@
|
|||||||
|
package com.njcn.gather.systemops.database.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
|
import com.njcn.gather.systemops.database.constant.DatabaseOpsConst;
|
||||||
|
import com.njcn.gather.systemops.database.mapper.DatabaseRestoreRecordMapper;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.BackupStrategyEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.OperationTypeEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.RestoreModeEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.TaskStatusEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseRestoreParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseBackupFile;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseOperationTask;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseRestoreRecord;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTaskCreateVO;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseBackupFileService;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseConnectionService;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseOperationTaskService;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseRestoreService;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseConnectionOperator;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseOperatorRegistry;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseRestoreOperator;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseOpsIdUtil;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据库恢复服务实现。
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class DatabaseRestoreServiceImpl extends ServiceImpl<DatabaseRestoreRecordMapper, DatabaseRestoreRecord> implements DatabaseRestoreService {
|
||||||
|
|
||||||
|
private final DatabaseConnectionService databaseConnectionService;
|
||||||
|
private final DatabaseOperationTaskService databaseOperationTaskService;
|
||||||
|
private final DatabaseBackupFileService databaseBackupFileService;
|
||||||
|
private final DatabaseOperatorRegistry databaseOperatorRegistry;
|
||||||
|
private final ObjectMapper objectMapper;
|
||||||
|
@Resource(name = "dbmsTaskExecutorService")
|
||||||
|
private ExecutorService dbmsTaskExecutorService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public DatabaseTaskCreateVO createRestoreTask(DatabaseRestoreParam.CreateParam param) {
|
||||||
|
DatabaseConnection connection = databaseConnectionService.requireEnabled(param.getConnectionId());
|
||||||
|
DatabaseBackupFile backupFile = requireBackupFile(param.getBackupFileId());
|
||||||
|
validateRestoreParam(param, connection, backupFile);
|
||||||
|
if (databaseOperationTaskService.existsRunningTask(connection.getId())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "当前连接存在运行中的任务");
|
||||||
|
}
|
||||||
|
DatabaseOperationTask task = buildRestoreTask(param, connection, backupFile);
|
||||||
|
databaseOperationTaskService.save(task);
|
||||||
|
DatabaseRestoreRecord record = buildRestoreRecord(param, connection, backupFile, task);
|
||||||
|
this.save(record);
|
||||||
|
dbmsTaskExecutorService.submit(() -> executeRestoreTask(task.getId(), record.getId(), param));
|
||||||
|
DatabaseTaskCreateVO vo = new DatabaseTaskCreateVO();
|
||||||
|
vo.setTaskId(task.getId());
|
||||||
|
vo.setTaskNo(task.getTaskNo());
|
||||||
|
vo.setTaskStatus(task.getTaskStatus());
|
||||||
|
return vo;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void executeRestoreTask(String taskId, String recordId, DatabaseRestoreParam.CreateParam param) {
|
||||||
|
DatabaseOperationTask task = databaseOperationTaskService.getById(taskId);
|
||||||
|
DatabaseRestoreRecord record = this.getById(recordId);
|
||||||
|
try {
|
||||||
|
markRunning(task);
|
||||||
|
DatabaseConnection connection = databaseConnectionService.requireEnabled(task.getConnectionId());
|
||||||
|
DatabaseBackupFile backupFile = requireBackupFile(record.getBackupFileId());
|
||||||
|
databaseBackupFileService.validateBackupFileReadable(backupFile);
|
||||||
|
String password = databaseConnectionService.resolvePassword(connection, param.getTemporaryPassword());
|
||||||
|
DatabaseRestoreOperator operator = databaseOperatorRegistry.getRestoreOperator(connection.getDbType(), backupFile.getBackupStrategy());
|
||||||
|
operator.executeRestore(task, record, backupFile, connection, password, param);
|
||||||
|
record.setResultMessage("恢复任务执行成功");
|
||||||
|
record.setUpdateTime(LocalDateTime.now());
|
||||||
|
this.updateById(record);
|
||||||
|
markSuccess(task, "恢复任务执行成功");
|
||||||
|
} catch (Exception exception) {
|
||||||
|
log.error("数据库恢复任务失败,taskId={}", taskId, exception);
|
||||||
|
record.setResultMessage(exception.getMessage());
|
||||||
|
record.setUpdateTime(LocalDateTime.now());
|
||||||
|
this.updateById(record);
|
||||||
|
markFail(task, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateRestoreParam(DatabaseRestoreParam.CreateParam param, DatabaseConnection connection, DatabaseBackupFile backupFile) {
|
||||||
|
if (!connection.getDbType().equals(backupFile.getDbType())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份文件数据库类型和目标连接数据库类型不一致");
|
||||||
|
}
|
||||||
|
String restoreMode = resolveRestoreMode(param.getRestoreMode());
|
||||||
|
if ((RestoreModeEnum.TRUNCATE.name().equals(restoreMode) || RestoreModeEnum.REPLACE.name().equals(restoreMode))
|
||||||
|
&& !DatabaseOpsConst.CONFIRM_OVERWRITE.equals(param.getOverwriteConfirmText())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "覆盖类恢复必须输入确认覆盖");
|
||||||
|
}
|
||||||
|
databaseBackupFileService.validateBackupFileReadable(backupFile);
|
||||||
|
String password = databaseConnectionService.resolvePassword(connection, param.getTemporaryPassword());
|
||||||
|
DatabaseConnectionOperator operator = databaseOperatorRegistry.getConnectionOperator(connection.getDbType());
|
||||||
|
if (!Boolean.TRUE.equals(operator.test(connection, password).getSuccess())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "目标连接测试失败,不能创建恢复任务");
|
||||||
|
}
|
||||||
|
if (BackupStrategyEnum.DATA_PUMP.name().equals(backupFile.getBackupStrategy())) {
|
||||||
|
if (StrUtil.isBlank(backupFile.getDirectoryName()) || StrUtil.isBlank(backupFile.getDumpFileName())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "Data Pump 备份记录缺少目录或文件名");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (BackupStrategyEnum.JDBC_EXPORT.name().equals(backupFile.getBackupStrategy())
|
||||||
|
&& StrUtil.isBlank(backupFile.getMetadataFilePath())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "JDBC_EXPORT 备份缺少元数据文件,不能恢复");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseOperationTask buildRestoreTask(DatabaseRestoreParam.CreateParam param, DatabaseConnection connection, DatabaseBackupFile backupFile) {
|
||||||
|
DatabaseOperationTask task = new DatabaseOperationTask();
|
||||||
|
task.setId(DatabaseOpsIdUtil.uuid());
|
||||||
|
task.setTaskNo(DatabaseOpsIdUtil.taskNo("DBMSR"));
|
||||||
|
task.setConnectionId(connection.getId());
|
||||||
|
task.setDbType(connection.getDbType());
|
||||||
|
task.setOperationType(OperationTypeEnum.RESTORE.name());
|
||||||
|
task.setBackupStrategy(backupFile.getBackupStrategy());
|
||||||
|
task.setTaskStatus(TaskStatusEnum.WAITING.name());
|
||||||
|
task.setSchemaName(resolveTargetSchemaName(param, connection));
|
||||||
|
task.setTargetNamesJson(backupFile.getTargetNamesJson());
|
||||||
|
task.setRequestParamJson(writeJsonWithoutPassword(param));
|
||||||
|
task.setProgressPercent(BigDecimal.ZERO);
|
||||||
|
task.setState(DatabaseOpsConst.STATE_ENABLED);
|
||||||
|
task.setCreateTime(LocalDateTime.now());
|
||||||
|
task.setUpdateTime(LocalDateTime.now());
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseRestoreRecord buildRestoreRecord(DatabaseRestoreParam.CreateParam param, DatabaseConnection connection,
|
||||||
|
DatabaseBackupFile backupFile, DatabaseOperationTask task) {
|
||||||
|
String restoreMode = resolveRestoreMode(param.getRestoreMode());
|
||||||
|
DatabaseRestoreRecord record = new DatabaseRestoreRecord();
|
||||||
|
record.setId(DatabaseOpsIdUtil.uuid());
|
||||||
|
record.setTaskId(task.getId());
|
||||||
|
record.setBackupFileId(backupFile.getId());
|
||||||
|
record.setConnectionId(connection.getId());
|
||||||
|
record.setDbType(connection.getDbType());
|
||||||
|
record.setRestoreMode(restoreMode);
|
||||||
|
record.setTargetSchemaName(resolveTargetSchemaName(param, connection));
|
||||||
|
record.setTargetNamesJson(backupFile.getTargetNamesJson());
|
||||||
|
record.setTableExistsAction(restoreMode);
|
||||||
|
record.setOverwriteConfirmed(DatabaseOpsConst.CONFIRM_OVERWRITE.equals(param.getOverwriteConfirmText()) ? 1 : 0);
|
||||||
|
record.setState(DatabaseOpsConst.STATE_ENABLED);
|
||||||
|
record.setCreateTime(LocalDateTime.now());
|
||||||
|
record.setUpdateTime(LocalDateTime.now());
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveTargetSchemaName(DatabaseRestoreParam.CreateParam param, DatabaseConnection connection) {
|
||||||
|
if (DatabaseOpsConst.DB_TYPE_MYSQL.equals(connection.getDbType())) {
|
||||||
|
return StrUtil.blankToDefault(param.getTargetSchemaName(), connection.getDatabaseName());
|
||||||
|
}
|
||||||
|
return StrUtil.blankToDefault(param.getTargetSchemaName(), connection.getSchemaName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseBackupFile requireBackupFile(String backupFileId) {
|
||||||
|
DatabaseBackupFile backupFile = databaseBackupFileService.getById(backupFileId);
|
||||||
|
if (backupFile == null || !Integer.valueOf(DatabaseOpsConst.STATE_ENABLED).equals(backupFile.getState())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份文件不存在或已删除");
|
||||||
|
}
|
||||||
|
return backupFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String resolveRestoreMode(String restoreMode) {
|
||||||
|
String value = StrUtil.blankToDefault(restoreMode, RestoreModeEnum.SKIP.name()).trim().toUpperCase(Locale.ROOT);
|
||||||
|
try {
|
||||||
|
return RestoreModeEnum.valueOf(value).name();
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "不支持的恢复模式:" + restoreMode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void markRunning(DatabaseOperationTask task) {
|
||||||
|
task.setTaskStatus(TaskStatusEnum.RUNNING.name());
|
||||||
|
task.setStartedAt(LocalDateTime.now());
|
||||||
|
task.setUpdateTime(LocalDateTime.now());
|
||||||
|
databaseOperationTaskService.updateById(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void markSuccess(DatabaseOperationTask task, String message) {
|
||||||
|
task.setTaskStatus(TaskStatusEnum.SUCCESS.name());
|
||||||
|
task.setResultMessage(message);
|
||||||
|
task.setProgressPercent(new BigDecimal("100.00"));
|
||||||
|
task.setFinishedAt(LocalDateTime.now());
|
||||||
|
task.setUpdateTime(LocalDateTime.now());
|
||||||
|
databaseOperationTaskService.updateById(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void markFail(DatabaseOperationTask task, String message) {
|
||||||
|
task.setTaskStatus(TaskStatusEnum.FAIL.name());
|
||||||
|
task.setResultMessage(message);
|
||||||
|
task.setFinishedAt(LocalDateTime.now());
|
||||||
|
task.setUpdateTime(LocalDateTime.now());
|
||||||
|
databaseOperationTaskService.updateById(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String writeJsonWithoutPassword(DatabaseRestoreParam.CreateParam param) {
|
||||||
|
try {
|
||||||
|
DatabaseRestoreParam.CreateParam copy = new DatabaseRestoreParam.CreateParam();
|
||||||
|
copy.setConnectionId(param.getConnectionId());
|
||||||
|
copy.setBackupFileId(param.getBackupFileId());
|
||||||
|
copy.setRestoreMode(param.getRestoreMode());
|
||||||
|
copy.setTargetSchemaName(param.getTargetSchemaName());
|
||||||
|
copy.setOverwriteConfirmText(param.getOverwriteConfirmText());
|
||||||
|
return objectMapper.writeValueAsString(copy);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.JSON_CONVERT_EXCEPTION, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
package com.njcn.gather.systemops.database.support.mysql;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.njcn.gather.systemops.database.constant.DatabaseOpsConst;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTableVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTestResultVO;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseConnectionOperator;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MySQL 连接能力实现。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class MysqlConnectionOperator implements DatabaseConnectionOperator {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean support(String dbType) {
|
||||||
|
return DatabaseOpsConst.DB_TYPE_MYSQL.equalsIgnoreCase(dbType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DatabaseTestResultVO test(DatabaseConnection connection, String password) {
|
||||||
|
DatabaseTestResultVO result = new DatabaseTestResultVO();
|
||||||
|
try (Connection ignored = openConnection(connection, password)) {
|
||||||
|
result.setSuccess(true);
|
||||||
|
result.setMessage("连接成功");
|
||||||
|
} catch (Exception exception) {
|
||||||
|
result.setSuccess(false);
|
||||||
|
result.setMessage(exception.getMessage());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<DatabaseTableVO> listTables(DatabaseConnection connection, String password, String schemaOrDatabaseName) throws Exception {
|
||||||
|
String databaseName = StrUtil.blankToDefault(schemaOrDatabaseName, connection.getDatabaseName());
|
||||||
|
String sql = "SELECT t.table_schema, t.table_name, t.auto_increment, t.update_time, "
|
||||||
|
+ "t.data_length, t.engine, t.table_rows, t.table_comment, "
|
||||||
|
+ "MAX(CASE WHEN c.extra LIKE '%auto_increment%' THEN 1 ELSE 0 END) AS has_auto_increment "
|
||||||
|
+ "FROM information_schema.tables t "
|
||||||
|
+ "LEFT JOIN information_schema.columns c "
|
||||||
|
+ "ON t.table_schema = c.table_schema AND t.table_name = c.table_name "
|
||||||
|
+ "WHERE t.table_schema = ? AND t.table_type = 'BASE TABLE' "
|
||||||
|
+ "GROUP BY t.table_schema, t.table_name, t.auto_increment, t.update_time, "
|
||||||
|
+ "t.data_length, t.engine, t.table_rows, t.table_comment "
|
||||||
|
+ "ORDER BY t.table_name";
|
||||||
|
try (Connection jdbcConnection = openConnection(connection, password);
|
||||||
|
PreparedStatement statement = jdbcConnection.prepareStatement(sql)) {
|
||||||
|
statement.setString(1, databaseName);
|
||||||
|
try (ResultSet resultSet = statement.executeQuery()) {
|
||||||
|
List<DatabaseTableVO> result = new ArrayList<>();
|
||||||
|
while (resultSet.next()) {
|
||||||
|
DatabaseTableVO table = new DatabaseTableVO();
|
||||||
|
table.setOwner(resultSet.getString("table_schema").toUpperCase(Locale.ROOT));
|
||||||
|
table.setTableName(resultSet.getString("table_name"));
|
||||||
|
if (resultSet.getInt("has_auto_increment") == 1) {
|
||||||
|
fillAutoIncrement(table, defaultZero(getLongValue(resultSet, "auto_increment")));
|
||||||
|
}
|
||||||
|
Timestamp updateTime = resultSet.getTimestamp("update_time");
|
||||||
|
table.setUpdateTime(updateTime == null ? null : updateTime.toLocalDateTime());
|
||||||
|
table.setDataLength(getLongValue(resultSet, "data_length"));
|
||||||
|
table.setEngine(resultSet.getString("engine"));
|
||||||
|
table.setTableRows(getLongValue(resultSet, "table_rows"));
|
||||||
|
table.setComments(resultSet.getString("table_comment"));
|
||||||
|
result.add(table);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Long getLongValue(ResultSet resultSet, String columnName) throws Exception {
|
||||||
|
long value = resultSet.getLong(columnName);
|
||||||
|
return resultSet.wasNull() ? null : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Long defaultZero(Long value) {
|
||||||
|
return value == null ? 0L : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillAutoIncrement(DatabaseTableVO table, Long autoIncrement) {
|
||||||
|
table.setAutoIncrementValue(autoIncrement);
|
||||||
|
table.setAutoIncrement(autoIncrement);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Connection openConnection(DatabaseConnection connection, String password) throws Exception {
|
||||||
|
if (StrUtil.isBlank(password)) {
|
||||||
|
throw new IllegalArgumentException("数据库密码不能为空");
|
||||||
|
}
|
||||||
|
return DriverManager.getConnection(MysqlJdbcUrlUtil.build(connection), connection.getUsername(), password);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
package com.njcn.gather.systemops.database.support.mysql;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
|
import com.njcn.gather.systemops.database.component.JdbcExportComponent;
|
||||||
|
import com.njcn.gather.systemops.database.config.DbmsProperties;
|
||||||
|
import com.njcn.gather.systemops.database.mapper.DatabaseOperationTaskMapper;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.BackupModeEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.BackupStrategyEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.FileFormatEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.TaskStatusEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseBackupParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseBackupFile;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseOperationTask;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseBackupOperator;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseChecksumUtil;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseFileNameUtil;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseOpsIdUtil;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabasePathUtil;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MySQL JDBC_EXPORT 大数据量备份实现。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class MysqlJdbcExportBackupOperator implements DatabaseBackupOperator {
|
||||||
|
|
||||||
|
private final JdbcExportComponent jdbcExportComponent;
|
||||||
|
private final DbmsProperties dbmsProperties;
|
||||||
|
private final DatabaseOperationTaskMapper databaseOperationTaskMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean support(String dbType, String backupStrategy) {
|
||||||
|
return "MYSQL".equalsIgnoreCase(dbType) && BackupStrategyEnum.JDBC_EXPORT.name().equals(backupStrategy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DatabaseBackupFile executeBackup(DatabaseOperationTask task, DatabaseConnection connection, String password,
|
||||||
|
DatabaseBackupParam.CreateParam param) throws Exception {
|
||||||
|
Path backupDirectory = buildManagedPath(dbmsProperties.getBackup().getStoragePath(), task.getTaskNo());
|
||||||
|
String metadataFileName = DatabaseFileNameUtil.appendTodayWithTask("mysql_jdbc_export_metadata.json", task.getTaskNo());
|
||||||
|
Path metadataFilePath = backupDirectory.resolve(metadataFileName).normalize();
|
||||||
|
int fetchSize = positiveOrDefault(dbmsProperties.getBackup().getMysqlFetchSize(), 1000);
|
||||||
|
long maxPartBytes = resolveMaxPartBytes(param);
|
||||||
|
try (Connection jdbcConnection = DriverManager.getConnection(MysqlJdbcUrlUtil.build(connection), connection.getUsername(), password)) {
|
||||||
|
jdbcExportComponent.exportMysqlCsvV2(jdbcConnection, connection.getDatabaseName(), task.getTaskNo(), param,
|
||||||
|
backupDirectory, metadataFilePath, fetchSize, maxPartBytes, () -> isTaskCancelled(task.getId()));
|
||||||
|
} catch (Exception exception) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, exception.getMessage() + ",导出目录:" + backupDirectory);
|
||||||
|
}
|
||||||
|
return buildBackupFile(task, connection, param, backupDirectory, metadataFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isTaskCancelled(String taskId) {
|
||||||
|
DatabaseOperationTask task = databaseOperationTaskMapper.selectById(taskId);
|
||||||
|
return task != null && TaskStatusEnum.CANCELLED.name().equals(task.getTaskStatus());
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseBackupFile buildBackupFile(DatabaseOperationTask task, DatabaseConnection connection,
|
||||||
|
DatabaseBackupParam.CreateParam param, Path backupDirectory,
|
||||||
|
Path metadataFilePath) throws Exception {
|
||||||
|
if (!Files.exists(metadataFilePath)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份元数据文件未生成");
|
||||||
|
}
|
||||||
|
DatabaseBackupFile file = new DatabaseBackupFile();
|
||||||
|
file.setId(DatabaseOpsIdUtil.uuid());
|
||||||
|
file.setTaskId(task.getId());
|
||||||
|
file.setConnectionId(connection.getId());
|
||||||
|
file.setDbType(connection.getDbType());
|
||||||
|
file.setBackupStrategy(task.getBackupStrategy());
|
||||||
|
file.setFileFormat(FileFormatEnum.CSV.name());
|
||||||
|
file.setSchemaName(task.getSchemaName());
|
||||||
|
file.setTargetNamesJson(task.getTargetNamesJson());
|
||||||
|
file.setBackupMode(StrUtil.blankToDefault(param.getBackupMode(), BackupModeEnum.FULL_TABLE.name()).toUpperCase(Locale.ROOT));
|
||||||
|
file.setBackupStartTime(param.getStartTime());
|
||||||
|
file.setBackupEndTime(param.getEndTime());
|
||||||
|
file.setTimeColumn(param.getTimeColumn());
|
||||||
|
file.setDirectoryName(null);
|
||||||
|
file.setDumpFileName(null);
|
||||||
|
file.setLogFileName(null);
|
||||||
|
file.setFileName(backupDirectory.getFileName().toString());
|
||||||
|
file.setFilePath(backupDirectory.toString());
|
||||||
|
file.setLogFilePath(null);
|
||||||
|
file.setMetadataFilePath(metadataFilePath.toString());
|
||||||
|
file.setFileSize(readDirectoryFileSize(backupDirectory));
|
||||||
|
file.setChecksum(DatabaseChecksumUtil.sha256(metadataFilePath));
|
||||||
|
file.setState(1);
|
||||||
|
file.setCreateTime(LocalDateTime.now());
|
||||||
|
file.setUpdateTime(LocalDateTime.now());
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
private long resolveMaxPartBytes(DatabaseBackupParam.CreateParam param) {
|
||||||
|
Integer maxFileSizeMb = param.getMaxFileSizeMb();
|
||||||
|
if (maxFileSizeMb == null || maxFileSizeMb <= 0) {
|
||||||
|
maxFileSizeMb = positiveOrDefault(dbmsProperties.getBackup().getDefaultMaxFileSizeMb(), 512);
|
||||||
|
}
|
||||||
|
return maxFileSizeMb.longValue() * 1024L * 1024L;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int positiveOrDefault(Integer value, int defaultValue) {
|
||||||
|
return value == null || value <= 0 ? defaultValue : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Long readDirectoryFileSize(Path directory) {
|
||||||
|
try {
|
||||||
|
if (directory != null && Files.exists(directory) && Files.isDirectory(directory)) {
|
||||||
|
final long[] total = new long[]{0L};
|
||||||
|
try (Stream<Path> paths = Files.walk(directory)) {
|
||||||
|
paths.filter(path -> Files.exists(path) && !Files.isDirectory(path))
|
||||||
|
.forEach(path -> {
|
||||||
|
try {
|
||||||
|
total[0] += Files.size(path);
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
// 忽略单个文件大小读取失败,避免影响备份记录生成。
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return total[0];
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Path buildManagedPath(String rootPath, String directoryName) {
|
||||||
|
Path root = DatabasePathUtil.normalize(rootPath);
|
||||||
|
if (root == null) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份目录未配置");
|
||||||
|
}
|
||||||
|
return root.resolve(directoryName).normalize();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package com.njcn.gather.systemops.database.support.mysql;
|
||||||
|
|
||||||
|
import com.njcn.gather.systemops.database.component.JdbcExportComponent;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.BackupStrategyEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseRestoreParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseBackupFile;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseOperationTask;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseRestoreRecord;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseBackupFileService;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseRestoreOperator;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MySQL JDBC_EXPORT 恢复实现。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class MysqlJdbcExportRestoreOperator implements DatabaseRestoreOperator {
|
||||||
|
|
||||||
|
private final JdbcExportComponent jdbcExportComponent;
|
||||||
|
private final DatabaseBackupFileService databaseBackupFileService;
|
||||||
|
|
||||||
|
public MysqlJdbcExportRestoreOperator(JdbcExportComponent jdbcExportComponent,
|
||||||
|
DatabaseBackupFileService databaseBackupFileService) {
|
||||||
|
this.jdbcExportComponent = jdbcExportComponent;
|
||||||
|
this.databaseBackupFileService = databaseBackupFileService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean support(String dbType, String backupStrategy) {
|
||||||
|
return "MYSQL".equalsIgnoreCase(dbType) && BackupStrategyEnum.JDBC_EXPORT.name().equals(backupStrategy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeRestore(DatabaseOperationTask task, DatabaseRestoreRecord record, DatabaseBackupFile backupFile,
|
||||||
|
DatabaseConnection connection, String password, DatabaseRestoreParam.CreateParam param) throws Exception {
|
||||||
|
Path dataFilePath = databaseBackupFileService.resolveManagedPath(backupFile, backupFile.getFilePath());
|
||||||
|
Path metadataFilePath = databaseBackupFileService.resolveManagedPath(backupFile, backupFile.getMetadataFilePath());
|
||||||
|
try (Connection jdbcConnection = DriverManager.getConnection(MysqlJdbcUrlUtil.build(connection), connection.getUsername(), password)) {
|
||||||
|
jdbcExportComponent.importCsv(jdbcConnection, dataFilePath, metadataFilePath,
|
||||||
|
connection.getDbType(), record.getRestoreMode(), null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.njcn.gather.systemops.database.support.mysql;
|
||||||
|
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MySQL JDBC URL 构造工具。
|
||||||
|
*/
|
||||||
|
public final class MysqlJdbcUrlUtil {
|
||||||
|
|
||||||
|
private MysqlJdbcUrlUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String build(DatabaseConnection connection) {
|
||||||
|
return "jdbc:mysql://" + connection.getHost() + ":" + connection.getPort() + "/" + connection.getDatabaseName()
|
||||||
|
+ "?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai"
|
||||||
|
+ "&useCursorFetch=true&connectTimeout=5000&socketTimeout=30000";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.njcn.gather.systemops.database.support.oracle;
|
||||||
|
|
||||||
|
import com.njcn.gather.systemops.database.component.OracleJdbcComponent;
|
||||||
|
import com.njcn.gather.systemops.database.constant.DatabaseOpsConst;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTableVO;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.vo.DatabaseTestResultVO;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseConnectionOperator;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oracle 连接能力实现。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class OracleConnectionOperator implements DatabaseConnectionOperator {
|
||||||
|
|
||||||
|
private final OracleJdbcComponent oracleJdbcComponent;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean support(String dbType) {
|
||||||
|
return DatabaseOpsConst.DB_TYPE_ORACLE.equalsIgnoreCase(dbType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DatabaseTestResultVO test(DatabaseConnection connection, String password) {
|
||||||
|
return oracleJdbcComponent.test(connection, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<DatabaseTableVO> listTables(DatabaseConnection connection, String password, String schemaOrDatabaseName) throws Exception {
|
||||||
|
return oracleJdbcComponent.listTables(connection, password, schemaOrDatabaseName);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
package com.njcn.gather.systemops.database.support.oracle;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
|
import com.njcn.gather.systemops.database.component.DataPumpCommandExecutor;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.BackupModeEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.BackupStrategyEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.FileFormatEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseBackupParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseBackupFile;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseOperationTask;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseBackupFileService;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseBackupOperator;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseChecksumUtil;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseFileNameUtil;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseOpsIdUtil;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabasePathUtil;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oracle DATA_PUMP 备份实现。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class OracleDataPumpBackupOperator implements DatabaseBackupOperator {
|
||||||
|
|
||||||
|
private final DataPumpCommandExecutor dataPumpCommandExecutor;
|
||||||
|
private final DatabaseBackupFileService databaseBackupFileService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean support(String dbType, String backupStrategy) {
|
||||||
|
return "ORACLE".equalsIgnoreCase(dbType) && BackupStrategyEnum.DATA_PUMP.name().equals(backupStrategy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DatabaseBackupFile executeBackup(DatabaseOperationTask task, DatabaseConnection connection, String password,
|
||||||
|
DatabaseBackupParam.CreateParam param) {
|
||||||
|
String directoryName = StrUtil.blankToDefault(param.getDirectoryName(), connection.getDirectoryName());
|
||||||
|
if (StrUtil.isBlank(directoryName)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "DATA_PUMP 备份需要 Oracle Directory 名称");
|
||||||
|
}
|
||||||
|
String baseName = buildBaseFileName(connection, task);
|
||||||
|
String dumpFileName = DatabaseFileNameUtil.appendTodayWithTask(baseName + ".dmp", task.getTaskNo());
|
||||||
|
String logFileName = DatabaseFileNameUtil.appendTodayWithTask(baseName + ".log", task.getTaskNo());
|
||||||
|
DataPumpCommandExecutor.CommandResult commandResult = dataPumpCommandExecutor.expdp(connection, password,
|
||||||
|
directoryName, dumpFileName, logFileName, param.getTargetNames());
|
||||||
|
if (!Boolean.TRUE.equals(commandResult.getSuccess())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "Data Pump 执行失败:" + commandResult.getOutput());
|
||||||
|
}
|
||||||
|
if (StrUtil.isBlank(connection.getDirectoryPath())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "Data Pump 备份需要配置可管理的 directoryPath");
|
||||||
|
}
|
||||||
|
Path dumpPath = buildManagedPath(connection.getDirectoryPath(), dumpFileName);
|
||||||
|
Path logPath = buildManagedPath(connection.getDirectoryPath(), logFileName);
|
||||||
|
return buildBackupFile(task, connection, param, FileFormatEnum.DMP.name(), dumpFileName, dumpPath, logFileName, logPath, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseBackupFile buildBackupFile(DatabaseOperationTask task, DatabaseConnection connection,
|
||||||
|
DatabaseBackupParam.CreateParam param, String fileFormat, String fileName,
|
||||||
|
Path filePath, String logFileName, Path logFilePath, Path metadataFilePath) {
|
||||||
|
if (filePath == null || !Files.exists(filePath)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份文件未生成");
|
||||||
|
}
|
||||||
|
DatabaseBackupFile file = new DatabaseBackupFile();
|
||||||
|
file.setId(DatabaseOpsIdUtil.uuid());
|
||||||
|
file.setTaskId(task.getId());
|
||||||
|
file.setConnectionId(connection.getId());
|
||||||
|
file.setDbType(connection.getDbType());
|
||||||
|
file.setBackupStrategy(task.getBackupStrategy());
|
||||||
|
file.setFileFormat(fileFormat);
|
||||||
|
file.setSchemaName(task.getSchemaName());
|
||||||
|
file.setTargetNamesJson(task.getTargetNamesJson());
|
||||||
|
file.setBackupMode(StrUtil.blankToDefault(param.getBackupMode(), BackupModeEnum.FULL_TABLE.name()).toUpperCase(Locale.ROOT));
|
||||||
|
file.setBackupStartTime(param.getStartTime());
|
||||||
|
file.setBackupEndTime(param.getEndTime());
|
||||||
|
file.setTimeColumn(param.getTimeColumn());
|
||||||
|
file.setDirectoryName(StrUtil.blankToDefault(param.getDirectoryName(), connection.getDirectoryName()));
|
||||||
|
file.setDumpFileName(FileFormatEnum.DMP.name().equals(fileFormat) ? fileName : null);
|
||||||
|
file.setLogFileName(logFileName);
|
||||||
|
file.setFileName(fileName);
|
||||||
|
file.setFilePath(filePath.toString());
|
||||||
|
file.setLogFilePath(logFilePath == null ? null : logFilePath.toString());
|
||||||
|
file.setMetadataFilePath(metadataFilePath == null ? null : metadataFilePath.toString());
|
||||||
|
file.setFileSize(readFileSize(filePath));
|
||||||
|
file.setChecksum(DatabaseChecksumUtil.sha256(filePath));
|
||||||
|
file.setState(1);
|
||||||
|
file.setCreateTime(LocalDateTime.now());
|
||||||
|
file.setUpdateTime(LocalDateTime.now());
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Long readFileSize(Path filePath) {
|
||||||
|
try {
|
||||||
|
if (filePath != null && Files.exists(filePath) && !Files.isDirectory(filePath)) {
|
||||||
|
return Files.size(filePath);
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Path buildManagedPath(String rootPath, String fileName) {
|
||||||
|
Path root = DatabasePathUtil.normalize(rootPath);
|
||||||
|
if (root == null) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份目录未配置");
|
||||||
|
}
|
||||||
|
return root.resolve(fileName).normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildBaseFileName(DatabaseConnection connection, DatabaseOperationTask task) {
|
||||||
|
return connection.getSchemaName() + "_" + task.getBackupStrategy().toLowerCase(Locale.ROOT);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.njcn.gather.systemops.database.support.oracle;
|
||||||
|
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
|
import com.njcn.gather.systemops.database.component.DataPumpCommandExecutor;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.BackupStrategyEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseRestoreParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseBackupFile;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseOperationTask;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseRestoreRecord;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseRestoreOperator;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseFileNameUtil;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oracle DATA_PUMP 恢复实现。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class OracleDataPumpRestoreOperator implements DatabaseRestoreOperator {
|
||||||
|
|
||||||
|
private final DataPumpCommandExecutor dataPumpCommandExecutor;
|
||||||
|
|
||||||
|
public OracleDataPumpRestoreOperator(DataPumpCommandExecutor dataPumpCommandExecutor) {
|
||||||
|
this.dataPumpCommandExecutor = dataPumpCommandExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean support(String dbType, String backupStrategy) {
|
||||||
|
return "ORACLE".equalsIgnoreCase(dbType) && BackupStrategyEnum.DATA_PUMP.name().equals(backupStrategy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeRestore(DatabaseOperationTask task, DatabaseRestoreRecord record, DatabaseBackupFile backupFile,
|
||||||
|
DatabaseConnection connection, String password, DatabaseRestoreParam.CreateParam param) {
|
||||||
|
DataPumpCommandExecutor.CommandResult result = dataPumpCommandExecutor.impdp(connection, password,
|
||||||
|
backupFile.getDirectoryName(), backupFile.getDumpFileName(), buildRestoreLogName(task),
|
||||||
|
record.getTableExistsAction());
|
||||||
|
if (!Boolean.TRUE.equals(result.getSuccess())) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "Data Pump 恢复失败:" + result.getOutput());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildRestoreLogName(DatabaseOperationTask task) {
|
||||||
|
return DatabaseFileNameUtil.appendTodayWithTask(task.getTaskNo() + "_restore.log", task.getTaskNo());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
package com.njcn.gather.systemops.database.support.oracle;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||||
|
import com.njcn.common.pojo.exception.BusinessException;
|
||||||
|
import com.njcn.gather.systemops.database.component.JdbcExportComponent;
|
||||||
|
import com.njcn.gather.systemops.database.component.OracleJdbcComponent;
|
||||||
|
import com.njcn.gather.systemops.database.config.DbmsProperties;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.BackupModeEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.BackupStrategyEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.FileFormatEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseBackupParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseBackupFile;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseOperationTask;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseBackupOperator;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseChecksumUtil;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseFileNameUtil;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabaseOpsIdUtil;
|
||||||
|
import com.njcn.gather.systemops.database.util.DatabasePathUtil;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oracle JDBC_EXPORT 备份实现。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class OracleJdbcExportBackupOperator implements DatabaseBackupOperator {
|
||||||
|
|
||||||
|
private final JdbcExportComponent jdbcExportComponent;
|
||||||
|
private final OracleJdbcComponent oracleJdbcComponent;
|
||||||
|
private final DbmsProperties dbmsProperties;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean support(String dbType, String backupStrategy) {
|
||||||
|
return "ORACLE".equalsIgnoreCase(dbType) && BackupStrategyEnum.JDBC_EXPORT.name().equals(backupStrategy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DatabaseBackupFile executeBackup(DatabaseOperationTask task, DatabaseConnection connection, String password,
|
||||||
|
DatabaseBackupParam.CreateParam param) throws Exception {
|
||||||
|
String baseName = buildBaseFileName(connection, task);
|
||||||
|
String fileName = DatabaseFileNameUtil.appendTodayWithTask(baseName + ".csv", task.getTaskNo());
|
||||||
|
String metadataFileName = DatabaseFileNameUtil.appendTodayWithTask(baseName + "_metadata.json", task.getTaskNo());
|
||||||
|
Path dataFilePath = buildManagedPath(dbmsProperties.getBackup().getStoragePath(), fileName);
|
||||||
|
Path metadataFilePath = buildManagedPath(dbmsProperties.getBackup().getStoragePath(), metadataFileName);
|
||||||
|
try (Connection jdbcConnection = oracleJdbcComponent.openConnection(connection, password)) {
|
||||||
|
jdbcExportComponent.exportCsv(jdbcConnection, connection.getSchemaName(), param, dataFilePath, metadataFilePath);
|
||||||
|
}
|
||||||
|
return buildBackupFile(task, connection, param, fileName, dataFilePath, metadataFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private DatabaseBackupFile buildBackupFile(DatabaseOperationTask task, DatabaseConnection connection,
|
||||||
|
DatabaseBackupParam.CreateParam param, String fileName, Path filePath,
|
||||||
|
Path metadataFilePath) {
|
||||||
|
if (filePath == null || !Files.exists(filePath)) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份文件未生成");
|
||||||
|
}
|
||||||
|
DatabaseBackupFile file = new DatabaseBackupFile();
|
||||||
|
file.setId(DatabaseOpsIdUtil.uuid());
|
||||||
|
file.setTaskId(task.getId());
|
||||||
|
file.setConnectionId(connection.getId());
|
||||||
|
file.setDbType(connection.getDbType());
|
||||||
|
file.setBackupStrategy(task.getBackupStrategy());
|
||||||
|
file.setFileFormat(FileFormatEnum.CSV.name());
|
||||||
|
file.setSchemaName(task.getSchemaName());
|
||||||
|
file.setTargetNamesJson(task.getTargetNamesJson());
|
||||||
|
file.setBackupMode(StrUtil.blankToDefault(param.getBackupMode(), BackupModeEnum.FULL_TABLE.name()).toUpperCase(Locale.ROOT));
|
||||||
|
file.setBackupStartTime(param.getStartTime());
|
||||||
|
file.setBackupEndTime(param.getEndTime());
|
||||||
|
file.setTimeColumn(param.getTimeColumn());
|
||||||
|
file.setDirectoryName(null);
|
||||||
|
file.setDumpFileName(null);
|
||||||
|
file.setLogFileName(null);
|
||||||
|
file.setFileName(fileName);
|
||||||
|
file.setFilePath(filePath.toString());
|
||||||
|
file.setLogFilePath(null);
|
||||||
|
file.setMetadataFilePath(metadataFilePath.toString());
|
||||||
|
file.setFileSize(readFileSize(filePath));
|
||||||
|
file.setChecksum(DatabaseChecksumUtil.sha256(filePath));
|
||||||
|
file.setState(1);
|
||||||
|
file.setCreateTime(LocalDateTime.now());
|
||||||
|
file.setUpdateTime(LocalDateTime.now());
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Long readFileSize(Path filePath) {
|
||||||
|
try {
|
||||||
|
if (filePath != null && Files.exists(filePath) && !Files.isDirectory(filePath)) {
|
||||||
|
return Files.size(filePath);
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Path buildManagedPath(String rootPath, String fileName) {
|
||||||
|
Path root = DatabasePathUtil.normalize(rootPath);
|
||||||
|
if (root == null) {
|
||||||
|
throw new BusinessException(CommonResponseEnum.FAIL, "备份目录未配置");
|
||||||
|
}
|
||||||
|
return root.resolve(fileName).normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildBaseFileName(DatabaseConnection connection, DatabaseOperationTask task) {
|
||||||
|
return connection.getSchemaName() + "_" + task.getBackupStrategy().toLowerCase(Locale.ROOT);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package com.njcn.gather.systemops.database.support.oracle;
|
||||||
|
|
||||||
|
import com.njcn.gather.systemops.database.component.JdbcExportComponent;
|
||||||
|
import com.njcn.gather.systemops.database.component.OracleJdbcComponent;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.enums.BackupStrategyEnum;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.param.DatabaseRestoreParam;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseBackupFile;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseConnection;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseOperationTask;
|
||||||
|
import com.njcn.gather.systemops.database.pojo.po.DatabaseRestoreRecord;
|
||||||
|
import com.njcn.gather.systemops.database.service.DatabaseBackupFileService;
|
||||||
|
import com.njcn.gather.systemops.database.support.spi.DatabaseRestoreOperator;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.sql.Connection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oracle JDBC_EXPORT 恢复实现。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class OracleJdbcExportRestoreOperator implements DatabaseRestoreOperator {
|
||||||
|
|
||||||
|
private final JdbcExportComponent jdbcExportComponent;
|
||||||
|
private final OracleJdbcComponent oracleJdbcComponent;
|
||||||
|
private final DatabaseBackupFileService databaseBackupFileService;
|
||||||
|
|
||||||
|
public OracleJdbcExportRestoreOperator(JdbcExportComponent jdbcExportComponent,
|
||||||
|
OracleJdbcComponent oracleJdbcComponent,
|
||||||
|
DatabaseBackupFileService databaseBackupFileService) {
|
||||||
|
this.jdbcExportComponent = jdbcExportComponent;
|
||||||
|
this.oracleJdbcComponent = oracleJdbcComponent;
|
||||||
|
this.databaseBackupFileService = databaseBackupFileService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean support(String dbType, String backupStrategy) {
|
||||||
|
return "ORACLE".equalsIgnoreCase(dbType) && BackupStrategyEnum.JDBC_EXPORT.name().equals(backupStrategy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeRestore(DatabaseOperationTask task, DatabaseRestoreRecord record, DatabaseBackupFile backupFile,
|
||||||
|
DatabaseConnection connection, String password, DatabaseRestoreParam.CreateParam param) throws Exception {
|
||||||
|
Path dataFilePath = databaseBackupFileService.resolveManagedPath(backupFile, backupFile.getFilePath());
|
||||||
|
Path metadataFilePath = databaseBackupFileService.resolveManagedPath(backupFile, backupFile.getMetadataFilePath());
|
||||||
|
try (Connection jdbcConnection = oracleJdbcComponent.openConnection(connection, password)) {
|
||||||
|
jdbcExportComponent.importCsv(jdbcConnection, dataFilePath, metadataFilePath,
|
||||||
|
connection.getDbType(), record.getRestoreMode(), record.getTargetSchemaName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user