Skip to content

Commit 5f056b6

Browse files
Merge pull request #61 from delphi-hub/eventTimeStamps
Implementation of TimeStamps for events #56
2 parents 4af50ec + d62dd0f commit 5f056b6

File tree

1 file changed

+41
-16
lines changed
  • src/main/scala/de/upb/cs/swt/delphi/instanceregistry/io/swagger/client/model

1 file changed

+41
-16
lines changed

src/main/scala/de/upb/cs/swt/delphi/instanceregistry/io/swagger/client/model/Event.scala

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model
22

33
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
4+
import akka.http.scaladsl.model.DateTime
45
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.EventEnums.EventType
56
import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.InstanceEnums.ComponentType
67
import spray.json.{DefaultJsonProtocol, DeserializationException, JsObject, JsString, JsValue, JsonFormat}
@@ -79,9 +80,31 @@ trait EventJsonSupport extends SprayJsonSupport with DefaultJsonProtocol with In
7980
}
8081

8182
}
83+
//Custom JSON format for event TimeStamp.
84+
implicit val timestampFormat: JsonFormat[DateTime] = new JsonFormat[DateTime] {
85+
/**
86+
* Custom write method for serialization of DateTime
87+
* @param obj DateTime object to serialize
88+
* @throws DeserializationException Exception in case of wrong input
89+
*/
90+
override def write(obj: DateTime) = JsString(obj.toIsoDateTimeString())
91+
/**
92+
* Custom read method for deserialization of DateTime
93+
* @param json JsValue that is to be deserialized
94+
* @throws DeserializationException Exception when JsValue is in incorrect format
95+
*/
96+
override def read(json: JsValue): DateTime = json match {
97+
case JsString(value) =>
98+
DateTime.fromIsoDateTimeString(value) match {
99+
case Some(date) => date
100+
case _ => throw DeserializationException("Failed to parse date time [" + value + "].")
101+
}
102+
case _ => throw DeserializationException("Failed to parse json string [" + json + "].")
103+
}
104+
}
82105

83106
//JSON format for RegistryEvents
84-
implicit val eventFormat : JsonFormat[RegistryEvent] = jsonFormat2(RegistryEvent)
107+
implicit val eventFormat : JsonFormat[RegistryEvent] = jsonFormat3(RegistryEvent)
85108

86109
//JSON format for an NumbersChangedPayload
87110
implicit val numbersChangedPayloadFormat: JsonFormat[NumbersChangedPayload] = jsonFormat2(NumbersChangedPayload)
@@ -103,10 +126,12 @@ trait EventJsonSupport extends SprayJsonSupport with DefaultJsonProtocol with In
103126
* The RegistryEvent used for communicating with the management application
104127
* @param eventType Type of the event
105128
* @param payload Payload of the event, depends on the type
129+
* @param timestamp TimeStamp of the event
106130
*/
107131
final case class RegistryEvent (
108132
eventType: EventType.Value,
109-
payload: RegistryEventPayload
133+
payload: RegistryEventPayload,
134+
timestamp: DateTime
110135
)
111136

112137
/**
@@ -118,59 +143,59 @@ object RegistryEventFactory {
118143
* Creates a new NumbersChangedEvent. Sets EventType and payload accordingly.
119144
* @param componentType ComponentType which's numbers have been updated
120145
* @param newNumber New number of components of the specified type
121-
* @return RegistryEvent with the respective type and payload.
146+
* @return RegistryEvent with the respective respective type, payload and current timestamp.
122147
*/
123148
def createNumbersChangedEvent(componentType: ComponentType, newNumber: Int) : RegistryEvent =
124-
RegistryEvent(EventType.NumbersChangedEvent, NumbersChangedPayload(componentType, newNumber))
149+
RegistryEvent(EventType.NumbersChangedEvent, NumbersChangedPayload(componentType, newNumber), DateTime.now)
125150

