mirror of
https://github.com/Evolix/chexpire.git
synced 2024-05-03 01:10:50 +02:00
[MIG] Adds a CheckNotification model, Notification becomes a template
This commit is contained in:
parent
0f3571b3bb
commit
565e06cc19
46
app/models/check_notification.rb
Normal file
46
app/models/check_notification.rb
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: check_notifications
|
||||||
|
#
|
||||||
|
# id :bigint(8) not null, primary key
|
||||||
|
# sent_at :datetime
|
||||||
|
# status :integer default("pending"), not null
|
||||||
|
# created_at :datetime not null
|
||||||
|
# updated_at :datetime not null
|
||||||
|
# check_id :bigint(8)
|
||||||
|
# notification_id :bigint(8)
|
||||||
|
#
|
||||||
|
# Indexes
|
||||||
|
#
|
||||||
|
# index_check_notifications_on_check_id (check_id)
|
||||||
|
# index_check_notifications_on_notification_id (notification_id)
|
||||||
|
#
|
||||||
|
# Foreign Keys
|
||||||
|
#
|
||||||
|
# fk_rails_... (check_id => checks.id)
|
||||||
|
# fk_rails_... (notification_id => notifications.id)
|
||||||
|
#
|
||||||
|
|
||||||
|
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Evolix <info@evolix.fr>
|
||||||
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
||||||
|
|
||||||
|
class CheckNotification < ApplicationRecord
|
||||||
|
belongs_to :check
|
||||||
|
belongs_to :notification, counter_cache: :checks_count
|
||||||
|
|
||||||
|
enum status: [:pending, :ongoing, :succeed, :failed]
|
||||||
|
|
||||||
|
scope :active_check, -> { Check.active }
|
||||||
|
scope :check_last_run_failed, -> { Check.last_run_failed }
|
||||||
|
|
||||||
|
def pending!
|
||||||
|
self.sent_at = nil
|
||||||
|
super
|
||||||
|
end
|
||||||
|
alias reset! pending!
|
||||||
|
|
||||||
|
def ongoing!
|
||||||
|
self.sent_at = Time.now
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,27 +1,29 @@
|
||||||
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Jeremy Lecour <jlecour@evolix.fr>, 2018 Evolix <info@evolix.fr>
|
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Jeremy Lecour <jlecour@evolix.fr>, 2018 Evolix <info@evolix.fr>
|
||||||
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
#
|
#
|
||||||
# Table name: notifications
|
# Table name: notifications
|
||||||
#
|
#
|
||||||
# id :bigint(8) not null, primary key
|
# id :bigint(8) not null, primary key
|
||||||
# channel :integer default("email"), not null
|
# channel :integer default("email"), not null
|
||||||
# interval :integer not null
|
# checks_count :integer default(0), not null
|
||||||
# recipient :string(255) not null
|
# interval :integer not null
|
||||||
# sent_at :datetime
|
# label :string(255)
|
||||||
# status :integer default("pending"), not null
|
# recipient :string(255) not null
|
||||||
# created_at :datetime not null
|
# created_at :datetime not null
|
||||||
# updated_at :datetime not null
|
# updated_at :datetime not null
|
||||||
# check_id :bigint(8)
|
# check_id :bigint(8)
|
||||||
|
# user_id :bigint(8)
|
||||||
#
|
#
|
||||||
# Indexes
|
# Indexes
|
||||||
#
|
#
|
||||||
# index_notifications_on_check_id (check_id)
|
# index_notifications_on_check_id (check_id)
|
||||||
|
# index_notifications_on_user_id (user_id)
|
||||||
#
|
#
|
||||||
# Foreign Keys
|
# Foreign Keys
|
||||||
#
|
#
|
||||||
# fk_rails_... (check_id => checks.id)
|
# fk_rails_... (check_id => checks.id)
|
||||||
|
# fk_rails_... (user_id => users.id)
|
||||||
#
|
#
|
||||||
|
|
||||||
class Notification < ApplicationRecord
|
class Notification < ApplicationRecord
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Evolix <info@evolix.fr>
|
||||||
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
||||||
|
|
||||||
class AddConsecutiveFailuresToChecks < ActiveRecord::Migration[5.2]
|
class AddConsecutiveFailuresToChecks < ActiveRecord::Migration[5.2]
|
||||||
def change
|
def change
|
||||||
add_column :checks, :consecutive_failures, :integer, default: 0, null: false
|
add_column :checks, :consecutive_failures, :integer, default: 0, null: false
|
||||||
|
|
15
db/migrate/20180829073920_create_check_notifications.rb
Normal file
15
db/migrate/20180829073920_create_check_notifications.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Evolix <info@evolix.fr>
|
||||||
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
||||||
|
|
||||||
|
class CreateCheckNotifications < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
create_table :check_notifications do |t|
|
||||||
|
t.references :check, foreign_key: true
|
||||||
|
t.references :notification, foreign_key: true
|
||||||
|
t.integer :status, null: false, default: 0, limit: 1
|
||||||
|
t.datetime :sent_at
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
41
db/migrate/20180829164227_add_fields_to_notifications.rb
Normal file
41
db/migrate/20180829164227_add_fields_to_notifications.rb
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Evolix <info@evolix.fr>
|
||||||
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
||||||
|
|
||||||
|
class AddFieldsToNotifications < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
add_reference :notifications, :user, foreign_key: true
|
||||||
|
add_column :notifications, :label, :string
|
||||||
|
add_column :notifications, :checks_count, :integer, default: 0, null: false
|
||||||
|
|
||||||
|
reversible do |dir|
|
||||||
|
dir.up do
|
||||||
|
# first set user & label for *all* notifications
|
||||||
|
Notification.find_each do |notification|
|
||||||
|
check = Check.find(notification.check_id) # check relation does not exist anymore
|
||||||
|
|
||||||
|
notification.user_id = check.user_id
|
||||||
|
notification.save!
|
||||||
|
end
|
||||||
|
|
||||||
|
# then build the equivalent check notification
|
||||||
|
Notification.find_each do |notification|
|
||||||
|
assoc_notification = Notification.where(
|
||||||
|
user_id: notification.user_id,
|
||||||
|
recipient: notification.recipient,
|
||||||
|
interval: notification.interval,
|
||||||
|
).order(checks_count: :desc).limit(1).first
|
||||||
|
|
||||||
|
CheckNotification.create!(
|
||||||
|
check_id: notification.check_id,
|
||||||
|
notification: assoc_notification,
|
||||||
|
status: notification.status,
|
||||||
|
sent_at: notification.sent_at
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
# last delete duplicate notification templates not used
|
||||||
|
Notification.where(checks_count: 0).destroy_all
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,9 @@
|
||||||
|
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Evolix <info@evolix.fr>
|
||||||
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
||||||
|
|
||||||
|
class RemoveObsoleteFieldsToNotifications < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
remove_column :notifications, :status, :integer, null: false, limit: 1, default: 0
|
||||||
|
remove_column :notifications, :sent_at, :datetime
|
||||||
|
end
|
||||||
|
end
|
22
db/schema.rb
22
db/schema.rb
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2018_08_29_134404) do
|
ActiveRecord::Schema.define(version: 2018_08_30_083927) do
|
||||||
|
|
||||||
create_table "check_logs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
|
create_table "check_logs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
|
||||||
t.bigint "check_id"
|
t.bigint "check_id"
|
||||||
|
@ -24,6 +24,17 @@ ActiveRecord::Schema.define(version: 2018_08_29_134404) do
|
||||||
t.index ["check_id"], name: "index_check_logs_on_check_id"
|
t.index ["check_id"], name: "index_check_logs_on_check_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "check_notifications", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
|
||||||
|
t.bigint "check_id"
|
||||||
|
t.bigint "notification_id"
|
||||||
|
t.integer "status", limit: 1, default: 0, null: false
|
||||||
|
t.datetime "sent_at"
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["check_id"], name: "index_check_notifications_on_check_id"
|
||||||
|
t.index ["notification_id"], name: "index_check_notifications_on_notification_id"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "checks", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
|
create_table "checks", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
|
||||||
t.bigint "user_id"
|
t.bigint "user_id"
|
||||||
t.integer "kind", null: false
|
t.integer "kind", null: false
|
||||||
|
@ -49,11 +60,13 @@ ActiveRecord::Schema.define(version: 2018_08_29_134404) do
|
||||||
t.integer "channel", default: 0, null: false
|
t.integer "channel", default: 0, null: false
|
||||||
t.string "recipient", null: false
|
t.string "recipient", null: false
|
||||||
t.integer "interval", null: false
|
t.integer "interval", null: false
|
||||||
t.integer "status", default: 0, null: false
|
|
||||||
t.datetime "sent_at"
|
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
|
t.bigint "user_id"
|
||||||
|
t.string "label"
|
||||||
|
t.integer "checks_count", default: 0, null: false
|
||||||
t.index ["check_id"], name: "index_notifications_on_check_id"
|
t.index ["check_id"], name: "index_notifications_on_check_id"
|
||||||
|
t.index ["user_id"], name: "index_notifications_on_user_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
|
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
|
||||||
|
@ -82,6 +95,9 @@ ActiveRecord::Schema.define(version: 2018_08_29_134404) do
|
||||||
end
|
end
|
||||||
|
|
||||||
add_foreign_key "check_logs", "checks"
|
add_foreign_key "check_logs", "checks"
|
||||||
|
add_foreign_key "check_notifications", "checks"
|
||||||
|
add_foreign_key "check_notifications", "notifications"
|
||||||
add_foreign_key "checks", "users"
|
add_foreign_key "checks", "users"
|
||||||
add_foreign_key "notifications", "checks"
|
add_foreign_key "notifications", "checks"
|
||||||
|
add_foreign_key "notifications", "users"
|
||||||
end
|
end
|
||||||
|
|
44
test/factories/check_notifications.rb
Normal file
44
test/factories/check_notifications.rb
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: check_notifications
|
||||||
|
#
|
||||||
|
# id :bigint(8) not null, primary key
|
||||||
|
# sent_at :datetime
|
||||||
|
# status :integer default("pending"), not null
|
||||||
|
# created_at :datetime not null
|
||||||
|
# updated_at :datetime not null
|
||||||
|
# check_id :bigint(8)
|
||||||
|
# notification_id :bigint(8)
|
||||||
|
#
|
||||||
|
# Indexes
|
||||||
|
#
|
||||||
|
# index_check_notifications_on_check_id (check_id)
|
||||||
|
# index_check_notifications_on_notification_id (notification_id)
|
||||||
|
#
|
||||||
|
# Foreign Keys
|
||||||
|
#
|
||||||
|
# fk_rails_... (check_id => checks.id)
|
||||||
|
# fk_rails_... (notification_id => notifications.id)
|
||||||
|
#
|
||||||
|
|
||||||
|
FactoryBot.define do
|
||||||
|
factory :check_notification do
|
||||||
|
check
|
||||||
|
notification
|
||||||
|
status :pending
|
||||||
|
sent_at nil
|
||||||
|
|
||||||
|
trait :ongoing do
|
||||||
|
status :ongoing
|
||||||
|
end
|
||||||
|
|
||||||
|
trait :succeed do
|
||||||
|
status :succeed
|
||||||
|
sent_at { 1.day.ago }
|
||||||
|
end
|
||||||
|
|
||||||
|
trait :failed do
|
||||||
|
status :failed
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,27 +1,29 @@
|
||||||
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Jeremy Lecour <jlecour@evolix.fr>, 2018 Evolix <info@evolix.fr>
|
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Jeremy Lecour <jlecour@evolix.fr>, 2018 Evolix <info@evolix.fr>
|
||||||
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
#
|
#
|
||||||
# Table name: notifications
|
# Table name: notifications
|
||||||
#
|
#
|
||||||
# id :bigint(8) not null, primary key
|
# id :bigint(8) not null, primary key
|
||||||
# channel :integer default("email"), not null
|
# channel :integer default("email"), not null
|
||||||
# interval :integer not null
|
# checks_count :integer default(0), not null
|
||||||
# recipient :string(255) not null
|
# interval :integer not null
|
||||||
# sent_at :datetime
|
# label :string(255)
|
||||||
# status :integer default("pending"), not null
|
# recipient :string(255) not null
|
||||||
# created_at :datetime not null
|
# created_at :datetime not null
|
||||||
# updated_at :datetime not null
|
# updated_at :datetime not null
|
||||||
# check_id :bigint(8)
|
# check_id :bigint(8)
|
||||||
|
# user_id :bigint(8)
|
||||||
#
|
#
|
||||||
# Indexes
|
# Indexes
|
||||||
#
|
#
|
||||||
# index_notifications_on_check_id (check_id)
|
# index_notifications_on_check_id (check_id)
|
||||||
|
# index_notifications_on_user_id (user_id)
|
||||||
#
|
#
|
||||||
# Foreign Keys
|
# Foreign Keys
|
||||||
#
|
#
|
||||||
# fk_rails_... (check_id => checks.id)
|
# fk_rails_... (check_id => checks.id)
|
||||||
|
# fk_rails_... (user_id => users.id)
|
||||||
#
|
#
|
||||||
|
|
||||||
FactoryBot.define do
|
FactoryBot.define do
|
||||||
|
|
30
test/models/check_notification_test.rb
Normal file
30
test/models/check_notification_test.rb
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
# == Schema Information
|
||||||
|
#
|
||||||
|
# Table name: check_notifications
|
||||||
|
#
|
||||||
|
# id :bigint(8) not null, primary key
|
||||||
|
# sent_at :datetime
|
||||||
|
# status :integer default("pending"), not null
|
||||||
|
# created_at :datetime not null
|
||||||
|
# updated_at :datetime not null
|
||||||
|
# check_id :bigint(8)
|
||||||
|
# notification_id :bigint(8)
|
||||||
|
#
|
||||||
|
# Indexes
|
||||||
|
#
|
||||||
|
# index_check_notifications_on_check_id (check_id)
|
||||||
|
# index_check_notifications_on_notification_id (notification_id)
|
||||||
|
#
|
||||||
|
# Foreign Keys
|
||||||
|
#
|
||||||
|
# fk_rails_... (check_id => checks.id)
|
||||||
|
# fk_rails_... (notification_id => notifications.id)
|
||||||
|
#
|
||||||
|
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class CheckNotificationTest < ActiveSupport::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
|
@ -1,27 +1,29 @@
|
||||||
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Jeremy Lecour <jlecour@evolix.fr>, 2018 Evolix <info@evolix.fr>
|
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Jeremy Lecour <jlecour@evolix.fr>, 2018 Evolix <info@evolix.fr>
|
||||||
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
#
|
#
|
||||||
# Table name: notifications
|
# Table name: notifications
|
||||||
#
|
#
|
||||||
# id :bigint(8) not null, primary key
|
# id :bigint(8) not null, primary key
|
||||||
# channel :integer default("email"), not null
|
# channel :integer default("email"), not null
|
||||||
# interval :integer not null
|
# checks_count :integer default(0), not null
|
||||||
# recipient :string(255) not null
|
# interval :integer not null
|
||||||
# sent_at :datetime
|
# label :string(255)
|
||||||
# status :integer default("pending"), not null
|
# recipient :string(255) not null
|
||||||
# created_at :datetime not null
|
# created_at :datetime not null
|
||||||
# updated_at :datetime not null
|
# updated_at :datetime not null
|
||||||
# check_id :bigint(8)
|
# check_id :bigint(8)
|
||||||
|
# user_id :bigint(8)
|
||||||
#
|
#
|
||||||
# Indexes
|
# Indexes
|
||||||
#
|
#
|
||||||
# index_notifications_on_check_id (check_id)
|
# index_notifications_on_check_id (check_id)
|
||||||
|
# index_notifications_on_user_id (user_id)
|
||||||
#
|
#
|
||||||
# Foreign Keys
|
# Foreign Keys
|
||||||
#
|
#
|
||||||
# fk_rails_... (check_id => checks.id)
|
# fk_rails_... (check_id => checks.id)
|
||||||
|
# fk_rails_... (user_id => users.id)
|
||||||
#
|
#
|
||||||
|
|
||||||
require "test_helper"
|
require "test_helper"
|
||||||
|
|
Loading…
Reference in a new issue