|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hive.common.log; |
| 19 | + |
| 20 | +import static org.fusesource.jansi.Ansi.ansi; |
| 21 | + |
| 22 | +import com.google.common.base.Function; |
| 23 | +import com.google.common.collect.Lists; |
| 24 | +import java.io.PrintStream; |
| 25 | +import java.io.StringWriter; |
| 26 | +import java.text.DecimalFormat; |
| 27 | +import java.util.List; |
| 28 | +import javax.annotation.Nullable; |
| 29 | +import org.apache.commons.lang3.StringUtils; |
| 30 | +import org.fusesource.jansi.Ansi; |
| 31 | + |
| 32 | +/** Renders information from ProgressMonitor to the stream provided. */ |
| 33 | +public class InPlaceUpdate { |
| 34 | + |
| 35 | + public static final int MIN_TERMINAL_WIDTH = 94; |
| 36 | + |
| 37 | + // keep this within 80 chars width. If more columns needs to be added then update min terminal |
| 38 | + // width requirement and SEPARATOR width accordingly |
| 39 | + private static final String HEADER_FORMAT = "%16s%10s %13s %5s %9s %7s %7s %6s %6s "; |
| 40 | + private static final String VERTEX_FORMAT = "%-16s%10s %13s %5s %9s %7s %7s %6s %6s "; |
| 41 | + private static final String FOOTER_FORMAT = "%-15s %-30s %-4s %-25s"; |
| 42 | + |
| 43 | + private static final int PROGRESS_BAR_CHARS = 30; |
| 44 | + private static final String SEPARATOR = |
| 45 | + new String(new char[MIN_TERMINAL_WIDTH]).replace("\0", "-"); |
| 46 | + |
| 47 | + /* Pretty print the values */ |
| 48 | + private final DecimalFormat secondsFormatter = new DecimalFormat("#0.00"); |
| 49 | + private int lines = 0; |
| 50 | + private PrintStream out; |
| 51 | + |
| 52 | + public InPlaceUpdate(PrintStream out) { |
| 53 | + this.out = out; |
| 54 | + } |
| 55 | + |
| 56 | + public InPlaceUpdate() { |
| 57 | + this(System.out); |
| 58 | + } |
| 59 | + |
| 60 | + public static void reprintLine(PrintStream out, String line) { |
| 61 | + out.print(ansi().eraseLine(Ansi.Erase.ALL).a(line).a('\n').toString()); |
| 62 | + out.flush(); |
| 63 | + } |
| 64 | + |
| 65 | + public static void rePositionCursor(PrintStream ps) { |
| 66 | + ps.print(ansi().cursorUp(0).toString()); |
| 67 | + ps.flush(); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * NOTE: Use this method only if isUnixTerminal is true. Erases the current line and prints the |
| 72 | + * given line. |
| 73 | + * |
| 74 | + * @param line - line to print |
| 75 | + */ |
| 76 | + private void reprintLine(String line) { |
| 77 | + reprintLine(out, line); |
| 78 | + lines++; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * NOTE: Use this method only if isUnixTerminal is true. Erases the current line and prints the |
| 83 | + * given line with the specified color. |
| 84 | + * |
| 85 | + * @param line - line to print |
| 86 | + * @param color - color for the line |
| 87 | + */ |
| 88 | + private void reprintLineWithColorAsBold(String line, Ansi.Color color) { |
| 89 | + out.print( |
| 90 | + ansi() |
| 91 | + .eraseLine(Ansi.Erase.ALL) |
| 92 | + .fg(color) |
| 93 | + .bold() |
| 94 | + .a(line) |
| 95 | + .a('\n') |
| 96 | + .boldOff() |
| 97 | + .reset() |
| 98 | + .toString()); |
| 99 | + out.flush(); |
| 100 | + lines++; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * NOTE: Use this method only if isUnixTerminal is true. Erases the current line and prints the |
| 105 | + * given multiline. Make sure the specified line is not terminated by linebreak. |
| 106 | + * |
| 107 | + * @param line - line to print |
| 108 | + */ |
| 109 | + private void reprintMultiLine(String line) { |
| 110 | + int numLines = line.split("\r\n|\r|\n").length; |
| 111 | + out.print(ansi().eraseLine(Ansi.Erase.ALL).a(line).a('\n').toString()); |
| 112 | + out.flush(); |
| 113 | + lines += numLines; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * NOTE: Use this method only if isUnixTerminal is true. Repositions the cursor back to line 0. |
| 118 | + */ |
| 119 | + private void repositionCursor() { |
| 120 | + if (lines > 0) { |
| 121 | + out.print(ansi().cursorUp(lines).toString()); |
| 122 | + out.flush(); |
| 123 | + lines = 0; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // [==================>>-----] |
| 128 | + private String getInPlaceProgressBar(double percent) { |
| 129 | + StringWriter bar = new StringWriter(); |
| 130 | + bar.append("["); |
| 131 | + int remainingChars = PROGRESS_BAR_CHARS - 4; |
| 132 | + int completed = (int) (remainingChars * percent); |
| 133 | + int pending = remainingChars - completed; |
| 134 | + for (int i = 0; i < completed; i++) { |
| 135 | + bar.append("="); |
| 136 | + } |
| 137 | + bar.append(">>"); |
| 138 | + for (int i = 0; i < pending; i++) { |
| 139 | + bar.append("-"); |
| 140 | + } |
| 141 | + bar.append("]"); |
| 142 | + return bar.toString(); |
| 143 | + } |
| 144 | + |
| 145 | + public void render(ProgressMonitor monitor) { |
| 146 | + if (monitor == null) return; |
| 147 | + // position the cursor to line 0 |
| 148 | + repositionCursor(); |
| 149 | + |
| 150 | + // print header |
| 151 | + // ------------------------------------------------------------------------------- |
| 152 | + // VERTICES STATUS TOTAL COMPLETED RUNNING PENDING FAILED KILLED |
| 153 | + // ------------------------------------------------------------------------------- |
| 154 | + reprintLine(SEPARATOR); |
| 155 | + reprintLineWithColorAsBold( |
| 156 | + String.format(HEADER_FORMAT, monitor.headers().toArray()), Ansi.Color.CYAN); |
| 157 | + reprintLine(SEPARATOR); |
| 158 | + |
| 159 | + // Map 1 .......... container SUCCEEDED 7 7 0 0 0 0 |
| 160 | + List<String> printReady = |
| 161 | + Lists.transform( |
| 162 | + monitor.rows(), |
| 163 | + new Function<List<String>, String>() { |
| 164 | + @Nullable |
| 165 | + @Override |
| 166 | + public String apply(@Nullable List<String> row) { |
| 167 | + return String.format(VERTEX_FORMAT, row.toArray()); |
| 168 | + } |
| 169 | + }); |
| 170 | + reprintMultiLine(StringUtils.join(printReady, "\n")); |
| 171 | + |
| 172 | + // ------------------------------------------------------------------------------- |
| 173 | + // VERTICES: 03/04 [=================>>-----] 86% ELAPSED TIME: 1.71 s |
| 174 | + // ------------------------------------------------------------------------------- |
| 175 | + String progressStr = "" + (int) (monitor.progressedPercentage() * 100) + "%"; |
| 176 | + float et = (float) (System.currentTimeMillis() - monitor.startTime()) / (float) 1000; |
| 177 | + String elapsedTime = "ELAPSED TIME: " + secondsFormatter.format(et) + " s"; |
| 178 | + String footer = |
| 179 | + String.format( |
| 180 | + FOOTER_FORMAT, |
| 181 | + monitor.footerSummary(), |
| 182 | + getInPlaceProgressBar(monitor.progressedPercentage()), |
| 183 | + progressStr, |
| 184 | + elapsedTime); |
| 185 | + |
| 186 | + reprintLine(SEPARATOR); |
| 187 | + reprintLineWithColorAsBold(footer, Ansi.Color.RED); |
| 188 | + reprintLine(SEPARATOR); |
| 189 | + } |
| 190 | +} |
0 commit comments