Skip to content

Commit

Permalink
Add sample "to human string" method for progress
Browse files Browse the repository at this point in the history
Still a work in progress.

Related to MID-6904.
  • Loading branch information
mederly committed Mar 17, 2021
1 parent 3daad30 commit be768dd
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 79 deletions.
Expand Up @@ -65,4 +65,12 @@ public String debugDump(int indent) {
DebugUtil.debugDumpWithLabel(sb, "Expected buckets", expectedBuckets, indent);
return sb.toString();
}

public float getPercentage() {
if (expectedBuckets != null && expectedBuckets > 0) {
return (float) getCompletedBuckets() / expectedBuckets;
} else {
return Float.NaN;
}
}
}
Expand Up @@ -84,4 +84,12 @@ public String debugDump(int indent) {
DebugUtil.debugDumpWithLabel(sb, "Expected total", expectedTotal, indent);
return sb.toString();
}

public float getPercentage() {
if (expectedTotal != null && expectedTotal > 0) {
return (float) progress / expectedTotal;
} else {
return Float.NaN;
}
}
}
Expand Up @@ -116,4 +116,49 @@ public String debugDump(int indent) {
DebugUtil.debugDumpWithLabel(sb, "Total items progress", itemsProgress, indent);
return sb.toString();
}

public boolean isBucketed() {
return bucketsProgress != null &&
(bucketsProgress.getExpectedBuckets() == null || bucketsProgress.getExpectedBuckets() > 1);
}

public String toHumanReadableString(boolean longForm) {
if (isBucketed()) {
return toHumanReadableStringForBucketed(longForm);
} else if (itemsProgress != null) {
return toHumanReadableStringForNonBucketed(longForm);
} else {
return "?"; // TODO
}
}

private String toHumanReadableStringForNonBucketed(boolean longForm) {
float percentage = itemsProgress.getPercentage();
if (Float.isNaN(percentage)) {
return String.valueOf(itemsProgress.getProgress());
}
if (longForm) {
return String.format("%.1f%% (%d of %d)", percentage * 100,
itemsProgress.getProgress(), itemsProgress.getExpectedTotal());
} else {
return String.format("%.1f%%", percentage * 100);
}
}

private String toHumanReadableStringForBucketed(boolean longForm) {
float percentage = bucketsProgress.getPercentage();
if (Float.isNaN(percentage)) {
if (longForm) {
return bucketsProgress.getCompletedBuckets() + " buckets";
} else {
return bucketsProgress.getCompletedBuckets() + "b";
}
}
if (longForm) {
return String.format("%.1f%% (%d of %d buckets)", percentage * 100,
bucketsProgress.getCompletedBuckets(), bucketsProgress.getExpectedBuckets());
} else {
return String.format("%.1f%%", percentage * 100);
}
}
}
Expand Up @@ -198,10 +198,38 @@ public String toString() {
'}';
}

public String toHumanReadableString(boolean longForm) {
StringBuilder sb = new StringBuilder();
currentPartToHumanReadableString(sb, longForm);
if (isMultiPart()) {
if (longForm) {
sb.append(" in part ").append(getCurrentPartNumber()).append(" of ").append(getAllPartsCount());
} else {
sb.append(" in ").append(getCurrentPartNumber()).append("/").append(getAllPartsCount());
}
}
return sb.toString();
}

private void currentPartToHumanReadableString(StringBuilder sb, boolean longForm) {
TaskPartProgressInformation currentPart = getCurrentPartInformation();
if (currentPart == null) {
return; // TODO?
} else {
sb.append(currentPart.toHumanReadableString(longForm));
}
}

private boolean isMultiPart() {
return getAllPartsCount() > 1;
}

@Override
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.debugDumpLabelLn(sb, getClass().getSimpleName(), indent);
DebugUtil.debugDumpWithLabelLn(sb, "Long form", toHumanReadableString(true), indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "Short form", toHumanReadableString(false), indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "All parts count", allPartsCount, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "Current part number", currentPartNumber, indent + 1);
DebugUtil.debugDumpWithLabelLn(sb, "Current part URI", currentPartUri, indent + 1);
Expand Down
Expand Up @@ -9,6 +9,9 @@

import static com.evolveum.midpoint.util.MiscUtil.or0;

import static java.util.Collections.singleton;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;
Expand All @@ -31,7 +34,7 @@ public static int getProgressForOutcome(StructuredTaskProgressType info, ItemPro
}
}

private static int getCounts(List<TaskPartProgressType> parts,
private static int getCounts(Collection<TaskPartProgressType> parts,
Predicate<OutcomeKeyedCounterType> counterFilter, boolean open) {
return parts.stream()
.flatMap(part -> (open ? part.getOpen() : part.getClosed()).stream())
Expand Down Expand Up @@ -110,6 +113,7 @@ private static Predicate<OutcomeKeyedCounterType> getCounterFilter(ItemProcessin
}

public static int getTotalProgress(TaskPartProgressType progress) {
return 0;
return getCounts(singleton(progress), c -> true, true) +
getCounts(singleton(progress), c -> true, false);
}
}

0 comments on commit be768dd

Please sign in to comment.