Merge branch 'master' into 1852_billing_period_calc
authorChristos KK Loverdos <loverdos@gmail.com>
Mon, 30 Jan 2012 15:24:57 +0000 (17:24 +0200)
committerChristos KK Loverdos <loverdos@gmail.com>
Mon, 30 Jan 2012 15:24:57 +0000 (17:24 +0200)
Conflicts:
src/main/scala/gr/grnet/aquarium/user/UserDataSnapshot.scala

1  2 
src/main/scala/gr/grnet/aquarium/user/UserDataSnapshot.scala
src/main/scala/gr/grnet/aquarium/user/UserStateComputations.scala

@@@ -91,7 -91,7 +91,7 @@@ class UserStateComputations 
        0L,
        ActiveSuspendedSnapshot(false, now),
        CreditSnapshot(0, now),
--      AgreementSnapshot(agreementName, now),
++      AgreementSnapshot(Agreement(agreementName, now, now) :: Nil, now),
        RolesSnapshot(List(), now),
        PaymentOrdersSnapshot(Nil, now),
        OwnedGroupsSnapshot(Nil, now),
      val billingDate = new DateCalculator(billingYear, billingMonth, 1)
      val billingDateMillis = billingDate.toMillis
  
--    if(billingDateMillis < knownUserState.startDateMillis) {
--      val userId = knownUserState.userId
--      val agreementName = knownUserState.agreement match {
--        case null      ⇒ "default"
--        case agreement ⇒ agreement.data
--      }
--      createFirstUserState(userId, agreementName)
--    } else {
++//    if(billingDateMillis < knownUserState.startDateMillis) {
++//      val userId = knownUserState.userId
++//      val agreementName = knownUserState.agreement match {
++//        case null      ⇒ "default"
++//        case agreement ⇒ agreement.data
++//      }
++//      createFirstUserState(userId, agreementName)
++//    } else {
        // We really need to compute the user state here
  
        // get all events that
        // FIXME: Implement
        knownUserState
-     }
++//    }
 +  }
 +
 +  /**
 +   * Find the previous resource event, if needed by the event's cost policy,
 +   * in order to use it for any credit calculations.
 +   */
 +  def findPreviousRCEventOf(previousRCEventsMap: mutable.Map[ResourceEvent.FullResourceType, ResourceEvent],
 +                            rcEvent: ResourceEvent,
 +                            costPolicy: DSLCostPolicy): Maybe[ResourceEvent] = {
 +
 +    if(costPolicy.needsPreviousEventForCreditCalculation) {
 +      // Get a previous resource only if this is needed by the policy
 +      previousRCEventsMap.get(rcEvent.fullResourceInfo) match {
 +        case Some(previousRCEvent) ⇒
 +          Just(previousRCEvent)
 +        case None ⇒
 +          queryForPreviousRCEvent(rcEvent)
 +      }
 +    } else {
 +      // No need for previous event. Will return NoVal
 +      NoVal
      }
    }
  
  
        // We need:
        // A. The previous event
 -      def findPreviousRCEventOf(rcEvent: ResourceEvent): Option[ResourceEvent] = {
 -        previousRCEventsMap.get(rcEvent.fullResourceInfo)
 -      }
 -      def updatePreviousRCEventWith(rcEvent: ResourceEvent): Unit = {
 -        previousRCEventsMap(rcEvent.fullResourceInfo) = rcEvent
 -      }
 -      
 -      val prevRCEvent = findPreviousRCEventOf(nextRCEvent) match {
 -        case Some(prevRCEvent) ⇒
 -          prevRCEvent
 -        case None ⇒
 -          // Must query the DB?????
 -      }
  
  
 -      // B. The current event: [✓][✔][✗][✘]☒ OK
 +      // The DSLCostPolicy for the resource does not change, so it is safe to use the default DSLPolicy to obtain it.
 +      val costPolicyM = newRCEvent.findCostPolicy(defaultPolicy)
 +      costPolicyM match {
 +        case Just(costPolicy) ⇒
 +          val previousRCEventM = findPreviousRCEventOf(previousRCEventsMap, newRCEvent, costPolicy)
 +          val previousRCEventValueM = previousRCEventM.map(_.value)
-           val previousRCInstanceAmount = workingUserState.ownedResources.
++//          val previousRCInstanceAmount = workingUserState.ownedResources.
 +
 +          // 1. Update resource state
 +          val newRCInstanceAmountM = costPolicy.computeNewResourceInstanceAmount(previousRCEventValueM, newRCEvent.value)
 +          newRCInstanceAmountM match {
 +            case Just(newRCInstanceAmount) ⇒
 +              workingUserState.ownedResources.addOrUpdateResourceSnapshot(
 +                newRCEvent.resource,
 +                newRCEvent.instanceId,
 +                newRCInstanceAmount,
 +                TimeHelpers.nowMillis)
 +            case NoVal ⇒
 +              () // ERROR
 +            case failed @ Failed(_, _) ⇒
 +              () // ERROR
 +          }
 +
 +          // 2. Update credit state
 +
 +          // 3. Calc wallet entries
 +
 +        case NoVal ⇒
 +          () // ERROR
 +        case failed @ Failed(_, _) ⇒
 +          () // ERROR
 +      }
  
 -//      accounting.chargeEvent()
 -    }
 +
 +      updatePreviousRCEventWith(previousRCEventsMap, newRCEvent)
 +    } // for(newResourceEvent <- allBillingPeriodRelevantRCEvents)
  
  
      null