Change effective timeslot method sig to accept timeslots
authorGeorgios Gousios <gousiosg@gmail.com>
Mon, 20 Feb 2012 15:05:49 +0000 (17:05 +0200)
committerGeorgios Gousios <gousiosg@gmail.com>
Mon, 20 Feb 2012 15:05:49 +0000 (17:05 +0200)
This is to ensure that for all expansions, the end date is after
the start date.

src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLUtils.scala
src/test/resources/policy.yaml
src/test/scala/gr/grnet/aquarium/logic/test/AccountingTest.scala
src/test/scala/gr/grnet/aquarium/logic/test/DSLUtilsTest.scala
src/test/scala/gr/grnet/aquarium/logic/test/PerfTest.scala

index bd742a9..f5a2823 100644 (file)
@@ -83,9 +83,18 @@ trait DSLUtils extends DateUtils {
       case _ => tbi.get
     }
 
+    // The following check that the policy is applicable within
+    // the timeframe of the requested resolution
+    assert(timeslot.to.before(policy.effective.to.getOrElse(maxdate)),
+      "Policy effectivity ends before expansion timeslot")
+    assert(timeslot.from.after(policy.effective.from),
+      "Policy effectivity starts after expansion timeslot")
+
     val eff = allEffectiveTimeslots(policy.effective,
-      oneYearBack(timeslot.from, policy.effective.from),
-      oneYearAhead (timeslot.to, policy.effective.to.getOrElse(maxdate)))
+      Timeslot(oneYearBack(timeslot.from, policy.effective.from),
+      oneYearAhead (timeslot.to, policy.effective.to.getOrElse(maxdate))))
+
+    logger.debug("effective timeslots: %d".format(eff.size))
 
     immutable.SortedMap[Timeslot, T]() ++
       timeslot.overlappingTimeslots(eff).flatMap {
@@ -154,24 +163,24 @@ trait DSLUtils extends DateUtils {
    * Get a list of all timeslots within which a timeframe
    * is effective, whithin the provided time bounds.
    */
-  def allEffectiveTimeslots(spec: DSLTimeFrame, from: Date, to: Date):
+  def allEffectiveTimeslots(spec: DSLTimeFrame, t: Timeslot):
   List[Timeslot] = {
 
     //A timeframe with no repetition defined
     if (spec.repeat.isEmpty) {
-      val fromDate = if (spec.from.before(from)) from else spec.from
-      val toDate = if (spec.to.getOrElse(to).after(to)) to else spec.to.getOrElse(to)
+      val fromDate = if (spec.from.before(t.from)) t.from else spec.from
+      val toDate = if (spec.to.getOrElse(t.to).after(t.to)) t.to else spec.to.getOrElse(t.to)
       return List(Timeslot(fromDate, toDate))
     }
 
     val l = spec.repeat.flatMap {
-      r => effectiveTimeslots(r, from, Some(to))
+      r => effectiveTimeslots(r, t.from, Some(t.to))
     } sortWith sorter
     mergeOverlaps(l)
   }
 
   /**
-   * Get a list of all time periods within which a time frame is active.
+   * Get a list of all timeslots within which a time frame is active.
    * If the to date is None, the expansion takes place within a timeframe
    * between `from .. from` + 1 year. The result is returned sorted by
    * timeframe start date.
@@ -179,8 +188,6 @@ trait DSLUtils extends DateUtils {
   def effectiveTimeslots(spec: DSLTimeFrameRepeat, from: Date, to: Option[Date]):
     List[Timeslot] = {
 
-    assert(spec.start.size == spec.end.size)
-
     val endDate = to match {
       case None => //One year from now
         val c = new GregorianCalendar()
index 3b276cc..d2ab230 100644 (file)
@@ -52,7 +52,7 @@ aquariumpolicy:
           - start: "00 00 18 * *"
             end:   "00 00 20 * *"
         from: 0
-        to: 124443
+        to: 1424442196 # Fri, 20 Feb 2015 14:23:16 GMT
 
   pricelists:
     - pricelist: 
@@ -136,3 +136,10 @@ aquariumpolicy:
           from: 1322061528 #Wed 23 Nov 2011 17:18
           to: 1324661839 #Fri, 23 Dec 2011 17:37
 
+    - agreement:
+      overrides: default
+      name: complextimeslots
+      creditplan: every10days
+      pricelist: foobar
+      algorithm: freedisk
+
index 1a3acea..8420ad5 100644 (file)
@@ -55,13 +55,14 @@ class AccountingTest extends DSLTestBase with Accounting with TestMethods {
   def testAlignTimeslots() {
     before
     val from = new Date(1322555880000L) //Tue, 29 Nov 2011 10:38:00 EET
-    val to = new Date(1322689082000L) //Wed, 30 Nov 2011 23:38:02 EET
-    val agr = dsl.findAgreement("scaledbandwidth").get
-    val a = resolveEffectiveAlgorithmsForTimeslot(Timeslot(from, to), agr).keys.toList
-    val b = resolveEffectivePricelistsForTimeslot(Timeslot(from, to), agr).keys.toList
+    val to = new Date(1322689082000L)  //Wed, 30 Nov 2011 23:38:02 EET
+    val agr = dsl.findAgreement("complextimeslots").get
+    val a = resolveEffectiveAlgorithmsForTimeslot(Timeslot(from, to), agr).keySet.toList
+    val b = resolveEffectivePricelistsForTimeslot(Timeslot(from, to), agr).keySet.toList
 
     val result = alignTimeslots(a, b)
-    assertEquals(12, result.size)
+    assertEquals(9, result.size)
+    assertEquals(result.last,  b.last)
   }
 
   @Test
@@ -95,7 +96,7 @@ class AccountingTest extends DSLTestBase with Accounting with TestMethods {
 
     //Simple, continuous resource
     var evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "bandwidthup", "1", "1", 123, Map())
-    var wallet = chargeEvent(evt, agr, 112, new Date(1325755902000L), List())
+    var wallet = chargeEvent(evt, agr, 112, new Date(1325755902000L), List(), None)
     wallet match {
       case Just(x) => assertEquals(2, x.size)
       case _ => fail("No results returned")
@@ -103,11 +104,11 @@ class AccountingTest extends DSLTestBase with Accounting with TestMethods {
 
     //Complex resource event without details, should fail
     evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "vmtime", "1", "1", 1, Map())
-    assertFailed[Exception, List[WalletEntry]](chargeEvent(evt, agr, 1, new Date(1325755902000L), List()))
+    assertFailed[Exception, List[WalletEntry]](chargeEvent(evt, agr, 1, new Date(1325755902000L), List(), None))
 
     //Complex, onoff resource
     evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "vmtime", "1", "1", 1, Map("vmid" -> "3"))
-    wallet = chargeEvent(evt, agr, 0, new Date(1325755902000L), List())
+    wallet = chargeEvent(evt, agr, 0, new Date(1325755902000L), List(), None)
     wallet match {
       case Just(x) => assertEquals(2, x.size)
       case _ => fail("No results returned")
@@ -115,11 +116,11 @@ class AccountingTest extends DSLTestBase with Accounting with TestMethods {
 
     //Complex, onoff resource, with wrong states, should fail
     evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "vmtime", "1", "1", 1, Map("vmid" -> "3"))
-    assertFailed[Exception, List[WalletEntry]](chargeEvent(evt, agr, 1, new Date(1325755902000L), List()))
+    assertFailed[Exception, List[WalletEntry]](chargeEvent(evt, agr, 1, new Date(1325755902000L), List(), None))
 
     //Simple, discrete resource
     evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "bookpages", "1", "1", 120, Map())
-    wallet = chargeEvent(evt, agr, 15, new Date(1325755902000L), List())
+    wallet = chargeEvent(evt, agr, 15, new Date(1325755902000L), List(), None)
     wallet match {
       case Just(x) => assertEquals(1, x.size)
       case _ => fail("No results returned")
@@ -127,12 +128,12 @@ class AccountingTest extends DSLTestBase with Accounting with TestMethods {
 
     //Simple, discrete resource, time of last update equal to current event's occurred time
     evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "bookpages", "1", "1", 120, Map())
-    wallet = chargeEvent(evt, agr, 15, new Date(1325762772000L), List())
+    wallet = chargeEvent(evt, agr, 15, new Date(1325762772000L), List(), None)
     assertEquals(1, wallet.getOr(List(WalletEntry.zero, WalletEntry.zero)).size)
 
     //Simple, continuous resource, time of last update equal to current event's occurred time
     evt = ResourceEvent("123", 1325762772000L, 1325762774000L, "12", "1", "bandwidthup", "1", "1", 123, Map())
-    wallet = chargeEvent(evt, agr, 15, new Date(1325762772000L), List())
+    wallet = chargeEvent(evt, agr, 15, new Date(1325762772000L), List(), None)
     assertEquals(0, wallet.getOr(List(WalletEntry.zero)).size)
   }
 }
\ No newline at end of file
index 43c926e..6ecca34 100644 (file)
@@ -182,12 +182,12 @@ class DSLUtilsTest extends DSLTestBase with DSLUtils with TestMethods {
       "00 20 * * 5")
     val tf = DSLTimeFrame(from, None, List(repeat1, repeat2))
 
-    var result = allEffectiveTimeslots(tf, from, to)
+    var result = allEffectiveTimeslots(tf, Timeslot(from, to))
     assertEquals(36, result.size)
     testSuccessiveTimeslots(result)
 
     result = allEffectiveTimeslots(DSLTimeFrame(new Date(0), None, List()),
-      new Date(14), new Date(40))
+      Timeslot(new Date(14), new Date(40)))
     assertEquals(1, result.size)
   }
 
index 583a535..abe12cd 100644 (file)
 package gr.grnet.aquarium.logic.test
 
 import org.junit.Test
-import gr.grnet.aquarium.logic.accounting.dsl.{DSLTimeFrame, DSLTimeFrameRepeat, DSL, DSLUtils}
 import java.util.{Date}
 import org.junit.Assume._
 import gr.grnet.aquarium.LogicTestsAssumptions
+import gr.grnet.aquarium.logic.accounting.dsl._
 
 /**
  * Performance tests for various critical path functions.
@@ -82,7 +82,7 @@ class PerfTest extends DSLUtils with DSL {
         while (rndEnd.before(rndStart)) rndEnd = new Date((min + (scala.math.random * (max - min) + 1)).toLong)
         val tf = DSLTimeFrame(rndStart, Some(rndEnd), List(repeat1, repeat2))
 
-        numResolved += allEffectiveTimeslots(tf, new Date(min), new Date(max)).size
+        numResolved += allEffectiveTimeslots(tf, Timeslot(new Date(min), new Date(max))).size
     }
 
     var total = System.currentTimeMillis() - start