Search in sources :

Example 1 with CsvDatumExportOutputFormatService

use of net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService in project solarnetwork-central by SolarNetwork.

the class DatumExportStandardServiceConfig method csvDatumExportOutputFormatService.

@Bean
public DatumExportOutputFormatService csvDatumExportOutputFormatService() {
    CsvDatumExportOutputFormatService service = new CsvDatumExportOutputFormatService();
    service.setTemporaryPath(temporaryDir);
    ResourceBundleMessageSource msgSource = new ResourceBundleMessageSource();
    msgSource.setBasenames(CsvDatumExportOutputFormatService.class.getName(), OutputConfiguration.class.getName());
    service.setMessageSource(msgSource);
    return service;
}
Also used : OutputConfiguration(net.solarnetwork.central.datum.export.domain.OutputConfiguration) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) CsvDatumExportOutputFormatService(net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService) Bean(org.springframework.context.annotation.Bean)

Example 2 with CsvDatumExportOutputFormatService

use of net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService in project solarnetwork-central by SolarNetwork.

the class S3DatumExportDestinationServiceTests method export.

@Test
public void export() throws IOException {
    // given
    AmazonS3 client = getS3Client();
    cleanS3Folder(client);
    S3DatumExportDestinationService service = new S3DatumExportDestinationService();
    Instant ts = LocalDateTime.of(2018, 4, 11, 11, 50).atZone(ZoneId.of("Pacific/Auckland")).toInstant();
    BasicConfiguration config = new BasicConfiguration();
    BasicDestinationConfiguration destConfig = new BasicDestinationConfiguration();
    destConfig.setServiceIdentifier(service.getId());
    Map<String, Object> destProps = new HashMap<>();
    TEST_PROPS.forEach((k, v) -> destProps.put(k.toString(), v));
    destConfig.setServiceProps(destProps);
    config.setDestinationConfiguration(destConfig);
    Map<String, Object> runtimeProps = config.createRuntimeProperties(ts, null, new CsvDatumExportOutputFormatService());
    DatumExportResource rsrc = getTestResource();
    // when
    List<Double> progress = new ArrayList<>(8);
    service.export(config, Collections.singleton(rsrc), runtimeProps, new ProgressListener<DatumExportService>() {

        @Override
        public void progressChanged(DatumExportService context, double amountComplete) {
            assertThat("Context is service", context, sameInstance(service));
            progress.add(amountComplete);
        }
    });
    // then
    assertThat("Progress was made", progress, not(hasSize(0)));
    assertThat("Progress complete", progress.get(progress.size() - 1), equalTo((Double) 1.0));
    // now list our folder to verify expected result
    ListObjectsV2Result listing = listFolder(client);
    Set<String> keys = listing.getObjectSummaries().stream().map(S3ObjectSummary::getKey).collect(Collectors.toSet());
    String keyPrefix = getObjectKeyPrefix();
    assertThat(keys, containsInAnyOrder(keyPrefix + "/data-export-2018-04-10.csv"));
    String exportContent = getObjectAsString(client, listing.getObjectSummaries().get(0).getKey());
    assertThat("Exported content", exportContent, equalTo(FileCopyUtils.copyToString(new InputStreamReader(getClass().getResourceAsStream("test-datum-export-01.txt"), "UTF-8"))));
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) BasicDestinationConfiguration(net.solarnetwork.central.datum.export.domain.BasicDestinationConfiguration) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) Instant(java.time.Instant) ArrayList(java.util.ArrayList) S3DatumExportDestinationService(net.solarnetwork.central.datum.export.dest.s3.S3DatumExportDestinationService) DatumExportService(net.solarnetwork.central.datum.export.biz.DatumExportService) BasicDatumExportResource(net.solarnetwork.central.datum.export.domain.BasicDatumExportResource) DatumExportResource(net.solarnetwork.central.datum.export.domain.DatumExportResource) S3Object(com.amazonaws.services.s3.model.S3Object) CsvDatumExportOutputFormatService(net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService) BasicConfiguration(net.solarnetwork.central.datum.export.domain.BasicConfiguration) AbstractCentralTest(net.solarnetwork.central.test.AbstractCentralTest) Test(org.junit.Test)

Example 3 with CsvDatumExportOutputFormatService

use of net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService in project solarnetwork-central by SolarNetwork.

the class CsvDatumExportOutputFormatServiceTests method exportCompressedGzip.

