Skip to content

Examples of JavaScript for Rules

This section contains a few examples of the JavaScript that is used to represent rule logic when you create them in Yellowbrick Manager or with a SQL command. You can use Advanced mode in Yellowbrick Manager to write your own JavaScript for rules. See JavaScript Properties for WLM Rules.

Note: WLM rules use strict equality comparisons with === instead of ==. For details, see Equality comparisons and sameness.

If the database is premdb, set the maximum execution time to 5 seconds:

if (w.database === 'premdb') {
	w.maximumExecTimeLimit = 5;
}

If the user who runs the query is bobr, log an INFO-level message:

if (w.user === 'bobr') {
	log.info('bobr ran this query');
}

If a select query is run from ybsql on the premdb database, set the maximum row limit to 10000:

if (w.application === 'ybsql' &&
   w.database === 'premdb' &&
   w.command === 'select') {
	w.maximumRowLimit = 100000;
}

If the SQL text matches the string sys.const, set Short Query Bias (SQB) to true:

if (String(w.SQLText).match(/sys\.const/)) {
	w.allowSQB = true;
}

If the SQL text contains extract, abort the query and log the message: Contains extract function.

if ((String(w.SQLText).indexOf('extract') >= 0)) {
	w.abort('Contains extract function');
}

Note that this rule may "misfire" if a table or a column name contains the string extract.

Using log.warn and log.error

If you write a rule that uses log.warn or log.error, a warning or error is logged in the sys.log_query_alerts table.

An example of an execution rule that logs warnings is the system rule named default_boostForHighMemory. This rule is defined as follows:

false && log.info('Query {} using maxMemory {}', w.execId, w.stats.maxMemory);
 
if (w.stats.maxMemory > (2*1024*1024*1024) && w.priority !== 'high') {
  w.priority = 'high';
  log.error('Boosted query {} to high priority, because it is using too much memory ({} GB)',
   w.execId, (w.stats.maxMemory / (1024*1024*1024)).toFixed(1));
}

This rule examines the current statistics for the query, and if it has used more than 2GB (2*1024*1024*1024), and its priority is not already high, its priority is set to high and an error is logged. (The decision to log an error is arbitrary; a warning might be more appropriate here.)