forked from ubicloud/ubicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add separate page model for lantern doctor * add dashboard view and routes for web to manage lantern incidents
- Loading branch information
Showing
22 changed files
with
707 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
Sequel.migration do | ||
up do | ||
create_enum(:doctor_page_status, %w[new triggered acknowledged resolved]) | ||
create_table(:lantern_doctor_page) do | ||
column :id, :uuid, primary_key: true, default: nil | ||
foreign_key :query_id, :lantern_doctor_query, type: :uuid | ||
foreign_key :page_id, :page, type: :uuid | ||
column :status, :doctor_page_status, null: false, default: "new" | ||
column :db, :text, null: true, default: "postgres" | ||
column :created_at, :timestamptz, null: false, default: Sequel.lit("now()") | ||
column :updated_at, :timestamptz, null: false, default: Sequel.lit("now()") | ||
end | ||
end | ||
|
||
down do | ||
drop_table(:lantern_doctor_page) | ||
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../model" | ||
|
||
class LanternDoctorPage < Sequel::Model | ||
one_to_one :page, class: Page, key: :id, primary_key: :page_id | ||
many_to_one :query, class: LanternDoctorQuery, key: :query_id, primary_key: :id | ||
|
||
include ResourceMethods | ||
|
||
def self.create_incident(query, db_name, err: "", output: "") | ||
pg = Prog::PageNexus.assemble_with_logs("Healthcheck: #{query.name} failed on #{query.doctor.resource.name} - #{query.doctor.resource.label} (#{db_name})", [query.ubid, query.doctor.ubid], {"stderr" => err, "stdout" => output}, query.severity, "LanternDoctorQueryFailed", query.id, db_name) | ||
LanternDoctorPage.create_with_id( | ||
query_id: query.id, | ||
page_id: pg.id, | ||
status: "new" | ||
) | ||
end | ||
|
||
def path | ||
"#{query.doctor.resource.path}/doctor/incidents/#{id}" | ||
end | ||
|
||
def error | ||
page.details["logs"]["stderr"] | ||
end | ||
|
||
def output | ||
page.details["logs"]["stdout"] | ||
end | ||
|
||
def ack | ||
update(status: "acknowledged") | ||
end | ||
|
||
def trigger | ||
update(status: "triggered") | ||
end | ||
|
||
def resolve | ||
page.incr_resolve | ||
update(status: "resolved") | ||
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
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
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,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class CloverWeb | ||
hash_branch(:project_prefix, "lantern-doctor") do |r| | ||
@serializer = Serializers::Web::LanternDoctorQuery | ||
|
||
r.get true do | ||
@lantern_incidents = serialize(LanternDoctorQuery.where(condition: "failed").all, :detailed) | ||
view "lantern-doctor/index" | ||
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
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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
class CloverWeb | ||
hash_branch(:project_location_lantern_prefix, "doctor") do |r| | ||
@serializer = Serializers::Api::LanternDoctorQuery | ||
@lantern_doctor = LanternResource[@pg[:id]].doctor | ||
|
||
r.on "incidents" do | ||
r.on String do |incident_id| | ||
incident = LanternDoctorPage[incident_id] | ||
|
||
unless incident | ||
response.status = 404 | ||
r.halt | ||
end | ||
|
||
r.post "trigger" do | ||
incident.trigger | ||
r.redirect "#{@project.path}/lantern-doctor" | ||
end | ||
|
||
r.post "ack" do | ||
incident.ack | ||
r.redirect "#{@project.path}/lantern-doctor" | ||
end | ||
|
||
r.post "resolve" do | ||
incident.resolve | ||
incident.query.update(condition: "healthy") | ||
r.redirect "#{@project.path}/lantern-doctor" | ||
end | ||
end | ||
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
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,30 @@ | ||
# frozen_string_literal: true | ||
|
||
class Serializers::Web::LanternDoctorQuery < Serializers::Base | ||
def self.base(query) | ||
{ | ||
id: query.id, | ||
name: query.name, | ||
label: "#{query.name} - #{query.doctor.resource.name} (#{query.doctor.resource.label})", | ||
type: query.type, | ||
condition: query.condition, | ||
last_checked: query.last_checked, | ||
schedule: query.schedule, | ||
db_name: query.db_name, | ||
sql: query.sql, | ||
severity: query.severity | ||
} | ||
end | ||
|
||
structure(:default) do |query| | ||
base(query) | ||
end | ||
|
||
structure(:detailed) do |query| | ||
base(query).merge({ | ||
incidents: query.new_and_active_pages.map { | ||
{path: _1.path, id: _1.id, status: _1.status, summary: _1.page.summary, error: _1.error, output: _1.output, triggered_at: _1.created_at} | ||
} | ||
}) | ||
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../spec_helper" | ||
|
||
RSpec.describe LanternDoctorPage do | ||
subject(:lantern_doctor_page) { | ||
described_class.new do |r| | ||
r.id = "6181ddb3-0002-8ad0-9aeb-084832c9273b" | ||
end | ||
} | ||
|
||
let(:pg) { | ||
instance_double( | ||
Page, | ||
severity: "critical" | ||
) | ||
} | ||
|
||
describe "#create_incident" do | ||
it "creates page" do | ||
query = instance_double(LanternDoctorQuery, ubid: "test", id: "test-id", severity: "error", name: "test", doctor: instance_double(LanternDoctor, ubid: "test-doc-ubid", resource: instance_double(LanternResource, name: "test-res", label: "test-label"))) | ||
db_name = "postgres" | ||
err = "test-err" | ||
output = "test-output" | ||
expect(Prog::PageNexus).to receive(:assemble_with_logs).with("Healthcheck: #{query.name} failed on #{query.doctor.resource.name} - #{query.doctor.resource.label} (#{db_name})", [query.ubid, query.doctor.ubid], {"stderr" => err, "stdout" => output}, query.severity, "LanternDoctorQueryFailed", query.id, db_name).and_return(instance_double(Page, id: "test-pg-id")) | ||
doctor_page = instance_double(described_class) | ||
expect(described_class).to receive(:create_with_id).with(query_id: query.id, page_id: "test-pg-id", status: "new").and_return(doctor_page) | ||
expect(described_class.create_incident(query, db_name, err: err, output: output)).to be(doctor_page) | ||
end | ||
end | ||
|
||
describe "#properties (logs)" do | ||
it "returns sterr and stdout from logs" do | ||
expect(lantern_doctor_page).to receive(:page).and_return(pg).at_least(:once) | ||
expect(pg).to receive(:details).and_return({"logs" => {"stdout" => "out", "stderr" => "err"}}).at_least(:once) | ||
expect(lantern_doctor_page.error).to eq("err") | ||
expect(lantern_doctor_page.output).to eq("out") | ||
end | ||
end | ||
|
||
describe "#actions (trigger, ack, resolve)" do | ||
it "triggers page" do | ||
expect(lantern_doctor_page).to receive(:update).with(status: "triggered") | ||
expect { lantern_doctor_page.trigger }.not_to raise_error | ||
end | ||
|
||
it "acks page" do | ||
expect(lantern_doctor_page).to receive(:update).with(status: "acknowledged") | ||
expect { lantern_doctor_page.ack }.not_to raise_error | ||
end | ||
|
||
it "resolves page" do | ||
expect(lantern_doctor_page).to receive(:update).with(status: "resolved") | ||
expect(lantern_doctor_page).to receive(:page).and_return(pg).at_least(:once) | ||
expect(pg).to receive(:incr_resolve) | ||
expect { lantern_doctor_page.resolve }.not_to raise_error | ||
end | ||
end | ||
|
||
describe "#path" do | ||
it "returns correct path" do | ||
query = instance_double(LanternDoctorQuery, ubid: "test", id: "test-id", severity: "error", name: "test", doctor: instance_double(LanternDoctor, ubid: "test-doc-ubid", resource: instance_double(LanternResource, name: "test-res", label: "test-label", path: "test-path"))) | ||
expect(lantern_doctor_page).to receive(:query).and_return(query) | ||
expect(lantern_doctor_page.path).to eq("test-path/doctor/incidents/#{lantern_doctor_page.id}") | ||
end | ||
end | ||
end |
Oops, something went wrong.