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());
}
}
}
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;
}
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);
}
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;
}
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);
}
Aggregations