2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
36 package gr.grnet.aquarium.user
38 import gr.grnet.aquarium.util.json.{JsonHelpers, JsonSupport}
39 import net.liftweb.json.{JsonAST, Xml}
40 import gr.grnet.aquarium.logic.accounting.dsl.DSLAgreement
41 import com.ckkloverdos.maybe.{Failed, Maybe}
42 import gr.grnet.aquarium.util.date.MutableDateCalc
43 import gr.grnet.aquarium.store.RecordID
44 import gr.grnet.aquarium.logic.events.{NewWalletEntry, UserEvent, WalletEntry}
48 * A comprehensive representation of the User's state.
50 * Note that it is made of autonomous parts that are actually data snapshots.
52 * The different snapshots need not agree on the snapshot time, ie. some state
53 * part may be stale, while other may be fresh.
55 * The user state is meant to be partially updated according to relevant events landing on Aquarium.
57 * @define communicatedByIM
58 * This is communicated to Aquarium from the `IM` system.
62 * The user ID. $communicatedByIM
63 * @param userCreationMillis
64 * When the user was created.
66 * Set to zero if unknown.
67 * @param stateChangeCounter
68 * @param isFullBillingMonthState
69 * @param theFullBillingMonth
70 * @param implicitlyIssuedSnapshot
71 * @param billingMonthWalletEntries
72 * @param outOfSyncWalletEntries
73 * @param latestResourceEventsSnapshot
74 * @param billingPeriodResourceEventsCounter
75 * @param billingPeriodOutOfSyncResourceEventsCounter
76 * @param activeStateSnapshot
77 * @param creditsSnapshot
78 * @param agreementsSnapshot
79 * @param rolesSnapshot
80 * @param ownedResourcesSnapshot
81 * @param newWalletEntries
82 * The wallet entries computed. Not all user states need to holds wallet entries,
83 * only those that refer to billing periods (end of billing period).
84 * @param lastChangeReasonCode
85 * The code for the `lastChangeReason`.
86 * @param lastChangeReason
87 * The [[gr.grnet.aquarium.user.UserStateChangeReason]] for which the usr state has changed.
88 * @param totalEventsProcessedCounter
89 * @param parentUserStateId
90 * The `ID` of the parent state. The parent state is the one used as a reference point in order to calculate
93 * The unique `ID` given by the store.
95 * @author Christos KK Loverdos <loverdos@gmail.com>
100 userCreationMillis: Long,
103 * Each time the user state is updated, this must be increased.
104 * The counter is used when accessing user state from the cache (user state store)
105 * in order to get the latest value for a particular billing period.
107 stateChangeCounter: Long,
110 * True iff this user state refers to a full billing period, that is a full billing month.
112 isFullBillingMonthState: Boolean,
115 * The full billing period for which this user state refers to.
116 * This is set when the user state refers to a full billing period (= month)
117 * and is used to cache the user state for subsequent queries.
119 theFullBillingMonth: BillingMonthInfo,
122 * If this is a state for a full billing month, then keep here the implicit OFF
123 * resource events or any other whose cost policy demands an implicit event at the end of the billing period.
125 * The use case is this: A VM may have been started (ON state) before the end of the billing period
126 * and ended (OFF state) after the beginning of the next billing period. In order to bill this, we must assume
127 * an implicit OFF even right at the end of the billing period and an implicit ON event with the beginning of the
128 * next billing period.
130 implicitlyIssuedSnapshot: ImplicitlyIssuedResourceEventsSnapshot,
133 * So far computed wallet entries for the current billing month.
135 billingMonthWalletEntries: List[WalletEntry],
138 * Wallet entries that were computed for out of sync events.
139 * (for the current billing month ??)
141 outOfSyncWalletEntries: List[WalletEntry],
144 * The latest (previous) resource events per resource instance.
146 latestResourceEventsSnapshot: LatestResourceEventsSnapshot,
149 * Counts the total number of resource events used to produce this user state for
150 * the billing period recorded by `billingPeriodSnapshot`
152 billingPeriodResourceEventsCounter: Long,
155 * The out of sync events used to produce this user state for
156 * the billing period recorded by `billingPeriodSnapshot`
158 billingPeriodOutOfSyncResourceEventsCounter: Long,
160 activeStateSnapshot: ActiveStateSnapshot,
161 creditsSnapshot: CreditSnapshot,
162 agreementsSnapshot: AgreementSnapshot,
163 rolesSnapshot: RolesSnapshot,
164 ownedResourcesSnapshot: OwnedResourcesSnapshot,
165 newWalletEntries: List[NewWalletEntry],
166 lastChangeReasonCode: UserStateChangeReasonCodes.ChangeReasonCode,
167 // The last known change reason for this userState
168 lastChangeReason: UserStateChangeReason = NoSpecificChangeReason,
169 totalEventsProcessedCounter: Long = 0L,
170 // The user state we used to compute this one. Normally the (cached)
171 // state at the beginning of the billing period.
172 parentUserStateId: Option[String] = None,
174 ) extends JsonSupport {
176 private[this] def _allSnapshots: List[Long] = {
178 activeStateSnapshot.snapshotTime,
179 creditsSnapshot.snapshotTime, agreementsSnapshot.snapshotTime, rolesSnapshot.snapshotTime,
180 ownedResourcesSnapshot.snapshotTime,
181 implicitlyIssuedSnapshot.snapshotTime,
182 latestResourceEventsSnapshot.snapshotTime
186 def oldestSnapshotTime: Long = _allSnapshots min
188 def newestSnapshotTime: Long = _allSnapshots max
191 def idOpt: Option[String] = _id match {
197 // def userCreationDate = new Date(userCreationMillis)
199 // def userCreationFormatedDate = new MutableDateCalc(userCreationMillis).toString
201 def maybeDSLAgreement(at: Long): Maybe[DSLAgreement] = {
202 agreementsSnapshot match {
203 case snapshot @ AgreementSnapshot(data, _) ⇒
204 snapshot.getAgreement(at)
206 Failed(new Exception("No agreement snapshot found for user %s".format(userId)))
210 def findResourceInstanceSnapshot(resource: String, instanceId: String): Maybe[ResourceInstanceSnapshot] = {
211 ownedResourcesSnapshot.findResourceInstanceSnapshot(resource, instanceId)
214 def getResourceInstanceAmount(resource: String, instanceId: String, defaultValue: Double): Double = {
215 ownedResourcesSnapshot.getResourceInstanceAmount(resource, instanceId, defaultValue)
218 def copyForResourcesSnapshotUpdate(resource: String, // resource name
219 instanceId: String, // resource instance id
221 snapshotTime: Long): UserState = {
223 val (newResources, _, _) = ownedResourcesSnapshot.computeResourcesSnapshotUpdate(resource, instanceId, newAmount, snapshotTime)
226 ownedResourcesSnapshot = newResources,
227 stateChangeCounter = this.stateChangeCounter + 1)
230 def copyForChangeReason(changeReason: UserStateChangeReason) = {
231 this.copy(lastChangeReasonCode = changeReason.code, lastChangeReason = changeReason)
234 def resourcesMap = ownedResourcesSnapshot.toResourcesMap
236 // def toShortString = "UserState(%s, %s, %s, %s, %s)".format(
239 // parentUserStateId,
240 // totalEventsProcessedCounter,
241 // calculationReason)
246 def fromJson(json: String): UserState = {
247 JsonHelpers.jsonToObject[UserState](json)
250 def fromJValue(jsonAST: JsonAST.JValue): UserState = {
251 JsonHelpers.jValueToObject[UserState](jsonAST)
254 def fromBytes(bytes: Array[Byte]): UserState = {
255 JsonHelpers.jsonBytesToObject[UserState](bytes)
258 def fromXml(xml: String): UserState = {
259 fromJValue(Xml.toJson(scala.xml.XML.loadString(xml)))
263 final val _id = "_id"
264 final val userId = "userId"
268 final class BillingMonthInfo private(val year: Int,
270 val startMillis: Long,
271 val stopMillis: Long) extends Ordered[BillingMonthInfo] {
273 def previousMonth: BillingMonthInfo = {
274 BillingMonthInfo.fromDateCalc(new MutableDateCalc(year, month).goPreviousMonth)
277 def nextMonth: BillingMonthInfo = {
278 BillingMonthInfo.fromDateCalc(new MutableDateCalc(year, month).goNextMonth)
282 def compare(that: BillingMonthInfo) = {
283 val ds = this.startMillis - that.startMillis
284 if(ds < 0) -1 else if(ds == 0) 0 else 1
288 override def equals(any: Any) = any match {
289 case that: BillingMonthInfo ⇒
290 this.year == that.year && this.month == that.month // normally everything else MUST be the same by construction
295 override def hashCode() = {
299 override def toString = "%s-%02d".format(year, month)
302 object BillingMonthInfo {
303 def fromMillis(millis: Long): BillingMonthInfo = {
304 fromDateCalc(new MutableDateCalc(millis))
307 def fromDateCalc(mdc: MutableDateCalc): BillingMonthInfo = {
308 val year = mdc.getYear
309 val month = mdc.getMonthOfYear
310 val startMillis = mdc.goStartOfThisMonth.getMillis
311 val stopMillis = mdc.goEndOfThisMonth.getMillis // no need to `copy` here, since we are discarding `mdc`
313 new BillingMonthInfo(year, month, startMillis, stopMillis)
317 sealed trait UserStateChangeReason {
319 * Return `true` if the result of the calculation should be stored back to the
320 * [[gr.grnet.aquarium.store.UserStateStore]].
323 def shouldStoreUserState: Boolean
325 def shouldStoreCalculatedWalletEntries: Boolean
327 def forPreviousBillingMonth: UserStateChangeReason
329 def calculateCreditsForImplicitlyTerminated: Boolean
331 def code: UserStateChangeReasonCodes.ChangeReasonCode
334 object UserStateChangeReasonCodes {
335 type ChangeReasonCode = Int
337 final val InitialCalculationCode = 1
338 final val NoSpecificChangeCode = 2
339 final val MonthlyBillingCode = 3
340 final val RealtimeBillingCode = 4
341 final val UserEventArrivalCode = 5
344 case object InitialUserStateCalculation extends UserStateChangeReason {
345 def shouldStoreUserState = true
347 def shouldStoreCalculatedWalletEntries = false
349 def forPreviousBillingMonth = this
351 def calculateCreditsForImplicitlyTerminated = false
353 def code = UserStateChangeReasonCodes.InitialCalculationCode
356 * A calculation made for no specific reason. Can be for testing, for example.
359 case object NoSpecificChangeReason extends UserStateChangeReason {
360 def shouldStoreUserState = false
362 def shouldStoreCalculatedWalletEntries = false
364 def forBillingMonthInfo(bmi: BillingMonthInfo) = this
366 def forPreviousBillingMonth = this
368 def calculateCreditsForImplicitlyTerminated = false
370 def code = UserStateChangeReasonCodes.NoSpecificChangeCode
374 * An authoritative calculation for the billing period.
376 * This marks a state for caching.
378 * @param billingMonthInfo
380 case class MonthlyBillingCalculation(billingMonthInfo: BillingMonthInfo) extends UserStateChangeReason {
381 def shouldStoreUserState = true
383 def shouldStoreCalculatedWalletEntries = true
385 def forPreviousBillingMonth = MonthlyBillingCalculation(billingMonthInfo.previousMonth)
387 def calculateCreditsForImplicitlyTerminated = true
389 def code = UserStateChangeReasonCodes.MonthlyBillingCode
393 * Used for the realtime billing calculation.
395 * @param forWhenMillis The time this calculation is for
397 case class RealtimeBillingCalculation(forWhenMillis: Long) extends UserStateChangeReason {
398 def shouldStoreUserState = false
400 def shouldStoreCalculatedWalletEntries = false
402 def forPreviousBillingMonth = this
404 def calculateCreditsForImplicitlyTerminated = false
406 def code = UserStateChangeReasonCodes.RealtimeBillingCode
409 case class UserEventArrival(userEvent: UserEvent) extends UserStateChangeReason {
410 def shouldStoreUserState = true
412 def shouldStoreCalculatedWalletEntries = false
414 def forPreviousBillingMonth = this
416 def calculateCreditsForImplicitlyTerminated = false
418 def code = UserStateChangeReasonCodes.UserEventArrivalCode