Process Data
After saving the token from the Authentication step, we can now use it to make another request for actually getting the collisions. To send another HTTP request, we have to select an HTTPCaller block and connect the output of the JSONExtractor mentioned here to the input of the HTTPCaller. The procedure for finding the blocks and settings is explained here.
HTTPCaller Settings
- Set the
Request URLto https://integration.cloud.tes.ca/api/collision/v2/CollisionsByDate - Select
POSTinHTTP Methodselect box. - In the
Headerssection, you need to writeAuthorizationin theNamecolumn. - In the
Valuecolumn, you need to first writeBearerand then click on the down-arrow icon and selectAttribute Valueand thentoken.
Th final view of the Header table:
- And click on
OK
Like before, we have to connect the output of the HTTPCaller to the input of a JSONExtractor to save the data we need from the response. The properties we are interested in, are results and errorMessages.
JSONExtractor Settings
- Set the
Input SourcetoJSON Document. - Click on the down-arrow icon next to the Input Source. From the dropdown menu, choose
Attribute Valueand then select_response_bodywhich is actually the response body of the HTTPCaller saved in_response_bodyattribute.
- Now, we have to extract the JSON properties we want to save in the attributes.
- In the
Target Attributecolumn, write theresultsand its correspondingJSON Queryvalue which isjson[“results”]. - Write another target attribute named
errorsand its correspondingJSON Querywhich isjson[“errorMessages”]. - Click on
OK
With errorMessages, we can check whether we have an error or not. The results is the collision data.
Next, we will select an AttributeKeeper from the Transformers to keep the results attribute in memory to lower the processing time later on.
AttributeKeeper Settings
- In
Generalsection, click on down-arrow icon and from the shown drop-down menu, chooseSelect Attributes.
- From the list, choose
results.
- Click on
OK
Next step is to use a JSONFragmenter and connect it to the output of the AttributeKeeper. JSONFragmentor is used to extract portions of Json into new FME features.
JSONFragmentor Settings
- In the Source section, Select
JSON attributefrom theInput Sourceselect box. - Select
resultsfrom theJSON Attributeselect box.
- In the General section, Click on the three dots icon next to the
JSON Queryinput.
- Write
json[*], which returns the whole JSON object.
- Select
JSONfor theFragment as Formatselect box. - Select
Yesfor theReject Features which Produce No Fragment.
- In the Flattening section, Select
Yesfor theFlatten Query Results into Attributesselect box. - Select
Yesfrom theRecursively Faltten Objects/Arraysselect box. - Click on the three-dots icon next to the
Attributes to Exposeto choose which attributes you want to save. - Here, we keep a set of properties in the attributes since we want to use them in the
Chartand theTable. - Click on the first empty row, write
dayOfWeek,accidentDateandaccNumber.
- And click on
OK
The next step is to save the property dayOfWeek. As we saw a lot of times, we need to add an AttributeKeeper and connect the output of the JSONFragmenter to AttributeKeeper's input.
AttributeKeeper Settings
- In the
Generalsection, click on the icon with three dots, besideAttributes to Keep.
- Choose
dayOfWeek,accidentDateandaccNumber. and click onOK.
At this point, we have to connect the output of the AttributeKeeper to two different blocks. One generates the Chart, and the other generates the Table.
For generating a chart
We have to add an Aggregator to aggregate our data. Depending on what type of report you want to generate, you can customize the Aggregator Settings. Like before, we have to connect the AttrubuteKeeper to Aggregator output.
Aggregator Settings
- Check the Group Processing checkbox.
Click on the icon with three dots, and choose
dayOfWeek. SelectProcess at Endfrom theGroup by Modeoptions.
- In the General section, select the
ModetoAttributes only. - Write
dayOfWeekCountin theCount Attributeinput.dayOfWeekCountwill be the name of the attribute we are making by grouping the data. - In the Attribute Accumulation section, select
Merge Incoming Attributesfrom theAccumulation Modeselect box's options. - Select
Merge Incoming Attributesfrom theAccumulation Modeselect box's options in theAttribute Accumulationsection.
For Generating a Table
- Add a
DateTimeConverterfrom thetransformersto convert the date format of theaccidentDateproperty. Connect the output of theAttributeKeeprto the input of theDateTimeConverter. - In the General section, select the
accidentDatefor theDatetime Attributeinput. - Don't change the
Input Formatsince the automatically detecting format is chosen by default. - Choose the
Output Formatyou want from its select box's options. - Click on
OK
After that, we need to add a Sorter to sort the accidentDate and connect it to the DateTimeConverter.
Sorter Settings
- In the Sort By section, select the
accidentDateas theAttributecolumn's value, selectNumericfor theAlpha/Numcolumn and selectDescendingorAscendingfor theOrdercolumn. - Click on
OK
We are done with processing the data, now we have to add two HTMLReportGenerators and connect them to the blocks of Table data and Chart data.
You can follow the tutorial for creating the report here