io.deepsense.neptune.clientlibrary.models.impl.job.OnlineJob 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.models.impl.job;
import com.google.common.base.Preconditions;
import io.deepsense.neptune.apiclient.ApiException;
import io.deepsense.neptune.clientlibrary.exceptions.channels.DuplicatedChannelNameException;
import io.deepsense.neptune.clientlibrary.exceptions.charts.DuplicatedChartNameException;
import io.deepsense.neptune.clientlibrary.models.*;
import io.deepsense.neptune.clientlibrary.models.impl.channels.OnlineChannel;
import io.deepsense.neptune.clientlibrary.models.impl.channels.OnlineImageChannel;
import io.deepsense.neptune.clientlibrary.models.impl.channels.OnlineNumericChannel;
import io.deepsense.neptune.clientlibrary.models.impl.channels.OnlineTextChannel;
import io.deepsense.neptune.clientlibrary.models.impl.channels.ChannelParamsImpl;
import io.deepsense.neptune.clientlibrary.models.impl.charts.OnlineChart;
import io.deepsense.neptune.clientlibrary.models.impl.charts.ChartParamsImpl;
import io.deepsense.neptune.clientlibrary.models.impl.properties.OnlineJobProperties;
import io.deepsense.neptune.clientlibrary.models.impl.tags.OnlineTags;
import io.deepsense.neptune.clientlibrary.services.apimodelconverter.ApiModelConverter;
import io.deepsense.neptune.clientlibrary.services.apiservice.ApiService;
import io.deepsense.neptune.clientlibrary.services.channelvaluesender.ChannelValueSendingProxy;
import io.deepsense.neptune.clientlibrary.utils.exceptions.ApiExceptionWrapper;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Predicate;
import static io.deepsense.neptune.clientlibrary.models.ChannelType.IMAGE;
import static io.deepsense.neptune.clientlibrary.models.ChannelType.NUMERIC;
import static io.deepsense.neptune.clientlibrary.models.ChannelType.TEXT;
public final class OnlineJob implements Job {
private final ApiService apiService;
private final ApiModelConverter apiModelConverter;
private final ChannelValueSendingProxy channelValueSendingProxy;
private final long maxChannelValueSize;
private final UUID id;
private JobState state;
private final List channels = new LinkedList<>();
private final List charts = new LinkedList<>();
private final Tags tags;
private final JobProperties properties;
private final Metric metric;
OnlineJob(
ApiService apiService,
ApiModelConverter apiModelConverter,
ChannelValueSendingProxy channelValueSendingProxy,
long maxChannelValueSize,
UUID id,
JobState state,
Metric metric) {
this.apiService = Preconditions.checkNotNull(apiService);
this.apiModelConverter = Preconditions.checkNotNull(apiModelConverter);
this.channelValueSendingProxy = Preconditions.checkNotNull(channelValueSendingProxy);
this.maxChannelValueSize = maxChannelValueSize;
this.id = Preconditions.checkNotNull(id);
this.state = Preconditions.checkNotNull(state);
this.tags = new OnlineTags(apiService, id);
this.properties = new OnlineJobProperties(apiService, id);
this.metric = metric;
}
@Override
public Channel createNumericChannel(String name, boolean isHistoryPersisted) {
assertChannelCanBeAdded(name);
try {
boolean isMetric = Optional.ofNullable(metric)
.map(metric -> metric.getChannelName().equals(name))
.orElse(false);
ChannelParams channelParams = new ChannelParamsImpl(
name,
NUMERIC,
isHistoryPersisted
);
UUID channelId = registerChannel(channelParams);
OnlineChannel newChannel = new OnlineNumericChannel(
apiModelConverter,
channelValueSendingProxy,
maxChannelValueSize,
channelId,
channelParams,
isMetric);
channels.add(newChannel);
return newChannel;
} catch (ApiException exc) {
throw ApiExceptionWrapper.wrappedApiException(exc);
}
}
@Override
public Channel createNumericChannel(String name) {
return createNumericChannel(name, true);
}
@Override
public Channel createTextChannel(String name, boolean isHistoryPersisted) {
assertChannelCanBeAdded(name);
try {
ChannelParams channelParams = new ChannelParamsImpl(
name,
TEXT,
isHistoryPersisted
);
UUID channelId = registerChannel(channelParams);
OnlineChannel newChannel = new OnlineTextChannel(
apiModelConverter,
channelValueSendingProxy,
maxChannelValueSize,
channelId,
channelParams);
channels.add(newChannel);
return newChannel;
} catch (ApiException exc) {
throw ApiExceptionWrapper.wrappedApiException(exc);
}
}
@Override
public Channel createTextChannel(String name) {
return createTextChannel(name, true);
}
@Override
public Channel createImageChannel(String name) {
assertChannelCanBeAdded(name);
try {
ChannelParams channelParams = new ChannelParamsImpl(
name,
IMAGE,
true
);
UUID channelId = registerChannel(channelParams);
OnlineChannel newChannel = new OnlineImageChannel(
apiModelConverter,
channelValueSendingProxy,
maxChannelValueSize,
channelId,
channelParams);
channels.add(newChannel);
return newChannel;
} catch (ApiException exc) {
throw ApiExceptionWrapper.wrappedApiException(exc);
}
}
@Override
public Chart createChart(String name, Iterable series) {
assertChartCanBeAdded(name);
try {
return registeredChart(new ChartParamsImpl(name, series));
} catch (ApiException exc) {
throw ApiExceptionWrapper.wrappedApiException(exc);
}
}
@Override
public Action registerAction(String name, Function handler) {
throw new UnsupportedOperationException();
}
@Override
public UUID getId() {
return id;
}
@Override
public JobState getJobState() {
return state;
}
@Override
public Tags getTags() {
return tags;
}
@Override
public JobProperties getProperties() {
return properties;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
OnlineJob job = (OnlineJob) o;
return Objects.equals(id, job.id)
&& state == job.state;
}
@Override
public int hashCode() {
return Objects.hash(id, state);
}
private UUID registerChannel(ChannelParams channelParams) throws ApiException {
io.deepsense.neptune.apiclient.model.Channel apiChannel = apiService.createChannel(id, channelParams);
return UUID.fromString(apiChannel.getId());
}
private Chart registeredChart(ChartParamsImpl chartParams) throws ApiException {
io.deepsense.neptune.apiclient.model.Chart apiChart = apiService.createChart(id, chartParams);
Chart newChart = new OnlineChart(
apiService,
id,
UUID.fromString(apiChart.getId()),
chartParams.getName(),
chartParams.getSeries());
charts.add(newChart);
return newChart;
}
private void assertChannelCanBeAdded(String channelName) {
if (channelNameAlreadyExists(channelName)) {
throw new DuplicatedChannelNameException(channelName);
}
}
private void assertChartCanBeAdded(String chartName) {
if (chartNameAlreadyExists(chartName)) {
throw new DuplicatedChartNameException(chartName);
}
}
private boolean channelNameAlreadyExists(String channelName) {
return entityNameAlreadyExists(channels, Channel::getName, channelName);
}
private boolean chartNameAlreadyExists(String chartName) {
return entityNameAlreadyExists(charts, Chart::getName, chartName);
}
private static boolean entityNameAlreadyExists(
Collection entitiesCollection,
Function entityNameGetter,
String newEntityName) {
return entitiesCollection.stream()
.map(entityNameGetter)
.filter(Predicate.isEqual(newEntityName))
.findAny()
.isPresent();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy