Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce transform history output #871

Merged
merged 7 commits into from
Aug 23, 2024
Merged
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
44 changes: 44 additions & 0 deletions src/main/java/emissary/core/TransformHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

import static emissary.place.IServiceProviderPlace.SPROUT_KEY;

public class TransformHistory implements Serializable {

private static final long serialVersionUID = -7252497842562281631L;
Expand Down Expand Up @@ -194,6 +198,46 @@ public int size(boolean includeCoordinated) {
return size;
}

public Deque<String> format() {
Deque<String> formattedHistory = new ArrayDeque<>();

String prevDataAndServiceType = "";
for (final History h : this.history) {
String key = h.getKey();
String currentDataAndServiceType = "";
StringBuilder displayStrings = new StringBuilder();

if (key.contains(SPROUT_KEY)) {
displayStrings.append(StringUtils.substringBefore(formattedHistory.removeLast(), ".")).append(".")
.append(KeyManipulator.getServiceType(key)).append(": ");
while (!formattedHistory.isEmpty()) {
String last = formattedHistory.removeLast();
if (last.contains(SPROUT_KEY)) {
formattedHistory.add(last);
break;
}
}
} else {
currentDataAndServiceType = KeyManipulator.getDataType(key) + "." + KeyManipulator.getServiceType(key);
if (currentDataAndServiceType.equals(prevDataAndServiceType)) {
displayStrings.append(formattedHistory.removeLast()).append(", ");
} else {
displayStrings.append(currentDataAndServiceType).append(": ");
}
}

displayStrings.append(KeyManipulator.getServiceClassname(key));
if (CollectionUtils.isNotEmpty(h.getCoordinated())) {
displayStrings
.append(h.getCoordinated().stream().map(KeyManipulator::getServiceClassname).collect(Collectors.joining(", ", "(", ")")));
}

formattedHistory.add(displayStrings.toString());
prevDataAndServiceType = currentDataAndServiceType;
}
return formattedHistory;
}

@Override
public String toString() {
final StringBuilder myOutput = new StringBuilder();
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/emissary/util/PayloadUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class PayloadUtil {
protected static final String REDUCED_HISTORY = "REDUCED_HISTORY";
protected static final String NO_URL = "NO_URL";

protected static boolean compactHistory;

static {
configure();
}
Expand All @@ -43,6 +45,7 @@ protected static void configure() {
Configurator configG = null;
try {
configG = ConfigUtil.getConfigInfo(PayloadUtil.class);
compactHistory = configG.findBooleanEntry("COMPACT_HISTORY", false);
} catch (IOException e) {
logger.error("Cannot open default config file", e);
}
Expand Down Expand Up @@ -129,6 +132,10 @@ public static String getPayloadDisplayString(final IBaseDataObject payload) {
sb.append(" ** reduced transform history **").append("\n").append(" dropOff -> ")
.append(payload.getLastPlaceVisited())
.append("\n");
} else if (compactHistory) {
for (String history : th.format()) {
sb.append(" ").append(history).append("\n");
}
} else {
for (final TransformHistory.History h : th.getHistory()) {
sb.append(" ").append(h.getKey(historyCase.equals(NO_URL))).append("\n");
Expand All @@ -137,6 +144,7 @@ public static String getPayloadDisplayString(final IBaseDataObject payload) {
}
}
}

return sb.toString();
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/emissary/util/PayloadUtil.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@

#NO_URL outputs just the places without URL
#NO_URL = FILE_TYPE

# output the transform history in compacted form
#COMPACT_HISTORY = "true"
57 changes: 57 additions & 0 deletions src/test/java/emissary/core/TransformHistoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.junit.jupiter.api.Test;

import java.util.Deque;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -170,6 +171,62 @@ void testToString() {
assertEquals(expected.toString(), th.toString());
}

@Test
void testFormat() {
TransformHistory th = new TransformHistory();
th.append("UNKNOWN.FILE_PICK_UP.INPUT.http://localhost:8001/FilePickUpPlace$5050");
th.append("KNOWN.COOL_STUFF.TRANSFORM.http://localhost:8001/CoolStuffPlace$5050");
th.append("KNOWN.COOL_STUFF.COORDINATE.http://localhost:8001/CoolStuffPlace$5050");
th.append("KNOWN.ONE_THING.ANALYZE.http://localhost:8001/DoOneThingPlace$5050", true);
th.append("KNOWN.ANOTHER_PLACE.VERIFY.http://localhost:8001/AnotherPlace$1010");
th.append("KNOWN.LAST_PLACE.VERIFY.http://localhost:8001/LastThingPlace$5050");

Deque<String> formatted = th.format();
assertEquals(4, formatted.size());
assertEquals("UNKNOWN.INPUT: FilePickUpPlace", formatted.pop());
assertEquals("KNOWN.TRANSFORM: CoolStuffPlace", formatted.pop());
assertEquals("KNOWN.COORDINATE: CoolStuffPlace(DoOneThingPlace)", formatted.pop());
assertEquals("KNOWN.VERIFY: AnotherPlace, LastThingPlace", formatted.pop());
}

@Test
void testFormatSprout() {
TransformHistory th = new TransformHistory();
th.append("UNKNOWN.FILE_PICK_UP.INPUT.http://localhost:8001/FilePickUpPlace$5050");
th.append("COOL.COOL_STUFF.TRANSFORM.http://localhost:8001/CoolStuffPlace$5050");
th.append("*.*.<SPROUT>.http://localhost:8001/CoolStuffPlace$0");
th.append("KNOWN.COOL_STUFF.COORDINATE.http://localhost:8001/CoolStuffPlace$5050");
th.append("KNOWN.ONE_THING.ANALYZE.http://localhost:8001/DoOneThingPlace$5050", true);
th.append("KNOWN.ANOTHER_PLACE.VERIFY.http://localhost:8001/AnotherPlace$1010");
th.append("KNOWN.LAST_PLACE.VERIFY.http://localhost:8001/LastThingPlace$5050");

Deque<String> formatted = th.format();
assertEquals(3, formatted.size());
assertEquals("COOL.<SPROUT>: CoolStuffPlace", formatted.pop());
assertEquals("KNOWN.COORDINATE: CoolStuffPlace(DoOneThingPlace)", formatted.pop());
assertEquals("KNOWN.VERIFY: AnotherPlace, LastThingPlace", formatted.pop());
}

@Test
void testFormatMultipleSprout() {
TransformHistory th = new TransformHistory();
th.append("UNKNOWN.FILE_PICK_UP.INPUT.http://localhost:8001/FilePickUpPlace$5050");
th.append("KNOWN.COOL_STUFF.TRANSFORM.http://localhost:8001/CoolStuffPlace$5050");
th.append("*.*.<SPROUT>.http://localhost:8001/CoolStuffPlace$0");
th.append("KNOWN.COOL_STUFF.COORDINATE.http://localhost:8001/CoolStuffPlace$5050");
th.append("KNOWN.ONE_THING.ANALYZE.http://localhost:8001/DoOneThingPlace$5050", true);
th.append("WHOA.YET_ANOTHER.TRANSFORM.http://localhost:8001/YetAnotherPlace$5050");
th.append("*.*.<SPROUT>.http://localhost:8001/YetAnotherPlace");
th.append("FINI.ANOTHER_PLACE.VERIFY.http://localhost:8001/AnotherPlace$1010");
th.append("FINI.LAST_PLACE.VERIFY.http://localhost:8001/LastThingPlace$5050");

Deque<String> formatted = th.format();
assertEquals(3, formatted.size());
assertEquals("KNOWN.<SPROUT>: CoolStuffPlace", formatted.pop());
assertEquals("WHOA.<SPROUT>: YetAnotherPlace", formatted.pop());
assertEquals("FINI.VERIFY: AnotherPlace, LastThingPlace", formatted.pop());
}

@Test
void testGetKeyNoUrl() {
String key1 = "UNKNOWN.FILE_PICK_UP.INPUT.http://localhost:8001/FilePickUpPlace$5050";
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/emissary/util/PayloadUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,71 @@ void testIsValidForm() {
}
}

@Test
void testCompactHistory() {
// setup
final String fn = "noMatch";
final IBaseDataObject d = DataObjectFactory.getInstance("abc".getBytes(), fn, Form.UNKNOWN);
d.appendTransformHistory("FOO.PLACE_ONE.FOOPLACE.http://example.com:1234/FooPlace");
d.appendTransformHistory("BAR.PLACE_TWO.BARPLACE.http://example.com:1234/BarPlace");
d.appendTransformHistory("BAR.PLACE_THREE.NONEPLACE.http://example.com:1234/NonePlace", true);
d.setCreationTimestamp(Instant.now());

// test
PayloadUtil.compactHistory = true;
final String answer = PayloadUtil.getPayloadDisplayString(d);
PayloadUtil.compactHistory = false;

// verify
assertTrue(answer.contains("transform history (3)"), "Answer history count is wrong");
assertTrue(answer.contains("FOO.FOOPLACE: FooPlace"), "Answer should have compacted history");
assertTrue(answer.contains("BAR.BARPLACE: BarPlace(NonePlace)"), "Answer should have compacted history");
}

@Test
void testCompactHistoryFormStack() {
// setup
final String fn = "noMatch";
final IBaseDataObject d = DataObjectFactory.getInstance("abc".getBytes(), fn, Form.UNKNOWN);
d.appendTransformHistory("FOO.PLACE_ONE.FOOPLACE.http://example.com:1234/FooPlace");
d.appendTransformHistory("[BAR-UPDATED].PLACE_TWO.BARPLACE.http://example.com:1234/BarPlace");
d.appendTransformHistory("[BAR].PLACE_THREE.NONEPLACE.http://example.com:1234/NonePlace");
d.setCreationTimestamp(Instant.now());

// test
PayloadUtil.compactHistory = true;
final String answer = PayloadUtil.getPayloadDisplayString(d);
PayloadUtil.compactHistory = false;

// verify
assertTrue(answer.contains("transform history (3)"), "Answer history count is wrong");
assertTrue(answer.contains("FOO.FOOPLACE: FooPlace"), "Answer should have compacted history");
assertTrue(answer.contains("[BAR-UPDATED].BARPLACE: BarPlace"), "Answer should have compacted history");
assertTrue(answer.contains("[BAR].NONEPLACE: NonePlace"), "Answer should have compacted history");
}

@Test
void testCompactHistorySprout() {
// setup
final String fn = "noMatch";
final IBaseDataObject d = DataObjectFactory.getInstance("abc".getBytes(), fn, Form.UNKNOWN);
d.appendTransformHistory("FOO.PLACE_ONE.FOOPLACE.http://example.com:1234/FooPlace");
d.appendTransformHistory("*.*.<SPROUT>.http://example.com:1234/FooPlace");
d.appendTransformHistory("BAR.PLACE_TWO.BARPLACE.http://example.com:1234/BarPlace");
d.appendTransformHistory("BAR.PLACE_THREE.NONEPLACE.http://example.com:1234/NonePlace", true);
d.setCreationTimestamp(Instant.now());

// test
PayloadUtil.compactHistory = true;
final String answer = PayloadUtil.getPayloadDisplayString(d);
PayloadUtil.compactHistory = false;

// verify
assertTrue(answer.contains("transform history (4)"), "Answer history count is wrong");
assertTrue(answer.contains("FOO.<SPROUT>: FooPlace"), "Answer should have compacted history");
assertTrue(answer.contains("BAR.BARPLACE: BarPlace(NonePlace)"), "Answer should have compacted history");
}

/**
* Compares the form to a set of valid characters
* <p>
Expand Down