定时任务:定时保存指标数据
This commit is contained in:
parent
db18f06c49
commit
8f03f3ea7a
@ -3,14 +3,20 @@ package iet.ustb.sf.controller;
|
|||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import iet.ustb.sf.common.R;
|
import iet.ustb.sf.common.R;
|
||||||
import iet.ustb.sf.service.TableColumnService;
|
import iet.ustb.sf.service.TableColumnService;
|
||||||
|
import iet.ustb.sf.service.TargetDataService;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
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.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* web中未使用的有用接口
|
* web中未使用的有用接口
|
||||||
* SyncController
|
* SyncController
|
||||||
@ -26,6 +32,9 @@ public class SyncController {
|
|||||||
@Resource
|
@Resource
|
||||||
private TableColumnService tableColumnService;
|
private TableColumnService tableColumnService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private TargetDataService targetDataService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 同步指定表结构
|
* 同步指定表结构
|
||||||
* @param params 表名
|
* @param params 表名
|
||||||
@ -51,4 +60,23 @@ public class SyncController {
|
|||||||
String result = tableColumnService.syncTableDataByDb(dbName);
|
String result = tableColumnService.syncTableDataByDb(dbName);
|
||||||
return R.ok(result);
|
return R.ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "定时保存指标数据")
|
||||||
|
@PostMapping("/saveTargetDate")
|
||||||
|
public R<?> saveTargetDate(@RequestBody JSONObject params) {
|
||||||
|
try {
|
||||||
|
String dateStr = params.getString("date");
|
||||||
|
if (StringUtils.isEmpty(dateStr)) {
|
||||||
|
return R.fail("日期不能为空!");
|
||||||
|
}
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
Date date = sdf.parse(dateStr);
|
||||||
|
targetDataService.saveTargetDate(date);
|
||||||
|
return R.ok();
|
||||||
|
} catch (ParseException e) {
|
||||||
|
return R.fail("日期格式不对,需要yyyy-MM-dd这种格式的");
|
||||||
|
} catch (Exception e) {
|
||||||
|
return R.fail(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import iet.ustb.sf.domain.Target;
|
|||||||
import iet.ustb.sf.domain.TargetData;
|
import iet.ustb.sf.domain.TargetData;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -22,4 +23,6 @@ public interface TargetDataService extends IService<TargetData> {
|
|||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
TargetData updateHistory(Target target, String[] strArr, Map<String, TargetData> map);
|
TargetData updateHistory(Target target, String[] strArr, Map<String, TargetData> map);
|
||||||
|
|
||||||
|
void saveTargetDate(Date date);
|
||||||
}
|
}
|
||||||
|
@ -60,4 +60,12 @@ public interface UtilService {
|
|||||||
* @return map结果
|
* @return map结果
|
||||||
*/
|
*/
|
||||||
Map<String, String> getTogethResult(Date date, String expr);
|
Map<String, String> getTogethResult(Date date, String expr);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取对应日期 周期的xShow, cycle, setDate, start, end
|
||||||
|
*
|
||||||
|
* @param date 日期
|
||||||
|
* @return map
|
||||||
|
*/
|
||||||
|
Map<String, String[]> getCurrentCycleData(Date date);
|
||||||
}
|
}
|
||||||
|
@ -4,17 +4,19 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
|
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
|
||||||
import iet.ustb.sf.domain.*;
|
import iet.ustb.sf.domain.*;
|
||||||
import iet.ustb.sf.mapper.MonitorMapper;
|
import iet.ustb.sf.mapper.*;
|
||||||
import iet.ustb.sf.mapper.RuleMapper;
|
|
||||||
import iet.ustb.sf.mapper.TargetMapper;
|
|
||||||
import iet.ustb.sf.service.TargetDataService;
|
import iet.ustb.sf.service.TargetDataService;
|
||||||
import iet.ustb.sf.mapper.TargetDataMapper;
|
|
||||||
import iet.ustb.sf.service.UtilService;
|
import iet.ustb.sf.service.UtilService;
|
||||||
import iet.ustb.sf.util.CheckUtils;
|
import iet.ustb.sf.util.CheckUtils;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.ZoneId;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,6 +43,92 @@ public class TargetDataServiceImpl extends ServiceImpl<TargetDataMapper, TargetD
|
|||||||
@Resource
|
@Resource
|
||||||
private RuleMapper ruleMapper;
|
private RuleMapper ruleMapper;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private JobLogMapper jobLogMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Scheduled(cron = "0 45 6 * * ?")
|
||||||
|
public void saveTargetDate(Date date) {
|
||||||
|
String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
Date now = new Date();
|
||||||
|
|
||||||
|
Map<String, String[]> map = utilService.getCurrentCycleData(date);
|
||||||
|
LocalDate day = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
||||||
|
for (String cycle : map.keySet()) {
|
||||||
|
if (day.getDayOfWeek().getValue() > 1 && "lastWeek".equals(cycle)) {
|
||||||
|
continue;
|
||||||
|
} else if ("lastMonth".equals(cycle) && day.getDayOfMonth() > 1) {
|
||||||
|
continue;
|
||||||
|
} else if ("lastProduct".equals(cycle) && day.getDayOfMonth() != 26) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
QueryWrapper<TargetData> queryWrapper = new QueryWrapper<>();
|
||||||
|
queryWrapper.eq("cycle", cycle);
|
||||||
|
queryWrapper.eq("x_show", map.get(cycle)[0]);
|
||||||
|
targetDataMapper.delete(queryWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Target> targets = targetMapper.selectList(new QueryWrapper<Target>().orderByAsc("type"));
|
||||||
|
for (Target target : targets) {
|
||||||
|
if (target.getIsKey() == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String[] strArr = map.get(target.getCycle());
|
||||||
|
if (strArr == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (Arrays.asList("lastWeek", "lastMonth").contains(target.getCycle())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String info = "OK";
|
||||||
|
int status = 0;
|
||||||
|
try {
|
||||||
|
String result;
|
||||||
|
if (target.getType() < 2) {
|
||||||
|
result = SqlRunner.db().selectOne(target.getResultSql(), strArr[3], strArr[4]).get("result").toString();
|
||||||
|
} else {
|
||||||
|
result = utilService.getTogethResult(date, target.getResultSql()).get("res");
|
||||||
|
}
|
||||||
|
TargetData targetData = TargetData.builder()
|
||||||
|
.id(UUID.randomUUID().toString())
|
||||||
|
.targetId(target.getId())
|
||||||
|
.setDate(sdf.parse(strArr[2]))
|
||||||
|
.xShow(strArr[0])
|
||||||
|
.cycle(strArr[1])
|
||||||
|
.createTime(now)
|
||||||
|
.updateTime(now)
|
||||||
|
.val(CheckUtils.roundToDecimalPlaces(result, 6))
|
||||||
|
.targetCycle(target.getCycle())
|
||||||
|
.build();
|
||||||
|
try {
|
||||||
|
targetDataMapper.insert(targetData);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
saveMonitor(targetData);
|
||||||
|
} catch (Exception e) {
|
||||||
|
StringWriter sw = new StringWriter();
|
||||||
|
PrintWriter pw = new PrintWriter(sw);
|
||||||
|
e.printStackTrace(pw);
|
||||||
|
info = sw.toString();
|
||||||
|
status = 1;
|
||||||
|
} finally {
|
||||||
|
JobLog jobLog = JobLog.builder()
|
||||||
|
.id(UUID.randomUUID().toString())
|
||||||
|
.targetId(target.getId())
|
||||||
|
.info(info)
|
||||||
|
.status(status)
|
||||||
|
.createTime(new Date())
|
||||||
|
.build();
|
||||||
|
jobLogMapper.insert(jobLog);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||||
|
System.out.println("定时保存指标数据 start:" + startTime);
|
||||||
|
System.out.println("定时保存指标数据 end:" + endTime);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TargetData updateHistory(Target target, String[] strArr, Map<String, TargetData> map) {
|
public TargetData updateHistory(Target target, String[] strArr, Map<String, TargetData> map) {
|
||||||
String xShow = strArr[0];
|
String xShow = strArr[0];
|
||||||
|
@ -277,6 +277,17 @@ public class UtilServiceImpl implements UtilService {
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, String[]> getCurrentCycleData(Date date) {
|
||||||
|
Map<String, String[]> map = new HashMap<>(9);
|
||||||
|
List<String> cycleList = Arrays.asList("yesterday", "nearlyWeek", "nearlyMonth", "week", "lastWeek", "month", "lastMonth", "product", "lastProduct");
|
||||||
|
|
||||||
|
for (String cycle : cycleList) {
|
||||||
|
map.put(cycle, getCurrentCycleDataByCycle(date, cycle));
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理函数方法
|
* 处理函数方法
|
||||||
*
|
*
|
||||||
|
@ -182,4 +182,17 @@ public class CheckUtils {
|
|||||||
boolean bl = nameCheck != null && (target.getId() == null || !nameCheck.getId().equals(target.getId()));
|
boolean bl = nameCheck != null && (target.getId() == null || !nameCheck.getId().equals(target.getId()));
|
||||||
ThrowUtils.throwIf(bl, ErrorCode.PARAMS_ERROR, "指标名称不能重复!");
|
ThrowUtils.throwIf(bl, ErrorCode.PARAMS_ERROR, "指标名称不能重复!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保留六位小数
|
||||||
|
*
|
||||||
|
* @param value 传入值
|
||||||
|
* @return 结果值
|
||||||
|
*/
|
||||||
|
public static double roundToDecimalPlaces(String value, int round) {
|
||||||
|
if (StringUtils.isEmpty(String.valueOf(value))) {
|
||||||
|
return 0f;
|
||||||
|
}
|
||||||
|
return roundToDecimalPlaces(Double.parseDouble(value), round);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user