Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 1.15.0 [unreleased]

### Features
1. [#191](https://github.com/influxdata/influxdb-client-java/pull/191): Added tail operator to FluxDSL

## 1.14.0 [2020-12-04]

### Features
Expand Down
16 changes: 16 additions & 0 deletions flux-dsl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,22 @@ Flux flux = Flux
.from("telegraf")
.sum();
```

### tail
Tail caps the number of records in output tables to a fixed size [[doc](http://bit.ly/flux-spec#tail)].
- `n` - The maximum number of records to output. [int]
- `offset` - The number of records to skip per table. Default to `0`. [int]
```java
Flux flux = Flux
.from("telegraf")
.tail(5);
```
```java
Flux flux = Flux
.from("telegraf")
.tail(100, 10);
```

### to
The To operation takes data from a stream and writes it to a bucket [[doc](http://bit.ly/flux-spec#to)].
- `bucket` - The bucket to which data will be written. [string]
Expand Down
45 changes: 45 additions & 0 deletions flux-dsl/src/main/java/com/influxdb/query/dsl/Flux.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import com.influxdb.query.dsl.functions.SpreadFlux;
import com.influxdb.query.dsl.functions.StddevFlux;
import com.influxdb.query.dsl.functions.SumFlux;
import com.influxdb.query.dsl.functions.TailFlux;
import com.influxdb.query.dsl.functions.ToBoolFlux;
import com.influxdb.query.dsl.functions.ToDurationFlux;
import com.influxdb.query.dsl.functions.ToFloatFlux;
Expand Down Expand Up @@ -1717,6 +1718,50 @@ public final SumFlux sum(@Nonnull final String column) {
return new SumFlux(this).withColumn(column);
}

/**
* Caps the number of records in output tables.
*
* <h3>The parameters had to be defined by:</h3>
* <ul>
* <li>{@link TailFlux#withN(int)}</li>
* <li>{@link TailFlux#withPropertyNamed(String)}</li>
* <li>{@link TailFlux#withPropertyNamed(String, String)}</li>
* <li>{@link TailFlux#withPropertyValueEscaped(String, String)}</li>
* </ul>
*
* @return {@link TailFlux}
*/
@Nonnull
public final TailFlux tail() {

return new TailFlux(this);
}

/**
* Caps the number of records in output tables.
*
* @param numberOfResults The number of results
* @return {@link TailFlux}
*/
@Nonnull
public final TailFlux tail(final int numberOfResults) {

return new TailFlux(this).withN(numberOfResults);
}

/**
* Caps the number of records in output tables.
*
* @param numberOfResults The number of results
* @param offset The number of records to skip per table.
* @return {@link TailFlux}
*/
@Nonnull
public final TailFlux tail(final int numberOfResults, final int offset) {

return new TailFlux(this).withN(numberOfResults).withOffset(offset);
}

/**
* To operation takes data from a stream and writes it to a bucket.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.influxdb.query.dsl.functions;

import javax.annotation.Nonnull;

import com.influxdb.Arguments;
import com.influxdb.query.dsl.Flux;

/**
* Tail caps the number of records in output tables to a fixed size <i>n</i>.
* <a href="http://bit.ly/flux-spec#tail">See SPEC</a>.
*
* <h3>Options</h3>
* <ul>
* <li><b>n</b> - The maximum number of records to output [int].</li>
* <li><b>offset</b> - The number of records to skip per table [int]. Default to <i>0</i>.</li>
* </ul>
*
* <h3>Example</h3>
* <pre>
* Flux flux = Flux
* .from("telegraf")
* .tail(5);
* </pre>
*
* @author Jakub Bednar (14/12/2020 10:20)
*/
public final class TailFlux extends AbstractParametrizedFlux {
public TailFlux(@Nonnull final Flux flux) {
super(flux);
}

@Nonnull
@Override
protected String operatorName() {
return "tail";
}

/**
* @param numberOfResults The number of results
* @return this
*/
@Nonnull
public TailFlux withN(final int numberOfResults) {

Arguments.checkPositiveNumber(numberOfResults, "Number of results");

this.withPropertyValue("n", numberOfResults);

return this;
}

/**
* @param offset The number of records to skip per table.
* @return this
*/
@Nonnull
public TailFlux withOffset(final int offset) {

Arguments.checkNotNegativeNumber(offset, "The number of records to skip");

this.withPropertyValue("offset", offset);

return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.influxdb.query.dsl.functions;

import java.util.HashMap;

import com.influxdb.query.dsl.Flux;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

/**
* @author Jakub Bednar (14/12/2020 10:24)
*/
@RunWith(JUnitPlatform.class)
class TailFluxTest
{
@Test
void tail() {

Flux flux = Flux
.from("telegraf")
.tail(5);

Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"telegraf\") |> tail(n: 5)");
}

@Test
void tailOffset() {

Flux flux = Flux
.from("telegraf")
.tail(100, 10);

Assertions.assertThat(flux.toString())
.isEqualToIgnoringWhitespace("from(bucket:\"telegraf\") |> tail(n: 100, offset:10)");
}

@Test
void tailOffsetZero() {

Flux flux = Flux
.from("telegraf")
.tail(100, 0);

Assertions.assertThat(flux.toString())
.isEqualToIgnoringWhitespace("from(bucket:\"telegraf\") |> tail(n: 100, offset:0)");
}

@Test
void tailPositive() {
Assertions.assertThatThrownBy(() -> Flux.from("telegraf").tail(-5))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Expecting a positive number for Number of results");
}

@Test
void tailByParameter() {

Flux flux = Flux
.from("telegraf")
.tail()
.withPropertyNamed("n", "tail");

HashMap<String, Object> parameters = new HashMap<>();
parameters.put("tail", 15);

Assertions.assertThat(flux.toString(parameters))
.isEqualToIgnoringWhitespace("from(bucket:\"telegraf\") |> tail(n: 15)");
}

@Test
void tailByParameterMissing() {

Assertions.assertThatThrownBy(() -> Flux.from("telegraf").tail().withPropertyNamed("tail").toString())
.isInstanceOf(IllegalStateException.class)
.hasMessage("The parameter 'tail' is not defined.");
}
}