|
| 1 | +package com.t11e.discovery.datatool; |
| 2 | + |
| 3 | +import java.text.NumberFormat; |
| 4 | +import java.util.concurrent.TimeUnit; |
| 5 | +import java.util.logging.Level; |
| 6 | +import java.util.logging.Logger; |
| 7 | + |
| 8 | +import org.apache.commons.lang.StringUtils; |
| 9 | + |
| 10 | +public class ProgressLoggerSimpleJavaUtil |
| 11 | + implements ProgressLogger |
| 12 | +{ |
| 13 | + private final NumberFormat format; |
| 14 | + private final Logger logger; |
| 15 | + private final Level level; |
| 16 | + /** |
| 17 | + * Don't log progress messages more often than this number of ms. |
| 18 | + */ |
| 19 | + private final long minLogIntervalNs; |
| 20 | + private boolean inProgress; |
| 21 | + private long startNs; |
| 22 | + private long progress; |
| 23 | + private String description; |
| 24 | + private long lastLogMessgeTime; |
| 25 | + private long lastProgress; |
| 26 | + private boolean failed; |
| 27 | + private String unitSingle; |
| 28 | + private String unitPlural; |
| 29 | + private long estimatedWork; |
| 30 | + |
| 31 | + public ProgressLoggerSimpleJavaUtil(final Logger logger, final Level level, final long minLogIntervalMs) |
| 32 | + { |
| 33 | + format = NumberFormat.getNumberInstance(); |
| 34 | + format.setMaximumFractionDigits(3); |
| 35 | + format.setGroupingUsed(true); |
| 36 | + this.logger = logger; |
| 37 | + this.level = level; |
| 38 | + minLogIntervalNs = TimeUnit.NANOSECONDS.convert(minLogIntervalMs, TimeUnit.MILLISECONDS); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public ProgressLogger begin(final String description) |
| 43 | + { |
| 44 | + if (inProgress) |
| 45 | + { |
| 46 | + setFailed(true); |
| 47 | + done(); |
| 48 | + } |
| 49 | + inProgress = true; |
| 50 | + progress = 0; |
| 51 | + lastProgress = 0; |
| 52 | + estimatedWork = 0; |
| 53 | + startNs = System.nanoTime(); |
| 54 | + this.description = description; |
| 55 | + logProgress(startNs, "[begin]"); |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public ProgressLogger done() |
| 61 | + { |
| 62 | + inProgress = false; |
| 63 | + final long now = System.nanoTime(); |
| 64 | + logProgress(now); |
| 65 | + failed = false; |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public ProgressLogger worked(final long worked) |
| 71 | + { |
| 72 | + progress += worked; |
| 73 | + maybeLogProgress(); |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public ProgressLogger setUnits(final String unitSingle, final String unitPlural) |
| 79 | + { |
| 80 | + this.unitSingle = unitSingle; |
| 81 | + this.unitPlural = unitPlural; |
| 82 | + maybeLogProgress(); |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public ProgressLogger setEstimatedWork(final long estimatedWork, final String unitSingle, final String unitPlural) |
| 88 | + { |
| 89 | + this.estimatedWork = estimatedWork; |
| 90 | + this.unitSingle = unitSingle; |
| 91 | + this.unitPlural = unitPlural; |
| 92 | + maybeLogProgress(); |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public ProgressLogger setDescription(final String description) |
| 98 | + { |
| 99 | + this.description = description; |
| 100 | + maybeLogProgress(); |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public ProgressLogger setFailed(final boolean failed) |
| 106 | + { |
| 107 | + this.failed = failed; |
| 108 | + maybeLogProgress(); |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public ProgressLogger addException(final Throwable throwable) |
| 114 | + { |
| 115 | + final long now = System.nanoTime(); |
| 116 | + final StringBuilder msg = new StringBuilder(); |
| 117 | + formatProgressBar(msg, now); |
| 118 | + msg.append(" [exception]"); |
| 119 | + logger.log(level, msg.toString(), throwable); |
| 120 | + lastLogMessgeTime = now; |
| 121 | + lastProgress = progress; |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + private void maybeLogProgress() |
| 126 | + { |
| 127 | + if (inProgress) |
| 128 | + { |
| 129 | + final long now = System.nanoTime(); |
| 130 | + if (now - lastLogMessgeTime > minLogIntervalNs) |
| 131 | + { |
| 132 | + logProgress(now); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private void logProgress(final long now, final String... extraLogContext) |
| 138 | + { |
| 139 | + final StringBuilder msg = new StringBuilder(); |
| 140 | + formatProgressBar(msg, now); |
| 141 | + for (final String ctx : extraLogContext) |
| 142 | + { |
| 143 | + msg.append(" ").append(ctx); |
| 144 | + } |
| 145 | + logger.log(level, msg.toString()); |
| 146 | + lastLogMessgeTime = now; |
| 147 | + lastProgress = progress; |
| 148 | + } |
| 149 | + |
| 150 | + private void formatProgressBar( |
| 151 | + final StringBuilder buffer, |
| 152 | + final long nowNs) |
| 153 | + { |
| 154 | + long eta = 0; |
| 155 | + if (estimatedWork > 0) |
| 156 | + { |
| 157 | + if (progress > 0) |
| 158 | + { |
| 159 | + final long elapsed = nowNs - startNs; |
| 160 | + final long unitsLeft = estimatedWork - progress; |
| 161 | + eta = (long) (unitsLeft * (elapsed / (double) progress)); |
| 162 | + } |
| 163 | + } |
| 164 | + final long timeDelta = nowNs - lastLogMessgeTime; |
| 165 | + final long workDelta = progress - lastProgress; |
| 166 | + |
| 167 | + buffer.append(description); |
| 168 | + if (inProgress && estimatedWork == 0) |
| 169 | + { |
| 170 | + buffer.append("..."); |
| 171 | + } |
| 172 | + final boolean estimation = estimatedWork > 0; |
| 173 | + if (estimation) |
| 174 | + { |
| 175 | + buffer.append(" ["); |
| 176 | + final int barLength = 15; |
| 177 | + final int fillChars = (int) (((double) progress / estimatedWork) * barLength); |
| 178 | + for (int i = 0; i < barLength; i++) |
| 179 | + { |
| 180 | + buffer.append(i < fillChars ? '#' : '.'); |
| 181 | + } |
| 182 | + buffer.append("]"); |
| 183 | + } |
| 184 | + if (failed) |
| 185 | + { |
| 186 | + buffer.append(" [failed]"); |
| 187 | + } |
| 188 | + if (!inProgress) |
| 189 | + { |
| 190 | + buffer.append(" [done]"); |
| 191 | + } |
| 192 | + if (estimatedWork > 1) |
| 193 | + { |
| 194 | + final int pc = (int) ((progress / (double) estimatedWork) * 100); |
| 195 | + buffer.append(StringUtils.rightPad(String.valueOf(pc), 6)); |
| 196 | + buffer.append("%"); |
| 197 | + } |
| 198 | + if (estimation) |
| 199 | + { |
| 200 | + buffer.append(" ["); |
| 201 | + final String s = format.format(estimatedWork); |
| 202 | + buffer.append(StringUtils.rightPad(format.format(progress), s.length())); |
| 203 | + buffer.append("/"); |
| 204 | + buffer.append(s); |
| 205 | + buffer.append("]"); |
| 206 | + } |
| 207 | + else if (progress > 0) |
| 208 | + { |
| 209 | + buffer.append(" ["); |
| 210 | + buffer.append(format.format(progress)); |
| 211 | + buffer.append("]"); |
| 212 | + } |
| 213 | + final long workTime = nowNs - startNs; |
| 214 | + final long workTimeRounded = roundNs(workTime); |
| 215 | + if (workTimeRounded > 0) |
| 216 | + { |
| 217 | + buffer.append(" ("); |
| 218 | + buffer.append(TimingUtil.formatTimingConcise(workTimeRounded, TimingUtil.UNITS_NS)); |
| 219 | + if (inProgress) |
| 220 | + { |
| 221 | + final long etaRounded = roundNs(eta); |
| 222 | + if (etaRounded > 0) |
| 223 | + { |
| 224 | + buffer.append(", ETA "); |
| 225 | + buffer.append(TimingUtil.formatTimingConcise(etaRounded, TimingUtil.UNITS_NS)); |
| 226 | + } |
| 227 | + } |
| 228 | + if (!inProgress && progress > 0) |
| 229 | + { |
| 230 | + buffer.append(", "); |
| 231 | + buffer.append(TimingUtil.formatItemTime(workTime, TimingUtil.UNITS_NS, progress, unitSingle, unitPlural)); |
| 232 | + } |
| 233 | + final int unitsPerSec = inProgress |
| 234 | + ? (timeDelta > 0 |
| 235 | + ? (int) (workDelta / (timeDelta / (1.0 * TimeUnit.NANOSECONDS.convert(1, TimeUnit.SECONDS)))) : 0) |
| 236 | + : (workTime > 0 |
| 237 | + ? (int) (progress / (workTime / (1.0 * TimeUnit.NANOSECONDS.convert(1, TimeUnit.SECONDS)))) : 0); |
| 238 | + if (unitsPerSec > 0) |
| 239 | + { |
| 240 | + buffer.append(", "); |
| 241 | + buffer.append(format.format(unitsPerSec)); |
| 242 | + buffer.append(" "); |
| 243 | + buffer.append(unitPlural); |
| 244 | + buffer.append("/sec"); |
| 245 | + } |
| 246 | + buffer.append(')'); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + private static long roundNs(final long workTime) |
| 251 | + { |
| 252 | + return workTime - (workTime % 1000000000); |
| 253 | + } |
| 254 | +} |
0 commit comments