Search in sources :

Example 1 with AWSCsvBillParser

use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSCsvBillParser in project photon-model by vmware.

the class AWSCostStatsService method downloadParseAndCreateStats.

private void downloadParseAndCreateStats(AWSCostStatsCreationContext statsData, String awsBucketName) throws IOException {
    try {
        // Creating a working directory for downloading and processing the bill
        final Path workingDirPath = Paths.get(System.getProperty(TEMP_DIR_LOCATION), UUID.randomUUID().toString());
        Files.createDirectories(workingDirPath);
        AWSCsvBillParser parser = new AWSCsvBillParser();
        final String csvBillZipFileName = parser.getCsvBillFileName(statsData.billMonthToDownload, statsData.accountId, true);
        Path csvBillZipFilePath = Paths.get(workingDirPath.toString(), csvBillZipFileName);
        ProgressListener listener = new ProgressListener() {

            @Override
            public void progressChanged(ProgressEvent progressEvent) {
                try {
                    ProgressEventType eventType = progressEvent.getEventType();
                    if (ProgressEventType.TRANSFER_COMPLETED_EVENT.equals(eventType)) {
                        OperationContext.restoreOperationContext(statsData.opContext);
                        LocalDate billMonth = new LocalDate(statsData.billMonthToDownload.getYear(), statsData.billMonthToDownload.getMonthOfYear(), 1);
                        logWithContext(statsData, Level.INFO, () -> String.format("Processing" + " bill for the month: %s.", billMonth));
                        parser.parseDetailedCsvBill(statsData.ignorableInvoiceCharge, csvBillZipFilePath, statsData.awsAccountIdToComputeStates.keySet(), getHourlyStatsConsumer(billMonth, statsData), getMonthlyStatsConsumer(billMonth, statsData));
                        deleteTempFiles();
                        // Continue downloading and processing the bills for past and current months' bills
                        statsData.billMonthToDownload = statsData.billMonthToDownload.plusMonths(1);
                        handleCostStatsCreationRequest(statsData);
                    } else if (ProgressEventType.TRANSFER_FAILED_EVENT.equals(eventType)) {
                        deleteTempFiles();
                        billDownloadFailureHandler(statsData, awsBucketName, new IOException("Download of AWS CSV Bill '" + csvBillZipFileName + "' failed."));
                    }
                } catch (Exception exception) {
                    deleteTempFiles();
                    billDownloadFailureHandler(statsData, awsBucketName, exception);
                }
            }

            private void deleteTempFiles() {
                try {
                    Files.deleteIfExists(csvBillZipFilePath);
                    Files.deleteIfExists(workingDirPath);
                } catch (IOException e) {
                // Ignore IO exception while cleaning files.
                }
            }
        };
        GetObjectRequest getObjectRequest = new GetObjectRequest(awsBucketName, csvBillZipFileName).withGeneralProgressListener(listener);
        statsData.s3Client.download(getObjectRequest, csvBillZipFilePath.toFile());
    } catch (AmazonS3Exception s3Exception) {
        billDownloadFailureHandler(statsData, awsBucketName, s3Exception);
    }
}
Also used : Path(java.nio.file.Path) ProgressEventType(com.amazonaws.event.ProgressEventType) ProgressListener(com.amazonaws.event.ProgressListener) AWSCsvBillParser(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSCsvBillParser) IOException(java.io.IOException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) ProgressEvent(com.amazonaws.event.ProgressEvent) LocalDate(org.joda.time.LocalDate) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException)

Example 2 with AWSCsvBillParser

use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSCsvBillParser in project photon-model by vmware.

the class MockCostStatsAdapterService method scheduleDownload.

@Override
protected void scheduleDownload(AWSCostStatsCreationContext statsData, AWSCostStatsCreationStages next) {
    validatePastMonthsBillsAreScheduledForDownload(statsData);
    AWSCsvBillParser parser = new AWSCsvBillParser();
    Path csvBillZipFilePath;
    try {
        csvBillZipFilePath = TestUtils.getTestResourcePath(TestAWSCostAdapterService.class, TestAWSSetupUtils.getCurrentMonthsSampleBillFilePath().toString());
        LocalDate billMonth = LocalDate.now(DateTimeZone.UTC).withDayOfMonth(1);
        parser.parseDetailedCsvBill(statsData.ignorableInvoiceCharge, csvBillZipFilePath, statsData.awsAccountIdToComputeStates.keySet(), getHourlyStatsConsumer(billMonth, statsData), getMonthlyStatsConsumer(billMonth, statsData));
    } catch (Throwable e) {
        statsData.taskManager.patchTaskToFailure(e);
        return;
    }
    statsData.stage = next;
    handleCostStatsCreationRequest(statsData);
}
Also used : Path(java.nio.file.Path) AWSCsvBillParser(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSCsvBillParser) LocalDate(org.joda.time.LocalDate)

Aggregations

AWSCsvBillParser (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSCsvBillParser)2 Path (java.nio.file.Path)2 LocalDate (org.joda.time.LocalDate)2 ProgressEvent (com.amazonaws.event.ProgressEvent)1 ProgressEventType (com.amazonaws.event.ProgressEventType)1 ProgressListener (com.amazonaws.event.ProgressListener)1 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)1 IOException (java.io.IOException)1