This is an automated email from the ASF dual-hosted git repository.

zehnder pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new b829126092 fix(#4133): Add originType for add timestamp rule migration 
(#4134)
b829126092 is described below

commit b82912609281628891146af4d7bc9df826e2401d
Author: Philipp Zehnder <[email protected]>
AuthorDate: Thu Jan 29 08:55:59 2026 +0100

    fix(#4133): Add originType for add timestamp rule migration (#4134)
---
 .../v099/connect/AdapterRuleConverter.java         | 24 +++++++++++++++++++---
 .../v099/MigrateAdaptersToUseScriptTest.java       | 13 +++++++++++-
 2 files changed, 33 insertions(+), 4 deletions(-)

diff --git 
a/streampipes-service-core/src/main/java/org/apache/streampipes/service/core/migrations/v099/connect/AdapterRuleConverter.java
 
b/streampipes-service-core/src/main/java/org/apache/streampipes/service/core/migrations/v099/connect/AdapterRuleConverter.java
index d964ae32ed..b5dbea2426 100644
--- 
a/streampipes-service-core/src/main/java/org/apache/streampipes/service/core/migrations/v099/connect/AdapterRuleConverter.java
+++ 
b/streampipes-service-core/src/main/java/org/apache/streampipes/service/core/migrations/v099/connect/AdapterRuleConverter.java
@@ -38,10 +38,12 @@ import 
org.apache.streampipes.model.connect.rules.value.TimestampTranfsformation
 import 
org.apache.streampipes.model.connect.rules.value.UnitTransformRuleDescription;
 import org.apache.streampipes.model.schema.EventPropertyPrimitive;
 import org.apache.streampipes.model.schema.EventSchema;
+import org.apache.streampipes.vocabulary.XSD;
 
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Locale;
 import java.util.Optional;
 
@@ -61,7 +63,7 @@ public class AdapterRuleConverter {
       handleDeleteRule((DeleteRuleDescription) rule, scriptBuilder);
 
     } else if (rule instanceof AddTimestampRuleDescription) {
-      handleAddTimestampRule((AddTimestampRuleDescription) rule, 
scriptBuilder);
+      handleAddTimestampRule((AddTimestampRuleDescription) rule, adapter, 
scriptBuilder);
 
     } else if (rule instanceof AddValueTransformationRuleDescription) {
       handleAddValueRule((AddValueTransformationRuleDescription) rule, 
scriptBuilder);
@@ -104,8 +106,18 @@ public class AdapterRuleConverter {
     scriptBuilder.appendLine(String.format("delete event['%s'];", 
rule.getRuntimeKey()));
   }
 
-  private void handleAddTimestampRule(AddTimestampRuleDescription rule, 
TransformationScriptBuilder scriptBuilder) {
-    scriptBuilder.appendLine(String.format("event['%s'] = Date.now();", 
rule.getRuntimeKey()));
+  private void handleAddTimestampRule(AddTimestampRuleDescription rule, 
AdapterDescription adapter, TransformationScriptBuilder scriptBuilder) {
+    // Add timestamp to the script
+    scriptBuilder.appendLine(String.format("event['%s'] = new 
Date().getTime();", rule.getRuntimeKey()));
+    if (adapter == null || rule.getRuntimeKey() == null || 
rule.getRuntimeKey().isEmpty()) {
+      return;
+    }
+
+    // add origin type to ensure that timestamp will be transformed to a long
+    findPrimitiveProperty(adapter, rule.getRuntimeKey()).ifPresent(property -> 
{
+      ensureAdditionalMetadata(property);
+      property.getAdditionalMetadata().put("originType", XSD.FLOAT.toString());
+    });
   }
 
   private void handleAddValueRule(AddValueTransformationRuleDescription rule, 
TransformationScriptBuilder scriptBuilder) {
@@ -192,6 +204,12 @@ public class AdapterRuleConverter {
         .findFirst();
   }
 
+  private void ensureAdditionalMetadata(EventPropertyPrimitive property) {
+    if (property.getAdditionalMetadata() == null) {
+      property.setAdditionalMetadata(new HashMap<>());
+    }
+  }
+
   private ReduceEventRateRule 
mapEventRate(EventRateTransformationRuleDescription rule) {
     return new ReduceEventRateRule(rule.getAggregationTimeWindow(), 
rule.getAggregationType());
   }
diff --git 
a/streampipes-service-core/src/test/java/org/apache/streampipes/service/core/migrations/v099/MigrateAdaptersToUseScriptTest.java
 
b/streampipes-service-core/src/test/java/org/apache/streampipes/service/core/migrations/v099/MigrateAdaptersToUseScriptTest.java
index 4d816d9432..a7507ccdf0 100644
--- 
a/streampipes-service-core/src/test/java/org/apache/streampipes/service/core/migrations/v099/MigrateAdaptersToUseScriptTest.java
+++ 
b/streampipes-service-core/src/test/java/org/apache/streampipes/service/core/migrations/v099/MigrateAdaptersToUseScriptTest.java
@@ -36,6 +36,7 @@ import 
org.apache.streampipes.model.schema.EventPropertyPrimitive;
 import org.apache.streampipes.model.schema.EventSchema;
 import 
org.apache.streampipes.service.core.migrations.v099.connect.MigrateAdaptersToUseScript;
 import org.apache.streampipes.storage.api.IAdapterStorage;
+import org.apache.streampipes.vocabulary.XSD;
 
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -44,6 +45,7 @@ import java.io.IOException;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -151,13 +153,22 @@ class MigrateAdaptersToUseScriptTest {
   @Test
   void executeMigration_TransformsAddTimestampRule() throws IOException {
     var adapter = createBaseAdapter(new 
AddTimestampRuleDescription("timestamp"));
+    var timestampProperty = new EventPropertyPrimitive();
+    timestampProperty.setRuntimeName("timestamp");
+    timestampProperty.setAdditionalMetadata(new HashMap<>());
+    
adapter.getDataStream().getEventSchema().setEventProperties(List.of(timestampProperty));
 
     when(mockStorage.findAll()).thenReturn(List.of(adapter));
     migration.executeMigration();
 
     var script = adapter.getTransformationConfig().getScript();
-    assertTrue(script.contains("event['timestamp'] = Date.now();"));
+    assertTrue(script.contains("event['timestamp'] = new Date().getTime();"));
     assertTrue(adapter.getTransformationConfig().isScriptActive());
+    var updatedProperty = (EventPropertyPrimitive) adapter.getDataStream()
+        .getEventSchema()
+        .getEventProperties()
+        .get(0);
+    assertEquals(XSD.FLOAT.toString(), 
updatedProperty.getAdditionalMetadata().get("originType"));
   }
 
   @Test

Reply via email to