Presentation is loading. Please wait.

Presentation is loading. Please wait.

Section 13 – Using External JavaScript Modules

Similar presentations


Presentation on theme: "Section 13 – Using External JavaScript Modules"— Presentation transcript:

1 Section 13 – Using External JavaScript Modules

2 Intro To JavaScript Modules for AIOps

3 What Is A Module? Good authors divide their books into chapters and sections; good programmers divide their programs into modules. Like a book chapter, modules are just clusters of words (or code, as the case may be). Good modules, however, are highly self-contained with distinct functionality, allowing them to be shuffled, removed, or added as necessary, without disrupting the system as a whole.

4 Why Use Modules? Maintainability: By definition, a module is self-contained. A well-designed module aims to lessen the dependencies on parts of the codebase as much as possible, so that it can grow and improve independently. Updating a single module is much easier when the module is decoupled from other pieces of code.

5 Why Use Modules? Reusability: We’ve all copied code we previously wrote into new projects at one point or another. For example, let’s imagine you copied some utility methods you wrote from a previous project to your current project If you find a better way to write some part of that code you’d have to go back and remember to update it everywhere else you wrote it. Updating your code in one place is much easier than trying to find everywhere you used it.

6 Some are Specific to a Lam
Some Included Modules There are several examples in AIOps of external modules in bots/lambots Some are Generic CommonUtils.js ExampleModule.js LamUtility.js RegExpUtils.js Some are Specific to a Lam NetcoolUtility.js ScomUtility.js SyslogUtils.js

7 Exploring the Lam Module ExampleModule.js
function CTestClass() { // Define a local logger so as not to collide with logger // in global scope var mLogger=LamBot.loadModule("Logger"); // Define self var self={ // A simple function definition prettyPrint: function(event) { mLogger.info("This is a pretty print of " + event.value("source") + " some other stuff"); } }; // Now create a blank object with self as prototype var F=function() {}; F.prototype=self; // And send that back return( new F() ); }

8 Exploring the Lam Module ExampleModule.js
function CTestClass() { // Define a local logger so as not to collide with logger // in global scope var mLogger=LamBot.loadModule("Logger"); // Define self var self={ // A simple function definition prettyPrint: function(event) { mLogger.info("This is a pretty print of " + event.value("source") + " some other stuff"); } }; // Now create a blank object with self as prototype var F=function() {}; F.prototype=self; // And send that back return( new F() ); } This Is The Good Part

9 Using Modules

10 Using A Module In AIOps Modules are initialized different for Lambots and Moobots Lambot Modules are specified in lam.conf before being assigned to a variable in the Lambot

11 Using A Module In AIOps Lambots
Add the Module to the Lam.conf file Assign the Module to a variable early Use the functions form the module like you would use local functions except you must prefix the function with the variable name you gave the module when you loaded it.

12 Using A Module In AIOps Moobots
Load the Module in Moobot Assign the Module to a variable Use the functions form the module like you would use local functions except you must prefix the function with the variable name you gave the module when you loaded it.

13 Creating Your Own Modules

14 Moobot Module Structure

15 Adding Functions to Modules
Slightly Different than regular functions Function Name First Function Statement Colon


Download ppt "Section 13 – Using External JavaScript Modules"

Similar presentations


Ads by Google