Search in sources :

Example 1 with HeaderColumnNameMappingStrategy

use of com.opencsv.bean.HeaderColumnNameMappingStrategy in project photon-model by vmware.

the class AzureDetailedBillHandler method parseDetailedCsv.

public BillParsingStatus parseDetailedCsv(File billFile, Set<String> newSubscriptions, BillParsingStatus status, long billProcessedTimeMillis, String currency, BiConsumer<Map<String, AzureSubscription>, Long> dailyStatsConsumer) throws IOException {
    logger.fine(() -> "Beginning to parse CSV file.");
    try (CSVReader csvReader = new CSVReader(new FileReader(billFile), AzureCostConstants.DEFAULT_COLUMN_SEPARATOR, AzureCostConstants.DEFAULT_QUOTE_CHARACTER, AzureCostConstants.DEFAULT_ESCAPE_CHARACTER, (int) status.getNoLinesRead())) {
        HeaderColumnNameMappingStrategy<EaDetailedBillElement> strategy = new HeaderColumnNameMappingStrategy<>();
        strategy.setType(EaDetailedBillElement.class);
        long timeToStartBillProcessing = getTimeToStartBillProcessing(billProcessedTimeMillis);
        // This map will contain daily subscription, service & resource cost. The subscription
        // GUID is the key and the subscription details is the value. This map is maintained
        // since daily-level stats are needed for services and resources.
        Map<String, AzureSubscription> monthlyBill = new HashMap<>();
        String[] nextRow;
        Long prevRowEpoch = null;
        while ((nextRow = csvReader.readNext()) != null) {
            final String[] finalNextRow = nextRow;
            if (nextRow.length != BillHeaders.values().length) {
                // Skip any blank or malformed rows
                logger.warning(() -> String.format("Skipping malformed row: %s", Arrays.toString(finalNextRow)));
                continue;
            }
            logger.fine(() -> String.format("Beginning to process row: %s", Arrays.toString(finalNextRow)));
            EaDetailedBillElement detailedBillElement = AzureCostHelper.sanitizeDetailedBillElement(nextRow, currency);
            AzureSubscription subscription = populateSubscriptionCost(monthlyBill, detailedBillElement);
            long curRowEpoch = detailedBillElement.epochDate;
            if (shouldCreateServiceAndResourceCost(detailedBillElement, newSubscriptions, timeToStartBillProcessing)) {
                AzureService service = populateServiceCost(subscription, detailedBillElement);
                populateResourceCost(service, detailedBillElement);
            }
            billProcessedTimeMillis = billProcessedTimeMillis < curRowEpoch ? curRowEpoch : billProcessedTimeMillis;
            monthlyBill.put(detailedBillElement.subscriptionGuid, subscription);
            if (prevRowEpoch != null && !prevRowEpoch.equals(curRowEpoch)) {
                // This indicates that we have processed all rows belonging to a
                // corresponding day in the current month's bill.
                // Consume the batch
                // Subtract 1, to account for detecting date change line.
                status.setNoLinesRead(csvReader.getLinesRead() - 1);
                dailyStatsConsumer.accept(monthlyBill, null);
                break;
            }
            prevRowEpoch = curRowEpoch;
        }
        if ((nextRow == null && monthlyBill.size() > 0) || (nextRow == null && csvReader.getLinesRead() == AzureCostConstants.DEFAULT_LINES_TO_SKIP)) {
            status.setParsingComplete(true);
            dailyStatsConsumer.accept(monthlyBill, billProcessedTimeMillis);
            logger.fine(() -> "Finished parsing CSV bill.");
        }
        return status;
    }
}
Also used : CSVReader(com.opencsv.CSVReader) HashMap(java.util.HashMap) EaDetailedBillElement(com.vmware.photon.controller.model.adapters.azure.model.cost.EaDetailedBillElement) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription) HeaderColumnNameMappingStrategy(com.opencsv.bean.HeaderColumnNameMappingStrategy) AzureService(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureService) FileReader(java.io.FileReader)

Aggregations

CSVReader (com.opencsv.CSVReader)1 HeaderColumnNameMappingStrategy (com.opencsv.bean.HeaderColumnNameMappingStrategy)1 AzureService (com.vmware.photon.controller.model.adapters.azure.model.cost.AzureService)1 AzureSubscription (com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription)1 EaDetailedBillElement (com.vmware.photon.controller.model.adapters.azure.model.cost.EaDetailedBillElement)1 FileReader (java.io.FileReader)1 HashMap (java.util.HashMap)1