Repository: apache/streampipes
Pull request #2762 (2024-04-23 09:39:00)

refactor: convert param classes into records
<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~    http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  ~
  -->
  
  <!--
Thanks for contributing! Here are some tips you can follow to help us incorporate your contribution quickly and easily:
1. If this is your first time, please read our contributor guidelines:
    - https://streampipes.apache.org/getinvolved.html
    - https://cwiki.apache.org/confluence/display/STREAMPIPES/Getting+Started
2. Make sure the PR title is formatted like: `[#<GitHub issue id>] PR title ...`
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., `[WIP][#<GitHub issue id>] PR title ...`.
4. Please write your PR title to summarize what this PR proposes/fixes.
5. Link the PR to the corresponding GitHub issue (if present) in the `Development` section in the right menu bar. 
6. Be sure to keep the PR description updated to reflect all changes.
7. If possible, provide a concise example to reproduce the issue for a faster review.
8. Make sure tests pass via `mvn clean install`.
9. (Optional) If the contribution is large, please file an Apache ICLA
    - http://apache.org/licenses/icla.pdf
-->

### Purpose
<!--
Please clarify what changes you are proposing and describe how those changes will address the issue.
Furthermore, describe potential consequences the changes might have.
-->

### Remarks
<!--
Is there anything left we need to pay attention on?
Are there some references that might be important? E.g. links to Confluence, or discussions
on the mailing list or GitHub.
-->
PR introduces (a) breaking change(s): no

PR introduces (a) deprecation(s): no



Patch:
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/influx/DataExplorerInfluxQueryExecutor.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/influx/DataExplorerInfluxQueryExecutor.java
index dbb8a9a0b2..0a1061ac0d 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/influx/DataExplorerInfluxQueryExecutor.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/influx/DataExplorerInfluxQueryExecutor.java
@@ -114,12 +114,12 @@ public class DataExplorerInfluxQueryExecutor extends DataExplorerQueryExecutor<Q
 
   @Override
   protected Query makeDeleteQuery(DeleteQueryParams params) {
-    String query = "DELETE FROM \"" + params.getMeasurementId() + "\"";
-    if (params.isTimeRestricted()) {
+    String query = "DELETE FROM \"" + params.measurementId() + "\"";
+    if (params.timeRestricted()) {
       query += "WHERE time > "
-          + params.getStartTime() * 1000000
+          + params.startTime() * 1000000
           + " AND time < "
-          + params.getEndTime() * 1000000;
+          + params.endTime() * 1000000;
     }
     return new Query(query, getDatabaseName());
   }
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/influx/DataLakeInfluxQueryBuilder.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/influx/DataLakeInfluxQueryBuilder.java
index 8c968ca885..abed48684e 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/influx/DataLakeInfluxQueryBuilder.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/influx/DataLakeInfluxQueryBuilder.java
@@ -177,13 +177,6 @@ public class DataLakeInfluxQueryBuilder implements IDataLakeQueryBuilder<Query>
     return this;
   }
 
