Skip to content

Instantly share code, notes, and snippets.

@jonbartels
Created November 19, 2024 20:01
Show Gist options
  • Save jonbartels/a0de755e2288d1fca91cbec6966a7d97 to your computer and use it in GitHub Desktop.
Save jonbartels/a0de755e2288d1fca91cbec6966a7d97 to your computer and use it in GitHub Desktop.
Script to read the connection status data from Mirth and generate a useful data structure
//Credit to Mike Klemens and Chris Gibson in Mirth Slack
// Get the server ID using ConfigurationController
var serverInstance = Packages.com.mirth.connect.server.controllers.ConfigurationController.getInstance();
var serverId = serverInstance.getServerId();
//Use Dashboard Status plugin
var defController = Packages.com.mirth.connect.server.controllers.DefaultExtensionController.create();
var monitor = defController.getServicePlugins().get(com.mirth.connect.plugins.dashboardstatus.DashboardConnectorStatusServletInterface.PLUGIN_POINT);
connectorListener = monitor.getConnectorListener();
var states = connectorListener.getConnectorStateMap(serverId);
// Check if states is a Map-like object (which should support entrySet)
if (states.entrySet) {
var statesEntries = states.entrySet();
var stateData = [];
// Use Iterator to loop through the entries
var iterator = statesEntries.iterator();
while (iterator.hasNext()) {
var entry = iterator.next();
stateData.push({ key: entry.getKey(), value: entry.getValue() });
}
//unfiltered data in JSON.stringify(stateData)
}
// Filter and extract relevant data
var filteredData = stateData.filter(function(item) {
return item.key.endsWith("_0"); // Check if the key ends with "_0"
}).map(function(item) {
var status = item.value[1]; // Get the second value from the "value" array
// Extract connection count from status if available (in parentheses), default to 0 if not
var connectionCount = 0;
var match = status.match(/\((\d+)\)/); // RegEx to Match the number inside parentheses
if (match) {
connectionCount = parseInt(match[1], 10); // Extract and parse the number inside parentheses
status = status.slice(0, status.indexOf('(')).trim(); // Remove the parentheses and the number inside
}
return {
channel: item.key.slice(0, -2), // Remove the last 2 characters (i.e., "_0")
status: status, // Status remains the same
connectionCount: connectionCount // Add connection count
};
});
// Output the filtered data (optional)
logger.info(JSON.stringify(filteredData));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment