Skip to content

Instantly share code, notes, and snippets.

View jonbartels's full-sized avatar

Jon Bartels jonbartels

  • Louisville, KY
  • 22:12 (UTC -04:00)
View GitHub Profile
@jonbartels
jonbartels / mirth-channel-error.sql
Created February 2, 2024 20:20
Show Mirth Connect errors by destination with PID.3 and PV1.19 from source message with short error and detail description
select
dm.message_id, status,connector_name ,
to_char(received_date AT TIME ZONE 'US/Eastern', 'YYYY-MM-DD hh24:mi') || ' US/Eastern' as message_date,
(regexp_matches(mc.content, 'PID\|(?:.*?\|){2}(.*?)\|'))[1] as pid_3_mrns,
(regexp_matches(mc.content, 'PV1\|(?:.*?\|){18}(.*?)\|'))[1] as pv1_19_visit_id,
(regexp_matches(mc_error.content, '.*ERROR MESSAGE:\s+(.*?)', 'n'))[1] as error_message,
(regexp_matches(mc_error.content, '.*DETAILS:\s+(.*?)', 'n'))[1] as detail_message
from mirth.public.d_mm76 dm
inner join d_mc76 mc on mc.message_id = dm.message_id and mc.metadata_id = 0 and mc.content_type = 1
inner join d_mc76 mc_error on mc_error.message_id = dm.message_id and mc_error.metadata_id = dm.id and mc_error.content_type = 12
@jonbartels
jonbartels / mirthSourceTransformer.js
Last active January 12, 2024 18:17
Alert receiver channel which does not alert if a message queued
//can use this to get the message content and check state and send attempts
// we have channelId and messageId
// pass metadataIds as null -> the connector message returned will then have one or many connector messages. #0 is the source, and source has connectorMessage.getDestinationIdMap(); which we can iterate to turn our connector name into the numeric metadata id
var shouldAlert = true;
var metaDataIds = null;
var alertedChannelId = msg['channelId'];
var messageId = parseInt(msg['messageId']);
var messageController = Packages.com.mirth.connect.server.controllers.messageController.getInstance();
var message = messageController.getMessageContent(alertedChannelId, messageId, metaDataIds);
var sourceMessage = message.getConnectorMessages().get(0);
@jonbartels
jonbartels / mc_certs.sql
Last active December 22, 2023 16:44
Mirth Connect channels using SSL certs by cert
with channel_xml as (
select
name,
xmlparse(document channel) as channel_xml
from channel c
)
, destination_connector_xml as (
SELECT
name as channel_name,
unnest(xpath('//destinationConnectors/connector/name/text()', channel_xml))::TEXT as connector_name,
@jonbartels
jonbartels / Dockerfile
Created May 26, 2023 00:55
Use SumoLogic logging in Mirth Connect. Shows the JARs to load to get MC to tolerate the Sumo appender for Log4j2
FROM nextgenhealthcare/connect:4.3.0 as connect
#clone some boilerplate from MC image
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
#TODO using a tarball and ADD also sets us up to get these artifacts from artifactory instead of from git
#TODO the parent entrypoint.sh does something with a custom-extensions directory, review it and see if it does this unpacking for us
#TODO I still think ADD is best here since it can fetch from remote URLs, it sets us up to use artifactory
@jonbartels
jonbartels / mirth_connect_events_by_channel_name.sql
Created May 25, 2023 21:06
Query Mirth Connect event table by event name and pick out channel name and channel ID from the attributes. Postgres 13 and newer
WITH attribute_rows AS (select e.name,
e.date_created,
xml_element.*
from "event" e,
xmltable('//linked-hash-map/entry'
passing (e."attributes"::xml)
columns "key" text PATH 'string[1]',
"value" text PATH 'string[2]'
) as xml_element
where e.name like '%Deploy%'
@jonbartels
jonbartels / report_on_paid_mirth_connectors.sql
Last active May 12, 2023 01:56
Report on Usage of Paid Extension Connectors in Mirth Connect
with channel_xml as (
select
name,
xmlparse(document channel) as channel_xml
from channel c
)
, destination_connector_xml as (
SELECT
name as channel_name,
unnest(xpath('//destinationConnectors/connector/name/text()', channel_xml))::TEXT as connector_name,
@jonbartels
jonbartels / dumpQuartzJobsFromMirthConnect.js
Created May 6, 2023 14:36
A hastily written script to enumerate the Quartz scheduler jobs from Mirth Connect in a JS reader or JS step and return JSON
var dump = {};
var factory = new org.quartz.impl.StdSchedulerFactory();
var allSchedulers = factory.getAllSchedulers();
dump.schedulerCount = allSchedulers.size();
dump.schedulers = [];
var iter = allSchedulers.iterator();
@jonbartels
jonbartels / mirth_daily_volume_report.sql
Last active December 15, 2023 21:13
I kept forgetting how to do dynamic SQL to query all the MC d_ tables so I saved it this time; returns XML
WITH channel_query AS (
SELECT dc.local_channel_id,
c.name,
format('SELECT ' ||
'%L::TEXT as channel_name, ' ||
'date_trunc(''day'', received_date)::TEXT as day, ' ||
'count(*)::BIGINT as cnt FROM d_m%s' ||
' WHERE received_date >= now() - INTERVAL ''%s DAYS'' GROUP BY 1, 2', c.name,
dc.local_channel_id, 90) as query
FROM d_channels dc
@jonbartels
jonbartels / create-mc-metadata-indexes.postgres.sql
Last active February 7, 2023 15:18
SQL to generate CREATE INDEX statements for Mirth Connect metadata columns for Postgres
--see https://github.com/nextgenhealthcare/connect/issues/4320
-- this generates create index statements for the metadata columns in MC excluding the default columns
-- the user SHOULD NOT BLINDLY CREATE ALL INDEXES
-- MC is update/insert heavy and having indexes can slow that performance.
-- These indexes should be used only on metadata colums which are searched frequently, on large tables, and where constraining by other indexed columns is not practical
-- Further, if you're searching on large tables you should run the damn pruner. see https://gist.github.com/MichaelLeeHobbs/40b4b7cf70ecbe30b73eed763367e626
SELECT t.table_name, c.column_name, format('CREATE INDEX CONCURRENTLY metadata_hax_%1$s_%2$s ON %1$s("%2$s");', t.table_name, c.column_name) as create_stmt
FROM information_schema.tables t
INNER JOIN information_schema.columns c ON c.table_name = t.table_name AND c.table_schema = c.table_schema
WHERE t.table_schema='public'
/*
I did a thing.
I wrote a channel that reads the current config map and then writes it to the Mirth DB so that you can switch from file backed config maps to DB backed config maps.
Just let the code run in a JS Reader and then flip the mirth.properties entry whenever you’re ready to change over.
*/
// read mirth.properties and check the configurationmap.location property
var classpathResource = com.mirth.connect.server.tools.ClassPathResource.getResourceURI('mirth.properties');
var propsInputStream;
var configmapLocation = 'Could not read!';
try {