io.deepsense.neptune.clientlibrary.services.apimodelconverter.ApiModelConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neptune-client-library Show documentation
Show all versions of neptune-client-library Show documentation
Enables integration with Neptune in your Java code
/**
* Copyright (c) 2016, CodiLime Inc.
*/
package io.deepsense.neptune.clientlibrary.services.apimodelconverter;
import io.deepsense.neptune.apiclient.model.ChannelValues;
import io.deepsense.neptune.apiclient.model.InputImage;
import io.deepsense.neptune.apiclient.model.Point;
import io.deepsense.neptune.apiclient.model.Series;
import io.deepsense.neptune.apiclient.model.SeriesType;
import io.deepsense.neptune.apiclient.model.Y;
import io.deepsense.neptune.clientlibrary.exceptions.common.NeptuneRuntimeException;
import io.deepsense.neptune.clientlibrary.models.Channel;
import io.deepsense.neptune.clientlibrary.models.ChannelType;
import io.deepsense.neptune.clientlibrary.models.Chart;
import io.deepsense.neptune.clientlibrary.models.ChartSeriesCollection;
import io.deepsense.neptune.clientlibrary.models.ChartSeriesType;
import io.deepsense.neptune.clientlibrary.models.JobState;
import io.deepsense.neptune.clientlibrary.models.NeptuneImage;
import io.deepsense.neptune.clientlibrary.models.Parameter;
import io.deepsense.neptune.clientlibrary.models.ParameterType;
import io.deepsense.neptune.clientlibrary.models.ParameterValue;
import io.deepsense.neptune.clientlibrary.models.impl.job.JobBuilder;
import io.deepsense.neptune.clientlibrary.models.impl.parameters.ParameterImpl;
import io.deepsense.neptune.clientlibrary.models.impl.parameters.ParameterValueImpl;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
public class ApiModelConverter {
ApiModelConverter() {
}
public JobBuilder jobBuilderFromApiModel(io.deepsense.neptune.apiclient.model.Job apiJob) {
return new JobBuilder()
.withFieldsFromApi(
UUID.fromString(apiJob.getId()),
jobStateFromApiModel(apiJob.getState()),
new File(apiJob.getDumpDirLocation()).toURI());
}
public JobState jobStateFromApiModel(io.deepsense.neptune.apiclient.model.JobState apiJobState) {
switch (apiJobState) {
case QUEUED:
return JobState.QUEUED;
case EXECUTING:
return JobState.EXECUTING;
case SUCCEEDED:
return JobState.SUCCEEDED;
case FAILED:
return JobState.FAILED;
case ABORTED:
return JobState.ABORTED;
default:
throw new NeptuneRuntimeException("Not supported API JobState: " + apiJobState);
}
}
public io.deepsense.neptune.apiclient.model.Channel channelToApiModel(Channel channel) {
io.deepsense.neptune.apiclient.model.Channel apiChannel =
new io.deepsense.neptune.apiclient.model.Channel();
apiChannel.setId(channel.getId().toString());
apiChannel.setName(channel.getName());
apiChannel.setChannelType(channelTypeToApiModel(channel.getType()));
apiChannel.setIsHistoryPersisted(channel.isHistoryPersisted());
apiChannel.setIsLastValueExposed(channel.isLastValueExposed());
return apiChannel;
}
public io.deepsense.neptune.apiclient.model.ChannelType channelTypeToApiModel(ChannelType channelType) {
switch (channelType) {
case NUMERIC:
return io.deepsense.neptune.apiclient.model.ChannelType.NUMERIC;
case TEXT:
return io.deepsense.neptune.apiclient.model.ChannelType.TEXT;
case IMAGE:
return io.deepsense.neptune.apiclient.model.ChannelType.IMAGE;
default:
throw new NeptuneRuntimeException("Not supported Neptune ChannelType: " + channelType);
}
}
public ChannelValues numericChannelValueToApiModel(UUID channelId, double x, Double y) {
Point dataPoint = new Point().x(x).y(new Y().numericValue(y));
return createChannelValueModel(channelId, dataPoint);
}
public ChannelValues textChannelValueToApiModel(UUID channelId, double x, String y) {
Point dataPoint = new Point().x(x).y(new Y().textValue(y));
return createChannelValueModel(channelId, dataPoint);
}
public ChannelValues imageChannelValueToApiModel(UUID channelId, double x, NeptuneImage y) {
Point dataPoint = new Point().x(x).y(new Y().inputImageValue(neptuneImageToApiModel(y)));
return createChannelValueModel(channelId, dataPoint);
}
public InputImage neptuneImageToApiModel(NeptuneImage neptuneImage) {
return new InputImage()
.name(neptuneImage.getName())
.description(neptuneImage.getDescription())
.data(imageToBase64Png(neptuneImage.getData()));
}
public io.deepsense.neptune.apiclient.model.Chart chartToApiModel(Chart chart) {
return new io.deepsense.neptune.apiclient.model.Chart()
.id(chart.getId().toString())
.name(chart.getName())
.series(multipleChartSeriesToApiModel(chart.getSeries()));
}
public List multipleChartSeriesToApiModel(ChartSeriesCollection chartSeriesCollection) {
return chartSeriesCollection.stream()
.map(elem -> singleChartSeriesToApiModel(elem.getName(), elem.getChannel(), elem.getType()))
.collect(Collectors.toList());
}
private Series singleChartSeriesToApiModel(String seriesName, Channel channel, ChartSeriesType type) {
return new Series()
.channelId(channel.getId().toString())
.label(seriesName)
.seriesType(SeriesType.valueOf(type.name()));
}
public ParameterType parameterTypeFromApi(
io.deepsense.neptune.apiclient.model.Parameter.ParameterTypeEnum apiParameterType) {
switch (apiParameterType) {
case INT:
return ParameterType.INT;
case DOUBLE:
return ParameterType.DOUBLE;
case STRING:
return ParameterType.STRING;
case BOOLEAN:
return ParameterType.BOOLEAN;
default:
throw new RuntimeException("Not supported API ParameterTypeEnum: " + apiParameterType);
}
}
public Parameter parameterFromApiModel(
io.deepsense.neptune.apiclient.model.Parameter apiParameter,
io.deepsense.neptune.apiclient.model.ParameterValue apiParameterValue) {
String parameterName = apiParameter.getName();
ParameterType parameterType = parameterTypeFromApi(apiParameter.getParameterType());
return new ParameterImpl(
parameterName,
parameterType,
parameterValueFromApiModel(parameterName, parameterType, apiParameterValue));
}
public ParameterValue parameterValueFromApiModel(
String parameterName,
ParameterType parameterType,
io.deepsense.neptune.apiclient.model.ParameterValue apiParameterValue) {
switch (parameterType) {
case INT:
return ParameterValueImpl.fromInteger(
parameterName,
Optional.ofNullable(apiParameterValue.getValue())
.map(Integer::valueOf)
.orElse(null));
case DOUBLE:
return ParameterValueImpl.fromDouble(
parameterName,
Optional.ofNullable(apiParameterValue.getValue())
.map(Double::valueOf)
.orElse(null));
case STRING:
return ParameterValueImpl.fromString(parameterName, apiParameterValue.getValue());
case BOOLEAN:
return ParameterValueImpl.fromBoolean(
parameterName,
Optional.ofNullable(apiParameterValue.getValue())
.map(Boolean::valueOf)
.orElse(null));
default:
throw new RuntimeException("Not supported Neptune ParameterType: " + parameterType);
}
}
private static String imageToBase64Png(BufferedImage image) {
byte[] imageInPngFormat = imageToPng(image);
return Base64.getEncoder().encodeToString(imageInPngFormat);
}
private static byte[] imageToPng(BufferedImage image) {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(image, "png", byteArrayOutputStream);
byteArrayOutputStream.flush();
return byteArrayOutputStream.toByteArray();
} catch (IOException exc) {
throw new NeptuneRuntimeException("Failed to write an image to a buffer!", exc);
}
}
private static ChannelValues createChannelValueModel(UUID channelId, Point dataPoint) {
return new ChannelValues()
.channelId(channelId.toString())
.values(Collections.singletonList(dataPoint));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy