-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
754 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/uk/gov/justice/framework/tools/command/Report.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package uk.gov.justice.framework.tools.command; | ||
|
||
import static uk.gov.justice.report.ReportGeneratorFactory.CREATED_AT_NAME_TOTAL_REPORT; | ||
|
||
import uk.gov.justice.artemis.manager.connector.MessageData; | ||
import uk.gov.justice.framework.tools.common.command.ShellCommand; | ||
import uk.gov.justice.report.ReportGeneratorFactory; | ||
|
||
import java.util.List; | ||
|
||
import com.beust.jcommander.Parameter; | ||
|
||
public class Report extends AbstractArtemisCommand implements ShellCommand { | ||
|
||
@Parameter(names = "-reportType", description = CREATED_AT_NAME_TOTAL_REPORT) | ||
String reportType; | ||
|
||
@Override | ||
public void run(final String[] args) { | ||
try { | ||
super.setup(); | ||
|
||
final List<MessageData> messageData = artemisConnector.messagesOf("DLQ"); | ||
|
||
final String report = new ReportGeneratorFactory().reportGeneratorFor(reportType).generate(messageData); | ||
|
||
outputPrinter.write(report); | ||
} catch (final Exception exception) { | ||
outputPrinter.writeStackTrace(exception); | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/uk/gov/justice/report/CreatedAtNameTotalReportGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package uk.gov.justice.report; | ||
|
||
import static java.lang.System.lineSeparator; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
import uk.gov.justice.artemis.manager.connector.MessageData; | ||
import uk.gov.justice.services.common.converter.ZonedDateTimes; | ||
import uk.gov.justice.services.messaging.JsonObjects; | ||
|
||
import java.time.LocalDate; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import javax.json.JsonObject; | ||
import javax.json.JsonString; | ||
|
||
public class CreatedAtNameTotalReportGenerator implements ReportGenerator { | ||
|
||
@Override | ||
public String generate(final List<MessageData> messageData) { | ||
|
||
final Map<LocalDate, Map<String, TotalByName>> createdAtNameTotals = new HashMap<>(); | ||
|
||
messageData.forEach(message -> { | ||
final JsonObject msgContent = message.getMsgContent(); | ||
|
||
final Optional<JsonString> nameJson = JsonObjects.getJsonString(msgContent, "_metadata", "name"); | ||
final Optional<JsonString> createdAtJson = JsonObjects.getJsonString(msgContent, "_metadata", "createdAt"); | ||
|
||
if (createdAtJson.isPresent()) { | ||
|
||
final LocalDate createdAtDate = ZonedDateTimes.fromJsonString(createdAtJson.get()).toLocalDate(); | ||
|
||
if (nameJson.isPresent()) { | ||
final String name = nameJson.get().getString(); | ||
|
||
final Map<String, TotalByName> totalsByName = createdAtNameTotals.computeIfAbsent(createdAtDate, key -> new HashMap<>()); | ||
final TotalByName totalByName = totalsByName.computeIfAbsent(name, name1 -> new TotalByName(createdAtDate, name1)); | ||
|
||
totalByName.addOneToTotal(); | ||
} | ||
} | ||
}); | ||
|
||
final String csv = createdAtNameTotals.keySet().stream() | ||
.sorted() | ||
.map(localDate -> createdAtNameTotals.get(localDate).values().stream() | ||
.sorted() | ||
.map(TotalByName::toString) | ||
.collect(joining(lineSeparator()))) | ||
.collect(joining(lineSeparator())); | ||
|
||
return "Created At,Name,Total Messages" | ||
+ lineSeparator() | ||
+ csv | ||
+ lineSeparator() | ||
+ "Total Messages,," + (long) messageData.size(); | ||
} | ||
|
||
private class TotalByName implements Comparable<TotalByName> { | ||
|
||
private final LocalDate createdAt; | ||
private final String name; | ||
private Long totalMessages = 0L; | ||
|
||
TotalByName(final LocalDate createdAt, final String name) { | ||
this.createdAt = createdAt; | ||
this.name = name; | ||
} | ||
|
||
Long getTotalMessages() { | ||
return totalMessages; | ||
} | ||
|
||
void addOneToTotal() { | ||
totalMessages = totalMessages + 1L; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return createdAt + "," + name + "," + totalMessages; | ||
} | ||
|
||
@Override | ||
public int compareTo(final TotalByName other) { | ||
return totalMessages.compareTo(other.getTotalMessages()); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/uk/gov/justice/report/NamesByOriginalDestinationReportGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package uk.gov.justice.report; | ||
|
||
import static java.lang.System.lineSeparator; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
import uk.gov.justice.artemis.manager.connector.MessageData; | ||
import uk.gov.justice.services.messaging.JsonObjects; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
import javax.json.JsonObject; | ||
import javax.json.JsonString; | ||
|
||
public class NamesByOriginalDestinationReportGenerator implements ReportGenerator { | ||
|
||
public String generate(final List<MessageData> messageData) { | ||
|
||
final Map<String, NameByOriginalDestination> messageTypesByOriginalDestination = new HashMap<>(); | ||
|
||
messageData.forEach(message -> { | ||
final JsonObject msgContent = message.getMsgContent(); | ||
|
||
final Optional<JsonString> nameJson = JsonObjects.getJsonString(msgContent, "_metadata", "name"); | ||
final String originalDestination = message.getOriginalDestination(); | ||
|
||
if (nameJson.isPresent()) { | ||
final String name = nameJson.get().getString(); | ||
final NameByOriginalDestination nameByOriginalDestination = messageTypesByOriginalDestination.computeIfAbsent(originalDestination, NameByOriginalDestination::new); | ||
|
||
nameByOriginalDestination.addMessageType(name); | ||
} | ||
}); | ||
|
||
final String csv = messageTypesByOriginalDestination.values().stream() | ||
.sorted() | ||
.map(NameByOriginalDestination::toString) | ||
.collect(joining()); | ||
|
||
return "Original Destination,Name" + lineSeparator() + csv; | ||
} | ||
|
||
private class NameByOriginalDestination implements Comparable<NameByOriginalDestination> { | ||
|
||
private final String originalDestination; | ||
private final Set<String> names = new TreeSet<>(); | ||
|
||
NameByOriginalDestination(final String originalDestination) { | ||
this.originalDestination = originalDestination; | ||
} | ||
|
||
void addMessageType(final String messageType) { | ||
names.add(messageType); | ||
} | ||
|
||
@Override | ||
public int compareTo(final NameByOriginalDestination other) { | ||
return originalDestination.compareTo(other.originalDestination); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
final StringBuilder stringBuilder = new StringBuilder(); | ||
|
||
for (String messageType : names) { | ||
stringBuilder | ||
.append(originalDestination) | ||
.append(",") | ||
.append(messageType) | ||
.append(lineSeparator()); | ||
} | ||
|
||
return stringBuilder.toString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package uk.gov.justice.report; | ||
|
||
import uk.gov.justice.artemis.manager.connector.MessageData; | ||
|
||
import java.util.List; | ||
|
||
public interface ReportGenerator { | ||
|
||
String generate(final List<MessageData> messageData); | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/uk/gov/justice/report/ReportGeneratorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package uk.gov.justice.report; | ||
|
||
import static java.lang.String.format; | ||
|
||
public class ReportGeneratorFactory { | ||
|
||
public static final String TOTALS_BY_NAME_REPORT = "totals-by-name-report"; | ||
public static final String NAMES_BY_ORIGINAL_DESTINATION_REPORT = "names-by-original-destination-report"; | ||
public static final String CREATED_AT_NAME_TOTAL_REPORT = "created-at-name-total-report"; | ||
|
||
public ReportGenerator reportGeneratorFor(final String reportType) { | ||
|
||
if (TOTALS_BY_NAME_REPORT.equals(reportType.toLowerCase())) { | ||
return new TotalsByNameReportGenerator(); | ||
} | ||
|
||
if (NAMES_BY_ORIGINAL_DESTINATION_REPORT.equals(reportType.toLowerCase())) { | ||
return new NamesByOriginalDestinationReportGenerator(); | ||
} | ||
|
||
if (CREATED_AT_NAME_TOTAL_REPORT.equals(reportType.toLowerCase())) { | ||
return new CreatedAtNameTotalReportGenerator(); | ||
} | ||
|
||
throw new ReportGeneratorFactoryException(format("Incorrect report type '%s'. Accepted types are: '%s', '%s', '%s'", | ||
reportType, TOTALS_BY_NAME_REPORT, NAMES_BY_ORIGINAL_DESTINATION_REPORT, CREATED_AT_NAME_TOTAL_REPORT)); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/uk/gov/justice/report/ReportGeneratorFactoryException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package uk.gov.justice.report; | ||
|
||
public class ReportGeneratorFactoryException extends RuntimeException { | ||
|
||
public ReportGeneratorFactoryException(final String message) { | ||
super(message); | ||
} | ||
} |
Oops, something went wrong.