Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.mail.MessagingException;
Expand Down Expand Up @@ -59,19 +61,20 @@ public class HealthReport extends DSpaceRunnable<HealthReportScriptConfiguration
private static final LinkedHashMap<String, Check> checks = Report.checks();

/**
* `-i`: Info, show help information.
* `-h`: Help, show help information.
*/
private boolean info = false;
private boolean help = false;

/**
* `-e`: Email, send report to specified email address.
*/
private String[] emails;

/**
* `-c`: Check, perform only specific check by index (0-`getNumberOfChecks()`).
* `-c`: Check, perform only specific checks by index (0-`getNumberOfChecks()`).
* Supports multiple values.
*/
private int specificCheck = -1;
private List<Integer> specificChecks = new ArrayList<>();

/**
* `-f`: For, specify the last N days to consider.
Expand All @@ -80,9 +83,9 @@ public class HealthReport extends DSpaceRunnable<HealthReportScriptConfiguration
private int forLastNDays = configurationService.getIntProperty("healthcheck.last_n_days");

/**
* `-o`: Output, specify a file to save the report.
* `-r`: Report, specify a file to save the report.
*/
private String fileName;
private String reportFile;

@Override
public HealthReportScriptConfiguration getScriptConfiguration() {
Expand All @@ -93,9 +96,9 @@ public HealthReportScriptConfiguration getScriptConfiguration() {
@Override
public void setup() throws ParseException {
ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
// `-i`: Info, show help information.
if (commandLine.hasOption('i')) {
info = true;
// `-h`: Help, show help information.
if (commandLine.hasOption('h')) {
help = true;
return;
}

Expand All @@ -105,40 +108,53 @@ public void setup() throws ParseException {
handler.logInfo("\nReport sent to this email address: " + String.join(", ", emails));
}

// `-c`: Check, perform only specific check by index (0-`getNumberOfChecks()`).
// `-c`: Check, perform only specific checks by index (0-`getNumberOfChecks()`).
// Supports multiple values e.g. -c 0 3 4
if (commandLine.hasOption('c')) {
String checkOption = commandLine.getOptionValue('c');
try {
specificCheck = Integer.parseInt(checkOption);
if (specificCheck < 0 || specificCheck >= getNumberOfChecks()) {
specificCheck = -1;
String[] checkOptions = commandLine.getOptionValues('c');
for (String checkOption : checkOptions) {
try {
int checkIndex = Integer.parseInt(checkOption);
if (checkIndex < 0 || checkIndex >= getNumberOfChecks()) {
handler.logError("Invalid value for check: " + checkOption +
". Must be an integer from 0 to " + (getNumberOfChecks() - 1) + ".");
throw new ParseException("Invalid check index: " + checkOption);
}
specificChecks.add(checkIndex);
} catch (NumberFormatException e) {
handler.logError("Invalid value for check: '" + checkOption +
"'. It has to be an integer number from 0 to " + (getNumberOfChecks() - 1) + ".");
throw new ParseException("Invalid check value: " + checkOption);
}
} catch (NumberFormatException e) {
log.info("Invalid value for check. It has to be a number from the displayed range.");
return;
}
}

// `-f`: For, specify the last N days to consider.
// `-f`: For, specify the last N days to consider. Must be a positive integer.
if (commandLine.hasOption('f')) {
String daysOption = commandLine.getOptionValue('f');
try {
forLastNDays = Integer.parseInt(daysOption);
if (forLastNDays <= 0) {
handler.logError("Invalid value for -f: " + daysOption +
". Must be a positive integer (greater than 0).");
throw new ParseException("Invalid -f value: " + daysOption);
}
} catch (NumberFormatException e) {
log.info("Invalid value for last N days. Argument f has to be a number.");
return;
handler.logError("Invalid value for -f: '" + daysOption +
"'. Must be a positive integer.");
throw new ParseException("Invalid -f value: " + daysOption);
}
}

// `-o`: Output, specify a file to save the report.
if (commandLine.hasOption('o')) {
fileName = commandLine.getOptionValue('o');
// `-r`: Report, specify a file to save the report.
if (commandLine.hasOption('r')) {
reportFile = commandLine.getOptionValue('r');
}
}

@Override
public void internalRun() throws Exception {
if (info) {
if (help) {
printHelp();
return;
}
Expand All @@ -157,7 +173,7 @@ public void internalRun() throws Exception {
JSONArray checksArray = new JSONArray();
for (Map.Entry<String, Check> check_entry : Report.checks().entrySet()) {
++position;
if (specificCheck != -1 && specificCheck != position) {
if (!specificChecks.isEmpty() && !specificChecks.contains(position)) {
continue;
}

Expand Down Expand Up @@ -198,9 +214,9 @@ public void internalRun() throws Exception {
context.commit();

// save output to file
if (fileName != null) {
if (reportFile != null) {
InputStream inputStream = toInputStream(sbReport.toString(), StandardCharsets.UTF_8);
handler.writeFilestream(context, fileName, inputStream, "export");
handler.writeFilestream(context, reportFile, inputStream, "export");

context.restoreAuthSystemState();

Expand All @@ -226,14 +242,15 @@ public void internalRun() throws Exception {

@Override
public void printHelp() {
handler.logInfo("\n\nINFORMATION\nThis process creates a health report of your DSpace.\n" +
handler.logInfo("\n\nHELP\nThis process creates a health report of your DSpace.\n" +
"You can choose from these available options:\n" +
" -i, --info Show help information\n" +
" -h, --help Show help information\n" +
" -e, --email Send report to specified email address\n" +
" -c, --check Perform only specific check by index (0-" + (getNumberOfChecks() - 1) + ")\n" +
" -f, --for Specify the last N days to consider\n" +
" -o, --output Specify a file to save the report\n\n" +
"If you want to execute only one check using -c, use check index:\n" + checksNamesToString() + "\n"
" -c, --check Perform specific check(s) by index (0-" + (getNumberOfChecks() - 1) +
"). Accepts multiple space-separated values, e.g. -c 0 3 4\n" +
" -f, --for Specify the last N days to consider (positive integer)\n" +
" -r, --report Specify a file to save the report\n\n" +
"Available checks:\n" + checksNamesToString() + "\n"
);
}

Expand All @@ -242,13 +259,12 @@ public void printHelp() {
* This method is used to print the options used for the report.
*/
private String printCommandlineOptions() {
// Return key-value pairs of options
StringBuilder options = new StringBuilder();
for (Option option : commandLine.getOptions()) {
String key = option.getOpt();
String value = commandLine.getOptionValue(key);
if (value != null) {
options.append(String.format(" -%s: %s\n", key, value));
String[] values = commandLine.getOptionValues(key);
if (values != null) {
options.append(String.format(" -%s: %s\n", key, String.join(", ", values)));
} else {
options.append(String.format(" -%s\n", key));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ public void setDspaceRunnableClass(Class<T> dspaceRunnableClass) {
public Options getOptions() {
if (options == null) {
Options options = new Options();
options.addOption("i", "info", false,
options.addOption("h", "help", false,
"Show help information.");
options.addOption("e", "email", true,
"Send report to this email address.");
options.getOption("e").setType(String.class);
options.addOption("c", "check", true,
String.format("Perform only specific check (use index from 0 to %d, " +
"otherwise perform default checks).", HealthReport.getNumberOfChecks() - 1));
String.format("Perform specific check(s) by index (0 to %d). " +
"Accepts multiple space-separated values, e.g. -c 0 3 4.",
HealthReport.getNumberOfChecks() - 1));
options.getOption("c").setType(String.class);
options.getOption("c").setArgs(org.apache.commons.cli.Option.UNLIMITED_VALUES);
options.addOption("f", "for", true,
"Report for last N days. Used only in general information for now.");
"Report for last N days (positive integer). Used only in general information for now.");
options.getOption("f").setType(String.class);
options.addOption("o", "output", true,
"Save report to the file.");
options.addOption("r", "report", true,
"Specify the report file to store the output.");

super.options = options;
}
Expand Down
Loading