import gr.grnet.aquarium.logic.accounting.algorithm.SimpleCostPolicyAlgorithmCompiler
import gr.grnet.aquarium.logic.events.{NewWalletEntry, ResourceEvent}
import gr.grnet.aquarium.util.date.{TimeHelpers, MutableDateCalc}
-import gr.grnet.aquarium.logic.accounting.dsl.{DSLCostPolicy, DSLResourcesMap, DSLPolicy}
+import gr.grnet.aquarium.logic.accounting.dsl.{DSLAgreement, DSLCostPolicy, DSLResourcesMap, DSLPolicy}
/**
*
* @author Christos KK Loverdos <loverdos@gmail.com>
*/
class UserStateComputations extends Loggable {
- def createFirstUserState(userId: String, agreementName: String = "default") = {
+ def createFirstUserState(userId: String,
+ millis: Long = TimeHelpers.nowMillis,
+ agreementName: String = DSLAgreement.DefaultAgreementName) = {
val now = 0L
UserState(
userId,
if(billingMonthStopMillis < userCreationMillis) {
// If the user did not exist for this billing month, piece of cake
- clog.debug("User did not exist before %s. Returning %s", userCreationDateCalc, zeroUserState)
+ clog.debug("User did not exist before %s", userCreationDateCalc)
+ clog.debug("Returning ZERO state %s".format(zeroUserState))
clog.endWith(Just(zeroUserState))
} else {
// Ask DB cache for the latest known user state for this billing period
accounting: Accounting,
contextualLogger: Maybe[ContextualLogger] = NoVal): Maybe[UserState] = Maybe {
+ def rcDebugInfo(rcEvent: ResourceEvent) = {
+ rcEvent.toDebugString(defaultResourcesMap, false)
+ }
+
val clog = ContextualLogger.fromOther(
contextualLogger,
logger,
// specified in the parameters.
var _workingUserState = startingUserState
- // Prepare the implicit OFF resource events
+ // Prepare the implicitly terminated resource events from previous billing period
val implicitlyTerminatedResourceEvents = _workingUserState.implicitlyTerminatedSnapshot.toMutableWorker
- clog.debug("implicitlyTerminatedResourceEvents = %s", implicitlyTerminatedResourceEvents)
+ if(implicitlyTerminatedResourceEvents.size > 0) {
+ clog.debug("%s implicitlyTerminatedResourceEvents", implicitlyTerminatedResourceEvents.size)
+ clog.withIndent {
+ implicitlyTerminatedResourceEvents.foreach(ev ⇒ clog.debug("%s", rcDebugInfo(ev)))
+ }
+ }
+
+ // Keep the resource events from this period that were first (and unused) of their kind
+ val ignoredFirstResourceEvents = IgnoredFirstResourceEventsWorker.Empty
/**
* Finds the previous resource event by checking two possible sources: a) The implicitly terminated resource
}
}
- def rcDebugInfo(rcEvent: ResourceEvent) = {
- rcEvent.toDebugString(defaultResourcesMap, false)
- }
-
// Find the actual resource events from DB
val allResourceEventsForMonth = resourceEventStore.findAllRelevantResourceEventsForBillingPeriod(
userId,
var _eventCounter = 0
clog.debug("resourceEventStore = %s".format(resourceEventStore))
- clog.debug("Found %s resource events, starting processing...", allResourceEventsForMonth.size)
-
+ if(allResourceEventsForMonth.size > 0) {
+ clog.debug("Found %s resource events, starting processing...", allResourceEventsForMonth.size)
+ } else {
+ clog.debug("Not found any resource events")
+ }
+
for {
currentResourceEvent <- allResourceEventsForMonth
} {
val theValue = currentResourceEvent.value
clog.indent()
+ clog.debug("")
clog.debug("Processing %s", currentResourceEvent)
- clog.debug("Friendlier %s", rcDebugInfo(currentResourceEvent))
+ clog.debug("+========= %s", rcDebugInfo(currentResourceEvent))
+
clog.indent()
if(previousResourceEvents.size > 0) {
clog.debug("%s previousResourceEvents", previousResourceEvents.size)
- clog.indent()
- previousResourceEvents.foreach(ev ⇒ clog.debug("%s", rcDebugInfo(ev)))
- clog.unindent()
+ clog.withIndent {
+ previousResourceEvents.foreach(ev ⇒ clog.debug("%s", rcDebugInfo(ev)))
+ }
}
if(implicitlyTerminatedResourceEvents.size > 0) {
clog.debug("%s implicitlyTerminatedResourceEvents", implicitlyTerminatedResourceEvents.size)
- clog.indent()
- implicitlyTerminatedResourceEvents.foreach(ev ⇒ clog.debug("%s", rcDebugInfo(ev)))
- clog.unindent()
+ clog.withIndent {
+ implicitlyTerminatedResourceEvents.foreach(ev ⇒ clog.debug("%s", rcDebugInfo(ev)))
+ }
+ }
+ if(ignoredFirstResourceEvents.size > 0) {
+ clog.debug("%s ignoredFirstResourceEvents", ignoredFirstResourceEvents.size)
+ clog.withIndent {
+ ignoredFirstResourceEvents.foreach(ev ⇒ clog.debug("%s", rcDebugInfo(ev)))
+ }
}
// Ignore the event if it is not billable (but still record it in the "previous" stuff).
// This must be the first resource event of its kind, ever.
// TODO: We should normally check the DB to verify the claim (?)
clog.info("Ignoring first event of its kind %s", rcDebugInfo(currentResourceEvent))
+ ignoredFirstResourceEvents.updateResourceEvent(currentResourceEvent)
} else {
val defaultInitialAmount = costPolicy.getResourceInstanceInitialAmount
val oldAmount = _workingUserState.getResourceInstanceAmount(theResource, theInstanceId, defaultInitialAmount)
// A. Compute new resource instance accumulating amount
val newAmount = costPolicy.computeNewAccumulatingAmount(oldAmount, theValue)
+
+ clog.debug("theValue = %s, oldAmount = %s, newAmount = %s, oldCredits = %s", theValue, oldAmount, newAmount, oldCredits)
// B. Compute new wallet entries
val alltimeAgreements = _workingUserState.agreementsSnapshot.agreementsByTimeslot
resourceDef,
defaultResourcesMap,
alltimeAgreements,
- SimpleCostPolicyAlgorithmCompiler
+ SimpleCostPolicyAlgorithmCompiler,
+ policyStore,
+ Just(clog)
)
// We have the chargeslots, let's associate them with the current event
fullChargeslotsM match {
case Just(fullChargeslots) ⇒
+ if(fullChargeslots.length == 0) {
+ // At least one chargeslot is required.
+ throw new Exception("No chargeslots computed")
+ }
+ clog.debug("chargeslots:")
+ clog.withIndent {
+ for(fullChargeslot <- fullChargeslots) {
+ clog.debug("%s", fullChargeslot)
+ }
+ }
+
// C. Compute new credit amount (based on the charge slots)
val newCreditsDiff = fullChargeslots.map(_.computedCredits.get).sum
val newCredits = oldCredits + newCreditsDiff
+ clog.debug("newCreditsDiff = %s, newCredits = %s", newCreditsDiff, newCredits)
+
val newWalletEntry = NewWalletEntry(
userId,
newCreditsDiff,
}
clog.unindent()
+ clog.debug("-========= %s", rcDebugInfo(currentResourceEvent))
clog.unindent()
}