Skip to content

Commit

Permalink
feat(#3292): Add code tab to adapter view
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikriemer committed Oct 8, 2024
1 parent 11f8225 commit 8a5d4ec
Show file tree
Hide file tree
Showing 23 changed files with 760 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

public class AdapterEnrichmentRuleGenerator implements AdapterModelGenerator {

public static final String TIMESTAMP_FIELD = "timestamp";

@Override
public void apply(AdapterDescription adapterDescription,
CompactAdapter compactAdapter) throws Exception {
Expand All @@ -33,7 +35,7 @@ public void apply(AdapterDescription adapterDescription,
var timestampRule = new AddTimestampRuleDescription(compactAdapter.enrich().timestamp());
adapterDescription.getRules().add(timestampRule);
adapterDescription.getEventSchema().addEventProperty(
EpProperties.timestampProperty("timestamp")
EpProperties.timestampProperty(TIMESTAMP_FIELD)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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.
*
*/

package org.apache.streampipes.connect.management.compact.generator;

import org.apache.streampipes.connect.shared.preprocessing.convert.ToOriginalSchemaConverter;
import org.apache.streampipes.manager.template.CompactConfigGenerator;
import org.apache.streampipes.model.connect.adapter.AdapterDescription;
import org.apache.streampipes.model.connect.adapter.compact.CompactEventProperty;
import org.apache.streampipes.model.connect.adapter.compact.CreateOptions;
import org.apache.streampipes.model.connect.adapter.compact.EnrichmentConfig;
import org.apache.streampipes.model.connect.adapter.compact.TransformationConfig;
import org.apache.streampipes.model.connect.rules.TransformationRuleDescription;
import org.apache.streampipes.model.connect.rules.schema.RenameRuleDescription;
import org.apache.streampipes.model.connect.rules.value.AddTimestampRuleDescription;
import org.apache.streampipes.model.connect.rules.value.UnitTransformRuleDescription;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CompactAdapterGenerator {

private final AdapterDescription adapterDescription;

public CompactAdapterGenerator(AdapterDescription adapterDescription) {
this.adapterDescription = adapterDescription;
}

public List<Map<String, Object>> getConfig() {
var configs = new ArrayList<Map<String, Object>>();
adapterDescription.getConfig().forEach(c -> {
configs.add(new CompactConfigGenerator(c).toTemplateValue());
});
return configs;
}

public Map<String, CompactEventProperty> getSchema() {
var map = new HashMap<String, CompactEventProperty>();
var originalProperties = new ToOriginalSchemaConverter(
adapterDescription.getEventSchema().getEventProperties()
).getTransformedProperties();
originalProperties
.forEach(ep -> map.put(ep.getRuntimeName(), new CompactEventProperty(
ep.getLabel(),
ep.getDescription(),
ep.getPropertyScope(),
ep.getSemanticType()
)));
return map;
}

public EnrichmentConfig getEnrichmentConfig() {
if (hasTimestampEnrichmentRule()) {
return new EnrichmentConfig(AdapterEnrichmentRuleGenerator.TIMESTAMP_FIELD);
} else {
return null;
}
}

public TransformationConfig getTransformationConfig() {
var renameRules = new HashMap<String, String>();
var unitTransformRules = new HashMap<String, String>();
if (hasTransformationRule()) {
if (hasRule(RenameRuleDescription.class)) {
var rules = getRules(RenameRuleDescription.class);
rules.forEach(rule -> {
renameRules.put(rule.getOldRuntimeKey(), rule.getNewRuntimeKey());
});
} else if (hasRule(UnitTransformRuleDescription.class)) {
var rules = getRules(UnitTransformRuleDescription.class);
rules.forEach(rule -> {
unitTransformRules.put(rule.getRuntimeKey(), rule.getToUnitRessourceURL());
});
}
}
return new TransformationConfig(renameRules, unitTransformRules);
}

public CreateOptions getCreateOptions() {
return new CreateOptions(
true,
true
);
}

private boolean hasTimestampEnrichmentRule() {
return hasRule(AddTimestampRuleDescription.class);
}

private boolean hasTransformationRule() {
return adapterDescription.getRules().stream()
.anyMatch(r -> hasRule(RenameRuleDescription.class) || hasRule(UnitTransformRuleDescription.class));
}

private boolean hasRule(Class<? extends TransformationRuleDescription> rule) {
return adapterDescription.getRules().stream().anyMatch(r -> r.getClass().equals(rule));
}

private <T extends TransformationRuleDescription> List<T> getRules(Class<T> rule) {
return adapterDescription.getRules()
.stream()
.filter(rule::isInstance)
.map(rule::cast)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.streampipes.connect.management.management;

import org.apache.streampipes.connect.management.compact.generator.AdapterModelGenerator;
import org.apache.streampipes.connect.management.compact.generator.CompactAdapterGenerator;
import org.apache.streampipes.model.connect.adapter.AdapterDescription;
import org.apache.streampipes.model.connect.adapter.compact.CompactAdapter;
import org.apache.streampipes.storage.api.IAdapterStorage;
Expand All @@ -45,6 +46,22 @@ public AdapterDescription convertToAdapterDescription(CompactAdapter compactAdap
return adapterDescription;
}

public CompactAdapter convertToCompactAdapter(AdapterDescription adapterDescription) throws Exception {
var generator = new CompactAdapterGenerator(adapterDescription);

return new CompactAdapter(
adapterDescription.getElementId(),
adapterDescription.getName(),
adapterDescription.getDescription(),
adapterDescription.getAppId(),
generator.getConfig(),
generator.getSchema(),
generator.getEnrichmentConfig(),
generator.getTransformationConfig(),
generator.getCreateOptions()
);
}

public AdapterDescription convertToAdapterDescription(CompactAdapter compactAdapter,
AdapterDescription existingAdapter) throws Exception {
var adapterDescription = convertToAdapterDescription(compactAdapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@

package org.apache.streampipes.model.connect.adapter.compact;

import org.apache.streampipes.model.shared.annotation.TsModel;

import java.util.List;
import java.util.Map;

@TsModel
public record CompactAdapter(
String id,
String name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

package org.apache.streampipes.model.connect.adapter.compact;

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public record CompactEventProperty(
String label,
String description,
Expand Down
Loading

0 comments on commit 8a5d4ec

Please sign in to comment.