@Test
public void exportCompressedGzip() throws IOException {
    // given
    CsvDatumExportOutputFormatService service = new CsvDatumExportOutputFormatService();
    BasicOutputConfiguration config = new BasicOutputConfiguration();
    config.setCompressionType(OutputCompressionType.GZIP);
    List<GeneralNodeDatumFilterMatch> data = new ArrayList<>(100);
    Instant start = LocalDateTime.of(2018, 4, 23, 11, 19).atZone(ZoneOffset.UTC).toInstant();
    for (int i = 0; i < 100; i++) {
        GeneralNodeDatumMatch d1 = new GeneralNodeDatumMatch();
        d1.setCreated(start.plus(i, ChronoUnit.MINUTES));
        d1.setNodeId(TEST_NODE_ID);
        d1.setSourceId(TEST_SOURCE_ID);
        d1.setLocalDateTime(d1.getCreated().atZone(ZoneOffset.UTC).toLocalDateTime());
        d1.setSampleJson("{\"i\":{\"watts\":123.456}}");
        data.add(d1);
    }
    List<Double> progress = new ArrayList<>(4);
    // when
    Iterable<DatumExportResource> results = null;
    try (DatumExportOutputFormatService.ExportContext context = service.createExportContext(config)) {
        assertThat("Context created", context, notNullValue());
        context.start(1);
        context.appendDatumMatch(data, new ProgressListener<DatumExportService>() {

            @Override
            public void progressChanged(DatumExportService ctx, double amountComplete) {
                assertThat("Same context", ctx, sameInstance(service));
                progress.add(amountComplete);
            }
        });
        results = context.finish();
    }
    // then
    assertThat("Result created", results, notNullValue());
    List<DatumExportResource> resultList = StreamSupport.stream(results.spliterator(), false).collect(Collectors.toList());
    assertThat(resultList, hasSize(1));
    assertThat("Progress provided", progress, hasSize(100));
    DatumExportResource r = resultList.get(0);
    assertThat(r, Matchers.instanceOf(BasicDatumExportResource.class));
    File tempFile = ((BasicDatumExportResource) r).getDelegate().getFile();
    assertThat("Temp file exists", tempFile.exists(), equalTo(true));
    assertThat("Temp file extension", tempFile.getName(), endsWith(".csv.gz"));
    assertThat("Content type", r.getContentType(), equalTo(OutputCompressionType.GZIP.getContentType()));
    String csv = FileCopyUtils.copyToString(new InputStreamReader(new GZIPInputStream(r.getInputStream()), "UTF-8"));
    assertThat("Temp file deleted", tempFile.exists(), equalTo(false));
    StringBuilder buf = new StringBuilder("created,nodeId,sourceId,localDate,localTime,watts\r\n");
    DateTimeFormatter tsFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'").withZone(ZoneOffset.UTC);
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneOffset.UTC);
    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss.SSS").withZone(ZoneOffset.UTC);
    for (int i = 0; i < 100; i++) {
        Instant date = start.plus(i, ChronoUnit.MINUTES);
        buf.append(String.format("%s,-1,test.source,%s,%s,123.456\r\n", tsFormatter.format(date), dateFormatter.format(date), timeFormatter.format(date)));
    }
    assertThat("Generated CSV", csv, equalTo(buf.toString()));
}
Also used : GeneralNodeDatumMatch(net.solarnetwork.central.datum.domain.GeneralNodeDatumMatch) BasicDatumExportResource(net.solarnetwork.central.datum.export.domain.BasicDatumExportResource) ArrayList(java.util.ArrayList) GZIPInputStream(java.util.zip.GZIPInputStream) DatumExportOutputFormatService(net.solarnetwork.central.datum.export.biz.DatumExportOutputFormatService) CsvDatumExportOutputFormatService(net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService) GeneralNodeDatumFilterMatch(net.solarnetwork.central.datum.domain.GeneralNodeDatumFilterMatch) InputStreamReader(java.io.InputStreamReader) Instant(java.time.Instant) DatumExportService(net.solarnetwork.central.datum.export.biz.DatumExportService) BasicDatumExportResource(net.solarnetwork.central.datum.export.domain.BasicDatumExportResource) DatumExportResource(net.solarnetwork.central.datum.export.domain.DatumExportResource) CsvDatumExportOutputFormatService(net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService) BasicOutputConfiguration(net.solarnetwork.central.datum.export.domain.BasicOutputConfiguration) File(java.io.File) DateTimeFormatter(java.time.format.DateTimeFormatter) Test(org.junit.Test)

Example 4 with CsvDatumExportOutputFormatService

use of net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService in project solarnetwork-central by SolarNetwork.

the class CsvDatumExportOutputFormatServiceTests method exportCompressedGzipNoHeader.

