Browse Source

增加按照流程类型进行统计分析的接口

tags/24080901
WendyYang 1 year ago
parent
commit
a1b1575685
3 changed files with 79 additions and 21 deletions
  1. +13
    -5
      pmapi/src/main/java/com/ningdatech/pmapi/sys/controller/ProcessStatisticsController.java
  2. +62
    -16
      pmapi/src/main/java/com/ningdatech/pmapi/sys/manage/ProcessStatisticsManage.java
  3. +4
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/sys/model/vo/ProcessDetailStatVO.java

+ 13
- 5
pmapi/src/main/java/com/ningdatech/pmapi/sys/controller/ProcessStatisticsController.java View File

@@ -1,8 +1,8 @@
package com.ningdatech.pmapi.sys.controller; package com.ningdatech.pmapi.sys.controller;


import com.ningdatech.pmapi.projectlib.enumeration.InstTypeEnum;
import com.ningdatech.pmapi.sys.manage.ProcessStatisticsManage; import com.ningdatech.pmapi.sys.manage.ProcessStatisticsManage;
import com.ningdatech.pmapi.sys.model.vo.ProcessDetailStatVO; import com.ningdatech.pmapi.sys.model.vo.ProcessDetailStatVO;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.RestController;


import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;


/** /**
@@ -30,10 +31,17 @@ public class ProcessStatisticsController {


private final ProcessStatisticsManage processStatisticsManage; private final ProcessStatisticsManage processStatisticsManage;


@GetMapping("detailByInstType")
public ProcessDetailStatVO processDetailStat(@RequestParam(required = false, defaultValue = "DAYS") TimeUnit unit,
@Valid @NotNull(message = "流程类型不能为空") InstTypeEnum instType) {
return processStatisticsManage.processDetailStat(unit, instType);
@GetMapping("/detail")
@ApiOperation("流程统计详情")
public ProcessDetailStatVO processStatDetail(@RequestParam(required = false, defaultValue = "DAYS") TimeUnit unit,
@Valid @NotNull(message = "流程类型不能为空") Integer instType) {
return processStatisticsManage.processStatDetail(unit, instType);
}

@GetMapping("/list")
@ApiOperation("流程统计详情列表")
public List<ProcessDetailStatVO> listProcessStatDetail(@RequestParam(required = false, defaultValue = "DAYS") TimeUnit unit) {
return processStatisticsManage.processStatDetailList(unit);
} }


} }

+ 62
- 16
pmapi/src/main/java/com/ningdatech/pmapi/sys/manage/ProcessStatisticsManage.java View File

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


} }

+ 4
- 0
pmapi/src/main/java/com/ningdatech/pmapi/sys/model/vo/ProcessDetailStatVO.java View File

@@ -16,6 +16,10 @@ import java.util.concurrent.TimeUnit;
@Data @Data
public class ProcessDetailStatVO { public class ProcessDetailStatVO {


private Integer instType;

private String instTypeName;

@ApiModelProperty("流程实例总数") @ApiModelProperty("流程实例总数")
private Integer totalInst; private Integer totalInst;




Loading…
Cancel
Save