import gr.grnet.aquarium.util.LogHelpers.Warn
import gr.grnet.aquarium.util.date.{MutableDateCalc, TimeHelpers}
import gr.grnet.aquarium.util.{Lifecycle, Loggable}
-import gr.grnet.aquarium.{AquariumInternalError, AquariumAwareSkeleton}
+import gr.grnet.aquarium.{Real, AquariumInternalError, AquariumAwareSkeleton}
import java.util.{Map ⇒ JMap}
-import gr.grnet.aquarium.event.CreditsModel
import gr.grnet.aquarium.charging.state.UserAgreementHistoryModel
/**
def calculateRealtimeUserState(
userAgreementHistoryModel: UserAgreementHistoryModel,
userStateMsg: UserStateMsg,
- billingMonthInfo: BillingMonthInfo,
resourceMapping: JMap[String, ResourceTypeMsg],
realtimeMillis: Long
) {
DebugSeq(logger, "virtualEvents", virtualEvents, 1)
processResourceEvents(
+ realtimeMillis,
virtualEvents,
userAgreementHistoryModel,
userStateMsg,
- billingMonthInfo,
resourceMapping,
realtimeMillis
)
}
def findOrCalculateWorkingUserStateAtEndOfBillingMonth(
+ processingTimeMillis: Long,
userAgreementHistoryModel: UserAgreementHistoryModel,
billingMonthInfo: BillingMonthInfo,
resourceMapping: JMap[String, ResourceTypeMsg],
def computeFullMonthBillingAndSaveState(): UserStateMsg = {
val fullMonthUserState = replayFullMonthBilling(
+ processingTimeMillis,
userAgreementHistoryModel,
billingMonthInfo,
resourceMapping,
// First ask if it exists and compute only if not
val initialUserState0 = MessageFactory.newInitialUserStateMsg(
userID,
- CreditsModel.from(0.0),
+ Real.Zero,
TimeHelpers.nowMillis()
)
*
* @param resourceEvent
* @param userStateMsg
- * @param billingMonthInfo
*/
def processResourceEvent(
+ processingTimeMillis: Long,
resourceEvent: ResourceEventMsg,
userAgreementHistoryModel: UserAgreementHistoryModel,
userStateMsg: UserStateMsg,
- billingMonthInfo: BillingMonthInfo,
- updateLatestMillis: Boolean,
- resourceMapping: JMap[String, ResourceTypeMsg]
- ): Boolean = {
- logger.warn("processResourceEvent:workingUserState=%s".format(userStateMsg)) //
+ resourceMapping: JMap[String, ResourceTypeMsg],
+ updateLatestMillis: Boolean
+ ) {
+ require(userStateMsg ne null, "userStateMsg ne null")
+
val resourceName = resourceEvent.getResource
val resourceTypeMsg = resourceMapping.get(resourceName)
- if(resourceTypeMsg eq null) {
- // Unknown (yet) resource, ignoring event.
- return false
- }
val chargingBehavior = aquarium.chargingBehaviorOf(resourceTypeMsg)
- val resourcesChargingState = userStateMsg.getStateOfResources.get(resourceName) match {
- case null ⇒
- // First time for this ChargingBehavior.
- val newState = MessageFactory.newResourcesChargingStateMsg(
- resourceName,
- chargingBehavior.initialChargingDetails
- )
- userStateMsg.getStateOfResources.put(resourceName, newState)
- newState
- case existingState ⇒
- existingState
- }
+ val resourcesChargingState = MessageHelpers.getOrInitializeResourcesChargingState(
+ userStateMsg,
+ resourceName,
+ chargingBehavior.initialChargingDetails
+ )
+
+ val eventOccurredMillis = resourceEvent.getOccurredMillis
+ val eventReceivedMillis = resourceEvent.getReceivedMillis
+ val billingMonthInfo = BillingMonthInfo.fromMillis(eventOccurredMillis)
+
+
+ // See if this is out-of-sync
+
+
val m0 = TimeHelpers.nowMillis()
val (walletEntriesCount, creditsToSubtract) = chargingBehavior.processResourceEvent(
}
def processResourceEvents(
+ processingTimeMillis: Long,
resourceEvents: Traversable[ResourceEventMsg],
userAgreementHistoryModel: UserAgreementHistoryModel,
userStateMsg: UserStateMsg,
- billingMonthInfo: BillingMonthInfo,
resourceMapping: JMap[String, ResourceTypeMsg],
latestUpdateMillis: Long
): Unit = {
var _counter = 0
for(currentResourceEvent ← resourceEvents) {
processResourceEvent(
+ processingTimeMillis,
currentResourceEvent,
userAgreementHistoryModel,
userStateMsg,
- billingMonthInfo,
- false,
- resourceMapping
+ resourceMapping,
+ false
)
_counter += 1
}
def replayFullMonthBilling(
+ processingTimeMillis: Long,
userAgreementHistoryModel: UserAgreementHistoryModel,
billingMonthInfo: BillingMonthInfo,
resourceMapping: JMap[String, ResourceTypeMsg],
): UserStateMsg = {
replayMonthChargingUpTo(
+ processingTimeMillis,
userAgreementHistoryModel,
billingMonthInfo,
billingMonthInfo.monthStopMillis,
* @return
*/
def replayMonthChargingUpTo(
+ processingTimeMillis: Long,
userAgreementHistoryModel: UserAgreementHistoryModel,
billingMonthInfo: BillingMonthInfo,
billingEndTimeMillis: Long,
// In order to replay the full month, we start with the state at the beginning of the month.
val previousBillingMonthInfo = billingMonthInfo.previousMonth
val userStateMsg = findOrCalculateWorkingUserStateAtEndOfBillingMonth(
+ processingTimeMillis,
userAgreementHistoryModel,
previousBillingMonthInfo,
resourceMapping,
Debug(logger, "Processing %s", currentResourceEvent)
processResourceEvent(
+ processingTimeMillis,
currentResourceEvent,
userAgreementHistoryModel,
userStateMsg,
- billingMonthInfo,
- false,
- resourceMapping
+ resourceMapping,
+ false
)
_rcEventsCounter += 1