mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
完善
This commit is contained in:
@@ -6,7 +6,9 @@ import lombok.experimental.UtilityClass;
|
||||
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -71,8 +73,9 @@ public class CollectionUtils {
|
||||
.getOrDefault(
|
||||
k,
|
||||
new ArrayList<>(
|
||||
Collections.singletonList(
|
||||
(B) null)))
|
||||
Collections
|
||||
.singletonList(
|
||||
null)))
|
||||
.stream()
|
||||
.map(bv -> new Tuple2<>(av, bv))))
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
@@ -104,4 +107,71 @@ public class CollectionUtils {
|
||||
return StreamSupport.stream(
|
||||
a == null ? new ArrayList<A>().spliterator() : a.spliterator(), false);
|
||||
}
|
||||
|
||||
public static <T> void rollingAvg(
|
||||
List<T> src,
|
||||
Comparator<T> comparator,
|
||||
Function<T, Object> getVal,
|
||||
BiConsumer<T, BigDecimal> setAvg) {
|
||||
rollingAvg(src, comparator, getVal, t -> 1, setAvg);
|
||||
}
|
||||
|
||||
public static <T> void rollingAvg(
|
||||
List<T> src,
|
||||
Comparator<T> comparator,
|
||||
Function<T, Object> getVal,
|
||||
Function<T, Object> getWeight,
|
||||
BiConsumer<T, BigDecimal> setAvg) {
|
||||
|
||||
if (org.apache.commons.collections4.CollectionUtils.isEmpty(src)) {
|
||||
return;
|
||||
}
|
||||
List<T> list = src.stream().sorted(comparator).toList();
|
||||
|
||||
BigDecimal valueSum = BigDecimal.ZERO;
|
||||
|
||||
BigDecimal weightSum = BigDecimal.ZERO;
|
||||
|
||||
int nullCount = 0;
|
||||
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
T t = list.get(i);
|
||||
|
||||
BigDecimal value = toBigDecimal(getVal.apply(t));
|
||||
BigDecimal weight = toBigDecimal(getWeight.apply(t));
|
||||
|
||||
if (value != null) {
|
||||
|
||||
valueSum = valueSum.add(value.multiply(weight));
|
||||
weightSum = weightSum.add(weight);
|
||||
} else {
|
||||
|
||||
nullCount++;
|
||||
}
|
||||
if (nullCount == i + 1) {
|
||||
setAvg.accept(t, null);
|
||||
} else {
|
||||
|
||||
setAvg.accept(t, valueSum.divide(weightSum));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static BigDecimal toBigDecimal(Object o) {
|
||||
if (o == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (o instanceof String s && s.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
if (o instanceof BigDecimal) {
|
||||
return (BigDecimal) o;
|
||||
}
|
||||
if (o instanceof Number) {
|
||||
return new BigDecimal(o.toString());
|
||||
}
|
||||
|
||||
return new BigDecimal(o.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import lombok.Data;
|
||||
|
||||
import org.hibernate.annotations.Comment;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class CronJobDto extends CommonDto {
|
||||
@ManyToOne
|
||||
@@ -22,4 +24,6 @@ public class CronJobDto extends CommonDto {
|
||||
|
||||
@Comment("quartz trigger id")
|
||||
private String quartzTriggerId;
|
||||
private LocalDateTime nextFireTime;
|
||||
private LocalDateTime prevFireTime;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,9 @@ import jakarta.persistence.ManyToOne;
|
||||
import lombok.Data;
|
||||
|
||||
import org.hibernate.annotations.Comment;
|
||||
import org.hibernate.annotations.Formula;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@@ -26,6 +29,13 @@ public class CronJobEntity extends CommonEntity {
|
||||
@Comment("quartz trigger id")
|
||||
private String quartzTriggerId;
|
||||
|
||||
@Formula("(select to_timestamp(t.prev_fire_time/ 1000) from qrtz_triggers t where t.trigger_name = quartz_trigger_id )")
|
||||
private LocalDateTime prevFireTime;
|
||||
|
||||
|
||||
@Formula("(select to_timestamp(t.next_fire_time/ 1000) from qrtz_triggers t where t.trigger_name = quartz_trigger_id )")
|
||||
private LocalDateTime nextFireTime;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ public class CronJobService extends BaseService<CronJobEntity, CronJobRepository
|
||||
|
||||
Trigger trigger = scheduler.getTrigger(TriggerKey.triggerKey(entity.getQuartzTriggerId()));
|
||||
|
||||
JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(entity.getQuartzTriggerId()));
|
||||
JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(entity.getQuartzJobId()));
|
||||
|
||||
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(entity.getCron());
|
||||
boolean newJob = false;
|
||||
@@ -165,7 +165,7 @@ public class CronJobService extends BaseService<CronJobEntity, CronJobRepository
|
||||
}else{
|
||||
|
||||
scheduler.pauseTrigger(TriggerKey.triggerKey(job.getQuartzTriggerId()));
|
||||
job.setStatus(1);
|
||||
job.setStatus(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
@@ -73,7 +74,7 @@ public class CronScriptJob implements Job {
|
||||
|
||||
jobLog.setEndTime(end);
|
||||
jobLog.setStackTrace(ex == null ? resultDto.getStackTrace() : ExceptionUtils.getStackTrace(ex));
|
||||
jobLog.setLogs(String.join("\n", resultDto.getLogs()).substring(0, 1024));
|
||||
jobLog.setLogs(StringUtils.substring(String.join("\n", resultDto.getLogs()), 0, 1024));
|
||||
|
||||
cronJobLogService.save(jobLog);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ public class CronJobLogEntity extends CommonEntity {
|
||||
private LocalDateTime startTime;
|
||||
private LocalDateTime endTime;
|
||||
|
||||
@Formula("(EXTRACT(EPOCH FROM (endTime - startTime)) * 1000)")
|
||||
@Formula("(EXTRACT(EPOCH FROM (end_time - start_time)) * 1000)")
|
||||
private long time;
|
||||
|
||||
private String logs;
|
||||
|
||||
@@ -172,6 +172,7 @@ class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepository>
|
||||
sysConfigService.getConfigVal(
|
||||
Constants.SYSCONFIG_SESSION_TIMEOUT)));
|
||||
this.repository.updateExpireTimeById(newExpireTime, loginDto.getId());
|
||||
this.clearCache(loginDto.getId());
|
||||
|
||||
log.info("更新会话过期时间: {} from {} to {}", loginDto.getId(), expireTime, newExpireTime);
|
||||
}
|
||||
|
||||
@@ -213,6 +213,7 @@ class ScriptService extends BaseService<ScriptEntity, ScriptRepository> {
|
||||
|
||||
@Cleanup("stop")
|
||||
ListAppender<ILoggingEvent> appender = new ListAppender<>();
|
||||
appender.setContext(logger.getLoggerContext());
|
||||
appender.start();
|
||||
logger.addAppender(appender);
|
||||
script.setProperty(
|
||||
|
||||
@@ -48,6 +48,9 @@ spring:
|
||||
org.quartz.jobStore.isClustered: true
|
||||
org.quartz.scheduler.instanceName: MyClusteredScheduler
|
||||
org.quartz.scheduler.instanceId: AUTO
|
||||
org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
|
||||
org.quartz.jobStore.class: org.springframework.scheduling.quartz.LocalDataSourceJobStore
|
||||
startup-delay: 10s
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
|
||||
print("hello world")
|
||||
println("hello world".repeat(1000))
|
||||
|
||||
|
||||
15
src/main/resources/scripts/dict/enum/cronJobDict.groovy
Normal file
15
src/main/resources/scripts/dict/enum/cronJobDict.groovy
Normal file
@@ -0,0 +1,15 @@
|
||||
package scripts.dict
|
||||
|
||||
import cn.lihongjie.coal.base.dto.CommonQuery
|
||||
import cn.lihongjie.coal.cronJob.controller.CronJobController
|
||||
import org.springframework.context.ApplicationContext
|
||||
|
||||
ApplicationContext ioc = ioc
|
||||
|
||||
def controller = ioc.getBean(CronJobController.class)
|
||||
|
||||
|
||||
|
||||
|
||||
return controller.list(new CommonQuery())
|
||||
|
||||
Reference in New Issue
Block a user