Skip to content

Commit

Permalink
Merge branch 'filter'
Browse files Browse the repository at this point in the history
  • Loading branch information
ianparkinson committed Dec 29, 2022
2 parents cd32aa4 + 758f1e3 commit 2c2d1ab
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 10 deletions.
61 changes: 51 additions & 10 deletions src/main/java/com/github/ianparkinson/helog/Helog.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,27 @@ public void setHost(String host) {
"device name (case sensitive).")
public List<String> device;

@Option(names = "--xdevice",
split = ",",
description = "Exclude specific devices, specified using either the numeric id or the full " +
"device name (case sensitive).",
paramLabel = "device")
public List<String> excludeDevice;

@Option(names = "--app",
split = ",",
description = "Writes logs for specific apps, specified using the numeric id. If used with " +
"@|bold log|@, the app name (case sensitive) can also be used.")
public List<String> app;

@Option(names = "--xapp",
split = ",",
description = "Exclude specific apps, specified using the numeric id. If used with " +
"@|bold log|@, the app name (case sensitive) can also be used.",
paramLabel = "app")
public List<String> excludeApp;


@ArgGroup
public Format format = new Format();

Expand Down Expand Up @@ -117,13 +132,27 @@ public Integer call() throws URISyntaxException {
}

private void validateParameters() throws ParameterException {
if (format.raw && device != null) {
throw parameterException("--device cannot be used with --raw");
if (format.raw) {
if (device != null) {
throw parameterException("--device cannot be used with --raw");
}
if (excludeDevice != null) {
throw parameterException("--xdevice cannot be used with --raw");
}
if (app != null) {
throw parameterException("--app cannot be used with --raw");
}
if (excludeApp != null) {
throw parameterException("--xapp cannot be used with --raw");
}
}
if (format.raw && app != null) {
throw parameterException("--app cannot be used with --raw");

if ((device != null || app != null) && (excludeDevice != null || excludeApp != null)) {
throw parameterException("--device or --app cannot be used with --xdevice or --xapp");
}
if (stream == Stream.EVENTS && app != null && !app.stream().allMatch(Strings::isInteger)) {

if (stream == Stream.EVENTS
&& (!stream(app).allMatch(Strings::isInteger) || !stream(excludeApp).allMatch(Strings::isInteger))) {
throw parameterException("Events cannot be filtered by app name. Use the numeric id instead.");
}
}
Expand All @@ -147,11 +176,23 @@ private <T> JsonStreamPrinter<T> createJsonStreamPrinter(JsonStream<T> jsonStrea
}

private <T> Predicate<T> createFilter(JsonStream<T> jsonStream) {
if (isNullOrEmpty(device) && isNullOrEmpty(app)) return e -> true;

Predicate<T> devicePredicate = stream(device).map(jsonStream::device).reduce(e -> false, Predicate::or);
Predicate<T> appPredicate = stream(app).map(jsonStream::app).reduce(e -> false, Predicate::or);
return devicePredicate.or(appPredicate);
if (!(isNullOrEmpty(device) && isNullOrEmpty(app))) {
Predicate<T> devicePredicate = stream(device).map(jsonStream::device).reduce(e -> false, Predicate::or);
Predicate<T> appPredicate = stream(app).map(jsonStream::app).reduce(e -> false, Predicate::or);
return devicePredicate.or(appPredicate);
} else if (!(isNullOrEmpty(excludeDevice) && isNullOrEmpty(excludeApp))) {
Predicate<T> devicePredicate = stream(excludeDevice)
.map(jsonStream::device)
.map(Predicate::not)
.reduce(e -> true, Predicate::and);
Predicate<T> appPredicate = stream(excludeApp)
.map(jsonStream::app)
.map(Predicate::not)
.reduce(e -> true, Predicate::and);
return devicePredicate.and(appPredicate);
} else {
return e -> true;
}
}

private <T> boolean isNullOrEmpty(List<T> list) {
Expand Down
123 changes: 123 additions & 0 deletions src/test/java/com/github/ianparkinson/helog/HelogEventsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,59 @@ public void deviceMultipleIds() {
assertThat(lines[1]).contains("ThirtySix");
}

@Test
public void excludeDeviceByName() {
webServer.content.add("{ \"source\":\"DEVICE\",\"name\":\"switch\",\"displayName\" : \"NoMatch\", " +
"\"value\" : \"NoMatch\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":34,\"hubId\":0," +
"\"installedAppId\":0,\"descriptionText\" : \"null\"}");
webServer.content.add("{ \"source\":\"DEVICE\",\"name\":\"switch\",\"displayName\" : \"Match\", " +
"\"value\" : \"DeviceMatch\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":35,\"hubId\":0," +
"\"installedAppId\":0,\"descriptionText\" : \"null\"}");
webServer.content.add("{ \"source\":\"APP\",\"name\":\"switch\",\"displayName\" : \"AppMatch\", " +
"\"value\" : \"AppMatch\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":0,\"hubId\":0," +
"\"installedAppId\":36,\"descriptionText\" : \"null\"}");
Helog.run("events", webServer.getHostAndPort(), "--xdevice=Match");
String[] lines = splitLines(out.getContent());
assertThat(lines).hasLength(2);
assertThat(lines[0]).contains("NoMatch");
assertThat(lines[1]).contains("AppMatch");
}

@Test
public void excludeDeviceById() {
webServer.content.add("{ \"source\":\"DEVICE\",\"name\":\"switch\",\"displayName\" : \"ThirtyFour\", " +
"\"value\" : \"ThirtyFour\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":34,\"hubId\":0," +
"\"installedAppId\":0,\"descriptionText\" : \"null\"}");
webServer.content.add("{ \"source\":\"DEVICE\",\"name\":\"switch\",\"displayName\" : \"ThirtyFive\", " +
"\"value\" : \"ThirtyFive\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":35,\"hubId\":0," +
"\"installedAppId\":0,\"descriptionText\" : \"null\"}");
webServer.content.add("{ \"source\":\"APP\",\"name\":\"switch\",\"displayName\" : \"ThirtySix\", " +
"\"value\" : \"ThirtySix\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":35,\"hubId\":0," +
"\"installedAppId\":35,\"descriptionText\" : \"null\"}");
Helog.run("events", webServer.getHostAndPort(), "--xdevice=35");
String[] lines = splitLines(out.getContent());
assertThat(lines).hasLength(2);
assertThat(lines[0]).contains("ThirtyFour");
assertThat(lines[1]).contains("ThirtySix");
}

@Test
public void excludeDeviceMultipleIds() {
webServer.content.add("{ \"source\":\"DEVICE\",\"name\":\"switch\",\"displayName\" : \"ThirtyFour\", " +
"\"value\" : \"ThirtyFour\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":34,\"hubId\":0," +
"\"installedAppId\":0,\"descriptionText\" : \"null\"}");
webServer.content.add("{ \"source\":\"DEVICE\",\"name\":\"switch\",\"displayName\" : \"ThirtyFive\", " +
"\"value\" : \"ThirtyFive\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":35,\"hubId\":0," +
"\"installedAppId\":0,\"descriptionText\" : \"null\"}");
webServer.content.add("{ \"source\":\"DEVICE\",\"name\":\"switch\",\"displayName\" : \"ThirtySix\", " +
"\"value\" : \"ThirtySix\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":36,\"hubId\":0," +
"\"installedAppId\":0,\"descriptionText\" : \"null\"}");
Helog.run("events", webServer.getHostAndPort(), "--xdevice=34,36");
String[] lines = splitLines(out.getContent());
assertThat(lines).hasLength(1);
assertThat(lines[0]).contains("ThirtyFive");
}

@Test
public void appMatchesId() {
webServer.content.add("{ \"source\":\"APP\",\"name\":\"switch\",\"displayName\" : \"\", " +
Expand Down Expand Up @@ -149,6 +202,48 @@ public void appDisallowsName() {
assertThat(code).isEqualTo(2);
}

@Test
public void excludeApp() {
webServer.content.add("{ \"source\":\"APP\",\"name\":\"AppNoMatch\",\"displayName\" : \"\", " +
"\"value\" : \"off\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":0,\"hubId\":0," +
"\"installedAppId\":34,\"descriptionText\" : \"null\"}");
webServer.content.add("{ \"source\":\"APP\",\"name\":\"AppMatch\",\"displayName\" : \"\", " +
"\"value\" : \"off\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":0,\"hubId\":0," +
"\"installedAppId\":35,\"descriptionText\" : \"null\"}");
webServer.content.add("{ \"source\":\"DEV\",\"name\":\"DeviceMatch\",\"displayName\" : \"\", " +
"\"value\" : \"off\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":35,\"hubId\":0," +
"\"installedAppId\":35,\"descriptionText\" : \"null\"}");
Helog.run("events", webServer.getHostAndPort(), "--xapp=35");
String[] lines = splitLines(out.getContent());
assertThat(lines).hasLength(2);
assertThat(lines[0]).contains("AppNoMatch");
assertThat(lines[1]).contains("DeviceMatch");
}

@Test
public void excludeAppDisallowsName() {
int code = Helog.run("events", webServer.getHostAndPort(), "--xapp=SomeApp");
assertThat(err.getContent()).startsWith(ERROR_PREFIX);
assertThat(code).isEqualTo(2);
}

@Test
public void excludeAppMultipleIds() {
webServer.content.add("{ \"source\":\"APP\",\"name\":\"ThirtyFour\",\"displayName\" : \"\", " +
"\"value\" : \"off\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":0,\"hubId\":0," +
"\"installedAppId\":34,\"descriptionText\" : \"null\"}");
webServer.content.add("{ \"source\":\"APP\",\"name\":\"ThirtyFive\",\"displayName\" : \"\", " +
"\"value\" : \"off\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":0,\"hubId\":0," +
"\"installedAppId\":35,\"descriptionText\" : \"null\"}");
webServer.content.add("{ \"source\":\"APP\",\"name\":\"ThirtySix\",\"displayName\" : \"\", " +
"\"value\" : \"off\", \"type\" : \"digital\", \"unit\":\"null\",\"deviceId\":0,\"hubId\":0," +
"\"installedAppId\":36,\"descriptionText\" : \"null\"}");
Helog.run("events", webServer.getHostAndPort(), "--xapp=34,36");
String[] lines = splitLines(out.getContent());
assertThat(lines).hasLength(1);
assertThat(lines[0]).contains("ThirtyFive");
}

@Test
public void deviceAndApp() {
webServer.content.add("{ \"source\":\"APP\",\"name\":\"switch\",\"displayName\" : \"\", " +
Expand All @@ -167,6 +262,20 @@ public void deviceAndApp() {
assertThat(lines[1]).contains("ThirtySix");
}

@Test
public void inclusiveAndExclusiveDeviceDisallowed() {
int code = Helog.run("events", webServer.getHostAndPort(), "--device=43", "--xdevice=42");
assertThat(err.getContent()).startsWith(ERROR_PREFIX);
assertThat(code).isEqualTo(2);
}

@Test
public void inclusiveAndExclusiveAppDisallowed() {
int code = Helog.run("events", webServer.getHostAndPort(), "--app=43", "--xapp=42");
assertThat(err.getContent()).startsWith(ERROR_PREFIX);
assertThat(code).isEqualTo(2);
}

@Test
public void writesInCsvFormat() {
webServer.content.add("{ \"source\":\"DEVICE\",\"name\":\"switch\",\"displayName\" : \"Christmas Tree\", " +
Expand All @@ -193,13 +302,27 @@ public void rawDisallowsDevice() {
assertThat(code).isEqualTo(2);
}

@Test
public void rawDisallowsXDevice() {
int code = Helog.run("events", webServer.getHostAndPort(), "--raw", "--xdevice=42");
assertThat(err.getContent()).startsWith(ERROR_PREFIX);
assertThat(code).isEqualTo(2);
}

@Test
public void rawDisallowsApp() {
int code = Helog.run("events", webServer.getHostAndPort(), "--raw", "--app=42");
assertThat(err.getContent()).startsWith(ERROR_PREFIX);
assertThat(code).isEqualTo(2);
}

@Test
public void rawDisallowsXApp() {
int code = Helog.run("events", webServer.getHostAndPort(), "--raw", "--xapp=42");
assertThat(err.getContent()).startsWith(ERROR_PREFIX);
assertThat(code).isEqualTo(2);
}

@Test
public void rawAndCsvMutuallyExclusive() {
int code = Helog.run("events", webServer.getHostAndPort(), "--raw", "--csv");
Expand Down
Loading

0 comments on commit 2c2d1ab

Please sign in to comment.