|
| 1 | +package com.arc_e_tect.gradle.detector.core.console; |
| 2 | + |
| 3 | +import org.gradle.api.logging.Logger; |
| 4 | + |
| 5 | +import java.util.concurrent.TimeUnit; |
| 6 | +import java.util.function.LongSupplier; |
| 7 | + |
| 8 | +/** |
| 9 | + * Emits periodic, low-overhead {@code LIFECYCLE}-level status lines for a long-running scan loop |
| 10 | + * (controller scanning, OpenAPI {@code $ref} resolution, verification-evidence scanning, ...), so |
| 11 | + * a consumer watching the build knows the task is still alive and roughly how far along it is. |
| 12 | + * |
| 13 | + * <p>Deliberately built only on the public {@link Logger} API - one plain line per status update, |
| 14 | + * never overwriting a line in place - rather than Gradle's internal, unsupported rich-console |
| 15 | + * single-line progress indicator. These plugins are published to the Gradle Plugin Portal for |
| 16 | + * consumers on Gradle versions this codebase doesn't control; the internal progress API has |
| 17 | + * changed shape across versions and offers no compatibility guarantee. This is a deliberate |
| 18 | + * trade-off in exchange for forward/backward compatibility, not an oversight.</p> |
| 19 | + * |
| 20 | + * <p>A status line is emitted every {@code everyNItems} items <strong>or</strong> every |
| 21 | + * {@code everySeconds} seconds since the last emission, whichever comes first - never on every |
| 22 | + * single item, and never silent for more than {@code everySeconds} regardless of how many items |
| 23 | + * are processed in between. {@link #complete()} always emits a final summary line, even if the |
| 24 | + * most recent {@link #step()} landed inside the throttle window - the final line is never |
| 25 | + * suppressed by throttling.</p> |
| 26 | + */ |
| 27 | +public final class ScanProgressReporter { |
| 28 | + |
| 29 | + /** Default number of items between emitted status lines, absent an explicit override. */ |
| 30 | + public static final int DEFAULT_EVERY_N_ITEMS = 50; |
| 31 | + |
| 32 | + /** Default number of seconds between emitted status lines, absent an explicit override. */ |
| 33 | + public static final long DEFAULT_EVERY_SECONDS = 2; |
| 34 | + |
| 35 | + private static final int INDETERMINATE_TOTAL = -1; |
| 36 | + |
| 37 | + private final Logger logger; |
| 38 | + private final String phaseLabel; |
| 39 | + private final int total; |
| 40 | + private final int everyNItems; |
| 41 | + private final long everyNanos; |
| 42 | + private final LongSupplier nanoTimeSource; |
| 43 | + |
| 44 | + private int count; |
| 45 | + private int lastEmittedCount; |
| 46 | + private long lastEmittedNanos; |
| 47 | + |
| 48 | + /** |
| 49 | + * Creates a reporter with the default throttle (every {@value #DEFAULT_EVERY_N_ITEMS} items or |
| 50 | + * every {@value #DEFAULT_EVERY_SECONDS} seconds) and the real wall-clock time source. Prefer |
| 51 | + * {@link #determinate(Logger, String, int)} or {@link #indeterminate(Logger, String)}. |
| 52 | + * |
| 53 | + * @param logger the logger status lines are emitted to, at {@code LIFECYCLE} level |
| 54 | + * @param phaseLabel short label identifying the scan phase, e.g. {@code "Scanning @RestController classes"} |
| 55 | + * @param total the total number of items expected, or a negative number for an |
| 56 | + * indeterminate-total scan (the total isn't known ahead of time) |
| 57 | + */ |
| 58 | + public ScanProgressReporter(Logger logger, String phaseLabel, int total) { |
| 59 | + this(logger, phaseLabel, total, DEFAULT_EVERY_N_ITEMS, DEFAULT_EVERY_SECONDS, System::nanoTime); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates a reporter with an explicit throttle and the real wall-clock time source. |
| 64 | + * |
| 65 | + * @param logger the logger status lines are emitted to, at {@code LIFECYCLE} level |
| 66 | + * @param phaseLabel short label identifying the scan phase |
| 67 | + * @param total the total number of items expected, or a negative number for an |
| 68 | + * indeterminate-total scan |
| 69 | + * @param everyNItems emit a status line at least this often, counted in processed items |
| 70 | + * @param everySeconds emit a status line at least this often, counted in elapsed seconds since |
| 71 | + * the last emission |
| 72 | + */ |
| 73 | + public ScanProgressReporter(Logger logger, String phaseLabel, int total, int everyNItems, long everySeconds) { |
| 74 | + this(logger, phaseLabel, total, everyNItems, everySeconds, System::nanoTime); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Creates a reporter with an explicit throttle and time source, for use by tests that need to |
| 79 | + * control elapsed time without a real {@code Thread.sleep}. |
| 80 | + * |
| 81 | + * @param logger the logger status lines are emitted to, at {@code LIFECYCLE} level |
| 82 | + * @param phaseLabel short label identifying the scan phase |
| 83 | + * @param total the total number of items expected, or a negative number for an |
| 84 | + * indeterminate-total scan |
| 85 | + * @param everyNItems emit a status line at least this often, counted in processed items |
| 86 | + * @param everySeconds emit a status line at least this often, counted in elapsed seconds |
| 87 | + * since the last emission |
| 88 | + * @param nanoTimeSource nanosecond tick source, normally {@code System::nanoTime} |
| 89 | + */ |
| 90 | + ScanProgressReporter( |
| 91 | + Logger logger, String phaseLabel, int total, int everyNItems, long everySeconds, |
| 92 | + LongSupplier nanoTimeSource) { |
| 93 | + this.logger = logger; |
| 94 | + this.phaseLabel = phaseLabel; |
| 95 | + this.total = total; |
| 96 | + this.everyNItems = everyNItems; |
| 97 | + this.everyNanos = TimeUnit.SECONDS.toNanos(everySeconds); |
| 98 | + this.nanoTimeSource = nanoTimeSource; |
| 99 | + this.lastEmittedNanos = nanoTimeSource.getAsLong(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Creates a reporter for a scan whose total item count is known ahead of time. Emitted lines |
| 104 | + * include a running fraction and percentage, e.g. {@code "Scanning @RestController classes: 150/438 (34%)"}. |
| 105 | + * |
| 106 | + * @param logger the logger status lines are emitted to, at {@code LIFECYCLE} level |
| 107 | + * @param phaseLabel short label identifying the scan phase |
| 108 | + * @param total the total number of items expected; {@code 0} is valid and not an error |
| 109 | + * @return a new determinate-mode reporter, using the default throttle |
| 110 | + */ |
| 111 | + public static ScanProgressReporter determinate(Logger logger, String phaseLabel, int total) { |
| 112 | + return new ScanProgressReporter(logger, phaseLabel, total); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Creates a reporter for a scan whose total item count isn't known ahead of time. Emitted |
| 117 | + * lines report only a running count, e.g. {@code "Resolving OpenAPI documents: 27 processed so far"}. |
| 118 | + * |
| 119 | + * @param logger the logger status lines are emitted to, at {@code LIFECYCLE} level |
| 120 | + * @param phaseLabel short label identifying the scan phase |
| 121 | + * @return a new indeterminate-mode reporter, using the default throttle |
| 122 | + */ |
| 123 | + public static ScanProgressReporter indeterminate(Logger logger, String phaseLabel) { |
| 124 | + return new ScanProgressReporter(logger, phaseLabel, INDETERMINATE_TOTAL); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Records one item processed, emitting a status line if the throttle window has elapsed. |
| 129 | + * Equivalent to {@link #step(String)} with no detail. |
| 130 | + */ |
| 131 | + public void step() { |
| 132 | + step(null); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Records one item processed, emitting a status line - with {@code detail} appended, when |
| 137 | + * given - if the throttle window has elapsed. |
| 138 | + * |
| 139 | + * @param detail short description of the current item, appended to the line only when this |
| 140 | + * call itself results in an emission; or {@code null}/blank for no detail |
| 141 | + */ |
| 142 | + public void step(String detail) { |
| 143 | + count++; |
| 144 | + long now = nanoTimeSource.getAsLong(); |
| 145 | + boolean dueByCount = (count - lastEmittedCount) >= everyNItems; |
| 146 | + boolean dueByTime = (now - lastEmittedNanos) >= everyNanos; |
| 147 | + if (dueByCount || dueByTime) { |
| 148 | + logger.lifecycle(progressLine(detail)); |
| 149 | + lastEmittedCount = count; |
| 150 | + lastEmittedNanos = now; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Emits a final summary line unconditionally, regardless of throttling state - the last |
| 156 | + * {@link #step()} may have landed inside the throttle window, but this line is never |
| 157 | + * suppressed. Safe to call on a reporter that never had {@link #step()} called at all (the |
| 158 | + * {@code 0}-items-processed case). |
| 159 | + */ |
| 160 | + public void complete() { |
| 161 | + logger.lifecycle(phaseLabel + ": done, " + count + " item(s)"); |
| 162 | + } |
| 163 | + |
| 164 | + private String progressLine(String detail) { |
| 165 | + String base = total >= 0 |
| 166 | + ? phaseLabel + ": " + count + "/" + total + " (" + percentage() + "%)" |
| 167 | + : phaseLabel + ": " + count + " processed so far"; |
| 168 | + return isBlank(detail) ? base : base + " - " + detail; |
| 169 | + } |
| 170 | + |
| 171 | + private long percentage() { |
| 172 | + return total == 0 ? 100 : Math.round(100.0 * count / total); |
| 173 | + } |
| 174 | + |
| 175 | + private boolean isBlank(String value) { |
| 176 | + return value == null || value.isBlank(); |
| 177 | + } |
| 178 | +} |
0 commit comments