|
@@ -14,9 +14,7 @@ import org.flowable.engine.HistoryService; |
|
|
import org.flowable.engine.history.HistoricProcessInstance; |
|
|
import org.flowable.engine.history.HistoricProcessInstance; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
import java.util.HashSet; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
import java.util.LongSummaryStatistics; |
|
|
|
|
|
|
|
|
import java.util.*; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
@@ -35,14 +33,12 @@ public class ProcessStatisticsManage { |
|
|
private final HistoryService historyService; |
|
|
private final HistoryService historyService; |
|
|
private final IProjectInstService projectInstService; |
|
|
private final IProjectInstService projectInstService; |
|
|
|
|
|
|
|
|
public ProcessDetailStatVO processDetailStat(TimeUnit unit, InstTypeEnum instType) { |
|
|
|
|
|
if (!TimeUnit.DAYS.equals(unit) && !TimeUnit.HOURS.equals(unit)) { |
|
|
|
|
|
throw BizException.wrap("仅支持以天和小时为单位的统计"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public ProcessDetailStatVO processStatDetail(TimeUnit unit, Integer instType) { |
|
|
|
|
|
unitCheck(unit); |
|
|
ProcessDetailStatVO detailStat = ProcessDetailStatVO.init(); |
|
|
ProcessDetailStatVO detailStat = ProcessDetailStatVO.init(); |
|
|
LambdaQueryWrapper<ProjectInst> modelQuery = Wrappers.lambdaQuery(ProjectInst.class) |
|
|
LambdaQueryWrapper<ProjectInst> modelQuery = Wrappers.lambdaQuery(ProjectInst.class) |
|
|
.select(ProjectInst::getInstCode) |
|
|
.select(ProjectInst::getInstCode) |
|
|
.eq(ProjectInst::getInstType, instType.getCode()); |
|
|
|
|
|
|
|
|
.eq(ProjectInst::getInstType, instType); |
|
|
List<ProjectInst> projectInsts = projectInstService.list(modelQuery); |
|
|
List<ProjectInst> projectInsts = projectInstService.list(modelQuery); |
|
|
if (projectInsts.isEmpty()) { |
|
|
if (projectInsts.isEmpty()) { |
|
|
return detailStat; |
|
|
return detailStat; |
|
@@ -51,24 +47,74 @@ public class ProcessStatisticsManage { |
|
|
List<HistoricProcessInstance> instances = historyService.createHistoricProcessInstanceQuery() |
|
|
List<HistoricProcessInstance> instances = historyService.createHistoricProcessInstanceQuery() |
|
|
.processInstanceIds(new HashSet<>(instCodeList)) |
|
|
.processInstanceIds(new HashSet<>(instCodeList)) |
|
|
.list(); |
|
|
.list(); |
|
|
detailStat.setTotalInst(instances.size()); |
|
|
|
|
|
|
|
|
buildProcessDetailStat(unit, detailStat, instances); |
|
|
|
|
|
return detailStat; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void unitCheck(TimeUnit unit) { |
|
|
|
|
|
if (!TimeUnit.DAYS.equals(unit) && !TimeUnit.HOURS.equals(unit)) { |
|
|
|
|
|
throw BizException.wrap("仅支持以天和小时为单位的统计"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void buildProcessDetailStat(TimeUnit unit, ProcessDetailStatVO stat, |
|
|
|
|
|
List<HistoricProcessInstance> instances) { |
|
|
|
|
|
stat.setTotalInst(instances.size()); |
|
|
LongSummaryStatistics statistics = instances.stream().filter(w -> { |
|
|
LongSummaryStatistics statistics = instances.stream().filter(w -> { |
|
|
boolean finished = w.getEndTime() != null; |
|
|
boolean finished = w.getEndTime() != null; |
|
|
if (finished) { |
|
|
if (finished) { |
|
|
detailStat.incrFinished(); |
|
|
|
|
|
|
|
|
stat.incrFinished(); |
|
|
} else { |
|
|
} else { |
|
|
detailStat.incrPending(); |
|
|
|
|
|
|
|
|
stat.incrPending(); |
|
|
} |
|
|
} |
|
|
return finished; |
|
|
return finished; |
|
|
}).map(HistoricProcessInstance::getDurationInMillis) |
|
|
}).map(HistoricProcessInstance::getDurationInMillis) |
|
|
.collect(Collectors.summarizingLong(Long::longValue)); |
|
|
.collect(Collectors.summarizingLong(Long::longValue)); |
|
|
if (detailStat.getFinishedInst() != 0) { |
|
|
|
|
|
|
|
|
if (stat.getFinishedInst() != 0) { |
|
|
long unitMillis = unit.toMillis(1); |
|
|
long unitMillis = unit.toMillis(1); |
|
|
detailStat.setAvgTime(NumberUtil.div(statistics.getAverage(), unitMillis, 1)); |
|
|
|
|
|
detailStat.setMaxTime(NumberUtil.div(statistics.getMax(), unitMillis, 1)); |
|
|
|
|
|
detailStat.setMinTime(NumberUtil.div(statistics.getMin(), unitMillis, 1)); |
|
|
|
|
|
|
|
|
stat.setAvgTime(NumberUtil.div(statistics.getAverage(), unitMillis, 1)); |
|
|
|
|
|
stat.setMaxTime(NumberUtil.div(statistics.getMax(), unitMillis, 1)); |
|
|
|
|
|
stat.setMinTime(NumberUtil.div(statistics.getMin(), unitMillis, 1)); |
|
|
} |
|
|
} |
|
|
return detailStat; |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public List<ProcessDetailStatVO> processStatDetailList(TimeUnit unit) { |
|
|
|
|
|
unitCheck(unit); |
|
|
|
|
|
List<ProjectInst> projectInsts = projectInstService.list(); |
|
|
|
|
|
if (projectInsts.isEmpty()) { |
|
|
|
|
|
return Collections.emptyList(); |
|
|
|
|
|
} |
|
|
|
|
|
HashSet<String> instCodes = new HashSet<>(); |
|
|
|
|
|
Map<InstTypeEnum, List<String>> mapByInstType = projectInsts.stream() |
|
|
|
|
|
.filter(w -> InstTypeEnum.getByCode(w.getInstType()) != null) |
|
|
|
|
|
.collect(Collectors.groupingBy(w -> InstTypeEnum.getByCode(w.getInstType()), |
|
|
|
|
|
Collectors.mapping(w -> { |
|
|
|
|
|
instCodes.add(w.getInstCode()); |
|
|
|
|
|
return w.getInstCode(); |
|
|
|
|
|
}, Collectors.toList()))); |
|
|
|
|
|
List<HistoricProcessInstance> instances = historyService.createHistoricProcessInstanceQuery() |
|
|
|
|
|
.processInstanceIds(new HashSet<>(instCodes)) |
|
|
|
|
|
.list(); |
|
|
|
|
|
List<ProcessDetailStatVO> res = new ArrayList<>(); |
|
|
|
|
|
for (Map.Entry<InstTypeEnum, List<String>> entry : mapByInstType.entrySet()) { |
|
|
|
|
|
List<String> currInstCodes = entry.getValue(); |
|
|
|
|
|
List<HistoricProcessInstance> currInsts = new ArrayList<>(); |
|
|
|
|
|
instances.removeIf(w -> { |
|
|
|
|
|
boolean contains = currInstCodes.contains(w.getId()); |
|
|
|
|
|
if (contains) { |
|
|
|
|
|
currInsts.add(w); |
|
|
|
|
|
} |
|
|
|
|
|
return contains; |
|
|
|
|
|
}); |
|
|
|
|
|
ProcessDetailStatVO item = ProcessDetailStatVO.init(); |
|
|
|
|
|
InstTypeEnum instType = entry.getKey(); |
|
|
|
|
|
item.setInstType(instType.getCode()); |
|
|
|
|
|
item.setInstTypeName(instType.getDesc()); |
|
|
|
|
|
buildProcessDetailStat(unit, item, currInsts); |
|
|
|
|
|
res.add(item); |
|
|
|
|
|
} |
|
|
|
|
|
res.sort(Comparator.comparing(ProcessDetailStatVO::getInstType)); |
|
|
|
|
|
return res; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |