houdini/lib/health_report.rb
Bradley M. Kuhn 6772312ea7 Relicense all .rb files under new project license.
The primary license of the project is changing to:
  AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later

with some specific files to be licensed under the one of two licenses:
   CC0-1.0
   LGPL-3.0-or-later

This commit is one of the many steps to relicense the entire codebase.

Documentation granting permission for this relicensing (from all past
contributors who hold copyrights) is on file with Software Freedom
Conservancy, Inc.
2018-03-25 15:10:40 -04:00

59 lines
2 KiB
Ruby

# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
require 'qx'
require 'format/csv'
require 'format/currency'
module HealthReport
# Send an email report about what has happend on the servers and database in the last 24hrs, and how things are running
# Returns a hash of metrics data
def self.query_data
# Transaction metrics
charges = Qx.select("COUNT(charges.id), SUM(charges.amount), SUM(charges.fee) as fees")
.from("charges")
.where("created_at > $d", d: 24.hours.ago)
.and_where("charges.status != 'failed'")
.ex.last
# Recurring donation metrics
rec_dons = Qx.select("COUNT(id), SUM(amount)")
.from("recurring_donations")
.where("active=TRUE")
.ex.last
# Info about disabled nonprofit accounts due to ident verification
disabled_nps = Qx.select("nonprofits.id", "nonprofits.name", "nonprofits.stripe_account_id")
.from("nonprofits")
.where("verification_status != 'verified'")
.and_where("created_at > $d", d: 3.months.ago)
.ex(format: 'csv')
return {
charges_count: charges['count'],
charges_sum: charges['sum'],
charges_fees: charges['fees'],
recently_disabled_nps: disabled_nps,
active_rec_don_count: rec_dons['count'],
active_rec_don_amount: rec_dons['sum']
}
end
# Given a hash of data, formats it into a multi-line string
def self.format_data data
disabled_nps = Format::Csv.from_array(data[:recently_disabled_nps])
return %Q(
Transaction Metrics for the last 24hrs:
Total count: #{data[:charges_count]}
Total amount: $#{Format::Currency.cents_to_dollars(data[:charges_sum])}
Total fees processed: $#{Format::Currency.cents_to_dollars(data[:charges_fees])}
Active recurring donation metrics:
Total active count: #{data[:active_rec_don_count]}
Total active amount: $#{Format::Currency.cents_to_dollars(data[:active_rec_don_amount])}
Recent nonprofit accounts that are disabled due to identity verification:
#{disabled_nps}
)
end
end