Skip to content

Commit

Permalink
Add ImportSummary class and reporting logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Blazej Pindelski committed Aug 20, 2014
1 parent 9679143 commit 8344806
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 3 deletions.
Expand Up @@ -41,6 +41,7 @@
import omero.model.Dataset;
import omero.model.Screen;

import org.apache.commons.lang.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -57,6 +58,9 @@ public class CommandLineImporter {
/** Logger for this class. */
private static Logger log = LoggerFactory.getLogger(CommandLineImporter.class);

/** StopWatch instance **/
private final StopWatch sw = new StopWatch();

/** Name that will be used for usage() */
private static final String APP_NAME = "importer-cli";

Expand Down Expand Up @@ -210,11 +214,11 @@ else if (candidates.size() < 1) {
}

else {
sw.start();
library.addObserver(new LoggingImportMonitor());
// error handler has been configured in constructor from main args
library.addObserver(this.handler);
successful = library.importCandidates(config, candidates);
report();
try {
List<String> paths = new ArrayList<String>();
for (ImportContainer ic : candidates.getContainers()) {
Expand All @@ -231,6 +235,9 @@ else if (candidates.size() < 1) {
} catch (CleanupFailure e) {
log.error("Failed to cleanup {} files", e.getFailedFiles().size());
return 3;
} finally {
sw.stop();
report();
}
}

Expand All @@ -242,8 +249,12 @@ void report() {
boolean report = config.sendReport.get();
boolean files = config.sendFiles.get();
boolean logs = config.sendLogFile.get();
boolean summary = config.summary.get();
if (report) {
handler.update(null, new ImportEvent.DEBUG_SEND(files, logs));
handler.update(null, new ImportEvent.DEBUG_SEND(files, logs));
} else if (summary) {
library.notifyObservers(new ImportEvent.IMPORT_SUMMARY(
sw.getTime(), handler.errorCount()));
}
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2013 University of Dundee & Open Microscopy Environment.
* Copyright (C) 2009-2014 University of Dundee & Open Microscopy Environment.
* All rights reserved.
*
* Use is subject to license terms supplied in LICENSE.txt
Expand All @@ -14,6 +14,7 @@
import omero.model.IObject;
import omero.model.Pixels;

import org.apache.commons.lang.time.DurationFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -27,6 +28,8 @@ public class LoggingImportMonitor implements IObserver
{
private static Logger log = LoggerFactory.getLogger(LoggingImportMonitor.class);

private final ImportSummary importSummary = new ImportSummary();

public void update(IObservable importLibrary, ImportEvent event)
{
if (event instanceof IMPORT_DONE) {
Expand All @@ -36,6 +39,12 @@ public void update(IObservable importLibrary, ImportEvent event)
// send the import results to stdout
// to enable external tools integration
outputGreppableResults(ev);
importSummary.update(ev);
} else if (event instanceof IMPORT_SUMMARY) {
IMPORT_SUMMARY ev = (IMPORT_SUMMARY) event;
importSummary.setTime(ev.importTime);
importSummary.setErrors(ev.errorCount);
importSummary.report();
} else if (event instanceof FILESET_UPLOAD_PREPARATION) {
log.info(event.toLog());
} else if (event instanceof FILESET_UPLOAD_START) {
Expand All @@ -48,6 +57,7 @@ public void update(IObservable importLibrary, ImportEvent event)
} else if (event instanceof FILE_UPLOAD_COMPLETE) {
FILE_UPLOAD_COMPLETE ev = (FILE_UPLOAD_COMPLETE) event;
log.info(event.toLog() + ": " + ev.filename);
importSummary.update(ev);
} else if (event instanceof PROGRESS_EVENT) {
log.info(event.toLog());
} else if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -86,4 +96,93 @@ private void outputGreppableResults(IMPORT_DONE ev) {
}
}
}

/**
* Placeholder used for printing a final import summary.
*/
private class ImportSummary {
private final static String PLATE_CLASS = "PlateI";

private int createdFilesets;
private int createdPlates;
private int errors;
private int importedImages;
private int uploadedFiles;

/** Time taken by import in milliseconds. **/
private long time;

/**
* Updates the state of the object using information held by given even
* type.
* @param event An import event.
*/
public void update(IMPORT_DONE event) {
importedImages += event.pixels.size();
createdFilesets++;
for (IObject object : event.objects) {
if (object.getClass().getSimpleName().equals(PLATE_CLASS)) {
createdPlates++;
}
}
}

/**
* Updates the state of the object using information held by given event
* type.
* @param event An import event.
*/
public void update(FILE_UPLOAD_COMPLETE event) {
uploadedFiles++;
}

/**
* Sets the import error count to the given number.
* @param errors Count.
*/
public void setErrors(int errors) {
this.errors = errors;
}

/**
* Sets the time taken by import to given value.
* @param time The time in milliseconds.
*/
public void setTime(long time) {
this.time = time;
}

/**
* Prints out the import summary to stderr.
*/
public void report() {
StringBuilder sb = new StringBuilder();
sb.append("\n==> Summary\n");
sb.append(entityCountToString("file", uploadedFiles));
sb.append(" uploaded, ");
sb.append(entityCountToString("fileset", createdFilesets));
if (createdPlates != 0) {
sb.append(", ");
sb.append(entityCountToString("plate", createdPlates));
}
sb.append(" created, ");
sb.append(entityCountToString("image", importedImages));
sb.append(" imported, ");
sb.append(entityCountToString("error", errors));
sb.append(String.format(" in %s\n",
DurationFormatUtils.formatDurationHMS(time)));
System.err.print(sb.toString());
}

/**
* Returns a string with a digit and singular/plural form of the
* provided entity name (e.g. "3 apples", "1 car").
* @param name The name of the entity used in the output.
* @param count The number of entity elements.
* @return See above.
*/
private String entityCountToString(String name, int count) {
return String.format("%d %s%s", count, name, count != 1 ? "s" : "");
}
}
}

0 comments on commit 8344806

Please sign in to comment.