2 * Copyright 2011 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.logic.events
38 import gr.grnet.aquarium.logic.accounting.{AccountingEventType, AccountingEvent}
41 object EventProcessor {
43 def process(events: List[Event])
44 (evtFilter: (Event) => Boolean): List[AccountingEvent] = {
46 val evts = events.filter(evtFilter)
47 val dummy = new AccountingEvent(AccountingEventType.VMTime, new Date(), new Date(), 0, 0, List())
49 evts.sortBy(_.when).map {
53 case v: VMStarted => //Find stop event and calculate resource usage
54 val stop = findVMStopEvent(evts, v)
55 val time = stop match {
56 case Some(x) => x.when
57 case None => new Date() //Now
59 val stopid = stop match {
63 val totaltime = time.getTime - v.w.getTime
65 new AccountingEvent(AccountingEventType.VMTime, e.when, time, u, totaltime, List(v.id, stopid))
66 // case v : VMStarted =>None
67 // case v : VMStopped =>None
68 case v: DiskSpaceChanged =>
70 new AccountingEvent(AccountingEventType.DiskSpace, e.when, e.when, u, v.bytes, List(v.id()))
71 case v: DataUploaded =>
73 new AccountingEvent(AccountingEventType.NetDataUp, e.when, e.when, u, v.bytes, List(v.id()))
74 case v: DataDownloaded =>
76 new AccountingEvent(AccountingEventType.NetDataDown, e.when, e.when, u, v.bytes, List(v.id()))
77 // case v : SSaasVMCreated => None
78 // case v : SSaasVMStarted =>None
79 // case v : SSaasVMStopped =>None
82 }.filter(p => p != dummy) //Remove dummy events
85 /**Find a the first corresponding VM stop event in a list of messages*/
86 def findVMStopEvent(events: List[Event], v: VMStarted): Option[VMStopped] = {
91 f.when().compareTo(v.when()) > 0) &&
92 f.isInstanceOf[VMStopped]
93 }.asInstanceOf[Option[VMStopped]]