BathHacked/energy-sparks

View on GitHub
app/services/rollbar_notifier_service.rb

Summary

Maintainability
A
0 mins
Test Coverage
require 'rollbar_api/rql_jobs'

class RollbarNotifierService
  def initialize(rql_jobs = RollbarApi::RqlJobs.new(ENV['ROLLBAR_READ_ACCESS_TOKEN']), description = nil)
    @rql_jobs = rql_jobs
    @description = description
  end

  REPORTS = {
    validation_errors: {
      title: 'Meter validation problems',
      description: 'Validation failed for the following schools in the last 48 hours. Failed caused by the listed meter',
      rql_query: <<-QUERY
      SELECT timestamp, body.trace.extra.school_id, body.trace.extra.school, body.trace.extra.mpan_mprn, body.trace.exception.message
      from item_occurrence
      where body.trace.extra.job = 'validate_amr_readings'
      AND timestamp > unix_timestamp() - 60 * 60 * 48
      ORDER by timestamp desc
      QUERY
    },
    n3rgy_errors: {
      title: 'N3RGY Meter loading errors',
      description: 'Errors reported when trying to load data from N3RGY',
      rql_query: <<-QUERY
      SELECT timestamp, body.trace.extra.meter_id, body.trace.extra.start_date, body.trace.extra.end_date, body.trace.exception.message
      from item_occurrence
      where body.trace.extra.job = 'n3rgy_download'
      AND timestamp > unix_timestamp() - 60 * 60 * 48
      ORDER by timestamp desc
      QUERY
    }
  }.freeze

  def run_queries
    results = {}
    REPORTS.each do |key, config|
      results[key] = config.dup
      results[key][:results] = run_report(config)
    end
    return results
  end

  def perform
    RollbarMailer.with(reported_results: run_queries, description: @description).report_errors.deliver_now
  end

  private

  def run_report(report)
    query_result = @rql_jobs.run_query(report[:rql_query])
    return {
      columns: query_result['result']['result']['columns'],
      rows: query_result['result']['result']['rows']
    }
  rescue => e
    Rails.logger.error "Exception: running RQL report '#{report[:title]}' against rollbar. #{e.class} #{e.message}"
    Rails.logger.error e.backtrace.join("\n")
    Rollbar.error(e)
    return { error: true, rows: [], columns: [] }
  end
end