Skip to content

Commit

Permalink
fix: #75 added exists operator support (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhajek authored and bednar committed Jan 3, 2020
1 parent 48f0bce commit 921d5ae
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions flux-dsl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ Supported Record restrictions:
- `less`
- `greater`
- `greater`
- `exists`
- `lessOrEqual`
- `greaterOrEqual`
- `custom` - the custom restriction by `Restrictions.value().custom(15L, "=~")`
Expand All @@ -211,6 +212,7 @@ Supported Record restrictions:
Restrictions restrictions = Restrictions.and(
Restrictions.measurement().equal("mem"),
Restrictions.field().equal("usage_system"),
Restrictions.value().exists(),
Restrictions.tag("service").equal("app-server")
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ public Restrictions custom(@Nonnull final Object value, @Nonnull final String op
return new OperatorRestrictions(fieldName, value, operator);
}

/**
* Check if an record contains a key or if that key’s value is null
* @return restriction
*/
@Nonnull
public Restrictions exists() {
return new ExistsRestrictions(fieldName);
}

private final class ExistsRestrictions extends Restrictions {
private final String fieldName;

public ExistsRestrictions(@Nonnull final String fieldName) {
this.fieldName = fieldName;
}

@Override
public String toString() {
return "exists r[\"" + fieldName + "\"]";
}
}

private final class OperatorRestrictions extends Restrictions {
private final String fieldName;
private final Object fieldValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void filterExample() {
Restrictions restriction = Restrictions.and(
Restrictions.measurement().equal("mem"),
Restrictions.field().equal("usage_system"),
Restrictions.value().exists(),
Restrictions.tag("service").equal("app-server")
);

Expand All @@ -74,7 +75,7 @@ void filterExample() {
.range(-4L, ChronoUnit.HOURS)
.count();

String expected = "from(bucket:\"telegraf\") |> filter(fn: (r) => (r[\"_measurement\"] == \"mem\" and r[\"_field\"] == \"usage_system\" and r[\"service\"] == \"app-server\")) |> "
String expected = "from(bucket:\"telegraf\") |> filter(fn: (r) => (r[\"_measurement\"] == \"mem\" and r[\"_field\"] == \"usage_system\" and exists r[\"_value\"] and r[\"service\"] == \"app-server\")) |> "
+ "range(start:-4h) |> count()";

Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ void greaterOrEqual() {

Assertions.assertThat(restrictions.toString()).isEqualTo("r[\"_value\"] >= 10");
}

@Test
void exists() {

Restrictions restrictions = Restrictions.value().exists();

Assertions.assertThat(restrictions.toString()).isEqualTo("exists r[\"_value\"]");
}
}

0 comments on commit 921d5ae

Please sign in to comment.