influxdata / influxdb-client-java

InfluxDB 2 JVM Based Clients
https://influxdata.github.io/influxdb-client-java/
MIT License
430 stars 129 forks source link

Add support for `elapsed` in the Flux DSL #662

Closed aivinog1 closed 4 months ago

aivinog1 commented 8 months ago

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)
bednar commented 8 months ago

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

UltimateFighter commented 4 months ago

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).