Skip to content

Commit

Permalink
Remove virtus from BackendStatus (#18444)
Browse files Browse the repository at this point in the history
* remove virtus from backend status

* fix linting error

* fix spec/factory error

* more linting

* minor refactoring

* reogranize comments
  • Loading branch information
stevenjcumming authored Sep 23, 2024
1 parent 9a4fb45 commit f9b9311
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 31 deletions.
51 changes: 29 additions & 22 deletions app/controllers/v0/backend_statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,48 @@ module V0
class BackendStatusesController < ApplicationController
service_tag 'maintenance-windows'
skip_before_action :authenticate
before_action :validate_service, only: [:show]

# NOTE: this endpoint is somewhat misleading. Index gets data from PagerDuty and
# show only looks at GI bill scheduled downtime (and gets no data from PagerDuty)
def index
statuses = ExternalServicesRedis::Status.new.fetch_or_cache
maintenance_windows = MaintenanceWindow.end_after(Time.zone.now)

options = { params: { maintenance_windows: } }
render json: BackendStatusesSerializer.new(statuses, options)
render json: BackendStatusesSerializer.new(backend_statuses, options)
end

# GET /v0/backend_statuses/:service
def show
@backend_service = params[:service]
raise Common::Exceptions::RecordNotFound, @backend_service unless recognized_service?
render json: BackendStatusSerializer.new(backend_status)
end

# get status
be_status = BackendStatus.new(name: @backend_service)
case @backend_service
when BackendServices::GI_BILL_STATUS
be_status.is_available = BenefitsEducation::Service.within_scheduled_uptime?
be_status.uptime_remaining = BenefitsEducation::Service.seconds_until_downtime
else
# default service is up!
be_status.is_available = true
be_status.uptime_remaining = 0
end
private

render json: BackendStatusSerializer.new(be_status)
# NOTE: Data is from PagerDuty
def backend_statuses
@backend_statuses ||= ExternalServicesRedis::Status.new.fetch_or_cache
end

private
def maintenance_windows
@maintenance_windows ||= MaintenanceWindow.end_after(Time.zone.now)
end

# NOTE: Data is GI bill scheduled downtime
def backend_status
@backend_status ||= BackendStatus.new(name: backend_service)
end

def backend_service
params[:service]
end

def validate_service
raise Common::Exceptions::RecordNotFound, backend_service unless recognized_service?
end

def recognized_service?
BackendServices.all.include?(@backend_service)
BackendServices.all.include?(backend_service)
end

def backend_status_is_available
backend_service == BackendServices::GI_BILL_STATUS
end
end
end
25 changes: 20 additions & 5 deletions app/models/backend_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@
class BackendStatus
include ActiveModel::Serialization
include ActiveModel::Validations
include Virtus.model(nullify_blank: true)

attribute :name, String
attribute :service_id, String
attribute :is_available, Boolean
attribute :uptime_remaining, Integer
attr_reader :name, :service_id

validates :name, presence: true
validates :is_available, presence: true
validates :uptime_remaining, presence: true

def initialize(name:, service_id: nil)
@name = name
@service_id = service_id
end

def available?
gibs_service? ? BenefitsEducation::Service.within_scheduled_uptime? : true
end

def uptime_remaining
gibs_service? ? BenefitsEducation::Service.seconds_until_downtime.to_i : 0
end

private

def gibs_service?
@name == BackendServices::GI_BILL_STATUS
end
end
2 changes: 1 addition & 1 deletion app/serializers/backend_status_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class BackendStatusSerializer

attribute :name
attribute :service_id
attribute :is_available
attribute :is_available, &:available?
attribute :uptime_remaining
end
4 changes: 2 additions & 2 deletions spec/factories/backend_statuses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

FactoryBot.define do
factory :backend_status do
is_available { true }
name { 'gibs' }
service_id { 'appeals' }
uptime_remaining { 39_522 }

initialize_with { new(name:, service_id:) }
end
end
2 changes: 1 addition & 1 deletion spec/serializers/backend_status_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
end

it 'includes :is_available' do
expect(attributes['is_available']).to eq backend_status.is_available
expect(attributes['is_available']).to eq backend_status.available?
end

it 'includes :uptime_remaining' do
Expand Down

0 comments on commit f9b9311

Please sign in to comment.