-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
458 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Audit < ApplicationRecord | ||
MAX_ATTEMPTS = 3 | ||
MAX_RUNTIME = 1.hour.freeze | ||
|
||
belongs_to :site, touch: true | ||
|
||
validates :url, presence: true, url: true | ||
normalizes :url, with: ->(url) { URI.parse(url).normalize.to_s } | ||
|
||
enum :status, ["pending", "running", "passed", "retryable", "failed"].index_by(&:itself), validate: true, default: :pending | ||
|
||
scope :sort_by_newest, -> { order(created_at: :desc) } | ||
scope :sort_by_url, -> { order(Arel.sql("REGEXP_REPLACE(url, '^https?://(www\.)?', '') ASC")) } | ||
scope :due, -> { pending.where("run_at <= now()") } | ||
scope :past, -> { where(status: [:passed, :failed]) } | ||
scope :scheduled, -> { where("run_at > now()") } | ||
scope :retryable, -> { failed.where(attempts: ...MAX_ATTEMPTS) } | ||
scope :to_run, -> { due.or(retryable) } | ||
scope :clean, -> { passed.where(attempts: 0) } | ||
scope :late, -> { pending.where("run_at <= ?", 1.hour.ago) } | ||
scope :retried, -> { passed.where(attempts: 1..) } | ||
scope :stalled, -> { running.where("run_at < ?", MAX_RUNTIME.ago) } | ||
scope :crashed, -> { failed.where(attempts: MAX_ATTEMPTS) } | ||
|
||
delegate :hostname, :path, to: :parsed_url | ||
|
||
def run_at = super || Time.zone.now | ||
def due? = pending? && run_at <= Time.zone.now | ||
def runnable? = due? || retryable? | ||
def parsed_url = @parsed_url ||= URI.parse(url).normalize | ||
def url_without_scheme = @url_without_scheme ||= [hostname, path == "/" ? nil : path].compact.join(nil) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,49 @@ | ||
class Site < ApplicationRecord | ||
extend FriendlyId | ||
|
||
friendly_id :url, use: [:slugged, :history] | ||
has_many :audits, dependent: :destroy | ||
has_one_of_many :audit, -> { order("audits.created_at DESC") }, class_name: "Audit" | ||
|
||
attribute :url, :string | ||
friendly_id :url_without_scheme, use: [:slugged, :history] | ||
|
||
delegate :url, :url_without_scheme, to: :audit | ||
|
||
class << self | ||
def find_or_create_by_url(attributes) | ||
url = attributes.to_h.delete(:url) | ||
attributes.to_h.delete(:name) if attributes[:name].blank? | ||
# Ignore http/https duplicates when searching | ||
normalized_url = [url, url.sub(/^https?/, url.start_with?("https") ? "http" : "https")] | ||
joins(:audits).find_by(audits: { url: normalized_url })&.tap { it.update(attributes) } \ | ||
|| create_with_audit(url:, **attributes) | ||
end | ||
|
||
def create_with_audit(url: nil, **attributes) | ||
new_with_audit(url:, **attributes).tap(&:save) | ||
end | ||
|
||
def new_with_audit(url: nil, **attributes) | ||
new(attributes).tap { |site| site.audits.build(url:) } | ||
end | ||
end | ||
|
||
def new(attributes = nil) | ||
return super if attributes.nil? | ||
|
||
attributes = attributes.to_h.symbolize_keys | ||
|
||
if url = attributes.delete(:url) | ||
self.class.find_or_create_by(url:, **attributes) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def url=(new_url) | ||
audit = audits.build(url: new_url) | ||
end | ||
|
||
def to_title = url_without_scheme | ||
def audit = super || audits.last || audits.build | ||
def should_generate_new_friendly_id? = new_record? || (audit && slug != url_without_scheme.parameterize) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class CreateAudits < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :audits do |t| | ||
t.belongs_to :site, null: false, foreign_key: true | ||
t.string :url, null: false | ||
t.string :status, null: false | ||
t.integer :attempts, null: false, default: 0 | ||
t.datetime :run_at, null: false, default: -> { "CURRENT_TIMESTAMP" } | ||
|
||
t.timestamps | ||
|
||
t.index :url | ||
t.index [:status, :run_at], name: "index_audits_on_status_and_run_at" | ||
t.index "REGEXP_REPLACE(url, '^https?://(www\.)?', '')", name: "index_audits_on_normalized_url" | ||
t.index :attempts, where: "status = 'failed' AND attempts > 0", name: "index_audits_on_retryable" | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
FactoryBot.define do | ||
factory :audit do | ||
site { association :site, url:, strategy: :build } | ||
url { "https://example.com" } | ||
|
||
trait :passed do | ||
status { "passed" } | ||
end | ||
|
||
trait :failed do | ||
status { "failed" } | ||
end | ||
|
||
trait :pending do | ||
status { "pending" } | ||
end | ||
|
||
trait :running do | ||
status { "running" } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
FactoryBot.define do | ||
factory :site do | ||
sequence(:slug) { |n| "www.example-#{n}.com" } | ||
sequence(:url) { |n| "https://www.example-#{n}.com/" } | ||
end | ||
end |
Oops, something went wrong.