Skip to content

Commit

Permalink
feat(#3292): Show adapter and pipeline configuration in UI (#3293)
Browse files Browse the repository at this point in the history
* feat(#3292): Add code tab to adapter view

* Show pipeline code in UI

* Add header

* Fix checkstyle

* Fix checkstyle

* Fix template visitor
  • Loading branch information
dominikriemer authored Oct 11, 2024
1 parent 11f8225 commit ae6d4b8
Show file tree
Hide file tree
Showing 45 changed files with 1,176 additions and 161 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

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

public record CreateOptions(boolean persist,
boolean start) {
import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public record CreateOptions(Boolean persist,
Boolean start) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

package org.apache.streampipes.model.pipeline.compact;

import com.fasterxml.jackson.annotation.JsonInclude;

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

@JsonInclude(JsonInclude.Include.NON_NULL)
public record CompactPipelineElement(String type,
String ref,
String id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
package org.apache.streampipes.manager.pipeline.compact;

import org.apache.streampipes.manager.matching.PipelineVerificationHandlerV2;
import org.apache.streampipes.manager.pipeline.compact.generation.CompactPipelineConverter;
import org.apache.streampipes.manager.pipeline.compact.generation.PipelineElementConfigurationStep;
import org.apache.streampipes.model.connect.adapter.compact.CreateOptions;
import org.apache.streampipes.model.pipeline.Pipeline;
import org.apache.streampipes.model.pipeline.PipelineModificationResult;
import org.apache.streampipes.model.pipeline.compact.CompactPipeline;
import org.apache.streampipes.storage.api.IPipelineElementDescriptionStorage;

import java.util.UUID;

public class CompactPipelineManagement {

private final IPipelineElementDescriptionStorage storage;
Expand All @@ -42,6 +46,16 @@ public PipelineModificationResult makePipeline(CompactPipeline compactPipeline)
return new PipelineVerificationHandlerV2(pipeline).makeModifiedPipeline();
}

public CompactPipeline convertPipeline(Pipeline pipeline) {
return new CompactPipeline(
pipeline.getElementId() != null ? pipeline.getElementId() : UUID.randomUUID().toString(),
pipeline.getName(),
pipeline.getDescription(),
new CompactPipelineConverter().convert(pipeline),
new CreateOptions(null, false)
);
}

private void applyPipelineBasics(CompactPipeline compactPipeline,
Pipeline pipeline) {
pipeline.setElementId(compactPipeline.id());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.manager.pipeline.compact.generation;

import org.apache.streampipes.manager.template.CompactConfigGenerator;
import org.apache.streampipes.model.pipeline.Pipeline;
import org.apache.streampipes.model.pipeline.compact.CompactPipelineElement;
import org.apache.streampipes.model.staticproperty.StaticProperty;

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

public class CompactPipelineConverter {

public List<CompactPipelineElement> convert(Pipeline pipeline) {
var pipelineElements = new ArrayList<CompactPipelineElement>();

pipeline.getStreams().stream()
.map(stream -> createElement(
PipelineElementConfigurationStep.STREAM_TYPE,
stream.getDom(),
stream.getElementId(),
null,
null))
.forEach(pipelineElements::add);

pipeline.getSepas().stream()
.map(processor -> createElement(
PipelineElementConfigurationStep.PROCESSOR_TYPE,
processor.getDom(),
processor.getAppId(),
processor.getConnectedTo(),
getConfig(processor.getStaticProperties())))
.forEach(pipelineElements::add);

pipeline.getActions().stream()
.map(sink -> createElement(
PipelineElementConfigurationStep.SINK_TYPE,
sink.getDom(),
sink.getAppId(),
sink.getConnectedTo(),
getConfig(sink.getStaticProperties())))
.forEach(pipelineElements::add);

return pipelineElements;
}

private CompactPipelineElement createElement(String type,
String ref,
String elementId,
List<String> connectedTo,
List<Map<String, Object>> config) {
var connections = connectedTo != null ? connectedTo.stream()
.map(this::replaceId)
.toList() : null;
return new CompactPipelineElement(type, replaceId(ref), elementId, connections, config);
}

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

private String replaceId(String id) {
return id.replaceAll(InvocablePipelineElementGenerator.ID_PREFIX, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

public class PipelineElementConfigurationStep implements CompactPipelineGenerator {

private static final String StreamType = "stream";
private static final String ProcessorType = "processor";
private static final String SinkType = "sink";
public static final String STREAM_TYPE = "stream";
public static final String PROCESSOR_TYPE = "processor";
public static final String SINK_TYPE = "sink";

private final IPipelineElementDescriptionStorage storage;

Expand All @@ -42,11 +42,11 @@ public PipelineElementConfigurationStep(IPipelineElementDescriptionStorage stora
public void apply(Pipeline pipeline,
CompactPipeline compactPipeline) throws Exception {
compactPipeline.pipelineElements().forEach(pe -> {
if (pe.type().equalsIgnoreCase(StreamType)) {
if (pe.type().equalsIgnoreCase(STREAM_TYPE)) {
pipeline.getStreams().add(makeStream(pe));
} else if (pe.type().equalsIgnoreCase(ProcessorType)) {
} else if (pe.type().equalsIgnoreCase(PROCESSOR_TYPE)) {
pipeline.getSepas().add(makeProcessor(pe));
} else if (pe.type().equalsIgnoreCase(SinkType)) {
} else if (pe.type().equalsIgnoreCase(SINK_TYPE)) {
pipeline.getActions().add(makeSink(pe));
}
});
Expand Down
Loading

0 comments on commit ae6d4b8

Please sign in to comment.