Files
ubicloud/model/usage_alert.rb
Jeremy Evans 30247a3800 Include model annotations at the bottom of all model files
This makes it easier for developers new to the codebase to
easily get important information on the model's table in the
same file as the model code.

To ensure the model annotations stay accurate, run them on
test_up/test_down.  In CI, regenerate the annotations, and
check for no changes, similar to how the linters work.
2024-11-13 09:13:30 -08:00

45 lines
1.6 KiB
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
# Table: usage_alert
# Columns:
# id | uuid | PRIMARY KEY
# project_id | uuid | NOT NULL
# name | text | NOT NULL
# limit | integer | NOT NULL
# user_id | uuid | NOT NULL
# last_triggered_at | timestamp with time zone | NOT NULL DEFAULT (now() - '42 days'::interval)
# Indexes:
# usage_alert_pkey | PRIMARY KEY btree (id)
# usage_alert_last_triggered_at_index | btree (last_triggered_at)
# Foreign key constraints:
# usage_alert_project_id_fkey | (project_id) REFERENCES project(id)
# usage_alert_user_id_fkey | (user_id) REFERENCES accounts(id)