private void aggregateEntries()

in src/main/java/com/amazonaws/partners/saasfactory/metering/aggregation/BillingEventAggregation.java [331:367]


    private void aggregateEntries(Map<ZonedDateTime, List<BillingEvent>> eventsByDate, TenantConfiguration tenant) {
        Map<String, AttributeValue> compositeKey = new HashMap<>();
        List<BillingEvent> eventsToCount = new ArrayList<>();

        for (Entry<ZonedDateTime, List<BillingEvent>> entry : eventsByDate.entrySet()) {
            ZonedDateTime time = entry.getKey();
            AttributeValue tenantIDValue = AttributeValue.builder()
                    .s(tenant.getTenantID())
                    .build();
            compositeKey.put(PRIMARY_KEY_NAME, tenantIDValue);

            AttributeValue aggregationEntryValue = AttributeValue.builder()
                    .s(formatAggregationEntry(time.toInstant().toEpochMilli()))
                    .build();
            compositeKey.put(SORT_KEY_NAME, aggregationEntryValue);

            List<BillingEvent> billingEvents = eventsByDate.get(time);

            for (BillingEvent event : billingEvents) {
                eventsToCount.add(event);
                // 24 purges, 1 update
                // Minus one because I need to leave room for the update statement
                if (eventsToCount.size() == MAXIMUM_BATCH_SIZE - 1) {
                    performTransaction(eventsToCount, compositeKey, time, tenant);
                    eventsToCount.clear();
                }
            }
            // Submit the last batch of items
            // If the number of requests lands on an increment of 25, need to make sure that no attempt is made to put
            // an empty list, which is why I'm confirming that the list isn't empty. If it isn't empty, put the remaining
            // events in the database
            if (!eventsToCount.isEmpty()) {
                performTransaction(eventsToCount, compositeKey, time, tenant);
                eventsToCount.clear();
            }
        }
    }