@Test
public void exportCompressedGzipNoHeader() throws IOException {
    // given
    CsvDatumExportOutputFormatService service = new CsvDatumExportOutputFormatService();
    BasicOutputConfiguration config = new BasicOutputConfiguration();
    config.setCompressionType(OutputCompressionType.GZIP);
    config.setServiceProps(Collections.singletonMap("includeHeader", "false"));
    List<GeneralNodeDatumFilterMatch> data = new ArrayList<>(100);
    Instant start = LocalDateTime.of(2018, 4, 23, 11, 19).atZone(ZoneOffset.UTC).toInstant();
    for (int i = 0; i < 100; i++) {
        GeneralNodeDatumMatch d1 = new GeneralNodeDatumMatch();
        d1.setCreated(start.plus(i, ChronoUnit.MINUTES));
        d1.setNodeId(TEST_NODE_ID);
        d1.setSourceId(TEST_SOURCE_ID);
        d1.setLocalDateTime(d1.getCreated().atZone(ZoneOffset.UTC).toLocalDateTime());
        d1.setSampleJson("{\"i\":{\"watts\":123.456}}");
        data.add(d1);
    }
    List<Double> progress = new ArrayList<>(4);
    // when
    Iterable<DatumExportResource> results = null;
    try (DatumExportOutputFormatService.ExportContext context = service.createExportContext(config)) {
        assertThat("Context created", context, notNullValue());
        context.start(1);
        context.appendDatumMatch(data, new ProgressListener<DatumExportService>() {

            @Override
            public void progressChanged(DatumExportService ctx, double amountComplete) {
                assertThat("Same context", ctx, sameInstance(service));
                progress.add(amountComplete);
            }
        });
        results = context.finish();
    }
    // then
    assertThat("Result created", results, notNullValue());
    List<DatumExportResource> resultList = StreamSupport.stream(results.spliterator(), false).collect(Collectors.toList());
    assertThat(resultList, hasSize(1));
    assertThat("Progress provided", progress, hasSize(100));
    DatumExportResource r = resultList.get(0);
    assertThat(r, Matchers.instanceOf(BasicDatumExportResource.class));
    File tempFile = ((BasicDatumExportResource) r).getDelegate().getFile();
    assertThat("Temp file exists", tempFile.exists(), equalTo(true));
    assertThat("Temp file extension", tempFile.getName(), endsWith(".csv.gz"));
    assertThat("Content type", r.getContentType(), equalTo(OutputCompressionType.GZIP.getContentType()));
    String csv = FileCopyUtils.copyToString(new InputStreamReader(new GZIPInputStream(r.getInputStream()), "UTF-8"));
    assertThat("Temp file deleted", tempFile.exists(), equalTo(false));
    StringBuilder buf = new StringBuilder();
    DateTimeFormatter tsFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'").withZone(ZoneOffset.UTC);
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneOffset.UTC);
    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss.SSS").withZone(ZoneOffset.UTC);
    for (int i = 0; i < 100; i++) {
        Instant date = start.plus(i, ChronoUnit.MINUTES);
        buf.append(String.format("%s,-1,test.source,%s,%s,123.456\r\n", tsFormatter.format(date), dateFormatter.format(date), timeFormatter.format(date)));
    }
    assertThat("Generated CSV", csv, equalTo(buf.toString()));
}
Also used : GeneralNodeDatumMatch(net.solarnetwork.central.datum.domain.GeneralNodeDatumMatch) BasicDatumExportResource(net.solarnetwork.central.datum.export.domain.BasicDatumExportResource) ArrayList(java.util.ArrayList) GZIPInputStream(java.util.zip.GZIPInputStream) DatumExportOutputFormatService(net.solarnetwork.central.datum.export.biz.DatumExportOutputFormatService) CsvDatumExportOutputFormatService(net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService) GeneralNodeDatumFilterMatch(net.solarnetwork.central.datum.domain.GeneralNodeDatumFilterMatch) InputStreamReader(java.io.InputStreamReader) Instant(java.time.Instant) DatumExportService(net.solarnetwork.central.datum.export.biz.DatumExportService) BasicDatumExportResource(net.solarnetwork.central.datum.export.domain.BasicDatumExportResource) DatumExportResource(net.solarnetwork.central.datum.export.domain.DatumExportResource) CsvDatumExportOutputFormatService(net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService) BasicOutputConfiguration(net.solarnetwork.central.datum.export.domain.BasicOutputConfiguration) File(java.io.File) DateTimeFormatter(java.time.format.DateTimeFormatter) Test(org.junit.Test)

Example 5 with CsvDatumExportOutputFormatService

use of net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService in project solarnetwork-central by SolarNetwork.

the class CsvDatumExportOutputFormatServiceTests method export.

