Files
ubicloud/model/usage_alert.rb
Burak Yucesoy 1ba705a863 Add usage alerting based on the total consumption
This commit adds feature of sending alerts when the total consumption exceeds
a user-defined threshold. We associate each alert to a user who created it and
send the email to that user when the alert is triggered. An alternative would
be allowing users to specify an email address to send the alert to, but that
opens an attack vector where a user could spam another user with alerts. This
vector can be mitigated by requiring the user to confirm the email address, but
implementing a whole confirmation workflow would significantly increase the
effort required to implement this feature.
2024-04-22 16:00:57 +02:00

30 lines
886 B
Ruby

# frozen_string_literal: true
require_relative "../model"
class UsageAlert < Sequel::Model
many_to_one :project
many_to_one :user, class: :Account, key: :user_id
include ResourceMethods
def self.ubid_type
UBID::TYPE_ETC
end
def trigger
send_email
update(last_triggered_at: Time.now)
end
def send_email
Util.send_email(user.email, "Usage alert is triggered for project #{project.name}",
greeting: "Hello #{user.name},",
body: ["The usage alert, #{name}, you set for project #{project.name} (id: #{project.ubid}) has been triggered.",
"Current cost: $#{project.current_invoice.content["cost"].to_f.round(2)}",
"Please note that this alert is only for informational purposes and no action is taken automatically."],
button_title: "See usage",
button_link: "#{Config.base_url}#{project.path}/billing")
end
end