package foundation.monitor;
|
|
import foundation.util.Util;
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
|
import java.text.MessageFormat;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Stack;
|
|
public class Phase {
|
public static final String STEP_TEMPLATE = "{0}%--------{1}/{2}";
|
public static final String LEVEL_EMPTY_TAB = " ";
|
|
private String code;
|
private String name;
|
private int no;
|
private String noString;
|
private long max;
|
private long subMax;
|
private long min;
|
private long step;
|
private long position;
|
private long stepBuffer;
|
private double percent;
|
private String stepMessage;
|
private boolean isFinish;
|
|
private List<Phase> parentPhaseList;
|
private Phase prePhase;
|
|
private int maxSubIndex;
|
private Phase subPhase;
|
private int level;
|
private static Logger logger;
|
|
static {
|
logger = LogManager.getLogger(Phase.class);
|
}
|
|
public void setMax(int max) {
|
this.max = max;
|
}
|
|
public void addSubPhase(Phase phase) {
|
if (phase != null) {
|
phase.setLevel(level + 1);
|
subPhase = phase;
|
int no = subPhase.getNo();
|
if (no > maxSubIndex) {
|
maxSubIndex = no;
|
}
|
}
|
|
}
|
|
|
public Phase(String code, String name) {
|
this.code = code;
|
this.name = name;
|
this.no = 1;
|
this.level = 1;
|
this.parentPhaseList = new ArrayList<>();
|
this.noString = "1.";
|
initStepInfo();
|
}
|
|
private void initStepInfo() {
|
max = 0;
|
isFinish = false;
|
subMax = 0;
|
min = 0;
|
step = 0;
|
position = 0;
|
stepBuffer = 0;
|
percent = 0;
|
maxSubIndex = 0;
|
|
stepMessage = MessageFormat.format(STEP_TEMPLATE, 0, 0, 0);
|
}
|
|
public void refreshPercent() {
|
int currentStep = no;
|
if (!isFinish) {
|
currentStep = no - 1;
|
}
|
if (max < no) {
|
max = no;
|
}
|
if (max > 0) {
|
double onePercent = 100 / max;
|
double currentPercent;
|
if (isFinish) {
|
currentPercent = currentStep * onePercent;
|
} else {
|
currentPercent = currentStep * onePercent;
|
double subPercent = 0;
|
if (subPhase != null) {
|
double subPhasePercent = subPhase.getPercent();
|
subPercent = onePercent * subPhasePercent / 100;
|
}
|
currentPercent = currentPercent + subPercent;
|
}
|
|
percent = Math.round(currentPercent);
|
}
|
|
stepMessage = MessageFormat.format(STEP_TEMPLATE, percent, currentStep, max == 0 ? 1 : max);
|
|
Phase parentPhase = getParentPhase();
|
if (parentPhase != null) {
|
if (isFinish && no == max) {
|
//子项全部完成
|
parentPhase.setFinish(true);
|
}
|
parentPhase.refreshPercent();
|
}
|
//logger.info("当前进度 {} {} level :{} 进度 {}", noString, name, level, stepMessage);
|
}
|
|
public double getPercent() {
|
return percent;
|
}
|
|
public String getCode() {
|
return code;
|
}
|
|
public String getName() {
|
return name;
|
}
|
|
public void setNo(Phase priorPhase, Stack<Phase> phaseStack) {
|
|
}
|
|
public void setNo(int compareLevel) {
|
if (compareLevel == 1) {
|
max = prePhase.subMax;
|
no = 1;
|
} else if (prePhase != null && compareLevel == 0) {
|
no = prePhase.no + 1;
|
max = prePhase.max;
|
} else {
|
if (prePhase != null) {
|
Phase prePhaseParentPhase = prePhase.getParentPhase();
|
no = prePhaseParentPhase.no + 1;
|
max = prePhaseParentPhase.max;
|
|
} else {
|
no = 1;
|
}
|
|
}
|
|
Phase parentPhase = getParentPhase();
|
if (parentPhase != null) {
|
parentPhase.setSubPhase(this);
|
String parentNoStr = parentPhase.getNoString().trim();
|
String lastStr = parentNoStr.substring(parentNoStr.length() - 1);
|
if (".".equalsIgnoreCase(lastStr)) {
|
noString = parentNoStr + no + " ";
|
} else {
|
noString = parentNoStr + "." + no + " ";
|
}
|
} else {
|
noString = no + ". ";
|
if (prePhase != null) {
|
level = prePhase.level;
|
}
|
}
|
}
|
|
public String getNoString() {
|
return noString;
|
}
|
|
|
public void stepIt() {
|
isFinish = true;
|
/*if (position < max) {
|
position++;
|
stepBuffer++;
|
|
if (stepBuffer >= step) {
|
double percent = position * 100 / max;
|
|
stepBuffer = 0;
|
stepMessage = Math.round(percent) + "%" + "--------" + position + "/" + max;
|
}
|
}
|
else {
|
stepMessage = "100%" + "--------" + max + "/" + max;
|
}
|
*/
|
refreshPercent();
|
|
}
|
|
public void stepIt(int aStep) {
|
if (position < max) {
|
position = Math.min(position + aStep, max);
|
stepBuffer = stepBuffer + aStep;
|
|
if (stepBuffer >= step) {
|
double percent = position * 100 / max;
|
stepBuffer = 0;
|
stepMessage = Math.round(percent) + "%" + "--------" + position + "/" + max;
|
}
|
} else {
|
stepMessage = "100%" + "--------" + max + "/" + max;
|
}
|
refreshPercent();
|
}
|
|
public int getLevel() {
|
return level;
|
}
|
|
public void setLevel(int level) {
|
this.level = level;
|
}
|
|
public int getNo() {
|
return no;
|
}
|
|
public Phase getPrePhase() {
|
return prePhase;
|
}
|
|
public void setPrePhase(Phase prePhase) {
|
this.prePhase = prePhase;
|
}
|
|
public String getStepMessage() {
|
return stepMessage;
|
}
|
|
public String getStartMessage() {
|
StringBuilder builder = new StringBuilder();
|
/*for (int i = 1; i < level; i++) {
|
builder.append(LEVEL_EMPTY_TAB);
|
}*/
|
builder.append(noString);
|
builder.append("【");
|
builder.append(name);
|
builder.append("】");
|
builder.append(Util.newDateTimeStr());
|
builder.append(" 开始执行...");
|
return builder.toString();
|
}
|
|
public String getFinishMessage(String message) {
|
StringBuilder builder = new StringBuilder();
|
/*for (int i = 1; i < level; i++) {
|
builder.append(LEVEL_EMPTY_TAB);
|
}*/
|
builder.append(noString);
|
builder.append(" 【");
|
builder.append(name);
|
builder.append("】 ");
|
builder.append(Util.newDateTimeStr());
|
if (Util.isEmpty(message)) {
|
builder.append(" 执行完成. ");
|
} else {
|
builder.append(" ");
|
builder.append(message);
|
}
|
return builder.toString();
|
}
|
|
public boolean isFinish() {
|
return isFinish;
|
}
|
|
public void setFinish(boolean finish) {
|
isFinish = finish;
|
}
|
|
public Phase getParentPhase() {
|
if (parentPhaseList.isEmpty()) {
|
return null;
|
}
|
Phase phase = parentPhaseList.get(parentPhaseList.size() - 1);
|
return phase;
|
}
|
|
public List<Phase> getParentPhaseList() {
|
return parentPhaseList;
|
}
|
|
public void setParentPhaseList(List<Phase> parentPhase) {
|
if (parentPhase != null) {
|
for (Phase phase : parentPhase) {
|
int oldLevel = phase.getLevel();
|
if (oldLevel >= level) {
|
continue;
|
}
|
this.parentPhaseList.add(phase);
|
}
|
}
|
}
|
|
public void addCurrentParentPhase(Phase parentPhase) {
|
if (parentPhase != null) {
|
this.parentPhaseList.add(parentPhase);
|
}
|
|
}
|
|
public long getSubMax() {
|
return subMax;
|
}
|
|
public void setSubMax(long subMax) {
|
this.subMax = subMax;
|
}
|
|
public Phase getSubPhase() {
|
return subPhase;
|
}
|
|
public void setSubPhase(Phase subPhase) {
|
this.subPhase = subPhase;
|
}
|
|
public void refreshNo(int compareLevel) {
|
setNo(compareLevel);
|
}
|
|
|
}
|