-  @Override
-  public DataLakeInfluxQueryBuilder withFilter(NestedClause clause) {
-    this.whereClauses.add(clause);
-
-    return this;
-  }
-
   @Override
   public DataLakeInfluxQueryBuilder withGroupByTime(String timeInterval) {
 
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/DeleteQueryParams.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/DeleteQueryParams.java
index 8bdda43de4..33d3634903 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/DeleteQueryParams.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/DeleteQueryParams.java
@@ -18,43 +18,11 @@
 
 package org.apache.streampipes.dataexplorer.param;
 
-public class DeleteQueryParams {
 
-  private final String measurementId;
+public record DeleteQueryParams(String measurementId, Long startTime, Long endTime, boolean timeRestricted) {
 
-  private long startTime;
-
-  private long endTime;
-
-  private boolean timeRestricted;
-
-  public DeleteQueryParams(String measurementId) {
-    this.measurementId = measurementId;
-    this.timeRestricted = false;
-  }
-
-  public DeleteQueryParams(String measurementId,
-                           Long startTime,
-                           Long endTime) {
-    this(measurementId);
-    this.startTime = startTime;
-    this.endTime = endTime;
-    this.timeRestricted = true;
-  }
-
-  public String getMeasurementId() {
-    return measurementId;
-  }
-
-  public long getStartTime() {
-    return startTime;
-  }
-
-  public long getEndTime() {
-    return endTime;
-  }
-
-  public boolean isTimeRestricted() {
-    return timeRestricted;
+  public DeleteQueryParams(String measurementId, Long startTime, Long endTime) {
+    this(measurementId, startTime != null ? startTime : 0, endTime != null ? endTime : 99999999999999L, true);
   }
 }
+
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/ProvidedRestQueryParamConverter.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/ProvidedRestQueryParamConverter.java
index 9b6f341dd7..186a5eb74c 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/ProvidedRestQueryParamConverter.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/ProvidedRestQueryParamConverter.java
@@ -111,13 +111,9 @@ public class ProvidedRestQueryParamConverter {
   }
 
   public static DeleteQueryParams getDeleteQueryParams(String measurementId,
-                                                                    Long startTime,
-                                                                    Long endTime) {
-    if (startTime != null || endTime != null) {
-      return new DeleteQueryParams(measurementId, startTime, endTime);
-    } else {
-      return new DeleteQueryParams(measurementId);
-    }
+                                                       Long startTime,
+                                                       Long endTime) {
+    return new DeleteQueryParams(measurementId, startTime, endTime);
   }
 
   private static boolean hasTimeParams(ProvidedRestQueryParams params) {
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/SelectQueryParams.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/SelectQueryParams.java
index be5392d8c3..4131d9bf22 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/SelectQueryParams.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/SelectQueryParams.java
@@ -95,7 +95,7 @@ public class SelectQueryParams {
 
   public int getLimit() {
     if (Objects.nonNull(limitParams)) {
-      return limitParams.getLimit();
+      return limitParams.limit();
     } else {
       return Integer.MIN_VALUE;
     }
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/model/LimitClauseParams.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/model/LimitClauseParams.java
index bdff3d070d..f1ecef0e77 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/model/LimitClauseParams.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/model/LimitClauseParams.java
@@ -21,22 +21,12 @@ package org.apache.streampipes.dataexplorer.param.model;
 import org.apache.streampipes.dataexplorer.api.IQueryStatement;
 import org.apache.streampipes.dataexplorer.querybuilder.IDataLakeQueryBuilder;
 
-public class LimitClauseParams implements IQueryStatement {
-
-  private final Integer limit;
-
-  public LimitClauseParams(Integer limit) {
-    this.limit = limit;
-  }
+public record LimitClauseParams(Integer limit) implements IQueryStatement {
 
   public static LimitClauseParams from(Integer limit) {
     return new LimitClauseParams(limit);
   }
 
-  public Integer getLimit() {
-    return limit;
-  }
-
   @Override
   public void buildStatement(IDataLakeQueryBuilder<?> builder) {
     builder.withLimit(limit);
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/model/OffsetClauseParams.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/model/OffsetClauseParams.java
index b8dbc94aea..6419b7a73e 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/model/OffsetClauseParams.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/model/OffsetClauseParams.java
@@ -21,22 +21,12 @@ package org.apache.streampipes.dataexplorer.param.model;
 import org.apache.streampipes.dataexplorer.api.IQueryStatement;
 import org.apache.streampipes.dataexplorer.querybuilder.IDataLakeQueryBuilder;
 
-public class OffsetClauseParams implements IQueryStatement {
-
-  private final Integer offset;
-
-  public OffsetClauseParams(Integer offset) {
-    this.offset = offset;
-  }
+public record OffsetClauseParams(Integer offset) implements IQueryStatement {
 
   public static OffsetClauseParams from(Integer offset) {
     return new OffsetClauseParams(offset);
   }
 
-  public Integer getOffset() {
-    return offset;
-  }
-
   @Override
   public void buildStatement(IDataLakeQueryBuilder<?> builder) {
     builder.withOffset(offset);
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/model/SelectClauseParams.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/model/SelectClauseParams.java
index fe7f8d0f65..02db7227f2 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/model/SelectClauseParams.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/param/model/SelectClauseParams.java
@@ -32,10 +32,6 @@ public class SelectClauseParams implements IQueryStatement {
   private List<SelectColumn> selectedColumnsCountOnly;
   private boolean selectWildcard = false;
 
-  public SelectClauseParams() {
-    this.selectWildcard = true;
-  }
-
   public SelectClauseParams(String columns,
                             boolean countOnly) {
     this.selectWildcard = false;
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/DataExplorerQueryExecutor.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/DataExplorerQueryExecutor.java
index 205046303a..f5e12bb247 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/DataExplorerQueryExecutor.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/DataExplorerQueryExecutor.java
@@ -18,7 +18,6 @@
 
 package org.apache.streampipes.dataexplorer.query;
 
-import org.apache.streampipes.dataexplorer.influx.DataExplorerInfluxQueryExecutor;
 import org.apache.streampipes.dataexplorer.param.DeleteQueryParams;
 import org.apache.streampipes.dataexplorer.param.SelectQueryParams;
 import org.apache.streampipes.model.datalake.DataSeries;
@@ -30,7 +29,7 @@ import org.slf4j.LoggerFactory;
 
 public abstract class DataExplorerQueryExecutor<X, W> {
 
-  private static final Logger LOG = LoggerFactory.getLogger(DataExplorerInfluxQueryExecutor.class);
+  private static final Logger LOG = LoggerFactory.getLogger(DataExplorerQueryExecutor.class);
   protected int maximumAmountOfEvents;
 
   protected boolean appendId = false;
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/ConfiguredJsonOutputWriter.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/ConfiguredJsonOutputWriter.java
index e1311c9e47..e14e866e05 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/ConfiguredJsonOutputWriter.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/ConfiguredJsonOutputWriter.java
@@ -22,7 +22,7 @@ import org.apache.streampipes.dataexplorer.param.ProvidedRestQueryParams;
 import org.apache.streampipes.dataexplorer.query.writer.item.ItemGenerator;
 import org.apache.streampipes.dataexplorer.query.writer.item.JsonItemGenerator;
 
-import com.google.gson.Gson;
+import com.fasterxml.jackson.databind.ObjectMapper;
 
 import java.io.IOException;
 import java.io.OutputStream;
@@ -36,8 +36,7 @@ public class ConfiguredJsonOutputWriter extends ConfiguredOutputWriter {
   private final ItemGenerator itemGenerator;
 
   public ConfiguredJsonOutputWriter() {
-    Gson gson = new Gson();
-    this.itemGenerator = new JsonItemGenerator(gson);
+    this.itemGenerator = new JsonItemGenerator(new ObjectMapper());
   }
 
   @Override
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/item/JsonItemGenerator.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/item/JsonItemGenerator.java
index e135bb8cdf..03d37197f0 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/item/JsonItemGenerator.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/query/writer/item/JsonItemGenerator.java
@@ -19,28 +19,35 @@
 
 package org.apache.streampipes.dataexplorer.query.writer.item;
 
-import com.google.gson.Gson;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class JsonItemGenerator extends ItemGenerator {
 
+  private static final Logger LOG = LoggerFactory.getLogger(JsonItemGenerator.class);
+
   private static final String BEGIN_OBJECT = "{";
   private static final String END_OBJECT = "}";
 
-  private final Gson gson;
+  private final ObjectMapper objectMapper;
 
-  public JsonItemGenerator(Gson gson) {
+  public JsonItemGenerator(ObjectMapper objectMapper) {
     super(COMMA_SEPARATOR);
-    this.gson = gson;
+    this.objectMapper = objectMapper;
   }
 
   @Override
   protected String makeItemString(String key,
                                   Object value) {
-    var stringValue = value != null ? gson.toJson(value) : null;
-    return "\""
-        + key
-        + "\": "
-        + stringValue;
+    String valueJsonString = null;
+    try {
+      valueJsonString = value != null ? this.objectMapper.writeValueAsString(value) : null;
+    } catch (JsonProcessingException e) {
+      LOG.error("Error while converting value to JSON string: {}", e.getMessage());
+    }
+    return "\"%s\": %s".formatted(key, valueJsonString);
   }
 
   @Override
diff --git a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/querybuilder/IDataLakeQueryBuilder.java b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/querybuilder/IDataLakeQueryBuilder.java
index 065c185621..3298a8ed4d 100644
--- a/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/querybuilder/IDataLakeQueryBuilder.java
+++ b/streampipes-data-explorer/src/main/java/org/apache/streampipes/dataexplorer/querybuilder/IDataLakeQueryBuilder.java
@@ -20,8 +20,6 @@ package org.apache.streampipes.dataexplorer.querybuilder;
 
 import org.apache.streampipes.dataexplorer.param.model.AggregationFunction;
 
-import org.influxdb.querybuilder.clauses.NestedClause;
-
 import java.util.List;
 
 public interface IDataLakeQueryBuilder<T> {
@@ -63,8 +61,6 @@ public interface IDataLakeQueryBuilder<T> {
 
   IDataLakeQueryBuilder<T> withInclusiveFilter(List<FilterCondition> filterConditions);
 
-  IDataLakeQueryBuilder<T> withFilter(NestedClause clause);
-
   IDataLakeQueryBuilder<T> withGroupByTime(String timeInterval);
 
   IDataLakeQueryBuilder<T> withGroupByTime(String timeInterval,


Test patch:
diff --git a/streampipes-data-explorer/src/test/java/org/apache/streampipes/dataexplorer/query/writer/item/TestJsonItemGenerator.java b/streampipes-data-explorer/src/test/java/org/apache/streampipes/dataexplorer/query/writer/item/TestJsonItemGenerator.java
index 2b3dcb58b1..786570bc83 100644
--- a/streampipes-data-explorer/src/test/java/org/apache/streampipes/dataexplorer/query/writer/item/TestJsonItemGenerator.java
+++ b/streampipes-data-explorer/src/test/java/org/apache/streampipes/dataexplorer/query/writer/item/TestJsonItemGenerator.java
@@ -18,7 +18,7 @@
 
 package org.apache.streampipes.dataexplorer.query.writer.item;
 
-import com.google.gson.Gson;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -29,7 +29,7 @@ public class TestJsonItemGenerator extends TestItemGenerator {
 
   @Test
   public void testJsonWriter() {
-    var writer = new JsonItemGenerator(new Gson());
+    var writer = new JsonItemGenerator(new ObjectMapper());
 
     String result = writer.createItem(row, columns);