Skip to content

Commit

Permalink
Merge cdfac25 into 4a4dbdd
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Apr 10, 2019
2 parents 4a4dbdd + cdfac25 commit dc01669
Show file tree
Hide file tree
Showing 16 changed files with 754 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## [Unreleased]

### Added
- Generate reports on the messages on the DLQ

## [3.2.0] - 2019-02-11

###Added
### Added
- Output of consumer name so that it's easy to see where the DLQ message was going to

## [3.1.0] - 2018-10-30
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,19 @@ In the examples below it is assumed a configuration file of artemis.config has b

**Note: ReprocessAll uses JMX port to connect to the Artemis broker.**

`java -jar artemis-manager.jar reprocessall @artemis.config
`java -jar artemis-manager.jar reprocessall @artemis.config`

## Reports on DLQ

* Generate report on the contents of the DLQ. There are three different reports available.

**Note: Report uses JMS to connect to the Artemis broker.**

`java -jar artemis-manager.jar browse @artemis.config -report totals-by-name-report`

`java -jar artemis-manager.jar browse @artemis.config -report names-by-original-destination-report`

`java -jar artemis-manager.jar browse @artemis.config -report created-at-name-total-report`

## Chaining Commands

Expand Down
18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<slf4j-version>1.7.10</slf4j-version>
<throwing-function.version>1.3</throwing-function.version>
<jcommander.version>1.48</jcommander.version>
<utilities.version>1.16.2</utilities.version>
<opencsv.version>4.5</opencsv.version>
</properties>

<artifactId>artemis-manager</artifactId>
Expand Down Expand Up @@ -105,6 +107,13 @@
<artifactId>throwing-function</artifactId>
<version>${throwing-function.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice.utils</groupId>
<artifactId>utilities-core</artifactId>
<version>${utilities.version}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -125,6 +134,12 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>${opencsv.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -160,7 +175,8 @@
<configuration>
<classpathDependencyExcludes>
<!-- slf4j-log4j12 is included in artemis-manager.jar so exclude it here -->
<classpathDependencyExclude>org.slf4j:slf4j-log4j12</classpathDependencyExclude>
<classpathDependencyExclude>org.slf4j:slf4j-log4j12
</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</plugin>
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/uk/gov/justice/framework/tools/command/Report.java
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);
}
}
}
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());
}
}
}
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();
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/uk/gov/justice/report/ReportGenerator.java
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 src/main/java/uk/gov/justice/report/ReportGeneratorFactory.java
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));
}
}
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);
}
}
Loading

0 comments on commit dc01669

Please sign in to comment.