Skip to content

Commit

Permalink
feat(CE): Sync alerts - added models (#741) (#555)
Browse files Browse the repository at this point in the history
Co-authored-by: Basil V Bose <bvb007@gmail.com>
  • Loading branch information
github-actions[bot] and bvb007 authored Dec 27, 2024
1 parent 8c395c7 commit 5c82069
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 1 deletion.
10 changes: 10 additions & 0 deletions server/app/models/alert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class Alert < ApplicationRecord
belongs_to :workspace
has_many :alert_channels, dependent: :destroy
accepts_nested_attributes_for :alert_channels

validates :workspace_id, presence: true
validates :row_failure_threshold_percent, numericality: { only_integer: true, allow_nil: true }
end
8 changes: 8 additions & 0 deletions server/app/models/alert_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class AlertChannel < ApplicationRecord
belongs_to :alert

validates :platform, presence: true, inclusion: { in: %w[email slack] }
enum :platform, %i[email slack]
end
1 change: 1 addition & 0 deletions server/app/models/workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Workspace < ApplicationRecord
has_many :data_app_sessions, dependent: :nullify
has_many :audit_logs, dependent: :nullify
has_many :custom_visual_component_files, dependent: :nullify
has_many :alerts, dependent: :nullify
belongs_to :organization

STATUS_ACTIVE = "active"
Expand Down
13 changes: 13 additions & 0 deletions server/db/migrate/20241219163555_create_alerts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateAlerts < ActiveRecord::Migration[7.1]
def change
create_table :alerts do |t|
t.string :name
t.references :workspace, null: false, foreign_key: true
t.boolean :alert_sync_success, default: false
t.boolean :alert_sync_failure, default: false
t.boolean :alert_row_failure, default: false
t.integer :row_failure_threshold_percent
t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions server/db/migrate/20241219163606_create_alert_channels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateAlertChannels < ActiveRecord::Migration[7.1]
def change
create_table :alert_channels do |t|
t.references :alert, null: false, foreign_key: true
t.integer :platform, null: false
t.jsonb :configuration
t.timestamps
end
end
end
25 changes: 24 additions & 1 deletion server/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions server/spec/factories/alert_channels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

FactoryBot.define do
factory :alert_channel do
association :alert

trait :email do
platform { "email" }
configuration { { extra_email_recipients: ["user1@example.com", "user2@example.com"] } }
end

trait :slack do
platform { "slack" }
configuration { { slack_email_alias: ["slackemailtemplate@slack.com"] } }
end
end
end
12 changes: 12 additions & 0 deletions server/spec/factories/alerts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

FactoryBot.define do
factory :alert do
association :workspace
name { "Test alert" }
alert_sync_success { false }
alert_sync_failure { false }
alert_row_failure { false }
row_failure_threshold_percent { 50 }
end
end
19 changes: 19 additions & 0 deletions server/spec/models/alert_channel_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe AlertChannel, type: :model do
describe "associations" do
it { should belong_to(:alert) }
end

describe "validations" do
it { should validate_presence_of(:platform) }
end

describe "platform" do
it "defines platform enum with specified values" do
expect(AlertChannel.platforms).to eq({ "email" => 0, "slack" => 1 })
end
end
end
15 changes: 15 additions & 0 deletions server/spec/models/alert_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alert, type: :model do
describe "associations" do
it { should belong_to(:workspace) }
it { should have_many(:alert_channels).dependent(:destroy) }
end

describe "validations" do
it { should validate_presence_of(:workspace_id) }
it { should validate_numericality_of(:row_failure_threshold_percent).only_integer.allow_nil }
end
end

0 comments on commit 5c82069

Please sign in to comment.