@Test
public void export() throws IOException {
    // given
    CsvDatumExportOutputFormatService service = new CsvDatumExportOutputFormatService();
    BasicOutputConfiguration config = new BasicOutputConfiguration();
    config.setCompressionType(OutputCompressionType.None);
    GeneralNodeDatumMatch d1 = new GeneralNodeDatumMatch();
    d1.setCreated(LocalDateTime.of(2018, 4, 23, 11, 19).atZone(ZoneOffset.UTC).toInstant());
    d1.setNodeId(TEST_NODE_ID);
    d1.setSourceId(TEST_SOURCE_ID);
    d1.setLocalDateTime(d1.getCreated().atZone(ZoneOffset.UTC).toLocalDateTime());
    d1.setPosted(d1.getCreated().plus(1, ChronoUnit.MINUTES));
    d1.setSampleJson("{\"i\":{\"watts\":123.456}}");
    List<GeneralNodeDatumFilterMatch> data = Arrays.asList(d1);
    List<Double> progress = new ArrayList<>(4);
    // when
    Iterable<DatumExportResource> results = null;
    try (DatumExportOutputFormatService.ExportContext context = service.createExportContext(config)) {
        assertThat("Context created", context, notNullValue());
        context.start(1);
        context.appendDatumMatch(data, new ProgressListener<DatumExportService>() {

            @Override
            public void progressChanged(DatumExportService ctx, double amountComplete) {
                assertThat("Same context", ctx, sameInstance(service));
                progress.add(amountComplete);
            }
        });
        results = context.finish();
    }
    // then
    assertThat("Result created", results, notNullValue());
    assertThat("Progress provided", progress, hasSize(1));
    List<DatumExportResource> resultList = StreamSupport.stream(results.spliterator(), false).collect(Collectors.toList());
    assertThat(resultList, hasSize(1));
    DatumExportResource r = resultList.get(0);
    assertThat(r, Matchers.instanceOf(BasicDatumExportResource.class));
    File tempFile = ((BasicDatumExportResource) r).getDelegate().getFile();
    assertThat("Temp file exists", tempFile.exists(), equalTo(true));
    assertThat("Temp file extension", tempFile.getName(), endsWith(".csv"));
    assertThat("Content type", r.getContentType(), equalTo(service.getExportContentType()));
    String csv = FileCopyUtils.copyToString(new InputStreamReader(r.getInputStream(), "UTF-8"));
    assertThat("Temp file deleted", tempFile.exists(), equalTo(false));
    assertThat("Generated CSV", csv, equalTo("created,nodeId,sourceId,localDate,localTime,watts\r\n" + "2018-04-23T11:19:00.000Z,-1,test.source,2018-04-23,11:19:00.000,123.456\r\n"));
}
Also used : GeneralNodeDatumMatch(net.solarnetwork.central.datum.domain.GeneralNodeDatumMatch) BasicDatumExportResource(net.solarnetwork.central.datum.export.domain.BasicDatumExportResource) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) DatumExportOutputFormatService(net.solarnetwork.central.datum.export.biz.DatumExportOutputFormatService) CsvDatumExportOutputFormatService(net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService) DatumExportService(net.solarnetwork.central.datum.export.biz.DatumExportService) GeneralNodeDatumFilterMatch(net.solarnetwork.central.datum.domain.GeneralNodeDatumFilterMatch) BasicDatumExportResource(net.solarnetwork.central.datum.export.domain.BasicDatumExportResource) DatumExportResource(net.solarnetwork.central.datum.export.domain.DatumExportResource) CsvDatumExportOutputFormatService(net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService) BasicOutputConfiguration(net.solarnetwork.central.datum.export.domain.BasicOutputConfiguration) File(java.io.File) Test(org.junit.Test)

Aggregations

CsvDatumExportOutputFormatService (net.solarnetwork.central.datum.export.standard.CsvDatumExportOutputFormatService)6 InputStreamReader (java.io.InputStreamReader)5 ArrayList (java.util.ArrayList)5 DatumExportService (net.solarnetwork.central.datum.export.biz.DatumExportService)5 BasicDatumExportResource (net.solarnetwork.central.datum.export.domain.BasicDatumExportResource)5 DatumExportResource (net.solarnetwork.central.datum.export.domain.DatumExportResource)5 Test (org.junit.Test)5 File (java.io.File)4 GeneralNodeDatumFilterMatch (net.solarnetwork.central.datum.domain.GeneralNodeDatumFilterMatch)4 GeneralNodeDatumMatch (net.solarnetwork.central.datum.domain.GeneralNodeDatumMatch)4 DatumExportOutputFormatService (net.solarnetwork.central.datum.export.biz.DatumExportOutputFormatService)4 BasicOutputConfiguration (net.solarnetwork.central.datum.export.domain.BasicOutputConfiguration)4 Instant (java.time.Instant)3 DateTimeFormatter (java.time.format.DateTimeFormatter)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 ListObjectsV2Result (com.amazonaws.services.s3.model.ListObjectsV2Result)1 S3Object (com.amazonaws.services.s3.model.S3Object)1 HashMap (java.util.HashMap)1 S3DatumExportDestinationService (net.solarnetwork.central.datum.export.dest.s3.S3DatumExportDestinationService)1