org.daisy.pipeline.client.models.JobMessages Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of clientlib-java Show documentation
Show all versions of clientlib-java Show documentation
A Java library for communicating with the DAISY Pipeline 2.
package org.daisy.pipeline.client.models;
import java.util.ArrayList;
import java.util.AbstractList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Stack;
import org.daisy.pipeline.client.Pipeline2Logger;
public class JobMessages extends AbstractList {
private List backingList = new ArrayList();
public Message get(int index) {
updateProgress();
return backingList.get(index);
}
public int size() {
return backingList.size();
}
@Override
public void add(int index, Message element) {
if (element == null) {
throw new NullPointerException();
}
backingList.add(index, element);
dirty = dirty || index < lastMessageCount;
modCount++;
}
@Override
public Message set(int index, Message element) {
if (element == null) {
throw new NullPointerException();
}
Message prev = backingList.set(index, element);
dirty = dirty || (index < lastMessageCount && !element.equals(prev));
modCount++;
return prev;
}
@Override
public Message remove(int index) {
dirty = dirty || index < lastMessageCount;
modCount++;
return backingList.remove(index);
}
public double getProgressEstimate(Long now) {
updateProgress();
Long previousTime = getProgressFromTime() == null ? now : getProgressFromTime();
Double previousPercentage = getProgressFrom();
Double nextPercentage = getProgressTo();
return nextPercentage - (nextPercentage - previousPercentage) * Math.exp(-(double)(now - previousTime) / progressTimeConstant);
}
private double progressLastPercentage;
private double progressNextPercentage;
private Long progressFirstTime;
private Long progressLastTime;
private double progressTimeConstant;
private static double initialProgressLastPercentage = 0.0;
private static double initialProgressNextPercentage = 100.0;
private static Long initialProgressFirstTime = null;
private static Long initialProgressLastTime = null;
private static double initialProgressTimeConstant = 20000.0;
double getProgressFrom() {
updateProgress();
return progressLastPercentage;
}
double getProgressTo() {
updateProgress();
return progressNextPercentage;
}
Long getProgressFromTime() {
updateProgress();
return progressLastTime;
}
// Progress format: [progress name FROM-TO sub-name]
// in the main script, name must be omitted, otherwise,
// name must be the same as either the preceding steps name or sub-name.
// FROM is required and must be an integer in the range [0,100].
// TO is optional if sub-name is omitted and will default to 100.
// If not omitted, TO must also be an integer in the range [0,100],
// and must be larger than or equal to FROM.
// sub-name is the name of a sub-step and can be used to get a more
// fine-grained progress info.
private static final Pattern PROGRESS_PATTERN;
static {
PROGRESS_PATTERN = Pattern.compile("^\\[progress(| [^\\s\\]]+) (\\d+)([^\\s\\]]* ?[^\\s\\]]*)\\] *(.*?)$", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
}
// $1: " my-name"
// $2: "from"
// $2: "-to sub-name"
public class Progress {
private Pattern namePattern;
private String nameString;
private boolean nameIsGlob = false;
public double from = 0.0;
public double to = 100.0;
public long timeStamp = new Date().getTime();
public boolean active = false; // will be set to true when a message on this level arrives
public Message currentMessage = null;
public Message.Level currentLevel = Message.Level.TRACE;
public Progress(String name) {
if (name.contains("*") || name.contains("?")) {
String regexName = "";
for (char c : name.toCharArray()) {
regexName += c == '?' ? "." : c == '*' ? ".*" : c == '.' ? "\\." : c;
}
this.namePattern = Pattern.compile(regexName, Pattern.DOTALL);
this.nameIsGlob = true;
} else {
this.nameString = name;
}
}
public String getName() {
if (nameIsGlob) {
return namePattern == null ? null : namePattern.pattern();
} else {
return nameString;
}
}
public boolean matchesName(String name) {
if (nameIsGlob) {
if (active) { return false; } // only the first match should be used; otherwise a '*' would match all following messages
return namePattern == null ? false : namePattern.matcher(name).matches();
} else {
return nameString == null ? false : nameString.equals(name);
}
}
public void resolveGlob(String name) {
this.nameString = name;
this.nameIsGlob = false;
}
public void updateSeverity(Message.Level level) {
if (level != null && currentMessage != null) {
if (currentMessage.inferredLevel == null || level.compareTo(currentMessage.inferredLevel) < 0) {
currentMessage.inferredLevel = level;
}
}
if (currentLevel == null) {
currentLevel = level;
} else if (level != null) {
if (level.compareTo(currentLevel) < 0) {
currentLevel = level;
}
}
}
}
private Stack