Skip to content

Commit

Permalink
Added Slack report
Browse files Browse the repository at this point in the history
  • Loading branch information
wfondrie committed Dec 4, 2023
1 parent 2ec2890 commit 0a67f08
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
34 changes: 34 additions & 0 deletions assets/slackreport.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"attachments": [
{
"fallback": "Plain-text summary of the attachment.",
"color": "<% if (success) { %>good<% } else { %>danger<%} %>",
"author_name": "TalusBio/nf-encyclopedia - ${runName}",
"author_icon": "https://www.nextflow.io/docs/latest/_static/favicon.ico",
"text": "<% if (success) { %>Pipeline completed successfully!<% } else { %>Pipeline completed with errors<% } %>",
"fields": [
{
"title": "Command used to launch the workflow",
"value": "```${commandLine}```",
"short": false
}
<%
if (!success) { %>
,
{
"title": "Full error message",
"value": "```${errorReport}```",
"short": false
},
{
"title": "Pipeline configuration",
"value": "<% out << summary.collect{ k,v -> k == "hook_url" ? "_${k}_: (_hidden_)" : ( ( v.class.toString().contains('Path') || ( v.class.toString().contains('String') && v.contains('/') ) ) ? "_${k}_: `${v}`" : (v.class.toString().contains('DateTime') ? ("_${k}_: " + v.format(java.time.format.DateTimeFormatter.ofLocalizedDateTime(java.time.format.FormatStyle.MEDIUM))) : "_${k}_: ${v}") ) }.join(",\n") %>",
"short": false
}
<% }
%>
],
"footer": "Completed at <% out << dateComplete.format(java.time.format.DateTimeFormatter.ofLocalizedDateTime(java.time.format.FormatStyle.MEDIUM)) %> (duration: ${duration})"
}
]
}
49 changes: 49 additions & 0 deletions lib/TalusTemplate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,53 @@ class TalusTemplate {
def email_html = html_template.toString()
return [subject, email_html]
}

//
// Construct and send a notification to a web server as JSON
// e.g. Microsoft Teams and Slack
//
public static void IM_notification(workflow, params, projectDir) {
def hook_url = params.hook_url
def misc_fields = [:]
misc_fields['start'] = workflow.start
misc_fields['complete'] = workflow.complete
misc_fields['scriptfile'] = workflow.scriptFile
misc_fields['scriptid'] = workflow.scriptId
if (workflow.repository) misc_fields['repository'] = workflow.repository
if (workflow.commitId) misc_fields['commitid'] = workflow.commitId
if (workflow.revision) misc_fields['revision'] = workflow.revision
misc_fields['nxf_version'] = workflow.nextflow.version
misc_fields['nxf_build'] = workflow.nextflow.build
misc_fields['nxf_timestamp'] = workflow.nextflow.timestamp

def summary = [:]
def msg_fields = [:]
msg_fields['runName'] = workflow.runName
msg_fields['success'] = workflow.success
msg_fields['dateComplete'] = workflow.complete
msg_fields['duration'] = workflow.duration
msg_fields['exitStatus'] = workflow.exitStatus
msg_fields['errorMessage'] = (workflow.errorMessage ?: 'None')
msg_fields['errorReport'] = (workflow.errorReport ?: 'None')
msg_fields['commandLine'] = workflow.commandLine.replaceFirst(/ +--hook_url +[^ ]+/, "")
msg_fields['projectDir'] = workflow.projectDir
msg_fields['summary'] = summary << misc_fields

// Render the JSON template
def engine = new groovy.text.GStringTemplateEngine()
// Different JSON depending on the service provider
// Defaults to "Adaptive Cards" (https://adaptivecards.io), except Slack which has its own format
def json_path = hook_url.contains("hooks.slack.com") ? "slackreport.json" : "adaptivecard.json"
def hf = new File("$projectDir/assets/${json_path}")
def json_template = engine.createTemplate(hf).make(msg_fields)
def json_message = json_template.toString()

// POST
def post = new URL(hook_url).openConnection();
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/json")
post.getOutputStream().write(json_message.getBytes("UTF-8"));
def postRC = post.getResponseCode();
}
}
15 changes: 14 additions & 1 deletion main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ def email() {
}


//
// Used for Slack notifications
//
def slack() {
if (params.hook_url) {
TalusTemplate.IM_notification(workflow, params, projectDir)
}
}


//
// Use the DLIB when the ELIB is unavailable.
//
Expand Down Expand Up @@ -133,4 +143,7 @@ workflow dummy {
}

// Email notifications:
workflow.onComplete { email() }
workflow.onComplete {
email()
slack()
}
1 change: 1 addition & 0 deletions nextflow.config
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ params {
report_dir = 'reports' /** \type{str} Where reports will be saved. */
mzml_dir = 'mzml' /** \type{str} Where mzML files will be saved. */
email = null /** \type{str} An email to alert on completion. */
hook_url = null /** \type{str} A URL for Slack notifications */

/** \group{Grouping Parameters} */
/** \type{boolean}
Expand Down

0 comments on commit 0a67f08

Please sign in to comment.