All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.deepsense.neptune.clientlibrary.models.impl.context.NeptuneContextBuilderFactory Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
/**
 * Copyright (c) 2016, CodiLime Inc.
 */

package io.deepsense.neptune.clientlibrary.models.impl.context;

import com.google.common.base.Preconditions;
import io.deepsense.neptune.apiclient.ApiException;
import io.deepsense.neptune.clientlibrary.config.internal.ChannelValueBufferConfiguration;
import io.deepsense.neptune.clientlibrary.config.internal.FileBasedInternalConfiguration;
import io.deepsense.neptune.clientlibrary.config.internal.InternalConfiguration;
import io.deepsense.neptune.clientlibrary.config.user.EnvironmentUserConfiguration;
import io.deepsense.neptune.clientlibrary.config.user.FileBasedUserConfiguration;
import io.deepsense.neptune.clientlibrary.config.user.MultiSourcedUserConfiguration;
import io.deepsense.neptune.clientlibrary.config.user.UserConfiguration;
import io.deepsense.neptune.clientlibrary.exceptions.common.NeptuneRuntimeException;
import io.deepsense.neptune.clientlibrary.models.JobParameters;
import io.deepsense.neptune.clientlibrary.models.JobProperties;
import io.deepsense.neptune.clientlibrary.models.Tags;
import io.deepsense.neptune.clientlibrary.models.impl.job.JobBuilder;
import io.deepsense.neptune.clientlibrary.models.impl.job.JobBuilderFactory;
import io.deepsense.neptune.clientlibrary.models.impl.parameters.OfflineJobParameters;
import io.deepsense.neptune.clientlibrary.models.impl.parameters.OnlineJobParametersFactory;
import io.deepsense.neptune.clientlibrary.models.impl.properties.OfflineJobProperties;
import io.deepsense.neptune.clientlibrary.models.impl.tags.OfflineTags;
import io.deepsense.neptune.clientlibrary.parsers.jobargumentsparser.JobArguments;
import io.deepsense.neptune.clientlibrary.parsers.jobargumentsparser.OfflineJobArguments;
import io.deepsense.neptune.clientlibrary.parsers.jobargumentsparser.OnlineJobArguments;
import io.deepsense.neptune.clientlibrary.services.apimodelconverter.ApiModelConverter;
import io.deepsense.neptune.clientlibrary.services.apimodelconverter.ApiModelConverterFactory;
import io.deepsense.neptune.clientlibrary.services.apiservice.ApiService;
import io.deepsense.neptune.clientlibrary.services.apiservice.ApiServiceBuilder;
import io.deepsense.neptune.clientlibrary.services.apiservice.retries.RequestRetryStrategyFactory;
import io.deepsense.neptune.clientlibrary.services.channelvaluesender.BufferedChannelValueSender;
import io.deepsense.neptune.clientlibrary.services.channelvaluesender.ChannelValueSender;
import io.deepsense.neptune.clientlibrary.services.channelvaluesender.ChannelValueSendingStrategy;
import io.deepsense.neptune.clientlibrary.services.channelvaluesender.TimedChannelValueSendingStrategy;
import io.deepsense.neptune.clientlibrary.services.serverconfigurationprovider.ServerConfigurationProvider;
import io.deepsense.neptune.clientlibrary.threads.ChannelValueSendingThread;
import io.deepsense.neptune.clientlibrary.utils.ThreadBrowser;
import org.apache.commons.configuration.ConfigurationException;

import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

public class NeptuneContextBuilderFactory {

    private InternalConfiguration internalConfiguration;

    private UserConfiguration userConfiguration;

    private ApiModelConverterFactory apiModelConverterFactory;

    private RequestRetryStrategyFactory requestRetryStrategyFactory;

    private ApiServiceBuilder apiServiceBuilder;

    private ApiModelConverter apiModelConverter;

    private ApiService apiService;

    private ServerConfigurationProvider serverConfigurationProvider;

    private ChannelValueSendingStrategy channelValueSendingStrategy;

    private ChannelValueSender channelValueSender;

    private ChannelValueSendingThread channelValueSendingThread;

    private io.deepsense.neptune.apiclient.model.Job apiJobModel;

    private io.deepsense.neptune.apiclient.model.Experiment apiExperimentModel;

    private JobBuilder jobBuilder;

    private OnlineJobParametersFactory onlineJobParametersFactory;

    private URI storageUrl;

    public NeptuneContextBuilder create(JobArguments jobArguments) throws ApiException {
        return create(jobArguments, new FileBasedInternalConfiguration(),  OfflineJobParameters.emptyParameters());
    }

    public NeptuneContextBuilder create(JobArguments jobArguments, JobParameters offlineJobParameters)
            throws ApiException {
        return create(jobArguments, new FileBasedInternalConfiguration(), offlineJobParameters);
    }

    public NeptuneContextBuilder create(JobArguments jobArguments, InternalConfiguration internalConfiguration)
            throws ApiException {
        return create(jobArguments, internalConfiguration, OfflineJobParameters.emptyParameters());
    }

    public NeptuneContextBuilder create(JobArguments jobArguments,
                                        InternalConfiguration internalConfiguration,
                                        JobParameters offlineJobParameters) throws ApiException {

        Preconditions.checkNotNull(jobArguments);
        Preconditions.checkNotNull(internalConfiguration);
        this.internalConfiguration = internalConfiguration;

        if (jobArguments.isOffline()) {
            System.err.println("neptune: Executing in Offline Mode.");
            return createNeptuneOfflineContextBuilder((OfflineJobArguments) jobArguments, offlineJobParameters);
        } else {
            logIgnoredOfflineJobParameters(offlineJobParameters);
            return createNeptuneOnlineContextBuilder((OnlineJobArguments) jobArguments);
        }
    }

