1 package gr.grnet.aquarium.user
4 import gr.grnet.aquarium.Configurator
5 import gr.grnet.aquarium.store.memory.MemStore
6 import gr.grnet.aquarium.util.date.DateCalculator
7 import gr.grnet.aquarium.logic.accounting.dsl._
9 import gr.grnet.aquarium.logic.accounting.Accounting
10 import com.ckkloverdos.maybe.NoVal
11 import simulation.{ConcurrentVMLocalUIDGenerator, ClientServiceSim, UserSim}
16 * @author Christos KK Loverdos <loverdos@gmail.com>
18 class UserStateComputationsTest {
21 val StartOfBillingYearDateCalc = new DateCalculator(2012, 1, 1)
22 // println("StartOfBillingYearDateCalc = %s".format(StartOfBillingYearDateCalc))
23 val UserCreationDateCalc = StartOfBillingYearDateCalc.copy.goMinusMonths(2)
24 // println("UserCreationDateCalc = %s".format(UserCreationDateCalc))
26 // TODO: integrate this with the rest of the simulation stuff
27 // TODO: since, right now, the resource strings have to be given twice
28 val VMTIME_RESOURCE = DSLComplexResource("vmtime", "Hr", OnOffCostPolicy, "")
29 val DISKSPACE_RESOURCE = DSLComplexResource("diskspace", "MB/Hr", ContinuousCostPolicy, "")
31 val DEFAULT_RESOURCES_MAP = new DSLResourcesMap(VMTIME_RESOURCE :: DISKSPACE_RESOURCE :: Nil)
33 val FOR_EVER = DSLTimeFrame(new Date(0), None, Nil)
34 val THE_PRICES_MAP: Map[DSLResource, Double] = Map(VMTIME_RESOURCE -> 1.0, DISKSPACE_RESOURCE -> 1.0)
36 val THE_PRICE_LIST = DSLPriceList("default", None, THE_PRICES_MAP, FOR_EVER)
37 val THE_CREDIT_PLAN = DSLCreditPlan("default", None, 100, Nil, "", FOR_EVER)
38 val THE_ALGORITHM = DSLAlgorithm("default", None, Map(), FOR_EVER)
39 val THE_AGREEMENT = DSLAgreement("default", None, THE_ALGORITHM, THE_PRICE_LIST, THE_CREDIT_PLAN)
41 val THE_RESOURCES = DEFAULT_RESOURCES_MAP.toResourcesList
43 val DEFAULT_POLICY = DSLPolicy(
45 THE_PRICE_LIST :: Nil,
47 THE_CREDIT_PLAN :: Nil,
51 val computer = new UserStateComputations
53 val mc = Configurator.MasterConfigurator.withStoreProviderClass(classOf[MemStore])
54 val storeProvider = mc.storeProvider
55 val userStateStore = storeProvider.userStateStore
56 val resourceEventStore = storeProvider.resourceEventStore
57 val policyStore = storeProvider.policyStore
59 // A new user is created on 2012-01-15 00:00:00.000
60 val christos = UserSim("Christos", UserCreationDateCalc.toDate, storeProvider.resourceEventStore)
62 // There are two client services, synnefo and pithos.
63 val uidGenerator = new ConcurrentVMLocalUIDGenerator
64 val synnefo = ClientServiceSim("synnefo")(uidGenerator)
65 val pithos = ClientServiceSim("pithos")(uidGenerator)
68 // - synnefo is for VMTime and
69 // - pithos is for Diskspace
70 val vm = synnefo.newVMTime(christos, "VM.1")
71 val disk = pithos.newDiskspace(christos, "DISK.1")
73 // Let's create our dates of interest
74 val vmStartDateCalc = StartOfBillingYearDateCalc.copy.goPlusDays(1).goPlusHours(1)
75 // println("vmStartDateCalc = %s".format(vmStartDateCalc))
76 // 2012-01-16 01:00:00.000
77 val vmStartDate = vmStartDateCalc.toDate
79 // Within January, create one VM ON-OFF ...
80 val onOff1_M = vm.newONOFF(vmStartDate, 9)
82 val diskConsumptionDateCalc = StartOfBillingYearDateCalc.copy.goPlusHours(3)
83 // 2012-01-16 04:00:00.000
84 val diskConsumptionDate1 = diskConsumptionDateCalc.toDate
85 // 2012-01-17 05:00:00.000
86 val diskConsumptionDate2 = diskConsumptionDateCalc.copy.goPlusDays(1).goPlusHours(1).toDate
88 // ... and two diskspace changes
89 val consume1_M = disk.consumeMB(diskConsumptionDate1, 99)
90 val consume2_M = disk.consumeMB(diskConsumptionDate2, 23)
92 // ... and one "future" event
93 // 2012-02-07 07:07:07.007
95 StartOfBillingYearDateCalc.copy.
96 goNextMonth.goPlusDays(6).
100 goPlusMillis(7).toDate,
103 println("=============================")
105 event <- christos.myResourceEventsByReceivedDate
107 val when = new DateCalculator(event.receivedMillis)
108 val resource = event.resource
109 val instanceId = event.instanceId
110 val value = event.beautifyValue(DEFAULT_RESOURCES_MAP)
111 println("%s [%s, %s] %s".format(when, resource, instanceId, value))
113 println("=============================")
115 val userStateM = computer.doFullMonthlyBilling(
117 StartOfBillingYearDateCalc.getYear,
118 StartOfBillingYearDateCalc.getMonthOfYear,
122 christos.userCreationDate.getTime,
123 computer.createFirstUserState(christos.userId),
124 computer.createFirstUserState(christos.userId),
126 DEFAULT_RESOURCES_MAP,
130 println("!! userStateM = %s".format(userStateM))
131 userStateM.forFailed { failed ⇒
132 failed.exception.printStackTrace()