Search in sources :

Example 6 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord in project killbill by killbill.

the class TestUsageInArrearBase method checkTrackingIds.

// 
// Each input `RawUsage` should end up creating one TrackingRecordId
// Regardless of how test records trackingId -- grouped in one, or multiple calls-- and regardless of test matrix,  the logics below should remain true.
// 
protected void checkTrackingIds(final List<RawUsageRecord> rawUsages, final Set<TrackingRecordId> trackingRecords) {
    // Verify we have same input and output
    assertEquals(rawUsages.size(), trackingRecords.size());
    final Map<String, List<RawUsageRecord>> trackingIdMapping = new HashMap<>();
    for (final RawUsageRecord u : rawUsages) {
        if (!trackingIdMapping.containsKey(u.getTrackingId())) {
            trackingIdMapping.put(u.getTrackingId(), new ArrayList<>());
        }
        trackingIdMapping.get(u.getTrackingId()).add(u);
    }
    final Set<String> trackingIds = ImmutableSet.copyOf(Iterables.transform(trackingRecords, new Function<TrackingRecordId, String>() {

        @Override
        public String apply(final TrackingRecordId input) {
            return input.getTrackingId();
        }
    }));
    // Verify the per trackingId input matches the per trackingId output
    assertEquals(trackingIdMapping.size(), trackingIds.size());
    for (final String id : trackingIdMapping.keySet()) {
        final List<RawUsageRecord> rawUsageForId = trackingIdMapping.get(id);
        for (RawUsageRecord u : rawUsageForId) {
            final TrackingRecordId found = Iterables.tryFind(trackingRecords, new Predicate<TrackingRecordId>() {

                @Override
                public boolean apply(final TrackingRecordId input) {
                    return input.getTrackingId().equals(u.getTrackingId()) && input.getRecordDate().equals(u.getDate()) && input.getUnitType().equals(u.getUnitType());
                }
            }).orNull();
            assertNotNull(found, "Cannot find tracking Id " + u.getTrackingId());
            assertEquals(found.getSubscriptionId(), subscriptionId);
            assertEquals(found.getInvoiceId(), invoiceId);
            assertEquals(found.getRecordDate(), u.getDate());
            assertEquals(found.getUnitType(), u.getUnitType());
        }
    }
}
Also used : Function(com.google.common.base.Function) TrackingRecordId(org.killbill.billing.invoice.generator.InvoiceWithMetadata.TrackingRecordId) RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Predicate(com.google.common.base.Predicate)

Example 7 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord in project killbill by killbill.

the class DefaultUsageUserApi method getRolledUpUnitsForRawPluginUsage.

private List<RolledUpUnit> getRolledUpUnitsForRawPluginUsage(final UUID subscriptionId, @Nullable final String unitType, final List<RawUsageRecord> rawAccountUsage) {
    final Map<String, Long> tmp = new HashMap<String, Long>();
    for (RawUsageRecord cur : rawAccountUsage) {
        // Filter out wrong subscriptionId
        if (cur.getSubscriptionId().compareTo(subscriptionId) != 0) {
            continue;
        }
        // Filter out wrong unitType if specified.
        if (unitType != null && !unitType.equals(cur.getUnitType())) {
            continue;
        }
        Long currentAmount = tmp.get(cur.getUnitType());
        Long updatedAmount = (currentAmount != null) ? currentAmount + cur.getAmount() : cur.getAmount();
        tmp.put(cur.getUnitType(), updatedAmount);
    }
    final List<RolledUpUnit> result = new ArrayList<RolledUpUnit>(tmp.size());
    for (final String curType : tmp.keySet()) {
        result.add(new DefaultRolledUpUnit(curType, tmp.get(curType)));
    }
    return result;
}
Also used : RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 8 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord in project killbill by killbill.

the class DefaultUsageUserApi method getUsageForSubscription.

@Override
public RolledUpUsage getUsageForSubscription(final UUID subscriptionId, final String unitType, final LocalDate startDate, final LocalDate endDate, final TenantContext tenantContextNoAccountId) {
    final InternalTenantContext internalCallContext = internalCallContextFactory.createInternalTenantContext(subscriptionId, ObjectType.SUBSCRIPTION, tenantContextNoAccountId);
    final TenantContext tenantContext = internalCallContextFactory.createTenantContext(internalCallContext);
    final List<RawUsageRecord> rawUsage = getSubscriptionUsageFromPlugin(subscriptionId, startDate, endDate, tenantContext);
    if (rawUsage != null) {
        final List<RolledUpUnit> rolledUpAmount = getRolledUpUnitsForRawPluginUsage(subscriptionId, unitType, rawUsage);
        return new DefaultRolledUpUsage(subscriptionId, startDate, endDate, rolledUpAmount);
    }
    final List<RolledUpUsageModelDao> usageForSubscription = rolledUpUsageDao.getUsageForSubscription(subscriptionId, startDate, endDate, unitType, internalCallContextFactory.createInternalTenantContext(subscriptionId, ObjectType.SUBSCRIPTION, tenantContext));
    final List<RolledUpUnit> rolledUpAmount = getRolledUpUnits(usageForSubscription);
    return new DefaultRolledUpUsage(subscriptionId, startDate, endDate, rolledUpAmount);
}
Also used : RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantContext(org.killbill.billing.util.callcontext.TenantContext) RolledUpUsageModelDao(org.killbill.billing.usage.dao.RolledUpUsageModelDao)

Example 9 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord in project killbill by killbill.

the class DefaultUsageUserApi method getAllUsageForSubscription.

