package foundation.monitor;
|
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Stack;
|
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
|
import foundation.json.IJSONProvider;
|
import foundation.json.IJSONWriter;
|
import foundation.translator.Translator;
|
|
public class Progressor implements Iterable<MessageLine>, IJSONProvider {
|
|
private static Logger logger;
|
private String taskName;
|
private Stack<Phase> phaseStack;
|
private List<MessageLine> messageList;
|
private Phase priorPhase;
|
|
private long max;
|
private long min;
|
private long step;
|
private long position;
|
private long stepBuffer;
|
private String stepMessage;
|
private boolean debug;
|
|
|
static {
|
logger = LogManager.getLogger(Progressor.class);
|
}
|
|
public Progressor(String taskName) {
|
super();
|
this.taskName = taskName;
|
}
|
|
public Progressor() {
|
messageList = new ArrayList<MessageLine>(100);
|
phaseStack = new Stack<Phase>();
|
|
max = 0;
|
min = 0;
|
step = 0;
|
position = 0;
|
stepBuffer = 0;
|
stepMessage = "0%--------0/0";
|
|
debug = true;
|
}
|
|
public void clear() {
|
priorPhase = null;
|
phaseStack.clear();
|
clearMessages();
|
clearStep();
|
}
|
|
public void clearStep() {
|
max = 0;
|
min = 0;
|
step = 0;
|
position = 0;
|
stepBuffer = 0;
|
stepMessage = "0%--------0/0";
|
}
|
|
public void stepIt() {
|
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;
|
}
|
}
|
|
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;
|
}
|
}
|
|
public long getMax() {
|
return max;
|
}
|
|
public void setMax(long max) {
|
clearStep();
|
this.max = max;
|
}
|
|
public long getMin() {
|
return min;
|
}
|
|
public void setMin(int min) {
|
this.min = min;
|
}
|
|
public String getStepMsg() {
|
return stepMessage;
|
}
|
|
public void newTask(String name) {
|
taskName = name;
|
|
clear();
|
|
Date datetime = new Date();
|
String text = "【" + name + "】 开始... " + Translator.toString(datetime);
|
|
appendLine(text);
|
}
|
|
public void endTask() {
|
String text = "【" + taskName + "】 结束";
|
appendLine(text);
|
}
|
|
public void newPhase(String code, String phaseName) {
|
Phase phase = new Phase(code, phaseName);
|
phase.setNo(priorPhase, phaseStack);
|
|
String indent = "";
|
for (int i = 0; i < phaseStack.size(); i++) {
|
indent = indent + " ";
|
}
|
|
String text = indent + phase.getNoString() + phaseName;
|
appendLine(text);
|
|
phaseStack.push(phase);
|
priorPhase = null;
|
}
|
|
public void newPhase(String phaseName) {
|
Phase phase = new Phase(null, phaseName);
|
phase.setNo(priorPhase, phaseStack);
|
|
String indent = "";
|
for (int i = 0; i < phaseStack.size(); i++) {
|
indent = indent + " ";
|
}
|
|
String text = indent + phase.getNoString() + phaseName;
|
appendLine(text);
|
|
phaseStack.push(phase);
|
priorPhase = null;
|
}
|
|
public void endPhase() {
|
clearStep();
|
priorPhase = phaseStack.pop();
|
}
|
|
public void clearMessages() {
|
messageList.clear();
|
}
|
|
public void appendLine(String message) {
|
int stackSize = phaseStack.size();
|
|
//1.
|
MessageLine line = new MessageLine(stackSize, message);
|
messageList.add(line);
|
|
//2.
|
if (debug) {
|
String indent = "";
|
for (int i = 0; i <= stackSize; i++) {
|
indent = indent + " ";
|
}
|
|
String text = indent + message;
|
logger.debug(text);
|
}
|
}
|
|
public void appendInlineMessage(String message) {
|
if (message != null) {
|
MessageLine line = null;
|
int stackSize = phaseStack.size();
|
|
if (messageList.isEmpty()) {
|
line = new MessageLine(stackSize, message);
|
messageList.add(line);
|
}
|
else {
|
int max = messageList.size() - 1;
|
line = messageList.get(max);
|
line.appendValue(message);
|
}
|
}
|
|
if (debug) {
|
logger.debug(message);
|
}
|
}
|
|
public void setStepName(String step) {
|
MessageLine line = new MessageLine(phaseStack.size(), step);
|
messageList.add(line);
|
}
|
|
public List<MessageLine> getMessageList() {
|
return messageList;
|
}
|
|
public void terminate(String error) {
|
MessageLine line = new MessageLine(phaseStack.size(), error);
|
messageList.add(line);
|
}
|
|
public Boolean isEmpty() {
|
return messageList.size() == 0;
|
}
|
|
public List<Phase> getCurrentPhases() {
|
List<Phase> resultList = new ArrayList<Phase>();
|
|
for (Phase phase : phaseStack) {
|
resultList.add(phase);
|
}
|
|
return resultList;
|
}
|
|
public String getTaskName() {
|
return taskName;
|
}
|
|
public void setStepLength(int batch_Write) {
|
// TODO Auto-generated method stub
|
|
}
|
|
public void setDebug(boolean debug) {
|
this.debug = debug;
|
}
|
|
@Override
|
public Iterator<MessageLine> iterator() {
|
return messageList.iterator();
|
}
|
|
@Override
|
public void writeJSON(IJSONWriter writer) {
|
writer.beginArray();
|
|
for (MessageLine line: messageList) {
|
writer.beginObject();
|
writer.write("placeHolder", line.getPlaceHolder());
|
writer.write("value", line.getValue());
|
writer.endObject();
|
}
|
|
writer.endArray();
|
}
|
|
}
|