web stats
Mirth Community - View Single Post - DIY Queue Depth Alerts
View Single Post
  #2  
Old 02-02-2011, 02:11 PM
TMarz TMarz is offline
OBX.2 Kenobi
 
Join Date: Jul 2009
Posts: 60
TMarz is on a distinguished road
Default Auto-magically Add Newly Created Channels to an Alert

After I wrote the above post, I decided to pursue my idea of automatically adding new channels to this alert. I wrote the following script that is meant to run in the Global Deploy Script. The only requirement is that you must tell it what the name of your alert is.

After a deploy, the script gets the alert id for the name you have entered. It then queries the DB to get all channels associated with that alert id from the table channel_alert. It then gets all the channel ids. Finally it compares the two arrays of channel id and if it finds a channel on the channel list that is not in the alert list, it inserts it into the database.

I tested this and the Alert on Mirth 2.0.1 running Linux, but that shouldn't matter.

Code:
// This script executes once when the mule engine is started
// You only have access to the globalMap here to persist data

/***************************************\
*	This adds any new channels to the	                *
*	queue depth alert.					*
****************************************/

//Get the ID of the alert
//Open database connection
var dbConn = DatabaseConnectionFactory.createDatabaseConnection('org.postgresql.Driver', 'jdbc:postgresql://localhost:5432/mirth', 'mirth', 'mirth');
var expression = 'SELECT id FROM alert WHERE name = \'Queue Depth Alert\';';
//channelResult is of java type ResultSet
var alertId = dbConn.executeCachedQuery(expression);
while(alertId.next())
{
	var id = alertId.getString("id");
}

//Get the list of channels
expression = 'SELECT id FROM channel ORDER BY id ASC;';
var channels = dbConn.executeCachedQuery(expression);
//Build an Array of channels
var channelArray = new Array();
while(channels.next())
{
	channelArray.push(channels.getString("id"));
}

//Get the list of channels for the Alert
expression = 'SELECT channel_id FROM channel_alert WHERE alert_id = \''+id+'\' ORDER BY channel_id ASC;';
var alertChannels = dbConn.executeCachedQuery(expression);
//Build an Array of channels
var channelAlertArray = new Array();
while(alertChannels.next())
{
	channelAlertArray.push(alertChannels.getString("channel_id"));
}

//Simple Exit Test
if(channelAlertArray.length != channelArray.length)
{
	 //index for the channelArray
	for(var i = 0; i < channelArray.length; i++)
	{
		var index = -1;
		//index for the Alert Channel Array
		for(var k = 0; k < channelAlertArray.length; k++)
		{
			if(channelAlertArray[k].toString() == channelArray[i].toString())
				index = k;
		}
		if( index == -1 )
		{
			//Insert into the table
			expression = 'INSERT INTO channel_alert VALUES (\''+channelArray[i]+'\', \''+id+'\');';
			dbConn.executeUpdate(expression);
		}
	}  
}
dbConn.close();
return;
Reply With Quote