@App:name("ReceiveAndCount")

@App:description('Receive events via HTTP transport and view the output on the console')

/*

Purpose:
    This application demonstrates how to configure WSO2 Stream Processor to receive events via HTTP transport and view the output on the console. The count of all events arriving to the stream is calculated.

Prerequisites:
    1) Save this sample

Executing the Sample:
    1) Start the Siddhi application by clicking on 'Run'
    2) If the Siddhi application starts successfully, the following messages would be shown on the console
        * Source Listener has created for url http://localhost:8006/inputStream
        * ReceiveAndCount.siddhi - Started Successfully!

    Notes:
    If you edit this application while it's running, stop the application -> Save -> Start.
    If the message "Source Listener has created for url http://localhost:8006/inputStream" does not appear,it could be due to port 8006, defined in the Siddhi application is already being used by a different program. To resolve this issue, please do the following,
    * Stop this Siddhi application (Click 'Run' on menu bar -> 'Stop')
    * Change the port 8006 to an unused port, in this Siddhi application's source configuration.
    * Start the application and check whether the specified messages appear on the console

Testing the Sample:
    Publish events to the http endpoint defined by 'receiver.url' in Source configuration. You may publish events with curl command or Postman application as follows,
    1) Publish events with curl command:
        Publish few events in json format to the http endpoint as follows (The values for name, age and country attributes can be changed as desired)
        curl -X POST -d "{\"event\":{\"name\":\"John\",\"age\":20,\"country\":\"SL\"}}" http://localhost:8006/inputStream --header "Content-Type:application/json"

    2) Publish events with Postman:
        a) Install 'Postman' application from Chrome web store
        b) Launch the application
        c) Make a 'Post' request to 'http://localhost:8006/inputStream' endpoint. Set the Content-Type to 'application/json' and set the request body in json format as follows,
            {
                "event": {
                    "name": "John",
                    "age": 20,
                    "country": "SL"
                }
            }

Viewing the Results:
    See the output on the console. Note how the count increments with every event you send.

    Note:
    Stop this Siddhi application, once you are done with the execution

*/

@Source(type = 'http',
        receiver.url='http://localhost:8006/inputStream',
        basic.auth.enabled='false',
        @map(type='json'))
define stream CustomerInformationStream(name string, age int, country string);

@sink(type='log')
define stream CountStream(name string, age int, country string, count long);

-- Count the incoming events
@info(name='query1')
from CustomerInformationStream
select name, age, country, count() as count
insert into CountStream;



