Step 9: Create and Configure the Circuit

As discussed in the Introduction, the circuit automates the executions of all the functions based on the workflow we design.

Therefore, after the user provides the input in the client application, the functions we configured will execute in a specific order and the email will sent to the user with the results they require.

To create a circuit from the console:

  1. Navigate to Circuits under Compute and click Create your first circuit.

  2. Enter a name for the circuit and click Create. You can enter any name.

The circuit will be created and the Builder View will open. You can learn about Circuits in detail from the Circuits help page.


Before we configure the circuit, perform the following action:

  1. Click on the arrow next to the circuit name on the top-left corner to navigate to the Circuits page. Copy the Circuit ID displayed in the page.


  1. Now, navigate to Functions and click the circuit function.


  1. Click the Code tab and open index.js by clicking it. Paste the Circuit ID that you copied on line 14 and click Save.



Configure the Circuit

Now let’s go back to configuring the circuit.

  1. Navigate to Circuits and open the circuit again.

    We will first create a parallel state to execute the RatingProcessor and TravellerTypeRatingProcessor functions in parallel.

    Note: Ensure that you enter all the values for the circuit's states and configurations as described in this section.

  1. Drag the Parallel state from the left panel onto the builder in the circuit’s builder and drop it anywhere.

    This will open a pop-up box.


  1. Enter the parallel state’s name as “Dataset Processing”. Set its previous state to “Start” and next state to the default “State1”. Click Create.


  1. Now in the Dataset Processing state’s configuration, click Add Path and enter path’s name as “rating_list”. This will automatically assign the path to a new state called New State 1. Similarly, add another path and enter its name as “traveller_type_rating_list”. This will assign the path to New State 2.


    Note: We will configure the Input/Output of all the states in the end.

  1. Now click on New State 1 in the builder. Enter the state’s name as “Rating Processor”. Assign its type as a Function state and associate it with the RatingProcessor function from the drop down-lists.


  1. Similarly, click on New State 2 in the builder. Enter the state’s name as “Traveller Type Rating Processor”. Assign its type as Function and associate it with the TravellerTypeRatingProcessor function.

    Both these functions will now execute in parallel every time the circuit is triggered.


  1. Now click on State1 in the builder. Enter the state’s name as “Merge Dataset” and associate it with the MergeDataSet function.


  1. Let us add another function state. Drag and drop a function state from the left panel, and provide the following values. Click Create.

    Associate this state with the Mailer function in the configuration.


  1. Let us now add a branch state before the Mailer state to branch the circuit’s processing. Drag and drop a branch state and provide the following values. Click Create.

    Note: We are adding the branch state after the Mailer state has been created to avoid creating a default branch.

  1. In the Check Result state’s configuration, click Add Condition and enter the condition as “$.result.museum_list.size() == 0”. This will automatically assign it to a New State 1.


  2. Now click on New State 1 from the builder and associate it with a Failure state. Name the state as “Failure”.

    Therefore, if there are no results matching the criteria of the user’s input, the circuit will traverse this branch and end in the failure state. Since a failure state is an end state, there will be no next state associated with it. If there are results matching the user’s criteria, the circuit will traverse the other branch and execute the Mailer function that emails the results to the user.


Your final circuit should look like this.


Let us now configure the input paths, result paths, and parameters of some states.

  • Dataset Processing: Click on Dataset Processing state from the builder and click the Input/Output tab for it. Enter the value for its result path as “$.dataset_results”. This will assign the results obtained from processing the RatingProcessor and TravellerTypeRatingProcessor functions to the variable dataset_results.

  • Rating Processor: Enter the following values for the Input/Output of the Rating Processor state. This selects the results obtained by the RatingProcessor function’s execution and assigns it to the variable result. That value is then added to the input JSON of the circuit. Add two parameters named “type” and “count” to pass the rating and count input values from the user to the function respectively.

  • Traveller Type Rating Processor: Enter the following values for the Input/Output of the Traveller Type Rating Processor state. These are the same as the previous one, except that the parameter “type” is assigned to the traveller input value.

  • Merge Dataset: Enter the following values for the Input/Output of the Merge Dataset state. The result assigned as dataset_results by the parallel processing previously, is passed as the input to this function, and the function’s output is assigned to the result variable again. We will not be passing any parameters here.

  • Mailer: Enter the following values for the Input/Output of the Mailer state. The final list (result.museum_list) obtained from the MergeDataSet function is passed as the content parameter and the user’s email address is passed as mail_id. The museum list is also passed as the circuit’s output by adding it as the result path in this state.

The circuit is now completely configured. Click Save to save it.

Note: You can also view the JSON code of the circuit to understand it better, by clicking the Code View tab in the builder page.

Last Updated 2024-06-18 12:08:24 +0530 +0530

RELATED LINKS

Circuits