Files
ubicloud/model/usage_alert.rb
Jeremy Evans 718370f4c9 Convert ResourceMethods to a plugin
Allow plugin to take an etc_type keyword argument for using the
TYPE_ETC ubid type, and remove the separate definitions in every
model that uses the TYPE_ETC ubid type.

This was the cleanest way to DRY things up.  You cannot extend
the models with a module to do this before including ResourceMethods,
because then ResourceMethods::ClassMethods will override it, and
you cannot extend the models with a module to do this after
including ResourceMethods, because the inclusion will not work
correctly due to the eager definition of @ubid_format.

Best reviewed without whitespace differences.
2025-06-04 04:55:45 +09:00

42 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
plugin ResourceMethods
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_project_id_user_id_name_uidx | UNIQUE btree (project_id, user_id, name)
# 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)