Configure the Function

Let’s begin coding the microservice. The functions directory, in this case functions/counter_based_alert_function, contains:

We will be adding code in the index.js file. You can use any IDE of your choice to configure the function.

Note: Please go through the code given in this section for understanding it better.

Copy the code given below and paste it in index.js and save the file.

    
index.js
copy
const catalyst = require("zcatalyst-sdk-node"); module.exports = async(context, basicIO) => { try { const FROM_EMAIL="emma@zylker.com"; // The email address of the sender. Replace this with the email address you configured in Mail. const TO_EMAIL="brandon.cooper@zylker.com"; // Replace this with the email address that you want the alert email to be sent to. let featureName = basicIO.getArgument("feature_name"); let mailCountThreshold = basicIO.getArgument("mail_count_threshold"); let app = catalyst.initialize(context); let value = null; let cache = app.cache(); let segment = cache.segment(); let cachePromise = segment.getValue("COUNTER_" + featureName); await cachePromise.then((entity) => { value = entity; }).catch((err) => { throw err; }); if (value == null) { await segment.put("COUNTER_" + featureName,1,1); } else { let count = parseInt(value); // If the value is not null, then the count is incremented by 1. count++; // Writes the count value to the default segment of Catalyst cache await segment.put("COUNTER_" + featureName,count,1); // If the current count is greater than the threshold value, then an email alert is sent to the receiver configured above if (count > parseInt(mailCountThreshold)) { //Create a config object with the email configuration let config = { from_email: FROM_EMAIL, to_email: [TO_EMAIL], subject: 'Alert!', content: "Count exceeded the threshold limit for the feature:" +featureName, }; //Send the mail by passing the config object let email = app.email(); let mailPromise = email.sendMail(config); await mailPromise.then((mailObject) => { console.log("Email Alert Sent"); }) .catch((err) => { throw err; }); } } // Handles the 200 status code basicIO.setStatus(200); basicIO.write("The function executed successfully"); } catch (error) { console.log("Exception occurred while processing",error.toString()); //Written to the logs. You can view this log from Logs under the Monitor section in the console // Handles the 500 status code, when an exception occurs basicIO.setStatus(500); basicIO.write("Error in the function execution"); } context.close(); //end of application };
View more
Note: Before you proceed to the next step, ensure that you replace the values of FROM_EMAIL in line 5 and TO_EMAIL in line 6 in the code as directed.

Working of the Basic I/O function

As discussed in the Introduction, the function obtains the inputs for feature_name and mail_count_threshold from the user through the API, when it is invoked. It obtains the current counter value from the cache and then increments it by 1 after every invocation. It then writes the new counter value to the cache.

Note: Because we are using the default segment in Catalyst Cache, we need not configure it or create a new segment. You can learn more about this from the Cache help page

If the current counter value is greater than the threshold limit specified by the user, the email alert will be sent to the address defined in the code. This information is also pushed to Catalyst Logs with an appropriate message. You can check this from Logs under the Monitor section in the Catalyst console.

The functions directory is now configured.

Last Updated 2023-12-15 18:54:08 +0530 +0530