    private void logIgnoredOfflineJobParameters(JobParameters offlineJobParameters) {
        if (!offlineJobParameters.isEmpty()) {
            System.err.println("neptune: Ignoring job parameters passed to NeptuneContextFactory.createContext.");
        }
    }

    private OfflineJobParameters mergeOfflineJobParametersWithArguments(JobParameters offlineJobParameters,
                                                                 OfflineJobArguments offlineJobArguments) {
        OfflineJobParameters mergedParameters = new OfflineJobParameters();
        mergedParameters.putAll(offlineJobParameters);
        mergedParameters.putAll(offlineJobArguments.getAdditionalJobParameters());
        return mergedParameters;
    }

    private NeptuneContextBuilder createNeptuneOnlineContextBuilder(OnlineJobArguments jobArguments)
            throws ApiException {

        UUID jobId = jobArguments.getJobId();
        URI restApiUrl = jobArguments.getRestApiUrl();

        buildUserConfiguration();
        buildRequestRetryStrategyFactory();
        buildServiceFactories(restApiUrl);
        buildServicesFromFactories();
        buildServerConfigurationProvider();
        buildChannelValueSendingStrategy();
        buildChannelValueSender(jobId);
        buildChannelValueSendingThread();
        buildApiJobModel(jobId);
        buildApiExperimentModel(apiJobModel);
        buildJobBuilder();
        buildJobParametersBuilder();
        buildStorageUrl();

        return new NeptuneOnlineContextBuilder()
                .withApiModelConverterFactory(apiModelConverterFactory)
                .withApiServiceBuilder(apiServiceBuilder)
                .withChannelValueSendingThread(channelValueSendingThread)
                .withJobBuilder(jobBuilder)
                .withJobParametersFactory(onlineJobParametersFactory)
                .withStorageUrl(storageUrl);
    }

    private NeptuneContextBuilder createNeptuneOfflineContextBuilder(OfflineJobArguments jobArguments,
                                                                     JobParameters jobParameters) {
        JobParameters mergedJobParameters = mergeOfflineJobParametersWithArguments(jobParameters, jobArguments);
        UUID jobId = jobArguments.getJobId();
        JobProperties jobProperties = new OfflineJobProperties();
        Tags tags = new OfflineTags();
        return new NeptuneOfflineContextBuilder(jobId, mergedJobParameters, jobProperties, tags);
    }

    private void buildUserConfiguration() {
        final Path configurationFilePath = Paths.get(System.getProperty("user.home"), ".neptune.cfg");

        try {
            userConfiguration = MultiSourcedUserConfiguration.create(
                    FileBasedUserConfiguration.create(configurationFilePath.toFile()),
                    new EnvironmentUserConfiguration());
        } catch (ConfigurationException exc) {
            throw new NeptuneRuntimeException("Configuration file " + configurationFilePath + "is invalid!", exc);
        }
    }

    private void buildRequestRetryStrategyFactory() {
        requestRetryStrategyFactory = new RequestRetryStrategyFactory(
                internalConfiguration.getRequestRetryStrategy());
    }

    private void buildServiceFactories(URI restApiUrl) {
        apiModelConverterFactory = new ApiModelConverterFactory();
        apiServiceBuilder = new ApiServiceBuilder()
                .withCredentials(userConfiguration)
                .withBaseUrl(restApiUrl)
                .withRequestRetryStrategyFactory(requestRetryStrategyFactory);
    }

    private void buildServicesFromFactories() {
        apiModelConverter = apiModelConverterFactory.create();
        apiService = apiServiceBuilder
                .withApiModelConverter(apiModelConverter)
                .build();
    }

    private void buildServerConfigurationProvider() {
        serverConfigurationProvider = new ServerConfigurationProvider(internalConfiguration, apiService);
    }

    private void buildChannelValueSendingStrategy() {
        ChannelValueBufferConfiguration channelValueBufferConfiguration =
                internalConfiguration.getChannelValueBufferConfiguration();
        channelValueSendingStrategy = new TimedChannelValueSendingStrategy(
                channelValueBufferConfiguration.getMaxPackageCount(),
                channelValueBufferConfiguration.getMinIntervalBetweenSendsMillis());
    }

    private void buildChannelValueSender(UUID jobId) {
        channelValueSender = new BufferedChannelValueSender(
                apiServiceBuilder,
                apiModelConverterFactory,
                jobId,
                serverConfigurationProvider.getConfigInfo(),
                internalConfiguration.getChannelValueBufferConfiguration(),
                channelValueSendingStrategy);
    }

    private void buildChannelValueSendingThread() {
        channelValueSendingThread = new ChannelValueSendingThread(
                new ThreadBrowser(),
                channelValueSender,
                internalConfiguration.getChannelValueSendingThreadConfiguration());
    }

    private void buildApiJobModel(UUID jobId) throws ApiException {
        apiJobModel = apiService.getJob(jobId);
    }

    private void buildApiExperimentModel(io.deepsense.neptune.apiclient.model.Job apiJobModel) throws ApiException {
        apiExperimentModel = apiService.getExperiment(UUID.fromString(apiJobModel.getExperimentId()));
    }

    private void buildJobBuilder() {
        jobBuilder = new JobBuilderFactory(
                apiService,
                apiModelConverter,
                serverConfigurationProvider,
                apiJobModel,
                apiExperimentModel)
                .create();
    }

    private void buildJobParametersBuilder() {
        onlineJobParametersFactory = new OnlineJobParametersFactory(apiModelConverter, apiJobModel);
    }

    private void buildStorageUrl() {
        storageUrl = jobBuilder.getStorageUrl();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy