Search in sources :

Example 21 with Preparation

use of org.talend.dataprep.api.preparation.Preparation in project data-prep by Talend.

the class OptimizedExportStrategyTest method testAcceptKO_noStepId.

@Test
public void testAcceptKO_noStepId() throws Exception {
    // Given
    preparationRepository.add(new Preparation("prep-1234", "1234", Step.ROOT_STEP.id(), "0.1"));
    ExportParameters exportParameters = new ExportParameters();
    exportParameters.setPreparationId("prep-1234");
    // Then
    assertFalse(optimizedExportStrategy.accept(exportParameters));
}
Also used : Preparation(org.talend.dataprep.api.preparation.Preparation) ExportParameters(org.talend.dataprep.api.export.ExportParameters) Test(org.junit.Test) TransformationServiceBaseTest(org.talend.dataprep.transformation.service.TransformationServiceBaseTest)

Example 22 with Preparation

use of org.talend.dataprep.api.preparation.Preparation in project data-prep by Talend.

the class OptimizedExportStrategy method performOptimizedTransform.

private void performOptimizedTransform(ExportParameters parameters, OutputStream outputStream) throws IOException {
    // Initial check
    final OptimizedPreparationInput optimizedPreparationInput = new OptimizedPreparationInput(parameters).invoke();
    if (optimizedPreparationInput == null) {
        throw new IllegalStateException("Unable to use this strategy (call accept() before calling this).");
    }
    final String preparationId = parameters.getPreparationId();
    final String dataSetId = optimizedPreparationInput.getDataSetId();
    final TransformationCacheKey transformationCacheKey = optimizedPreparationInput.getTransformationCacheKey();
    final DataSetMetadata metadata = optimizedPreparationInput.getMetadata();
    final String previousVersion = optimizedPreparationInput.getPreviousVersion();
    final String version = optimizedPreparationInput.getVersion();
    final ExportFormat format = getFormat(parameters.getExportType());
    // Get content from previous step
    try (JsonParser parser = mapper.getFactory().createParser(new InputStreamReader(contentCache.get(transformationCacheKey), UTF_8))) {
        // Create dataset
        final DataSet dataSet = mapper.readerFor(DataSet.class).readValue(parser);
        dataSet.setMetadata(metadata);
        // get the actions to apply (no preparation ==> dataset export ==> no actions)
        final String actions = getActions(preparationId, previousVersion, version);
        final PreparationMessage preparation = getPreparation(preparationId);
        preparation.setSteps(getMatchingSteps(preparation.getSteps(), previousVersion, version));
        LOGGER.debug("Running optimized strategy for preparation {} @ step #{}", preparationId, version);
        // create tee to broadcast to cache + service output
        final TransformationCacheKey key = // 
        cacheKeyGenerator.generateContentKey(// 
        dataSetId, // 
        preparationId, // 
        version, // 
        parameters.getExportType(), // 
        parameters.getFrom(), // 
        parameters.getArguments(), // 
        parameters.getFilter());
        LOGGER.debug("Cache key: " + key.getKey());
        LOGGER.debug("Cache key details: " + key.toString());
        try (final TeeOutputStream tee = new TeeOutputStream(outputStream, contentCache.put(key, ContentCache.TimeToLive.DEFAULT))) {
            final Configuration configuration = // 
            Configuration.builder().args(// 
            parameters.getArguments()).outFilter(// 
            rm -> filterService.build(parameters.getFilter(), rm)).sourceType(parameters.getFrom()).format(// 
            format.getName()).actions(// 
            actions).preparation(// 
            preparation).stepId(// 
            version).volume(// 
            Configuration.Volume.SMALL).output(// 
            tee).limit(// 
            limit).build();
            factory.get(configuration).buildExecutable(dataSet, configuration).execute();
            tee.flush();
        } catch (Throwable e) {
            // NOSONAR
            contentCache.evict(key);
            throw e;
        }
    } catch (TDPException e) {
        throw e;
    } catch (Exception e) {
        throw new TDPException(TransformationErrorCodes.UNABLE_TO_TRANSFORM_DATASET, e);
    }
}
Also used : ExportFormat(org.talend.dataprep.format.export.ExportFormat) StringUtils(org.apache.commons.lang.StringUtils) TDPException(org.talend.dataprep.exception.TDPException) TransformationErrorCodes(org.talend.dataprep.exception.error.TransformationErrorCodes) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Configuration(org.talend.dataprep.transformation.api.transformer.configuration.Configuration) PreparationMessage(org.talend.dataprep.api.preparation.PreparationMessage) ArrayList(java.util.ArrayList) CacheKeyGenerator(org.talend.dataprep.cache.CacheKeyGenerator) TransformationMetadataCacheKey(org.talend.dataprep.cache.TransformationMetadataCacheKey) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) ExportUtils(org.talend.dataprep.transformation.service.ExportUtils) DataSet(org.talend.dataprep.api.dataset.DataSet) OutputStream(java.io.OutputStream) Preparation(org.talend.dataprep.api.preparation.Preparation) ExportParameters(org.talend.dataprep.api.export.ExportParameters) Logger(org.slf4j.Logger) BaseExportStrategy(org.talend.dataprep.transformation.service.BaseExportStrategy) JsonParser(com.fasterxml.jackson.core.JsonParser) UTF_8(java.nio.charset.StandardCharsets.UTF_8) StreamingResponseBody(org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody) IOException(java.io.IOException) Step(org.talend.dataprep.api.preparation.Step) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) ContentCache(org.talend.dataprep.cache.ContentCache) CSVFormat(org.talend.dataprep.transformation.format.CSVFormat) List(java.util.List) Component(org.springframework.stereotype.Component) TransformationCacheKey(org.talend.dataprep.cache.TransformationCacheKey) InputStream(java.io.InputStream) TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) InputStreamReader(java.io.InputStreamReader) Configuration(org.talend.dataprep.transformation.api.transformer.configuration.Configuration) DataSet(org.talend.dataprep.api.dataset.DataSet) ExportFormat(org.talend.dataprep.format.export.ExportFormat) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) TDPException(org.talend.dataprep.exception.TDPException) IOException(java.io.IOException) TransformationCacheKey(org.talend.dataprep.cache.TransformationCacheKey) TDPException(org.talend.dataprep.exception.TDPException) PreparationMessage(org.talend.dataprep.api.preparation.PreparationMessage) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 23 with Preparation

use of org.talend.dataprep.api.preparation.Preparation in project data-prep by Talend.

the class PreparationExportStrategyTest method configurePreparation.

private void configurePreparation(Preparation preparation, String preparationId, String stepId) throws IOException {
    final StringWriter preparationAsString = new StringWriter();
    mapper.writerFor(Preparation.class).writeValue(preparationAsString, preparation);
    when(preparationDetailsGet.execute()).thenReturn(new ByteArrayInputStream(preparationAsString.toString().getBytes()));
    when(applicationContext.getBean(eq(PreparationDetailsGet.class), eq(preparationId), eq(stepId))).thenReturn(preparationDetailsGet);
}
Also used : StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) Preparation(org.talend.dataprep.api.preparation.Preparation) PreparationDetailsGet(org.talend.dataprep.command.preparation.PreparationDetailsGet)

Example 24 with Preparation

use of org.talend.dataprep.api.preparation.Preparation in project data-prep by Talend.

the class AddAllPreparationsTaskToHome method run.

/**
 * @see UpgradeTask#run()
 */
@Override
public void run() {
    final Stream<Preparation> preparations = preparationRepository.list(Preparation.class);
    final String homeId = folderRepository.getHome().getId();
    preparations.forEach(p -> {
        folderRepository.addFolderEntry(new FolderEntry(FolderContentType.PREPARATION, p.id()), homeId);
        LOG.debug("preparation #{} added to home", p.getId());
    });
    LOG.debug("All preparations were added to the home folder.");
}
Also used : Preparation(org.talend.dataprep.api.preparation.Preparation) FolderEntry(org.talend.dataprep.api.folder.FolderEntry)

Example 25 with Preparation

use of org.talend.dataprep.api.preparation.Preparation in project data-prep by Talend.

the class PreparationCleanerTest method removeOrphanSteps_should_not_remove_step_that_still_belongs_to_a_preparation.

@Test
public void removeOrphanSteps_should_not_remove_step_that_still_belongs_to_a_preparation() {
    // given
    final String version = "1.2.3";
    final Step firstStep = new Step(Step.ROOT_STEP.id(), "first", version);
    final Step secondStep = new Step(firstStep.id(), "second", version);
    final Step thirdStep = new Step(secondStep.id(), "third", version);
    when(repository.list(eq(Step.class))).thenReturn(Stream.of(firstStep, secondStep, thirdStep));
    when(repository.list(eq(PersistentStep.class))).thenReturn(Stream.of(firstStep, secondStep, thirdStep).map(s -> {
        final PersistentStep persistentStep = new PersistentStep();
        persistentStep.setId(s.id());
        persistentStep.setContent(s.getContent());
        return persistentStep;
    }));
    final Preparation firstPreparation = new Preparation("#458", "1", firstStep.id(), version);
    firstPreparation.setSteps(Collections.singletonList(firstStep));
    final Preparation secondPreparation = new Preparation("#5428", "2", thirdStep.id(), version);
    secondPreparation.setSteps(Arrays.asList(thirdStep, secondStep, firstStep));
    when(repository.list(eq(Preparation.class))).thenReturn(Stream.of(firstPreparation, secondPreparation));
    // when
    cleaner.removeOrphanSteps();
    // then
    verify(repository, never()).remove(eq(firstStep));
    verify(repository, never()).remove(eq(secondStep));
    verify(repository, never()).remove(eq(thirdStep));
}
Also used : InjectMocks(org.mockito.InjectMocks) Preparation(org.talend.dataprep.api.preparation.Preparation) Arrays(java.util.Arrays) TqlBuilder(org.talend.tql.api.TqlBuilder) Mock(org.mockito.Mock) BaseMaintenanceTest(org.talend.dataprep.maintenance.BaseMaintenanceTest) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Test(org.junit.Test) Step(org.talend.dataprep.api.preparation.Step) PreparationRepository(org.talend.dataprep.preparation.store.PreparationRepository) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) PreparationActions(org.talend.dataprep.api.preparation.PreparationActions) Action(org.talend.dataprep.api.preparation.Action) Collections(java.util.Collections) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Preparation(org.talend.dataprep.api.preparation.Preparation) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Step(org.talend.dataprep.api.preparation.Step) BaseMaintenanceTest(org.talend.dataprep.maintenance.BaseMaintenanceTest) Test(org.junit.Test)

Aggregations

Preparation (org.talend.dataprep.api.preparation.Preparation)65 Test (org.junit.Test)44 Step (org.talend.dataprep.api.preparation.Step)18 List (java.util.List)14 ServiceBaseTest (org.talend.ServiceBaseTest)11 ArrayList (java.util.ArrayList)10 Action (org.talend.dataprep.api.preparation.Action)10 PreparationActions (org.talend.dataprep.api.preparation.PreparationActions)10 Response (com.jayway.restassured.response.Response)9 Arrays (java.util.Arrays)8 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)8 ExportParameters (org.talend.dataprep.api.export.ExportParameters)8 UserPreparation (org.talend.dataprep.preparation.service.UserPreparation)8 TransformationCacheKey (org.talend.dataprep.cache.TransformationCacheKey)7 PreparationRepository (org.talend.dataprep.preparation.store.PreparationRepository)7 RowMetadata (org.talend.dataprep.api.dataset.RowMetadata)6 Folder (org.talend.dataprep.api.folder.Folder)6 TDPException (org.talend.dataprep.exception.TDPException)6 PersistentStep (org.talend.dataprep.preparation.store.PersistentStep)6 PreparationRepositoryTest (org.talend.dataprep.preparation.store.PreparationRepositoryTest)6