Browse Source

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

master
WendyYang 1 year ago
parent
commit
9dd4034905
3 changed files with 169 additions and 0 deletions
  1. +39
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/sys/controller/ProcessStatisticsController.java
  2. +74
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/sys/manage/ProcessStatisticsManage.java
  3. +56
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/sys/model/vo/ProcessDetailStatVO.java

+ 39
- 0
pmapi/src/main/java/com/ningdatech/pmapi/sys/controller/ProcessStatisticsController.java View File

@@ -0,0 +1,39 @@
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.model.vo.ProcessDetailStatVO;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
* <p>
* ProcessStatisticsController
* </p>
*
* @author WendyYang
* @since 2023/7/31
**/
@Validated
@RestController
@RequestMapping("/api/v1/process/statistics")
@AllArgsConstructor
public class ProcessStatisticsController {

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

}

+ 74
- 0
pmapi/src/main/java/com/ningdatech/pmapi/sys/manage/ProcessStatisticsManage.java View File

@@ -0,0 +1,74 @@
package com.ningdatech.pmapi.sys.manage;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.projectlib.enumeration.InstTypeEnum;
import com.ningdatech.pmapi.projectlib.model.entity.ProjectInst;
import com.ningdatech.pmapi.projectlib.service.IProjectInstService;
import com.ningdatech.pmapi.sys.model.vo.ProcessDetailStatVO;
import lombok.AllArgsConstructor;
import org.flowable.engine.HistoryService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* <p>
* ProcessStatisticsManage
* </p>
*
* @author WendyYang
* @since 2023/7/31
**/
@Component
@AllArgsConstructor
public class ProcessStatisticsManage {

private final HistoryService historyService;
private final IProjectInstService projectInstService;

public ProcessDetailStatVO processDetailStat(TimeUnit unit, InstTypeEnum instType) {
if (!TimeUnit.DAYS.equals(unit) && !TimeUnit.HOURS.equals(unit)) {
throw BizException.wrap("仅支持以天和小时为单位的统计");
}
ProcessDetailStatVO detailStat = ProcessDetailStatVO.init();
LambdaQueryWrapper<ProjectInst> modelQuery = Wrappers.lambdaQuery(ProjectInst.class)
.select(ProjectInst::getInstCode)
.eq(ProjectInst::getInstType, instType.getCode());
List<ProjectInst> projectInsts = projectInstService.list(modelQuery);
if (projectInsts.isEmpty()) {
return detailStat;
}
List<String> instCodeList = CollUtils.fieldList(projectInsts, ProjectInst::getInstCode);
List<HistoricProcessInstance> instances = historyService.createHistoricProcessInstanceQuery()
.processInstanceIds(new HashSet<>(instCodeList))
.list();
detailStat.setTotalInst(instances.size());
LongSummaryStatistics statistics = instances.stream().filter(w -> {
boolean finished = w.getEndTime() != null;
if (finished) {
detailStat.incrFinished();
} else {
detailStat.incrPending();
}
return finished;
}).map(HistoricProcessInstance::getDurationInMillis)
.collect(Collectors.summarizingLong(Long::longValue));
if (detailStat.getFinishedInst() != 0) {
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));
}
return detailStat;
}

}

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

@@ -0,0 +1,56 @@
package com.ningdatech.pmapi.sys.model.vo;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.concurrent.TimeUnit;

/**
* <p>
* ProcessDetailStatVO
* </p>
*
* @author WendyYang
* @since 2023/7/31
**/
@Data
public class ProcessDetailStatVO {

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

@ApiModelProperty("已完成流程实例数")
private Integer finishedInst;

@ApiModelProperty("已完成流程实例数")
private Integer pendingInst;

@ApiModelProperty("平均耗时")
private double avgTime;

@ApiModelProperty("最长耗时")
private double maxTime;

@ApiModelProperty("最短耗时")
private double minTime;

@ApiModelProperty("时间单位")
private TimeUnit timeUnit;

public static ProcessDetailStatVO init() {
ProcessDetailStatVO stat = new ProcessDetailStatVO();
stat.setFinishedInst(0);
stat.setTotalInst(0);
stat.setPendingInst(0);
return stat;
}

public void incrPending() {
this.pendingInst += 1;
}

public void incrFinished() {
this.finishedInst += 1;
}

}

Loading…
Cancel
Save