influxdb-client-java icon indicating copy to clipboard operation
influxdb-client-java copied to clipboard

Add support for `elapsed` in the Flux DSL

Open aivinog1 opened this issue 1 year ago • 1 comments
trafficstars

Proposal: The Flux already has the elapsed function. But the Flux DSL is missing this function.

Current behavior: There is no such function in the com.influxdb.query.dsl.Flux class.

Desired behavior: I should be able to use the elapsed function, something like this:

Flux flux = Flux
    .from("telegraf")
    .groupBy("_measurement")
    .elaspsed(); // the default unit is 1s

Alternatives considered: I can't see any alternatives.

Use case: I am using this while I am building queries:


public static class ElapsedFunction extends AbstractParametrizedFlux {

        public ElapsedFunction(final Flux source) {
            super(source);
        }

        @NotNull
        @Override
        protected String operatorName() {
            return "elapsed";
        }

        public ElapsedFunction withUnit(final Long amount,
                                        @Nullable final ChronoUnit unit) {
            this.withPropertyValue("unit", amount, unit);
            return this;
        }
 }

final Flux query = Flux
                    .from(INFLUX_DB_CONTAINER.getBucket())
                    .withBucket(INFLUX_DB_CONTAINER.getBucket())
                    .range().withStart(Instant.now(clock).minus(1, ChronoUnit.HOURS)).withStop(Instant.now(clock).plus(1, ChronoUnit.HOURS))
                    .filter(Restrictions.measurement().equal("measurement"))
                    .filter(Restrictions.tag("someTag").equal("someValue"))
                    .groupBy("_value")
                    .function(ElapsedFunction.class).withUnit(1L, ChronoUnit.NANOS)
                    .group()
                    .quantile().withColumn("elapsed").withQuantile(0.9999f)

aivinog1 avatar Dec 25 '23 06:12 aivinog1

Hi @aivinog1,

thanks for your suggestion.

Is this something you would be willing to help with? All PR is welcome and we will be happy to review your submission.

Regards

bednar avatar Jan 08 '24 08:01 bednar

I think this is an inherent function. Without the FLUX DSL in Java and the possibility of prepare statements, any development with the Java API is quite complicated and time-consuming, as you should not simply pass the string queries to the DB (risk of injection).

UltimateFighter avatar Apr 22 '24 11:04 UltimateFighter