Skip to content

Commit

Permalink
MID-8842 ninja - download action log improvements
Browse files Browse the repository at this point in the history
(cherry picked from commit b2d5e18)
  • Loading branch information
1azyman committed Aug 3, 2023
1 parent 6bbffca commit bcbd280
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ private void executeScripts(@NotNull DataSource dataSource, @NotNull List<File>
}

stmt.close();

log.info(ConsoleFormat.formatSuccessMessage("Script executed successfully."));
}
} finally {
connection.setAutoCommit(autocommit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,33 @@

package com.evolveum.midpoint.ninja.action.upgrade;

import java.io.PrintStream;
import java.text.DecimalFormat;

import org.apache.commons.io.FileUtils;
import org.fusesource.jansi.Ansi;

import com.evolveum.midpoint.ninja.impl.Log;
import com.evolveum.midpoint.ninja.impl.LogLevel;
import com.evolveum.midpoint.ninja.util.ConsoleFormat;

public class ConsoleProgressListener implements ProgressListener {

private static final DecimalFormat FORMAT = new DecimalFormat("###");

private PrintStream stream;
private final Log log;

boolean firstUpdate = true;
private boolean firstUpdate = true;

double progress = 0;
private double progress = 0;

public ConsoleProgressListener(PrintStream stream) {
this.stream = stream;
public ConsoleProgressListener(Log log) {
this.log = log;
}

@Override
public void update(long bytesRead, long contentLength, boolean done) {
if (done) {
stream.println(Ansi.ansi().cursorUpLine().eraseLine().fgGreen().a("Download complete").reset());
log.info(ConsoleFormat.formatSuccessMessage("Download complete"));
return;
}

Expand All @@ -39,16 +42,27 @@ public void update(long bytesRead, long contentLength, boolean done) {

String size = contentLength == -1 ? "unknown" : FileUtils.byteCountToDisplaySize(contentLength);

stream.println(Ansi.ansi().fgBlack().a("Download size: " + size).reset());
// this empy line will be removed with progress update
stream.println();
log.info(ConsoleFormat.formatMessageWithInfoParameters("Download size: {}", size));

// this empty line will be removed with progress update
log.info("");
}

double newProgress = (double) (100 * bytesRead) / contentLength;
if (newProgress - progress > 1) {
stream.println(Ansi.ansi().cursorUpLine().eraseLine(Ansi.Erase.ALL).a("Progress: ").fgBlue().a(FORMAT.format(newProgress)).a("%").reset());
logProgress(newProgress);

progress = newProgress;
}
}

private void logProgress(double newProgress) {
log.info(Ansi.ansi()
.cursorToColumn(1)
.cursorUpLine().eraseLine(Ansi.Erase.ALL)
.a(
ConsoleFormat.formatLogMessage(LogLevel.INFO,
ConsoleFormat.formatMessageWithInfoParameters("Progress: {}%", FORMAT.format(newProgress)))
).toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public DownloadDistributionResult execute() throws Exception {
log.info("Downloading version: {}", version);

DistributionManager manager = new DistributionManager(tempDirectory);
ProgressListener listener = new ConsoleProgressListener(context.out);
// todo handle if distribution doesn't exist (http 404)
ProgressListener listener = new ConsoleProgressListener(log);
distributionZipFile = manager.downloadDistribution(version, listener);
} else {
log.info("Distribution zip already downloaded.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
public final class ConsoleFormat {

public enum Level {
public enum Color {

DEFAULT(Ansi.Color.DEFAULT),

Expand All @@ -24,7 +24,7 @@ public enum Level {

public final Ansi.Color color;

Level(Ansi.Color color) {
Color(Ansi.Color color) {
this.color = color;
}
}
Expand All @@ -42,18 +42,18 @@ public static String formatActionStartMessage(Action action) {
}

public static String formatMessageWithErrorParameters(String message, Object... parameters) {
return formatMessageWithParameter(message, parameters, Level.ERROR);
return formatMessageWithParameter(message, parameters, Color.ERROR);
}

public static String formatMessageWithWarningParameters(String message, Object... parameters) {
return formatMessageWithParameter(message, parameters, Level.WARN);
return formatMessageWithParameter(message, parameters, Color.WARN);
}

public static String formatMessageWithInfoParameters(String message, Object... parameters) {
return formatMessageWithParameter(message, parameters, Level.INFO);
return formatMessageWithParameter(message, parameters, Color.INFO);
}

public static String formatMessageWithParameter(String message, Object[] parameters, Level level) {
public static String formatMessageWithParameter(String message, Object[] parameters, Color level) {
String[] formatted = new String[parameters.length];
for (int i = 0; i < parameters.length; i++) {
formatted[i] = Ansi.ansi().fgBright(level.color).a(parameters[i]).reset().toString();
Expand All @@ -62,6 +62,14 @@ public static String formatMessageWithParameter(String message, Object[] paramet
return NinjaUtils.printFormatted(message, formatted);
}

public static String formatSuccessMessage(String message) {
return formatMessage(message, Color.SUCCESS);
}

public static String formatMessage(String message, Color color) {
return Ansi.ansi().fgBright(color.color).a(message).reset().toString();
}

public static String formatLogMessage(LogLevel level, String msg) {
return Ansi.ansi()
.reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static void testJANSI() throws Exception {
// }
// System.out.println(Ansi.ansi().fgRed().a("Complete").reset());

for (ConsoleFormat.Level level : ConsoleFormat.Level.values()) {
for (ConsoleFormat.Color level : ConsoleFormat.Color.values()) {
System.out.println(Ansi.ansi().fg(level.color).a(level.name()).reset());
System.out.println(Ansi.ansi().fgBright(level.color).a(level.name() + "-bright").reset());
}
Expand Down

0 comments on commit bcbd280

Please sign in to comment.