Skip to content

Commit 9ff544b

Browse files
committed
added limit for dates list
1 parent 8eaa4c0 commit 9ff544b

4 files changed

Lines changed: 57 additions & 8 deletions

File tree

dspace-api/src/main/java/org/dspace/app/healthreport/HealthReport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void internalRun() throws Exception {
143143
return;
144144
}
145145

146-
try (Context context = new Context()){
146+
try (Context context = new Context()) {
147147
context.setCurrentUser(ePersonService.find(context, this.getEpersonIdentifier()));
148148

149149
ReportInfo ri = new ReportInfo(this.forLastNDays);

dspace-api/src/main/java/org/dspace/app/reportdiff/ReportDiff.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.Map;
2222
import java.util.Objects;
2323

24+
import javax.mail.MessagingException;
25+
2426
import com.fasterxml.jackson.databind.JsonNode;
2527
import com.fasterxml.jackson.databind.ObjectMapper;
2628
import com.flipkart.zjsonpatch.JsonDiff;
@@ -39,8 +41,6 @@
3941
import org.dspace.scripts.DSpaceRunnable;
4042
import org.dspace.utils.DSpace;
4143

42-
import javax.mail.MessagingException;
43-
4444
/**
4545
* This class implements a DSpace script that compares two health reports
4646
* and shows the differences between them.
@@ -66,6 +66,12 @@ public class ReportDiff extends DSpaceRunnable<ReportDiffScriptConfiguration> {
6666
*/
6767
private boolean showDates = false;
6868

69+
/**
70+
* `-l`: Limits the number of report entries (dates) displayed when using the --date option.
71+
* Default is -1 (no limit).
72+
*/
73+
private long limit = -1;
74+
6975
/**
7076
* `-c`: Check, perform only specific check by index (0-`getNumberOfChecks()`).
7177
*/
@@ -113,7 +119,16 @@ public void setup() throws ParseException {
113119
}
114120

115121
// `-d`: Dates, show all dates that the report was generated for a specific check type.
116-
showDates = commandLine.hasOption('d');
122+
if (commandLine.hasOption('d')) {
123+
showDates = commandLine.hasOption('d');
124+
try {
125+
limit = Long.parseLong(commandLine.getOptionValue("l"));
126+
} catch (NumberFormatException e) {
127+
handler.logError("Invalid value for -l. Must be a valid number.");
128+
return;
129+
}
130+
}
131+
117132

118133
// `-f`: From, specify the start date for the report.
119134
from = parseDateOption(commandLine.getOptionValue('f'));
@@ -256,9 +271,12 @@ private void displayReportDates() {
256271
try (Context context = new Context()) {
257272
context.setCurrentUser(ePersonService.find(context, getEpersonIdentifier()));
258273
List<ReportResult> allReports = reportResultService.findAll(context);
259-
274+
// Determine how many reports to process, respecting the `limit` if it's within valid range
275+
long limitCount = (limit > 0 && limit < allReports.size()) ? limit : allReports.size();
260276
Map<String, List<DateWithArgs>> reportDatesMap = new HashMap<>();
261-
for (ReportResult report : allReports) {
277+
for (long i = 0; i < limitCount; i++) {
278+
// the newest report is at the end of the list, so we reverse the index
279+
ReportResult report = allReports.get(allReports.size() - 1 - (int) i);
262280
String formattedDate = FORMATTER.format(report.getLastModified()
263281
.toInstant()
264282
.atZone(ZoneId.systemDefault()).toLocalDateTime());
@@ -276,7 +294,7 @@ private void displayReportDates() {
276294
.append(" - ")
277295
.append(dwa.getDate())
278296
.append(" | ")
279-
.append(dwa.getArgs().stripLeading())
297+
.append(dwa.getArgs() != null ? dwa.getArgs().strip() : "")
280298
.append("\n"));
281299
});
282300

dspace-api/src/main/java/org/dspace/app/reportdiff/ReportDiffScriptConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public Options getOptions() {
4343
"otherwise perform default checks).", HealthReport.getNumberOfChecks() - 1));
4444
options.getOption("c").setType(String.class);
4545

46-
options.addOption("d", "dates", false, "Show all report dates");
46+
options.addOption("d", "dates", false,options.addOption("l", "limit", true, "Limit the number of report entries to this value. " +
47+
"If not specified, all entries are shown.");
48+
options.getOption("l").setType(String.class);
4749

4850
options.addOption("f", "from", true,"Report from specific date [YYYY-MM-DD HH:mm:ss.SSS].");
4951
options.getOption("f").setType(String.class);

dspace-api/src/test/java/org/dspace/scripts/ReportDiffIT.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,33 @@ public void testReportDiff() throws Exception {
308308
List<String> infoMessages = handler.getInfoMessages();
309309
assertThat(infoMessages, hasItem(containsString("REPLACE at /checks/0/report/key: \"value1\" -> \"value2\"")));
310310
}
311+
312+
@Test
313+
public void testShowDatesLimit() throws Exception {
314+
context.turnOffAuthorisationSystem();
315+
316+
ReportResult report1 = reportResultService.create(context);
317+
report1.setType("healthcheck");
318+
report1.setValue("{\"checks\":[]}");
319+
report1.setLastModified(new Date(1000));
320+
reportResultService.update(context, report1);
321+
322+
ReportResult report2 = reportResultService.create(context);
323+
report2.setType("healthcheck");
324+
report2.setValue("{\"checks\":[]}");
325+
report2.setLastModified(new Date(2000));
326+
reportResultService.update(context, report2);
327+
328+
context.restoreAuthSystemState();
329+
330+
TestDSpaceRunnableHandler handler = new TestDSpaceRunnableHandler();
331+
String[] args = new String[] { "report-diff", "-d", "-l", "1" };
332+
ScriptLauncher.handleScript(args, ScriptLauncher.getConfig(kernelImpl), handler, kernelImpl);
333+
334+
List<String> infoMessages = handler.getInfoMessages();
335+
assertThat(infoMessages, hasItem(containsString("Report Dates Summary:")));
336+
assertThat(infoMessages, hasItem(containsString("Report Type: healthcheck")));
337+
assertThat(infoMessages, not(hasItem(containsString(formatDate(report1.getLastModified())))));
338+
assertThat(infoMessages, hasItem(containsString(formatDate(report2.getLastModified()))));
339+
}
311340
}

0 commit comments

Comments
 (0)