This operation computes the duration between timestamps of correlated events.
About this task
Duration operations require you to define, in the processing configuration, a `started` and a
`completed` condition. The resulting field in the summary is the difference between the timestamp of
event ending and of event starting, in milliseconds. The duration operation provides the field name
to store the resulting field.
Procedure
-
Create a folder named test-app containing the following file:
processing-conf.json - a file defining a Kafka ingress, a context with duration
operation and Kafka egress.
{
"ingresses": [
{
"uid": "I0001",
"type": "kafka",
"topic": "icp4ba-bai-orders"
}
],
"contexts": [
{
"name": "orders",
"correlationPath": {
"type": "jslt-inline",
"expression": ".id"
},
"operations": [
{
"uid": "C0001",
"type": "duration",
"name": "time-elapsed",
"conditions": {
"started": {
"type": "jslt-inline",
"expression": ".status == \"ORDER_CREATED\""
},
"completed": {
"type": "jslt-inline",
"expression": ".status == \"ORDER_DONE\""
}
},
"egressRefs": ["time-elapsed-topic"]
}
]
}
],
"egresses": [
{
"uid": "E0010",
"type": "kafka",
"name": "time-elapsed-topic",
"topic": "icp4ba-bai-time-elapsed-egress"
}
],
"settings": {
"verboseLogs": true
}
}
- Create a folder named
test-data containing order.events.txt file with
following content:
{ "id": "order_1", "kind": "order", "timestamp": "2021-05-07T00:00:01.000-04:00", "status": "ORDER_CREATED" }
{ "id": "order_2", "kind": "order", "timestamp": "2021-05-07T00:00:01.000-04:00", "status": "ORDER_CREATED" }
{ "id": "order_3", "kind": "order", "timestamp": "2021-05-07T00:00:07.000-04:00", "status": "ORDER_DONE", "data": { "paid": true } }
{ "id": "order_1", "kind": "order", "timestamp": "2021-05-07T00:00:07.000-04:00", "status": "ORDER_DONE", "data": { "paid": true } }
- Create a Processing Application.
management-cli processing-app create --name="test-app"
- Import the configuration.
management-cli processing-conf import --name="test-app" --directory="./test-app"
- Deploy the application.
management-cli processing-app deploy --name="test-app"
- Use the events processed by the application to check the result of the processing. Run
the following command in a dedicated terminal:
management-cli kafka consumer-json --topic=icp4ba-bai-time-elapsed-egress
The following message appears: Retrieving events from topic
icp4ba-bai-time-elapsed-egress.
Events output to the egress topic is displayed in this terminal.
- Send the events.
management-cli kafka producer-json --topic=icp4ba-bai-orders --file=./test-data/order.events.txt --batch
Results
The start and end events are correlated by the value of their id attribute. That
is why the duration operation only applies to events that match the start and end conditions, and
have the same id value.
In this example, after sending events with the same id value and meeting the
started and completed conditions, we expect this type of summary result:
{
"correlationId":"order_1",
"timestamp":"2021-05-07T00:00:07.000-04:00",
"startTimestamp":"2021-05-07T00:00:01.000-04:00",
"endTimestamp":"2021-05-07T00:00:07.000-04:00",
"time-elapsed":6000
}
What to do next
See Cleaning up the Processing
Application to clean all the resources after a deployment.