126151
/**
127152
* Creates a new InstanceAddedEvent. Sets EventType and payload accordingly.
128153
* @param instance Instance that has been added.
129-
* @return RegistryEvent with the respective type and payload.
154+
* @return RegistryEvent with the respective type, payload and current timestamp.
130155
*/
131156
def createInstanceAddedEvent(instance: Instance) : RegistryEvent =
132-
RegistryEvent(EventType.InstanceAddedEvent, InstancePayload(instance))
157+
RegistryEvent(EventType.InstanceAddedEvent, InstancePayload(instance), DateTime.now)
133158

134159
/**
135160
* Creates a new InstanceRemovedEvent. Sets EventType and payload accordingly.
136161
* @param instance Instance that has been removed.
137-
* @return RegistryEvent with the respective type and payload.
162+
* @return RegistryEvent with the respective type, payload and current timestamp.
138163
*/
139164
def createInstanceRemovedEvent(instance: Instance) : RegistryEvent =
140-
RegistryEvent(EventType.InstanceRemovedEvent, InstancePayload(instance))
165+
RegistryEvent(EventType.InstanceRemovedEvent, InstancePayload(instance), DateTime.now)
141166

142167
/**
143168
* Creates a new StateChangedEvent. Sets EventType and payload accordingly.
144169
* @param instance Instance which's state was changed.
145-
* @return RegistryEvent with tht respective type and payload.
170+
* @return RegistryEvent with tht respective type, payload and current timestamp.
146171
*/
147172
def createStateChangedEvent(instance: Instance) : RegistryEvent =
148-
RegistryEvent(EventType.StateChangedEvent, InstancePayload(instance))
173+
RegistryEvent(EventType.StateChangedEvent, InstancePayload(instance), DateTime.now)
149174

150175
/**
151176
* Creates a new DockerOperationErrorEvent. Sets EventType and payload accordingly.
152177
* @param affectedInstance Option[Instance] containing the instance that may be affected
153178
* @param message Error message
154-
* @return RegistryEvent with the respective type and payload.
179+
* @return RegistryEvent with the respective respective type, payload and current timestamp.
155180
*/
156181
def createDockerOperationErrorEvent(affectedInstance: Option[Instance], message: String) : RegistryEvent =
157-
RegistryEvent(EventType.DockerOperationErrorEvent, DockerOperationErrorPayload(affectedInstance, message))
182+
RegistryEvent(EventType.DockerOperationErrorEvent, DockerOperationErrorPayload(affectedInstance, message),DateTime.now)
158183

159184
/**
160185
* Creates a new LinkAddedEvent. Sets EventType and payload accordingly
161186
* @param link Link that was added
162-
* @return RegistryEvent with the respective type and payload
187+
* @return RegistryEvent with the respective type, payload and current timestamp.
163188
*/
164189
def createLinkAddedEvent(link: InstanceLink, instanceFrom: Instance, instanceTo: Instance) : RegistryEvent =
165-
RegistryEvent(EventType.LinkAddedEvent, InstanceLinkPayload(link, instanceFrom, instanceTo))
190+
RegistryEvent(EventType.LinkAddedEvent, InstanceLinkPayload(link, instanceFrom, instanceTo),DateTime.now)
166191

167192
/**
168193
* Creates a new LinkStateChangedEvent. Sets EventType and payload accordingly.
169194
* @param link Link whichs state has been changed
170-
* @return RegistryEvent with the respective type and payload
195+
* @return RegistryEvent with the respective type, payload and current timestamp.
171196
*/
172197
def createLinkStateChangedEvent(link: InstanceLink, instanceFrom: Instance, instanceTo: Instance) : RegistryEvent =
173-
RegistryEvent(EventType.LinkStateChangedEvent, InstanceLinkPayload(link, instanceFrom, instanceTo))
198+
RegistryEvent(EventType.LinkStateChangedEvent, InstanceLinkPayload(link, instanceFrom, instanceTo),DateTime.now)
174199
}
175200

176201
/**

0 commit comments

Comments
 (0)