@Override
public List<RolledUpUsage> getAllUsageForSubscription(final UUID subscriptionId, final List<LocalDate> transitionTimes, final TenantContext tenantContextNoAccountId) {
    final InternalTenantContext internalCallContext = internalCallContextFactory.createInternalTenantContext(subscriptionId, ObjectType.SUBSCRIPTION, tenantContextNoAccountId);
    final TenantContext tenantContext = internalCallContextFactory.createTenantContext(internalCallContext);
    List<RolledUpUsage> result = new ArrayList<RolledUpUsage>();
    LocalDate prevDate = null;
    for (LocalDate curDate : transitionTimes) {
        if (prevDate != null) {
            final List<RawUsageRecord> rawUsage = getSubscriptionUsageFromPlugin(subscriptionId, prevDate, curDate, tenantContext);
            if (rawUsage != null) {
                final List<RolledUpUnit> rolledUpAmount = getRolledUpUnitsForRawPluginUsage(subscriptionId, null, rawUsage);
                result.add(new DefaultRolledUpUsage(subscriptionId, prevDate, curDate, rolledUpAmount));
            } else {
                final List<RolledUpUsageModelDao> usageForSubscription = rolledUpUsageDao.getAllUsageForSubscription(subscriptionId, prevDate, curDate, internalCallContext);
                final List<RolledUpUnit> rolledUpAmount = getRolledUpUnits(usageForSubscription);
                result.add(new DefaultRolledUpUsage(subscriptionId, prevDate, curDate, rolledUpAmount));
            }
        }
        prevDate = curDate;
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantContext(org.killbill.billing.util.callcontext.TenantContext) LocalDate(org.joda.time.LocalDate) RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) RolledUpUnit(org.killbill.billing.usage.api.RolledUpUnit) RolledUpUsage(org.killbill.billing.usage.api.RolledUpUsage) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) RolledUpUsageModelDao(org.killbill.billing.usage.dao.RolledUpUsageModelDao)

Example 10 with RawUsageRecord

use of org.killbill.billing.usage.api.RawUsageRecord in project killbill by killbill.

the class RawUsageOptimizer method getInArrearUsage.

public RawUsageOptimizerResult getInArrearUsage(final LocalDate firstEventStartDate, final LocalDate targetDate, final Iterable<InvoiceItem> existingUsageItems, final Map<String, Usage> knownUsage, @Nullable final DryRunInfo dryRunInfo, final InternalCallContext internalCallContext) {
    final int configRawUsagePreviousPeriod = config.getMaxRawUsagePreviousPeriod(internalCallContext);
    final LocalDate optimizedStartDate = configRawUsagePreviousPeriod >= 0 ? getOptimizedRawUsageStartDate(firstEventStartDate, targetDate, existingUsageItems, knownUsage, internalCallContext) : firstEventStartDate;
    log.debug("RawUsageOptimizerResult accountRecordId='{}', configRawUsagePreviousPeriod='{}', firstEventStartDate='{}', optimizedStartDate='{}',  targetDate='{}'", internalCallContext.getAccountRecordId(), configRawUsagePreviousPeriod, firstEventStartDate, optimizedStartDate, targetDate);
    final List<RawUsageRecord> rawUsageData = usageApi.getRawUsageForAccount(optimizedStartDate, targetDate, dryRunInfo, internalCallContext);
    final List<InvoiceTrackingModelDao> trackingIds = invoiceDao.getTrackingsByDateRange(optimizedStartDate, targetDate, internalCallContext);
    final Set<TrackingRecordId> existingTrackingIds = new HashSet<TrackingRecordId>();
    for (final InvoiceTrackingModelDao invoiceTrackingModelDao : trackingIds) {
        existingTrackingIds.add(new TrackingRecordId(invoiceTrackingModelDao.getTrackingId(), invoiceTrackingModelDao.getInvoiceId(), invoiceTrackingModelDao.getSubscriptionId(), invoiceTrackingModelDao.getUnitType(), invoiceTrackingModelDao.getRecordDate()));
    }
    return new RawUsageOptimizerResult(optimizedStartDate, rawUsageData, existingTrackingIds);
}
Also used : TrackingRecordId(org.killbill.billing.invoice.generator.InvoiceWithMetadata.TrackingRecordId) RawUsageRecord(org.killbill.billing.usage.api.RawUsageRecord) InvoiceTrackingModelDao(org.killbill.billing.invoice.dao.InvoiceTrackingModelDao) LocalDate(org.joda.time.LocalDate) HashSet(java.util.HashSet)

Aggregations

RawUsageRecord (org.killbill.billing.usage.api.RawUsageRecord)23 LocalDate (org.joda.time.LocalDate)19 ArrayList (java.util.ArrayList)16 DefaultRawUsage (org.killbill.billing.usage.api.svcs.DefaultRawUsage)16 Test (org.testng.annotations.Test)15 BigDecimal (java.math.BigDecimal)11 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)11 FixedPriceInvoiceItem (org.killbill.billing.invoice.model.FixedPriceInvoiceItem)11 UsageInvoiceItem (org.killbill.billing.invoice.model.UsageInvoiceItem)11 DefaultTier (org.killbill.billing.catalog.DefaultTier)8 DefaultUsage (org.killbill.billing.catalog.DefaultUsage)8 BillingEvent (org.killbill.billing.junction.BillingEvent)8 DefaultTieredBlock (org.killbill.billing.catalog.DefaultTieredBlock)7 UsageConsumableInArrearTierUnitAggregate (org.killbill.billing.invoice.usage.details.UsageConsumableInArrearTierUnitAggregate)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 TrackingRecordId (org.killbill.billing.invoice.generator.InvoiceWithMetadata.TrackingRecordId)4 UsageConsumableInArrearAggregate (org.killbill.billing.invoice.usage.details.UsageConsumableInArrearAggregate)4 RolledUpUnit (org.killbill.billing.usage.api.RolledUpUnit)4 HashMap (java.util.HashMap)3 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)3