class GrafanaReporter::ReportWebhook

This class provides a default webhook implementation for report events. It sends out a webrequest to the configured callback_url with all necessary information about the event and the report.

Public Class Methods

new(callback_url) click to toggle source
# File lib/grafana_reporter/report_webhook.rb, line 8
def initialize(callback_url)
  @callback_url = callback_url
end

Public Instance Methods

callback(event, report) click to toggle source

Implements the call of the configured webhook. Provides the following report information in JSON format:

:object_id      - id of the current report
:path           - file path to the report
:status         - report status as string, e.g. `cancelled`, `finished` or `in progress`
:execution_time - execution time in seconds of the report
:template       - name of the used template
:start_time     - time when the report creation started
:end_time       - time when the report creation ended
:event          - event, which has happened, e.g. `on-before-create`

Please note that this callback is a non-blocking event, i.e. the report generation is proceeding, no matter if the callback is successfull and no matter how long the execution of the callback does take.

# File lib/grafana_reporter/report_webhook.rb, line 27
def callback(event, report)
  # build report information as JSON
  data = { object_id: report.object_id, path: report.path, status: report.status,
           execution_time: report.execution_time, template: report.template,
           start_time: report.start_time, end_time: report.end_time, event: event }

  request = { body: JSON.generate(data), accept: nil, content_type: nil }
  res = ::Grafana::WebRequest.new(@callback_url, request).execute

  "#{res} - Body: #{res.body}"
end