Sunday, October 4, 2015

Geo Dashboard - Geo event processing with WSO2 CEP 4.0.0

Complex Event Processing has been a key technology for a business to become a more agile connected business.In a fast changing competitive business model, supporting various business use cases is a huge task. With the release of newer version of WSO2 CEP, capabilities one could expect has grown immensely.  I'll be writing series of blogs regarding the newer and improved features of WSO2 CEP to cater these various use cases. 

Geo Event processing is one of the use cases implemented by leveraging new and improved capabilities of WSO2 CEP 4.0.0. You can download the newly released CEP from the product page[1].  

Said Use case is readily available for you to try out with step by steps[2]. In the sample once the producer runs you will see the real time spatial objects renders on the Jaggery application.I will explain how the inner functionalities are implemented and how they can be further extended easily .

Below shows the simple architecture behind the Geo application. 


To get more knowledge on how these functionalities are been implemented, we can ask our selves three questions.
 1. How and In what ways we can send data to CEP Engine.
 2. How we can identify meaningful events from received data
 3. How these meaningful events can be sent to outside so that we can identify them. 

Following describes three aspects with regards to the Geo Dashboard. So it will be easier for you to understand the terminology better.

Question One.

WSO2 CEP has many endpoints(Event Receivers) defined so that different sources can send data in different ways.

  • email
  • kafka
  • jms
  • mqtt
  • soap 
  • http etc. 

You can follow [3] to identify available event receivers and there functionalities in detail. Geo Dashboard is using TFL(Transport for London) streams which are provided by two API's . As showed above an Event Producer has been written to call these API's and send them to CEP using HTTP input event receiver. You must be wondering in what format we should send data to receiver.  

By default, event receivers process incoming messages in the XML, JSON, Text, Map (Key-value pairs), and WSO2Event formats. You can select desired format when you create the HTTP event receiver and map the incoming attributes to the stream. I will explained more on the concept of stream in the next question. So what happens in the the written custom producer is , its first call those TFL API's and get the data and creates a JSON object out of it. And send this JSON Object to WSO2 CEP HTTP endpoint. So the already created HTTP Event receiver has the mapping type of JSON which maps these JSON elements to a Stream.


Question Two.

In WSO2 CEP All receiving data is mapped to a Stream. Hence different mapping types as mentioned above. A Stream is a basic data model which has a definition. You can read following[4] which has in depth details of a stream. You can think of it as a object representation of data which those data are manipulated in the stages of processors inside CEP Engine.  

To identify meaningful events we need a way to specify rules for the incoming data. This is where SIDDHIQL language comes into play.Please follow [5] to understand more on SiddhiQL language. Below is a sample event stream definition of a Geo Event Stream.


{
"name": "rawInputStream",
"version": "1.0.0",
"nickName": "",
"description": "",
 "payloadData": [
            {
               "name": "id",
               "type": "STRING"
            },
            {
             "name": "timeStamp",
             "type": "LONG"
            },
            {
              "name": "latitude",
              "type": "DOUBLE"
            },
            {
              "name": "longitude",
              "type": "DOUBLE"
            },
            {
              "name": "type",
              "type": "STRING"
            },
            {
              "name": "speed",
               "type": "FLOAT"
            },
            {
              "name": "heading",
              "type": "FLOAT"
            }
             ]
}

You can think of this as the attributes of a car which is in a specific Geo location. So it has a latitude, longitude , speed etc . Since we have this data, a simple siddhiql query can be written to identify a event such as whether these cars exceeds certain speed limit. Then we can sent this trigger to outside where it shows that this specific car has exceeded the speed. Like wise Geo Dashboard has implemented several functionalities,

  • Speed Alert - User can specify a speed limit and spatial objects(cars) which violates will be notified
  • Proximity Alert - User can specify the radius of the area and then SiddhiQL queries checks whether a certain spatial object has come dangerously close to another. 
  • Within Alert - User can draw a certain area in the map and queries checks whether a spatial object is within that area.
  • Stationary Alert - User can draw a certain area and specify a time so that if a spatial object is within the area and is been the area for specified time, then an alert is generated
  • Congestion Alert - For this one of the TFL API's sends traffic congestion areas. So the user can draw a specific area where he wants to get traffic alerts on. So using SiddhiQL we check whether the incoming traffic areas are within the user drawn area. 
Question Three.

So now we have identified meaningful events using SiddhiQL and want to send them out so that they can be shown to the end user. For this purpose WSO2CEP has implemented many publishers as per the receivers mentioned above. 

  • Cassandra 
  • Http
  • kafka
  • RDBMS
  • sms
  • soap etc.

You can follow[6] to identify range of supported Publishers readily available in WSO2 CEP. Geo Dashboard is using Websocket Local publisher and RDBMS Publisher to send data to client side and persist data. 

In order to present data A web application is implemented using JAGGERY which is a open-source and released under Apache 2.0 and a product of WSO2.[7] It has map of London which renders vehicles and bus stops according to events received from TFL streams as mentioned above. 



Above is a Snapshot of Geo dashboard which showing vehicles(blue traingles) and bus stops (orange markers).  Jaggery application has a websocket connected to the websocket local publisher. So that CEP sent output data is received from client side. Here the leaflet js is used to render the maps and do other Geo related functionalities.