Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #4

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ Layout/HashAlignment:

RSpec/MultipleExpectations:
Enabled: false

RSpec/AnyInstance:
Enabled: false

Metrics/MethodLength:
Exclude:
- lib/generators/ez/status/install_generator.rb
- lib/generators/ez/status/migrations_generator.rb
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ GEM
parallel (1.20.1)
parser (3.0.1.1)
ast (~> 2.4.1)
pg (1.2.3)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -296,6 +297,7 @@ DEPENDENCIES
generator_spec
guard-rspec
launchy
pg
pry-rails
rails-controller-testing
redis
Expand Down
120 changes: 120 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Configuration interface allows you to change default behavior

```ruby
# config/initializers/ez_status.rb

require 'ez/status/providers/database'
require 'ez/status/providers/cache'
# require 'ez/status/providers/delayed_job'
Expand Down Expand Up @@ -61,15 +62,25 @@ Ez::Status.configure do |config|
config.status_base_controller = 'ApplicationController'
config.status_base_routes = '/status'

# You can change layout, default is using application layout
# config.layout = 'layouts/application'

# You can change the header text
# config.ui_header = 'MyStatus'

# Basic Authentication
# config.basic_auth_credentials = {
# username: 'MyUsername',
# password: 'MyPassword'
# }

# Order columns and which columns need to show
# config.columns = [ :message, :value, :result, :monitor_name,]

# DB table name, that you can change as well
# config.active_record_table_name = :ez_status_records

# Define your monitors
config.monitors = [
Ez::Status::Providers::Database,
Ez::Status::Providers::Cache,
Expand All @@ -78,9 +89,93 @@ Ez::Status.configure do |config|
# Ez::Status::Providers::Sidekiq,
# MyCustomProvider
]

# Pass your custom css classes through css_map config
# Defaults would be merged with yours:
# config.ui_custom_css_map = {
# 'ez-status-index-container' => 'your_css_class',
# 'ez-status-index-inner-container' => 'your_css_class',
#
# # header
# 'ez-status-index-inner-header' => 'your_css_class',
# 'ez-status-index-inner-title' => 'your_css_class',
# 'ez-status-index-inner-title-span' => 'your_css_class',
#
# # monitors
# 'ez-status-index-monitors-collection' => 'your_css_class',
#
# # monitor
# 'ez-status-index-status' => 'your_css_class',
# 'ez-status-index-failed' => 'your_css_class',
#
# 'ez-status-index-check-monitor-name' => 'your_css_class',
# 'ez-status-index-check-monitor-name-span' => 'your_css_class'
#
# 'ez-status-index-check-message' => 'your_css_class',
# 'ez-status-index-check-message-span' => 'your_css_class',
#
# 'ez-status-index-check-value' => 'your_css_class',
# 'ez-status-index-check-value-span' => 'your_css_class',
#
# 'ez-status-index-check-result' => 'your_css_class',
# 'ez-status-index-check-result-span' => 'your_css_class',
#
# }
end
```

### ActiveRecord migrations:

**If you want save results to DB (PostgreSQL, etc), please change configuration first**

Uncomment DB table name:
```ruby
# config/initializers/ez_status.rb

# DB table name, that you can change as well
config.active_record_table_name = :ez_status_records
```

And run the generator:

```bash
rails generate ez:status:migrations
```

Then run migrations

```bash
rails db:migrate
```

After that you have a method to save all results to DB

```bash
Ez::Status.capture!
```

#### Example for [Whenever gem](https://github.com/javan/whenever)
```bash
# config/schedule.rb

every 5.minute do
runner "Ez::Status.capture!"
end

```

#### Example for [Sidekiq-Cron gem](https://github.com/ondrejbartas/sidekiq-cron)
```bash
# config/schedule.rb

ez_status_check_all_services:
name: "Ez::Status check all services in config.monitors and save results to DB"
cron: "*/5 * * * *"
class: "Ez::Status.capture!"
queue: scheduler

```

### Basic Authentication

```ruby
Expand All @@ -100,6 +195,13 @@ Ez::Status.configure do |config|
end
```

### Change columns view

```ruby
# Order columns and which columns need to show
config.columns = [:message, :value, :result, :monitor_name,]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config. ui_columns

```

## Examples

```ruby
Expand Down Expand Up @@ -154,6 +256,24 @@ class MyCustomProvider
end
```

## Creating a Custom Style

### Style for Semantic Ui

```ruby
config.ui_custom_css_map = {
'ez-status-index-container' => 'ui grid',
'ez-status-index-inner-container' => 'six wide column',
'ez-status-index-monitors-collection' => 'ui middle aligned divided list',
'ez-status-index-status' => 'item',
'ez-status-index-check-monitor-name' => 'content',
'ez-status-index-check-monitor-name-span' => 'ui green label',
'ez-status-index-check-message' => 'right floated content',
'ez-status-index-check-value' => 'right floated content',
'ez-status-index-check-result' => 'right floated content'
}
```

## TODO

-
Expand Down
5 changes: 5 additions & 0 deletions app/cells/ez/status/application_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def css_for(item)
custom_css_map[scoped_item] || scoped_item
end

def css_for_column_name(item)
item.gsub!(/_/, '-')
css_for(item)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

css_for(item.gsub(/_/, '-'))

end

def custom_css_map
@custom_css_map ||= Ez::Status.config.ui_custom_css_map || {}
end
Expand Down
27 changes: 6 additions & 21 deletions app/cells/ez/status/index/show.slim
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,12 @@
= div_for 'index-inner-container'
header class="#{css_for('index-header')}"
h1 class="#{css_for('index-title')}"
span class="#{css_for('index-top')}" = header
span class="#{css_for('index-bottom')}"
span class="#{css_for('index-title-span')}" = header

div class="#{css_for('index-monitors-collection')}"
- monitors.each do |monitor|
div class="#{css_for('index-status')} #{css_for('failed') if monitor[:result] == 'FAILURE'}" id=(monitor[:name].to_sym)

- if monitor[:msg]
div class="#{css_for('index-check-message')}"
span class="#{css_for('index-check-name-message')}"
= monitor[:msg]

- if monitor[:value]
div class="#{css_for('index-check-value')}"
span class="#{css_for('index-check-name-value')}"
= monitor[:value]

div class="#{css_for('index-check-result')}"
span class="#{css_for('index-check-name-result')}"
= monitor[:result]

div class="#{css_for('index-check-name')}"
span class="#{css_for('index-check-name-span')}"
= monitor[:name]
div class="#{css_for('index-status')} #{css_for('index-failed') if monitor[:result] == 'FAILURE'}" id=(monitor[:monitor_name].to_sym)
- columns.each do |column_name|
div class="#{css_for_column_name("index-check-#{column_name}")}"
span class="#{css_for_column_name("index-check-#{column_name}-span")}"
= monitor[column_name]
25 changes: 4 additions & 21 deletions app/cells/ez/status/index_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,16 @@
module Ez
module Status
class IndexCell < ApplicationCell
OK = 'OK'
FAILURE = 'FAILURE'

def header
Ez::Status.config.ui_header || 'Status'
end

def monitors
@monitors ||= Ez::Status.config.monitors.map do |monitor|
result, msg, value = monitor.new.check
build_monitor_acum(monitor, result, msg, value)
rescue StandardError => e
msg = e.message
ensure
build_monitor_acum(monitor, result, msg, value)
end
def columns
Ez::Status.config.columns
end

private

def build_monitor_acum(monitor, result, msg, value)
{
name: monitor.name.demodulize,
result: result ? OK : FAILURE,
msg: msg,
value: value
}
def monitors
@monitors ||= Ez::Status.check_all
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions app/models/ez/status/application_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Ez
module Status
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
end
end
11 changes: 11 additions & 0 deletions app/models/ez/status/records.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Ez
module Status
class Records < ApplicationRecord
self.table_name = Ez::Status.config.active_record_table_name

validates :monitor_name, presence: true
end
end
end
1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
root to: 'status#index'
end
end

1 change: 1 addition & 0 deletions ez-status.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'generator_spec'
spec.add_development_dependency 'guard-rspec'
spec.add_development_dependency 'launchy'
spec.add_development_dependency 'pg'
spec.add_development_dependency 'pry-rails'
spec.add_development_dependency 'rails-controller-testing'
spec.add_development_dependency 'redis'
Expand Down
12 changes: 12 additions & 0 deletions lib/ez/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@

require 'ez/status/version'
require 'ez/status/engine'
require 'ez/status/services'

require 'ez/configurator'

module Ez
module Status
DEFAULT_COLUMNS = %i[monitor_name message value result].freeze

include Ez::Configurator

extend Ez::Status::Services::Store
extend Ez::Status::Services::Monitors

configure do |config|
config.status_base_controller = nil
config.status_base_routes = nil

config.layout = nil

config.basic_auth_credentials = {}

config.columns = DEFAULT_COLUMNS

config.active_record_table_name = :ez_status_records

config.monitors = []

config.ui_header = nil
Expand Down
42 changes: 42 additions & 0 deletions lib/ez/status/services.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module Ez
module Status
module Services
# Ez::Status.capture!
module Store
def capture!
Ez::Status::Records.insert_all Ez::Status.check_all
end
end

# Ez::Status.check_all
module Monitors
OK = 'OK'
FAILURE = 'FAILURE'

def check_all
Ez::Status.config.monitors.map do |monitor|
result, msg, value = monitor.new.check
build_monitor_acum(monitor, result, msg, value)
rescue StandardError => e
msg = e.message
ensure
build_monitor_acum(monitor, result, msg, value)
end
end

private

def build_monitor_acum(monitor, result, msg, value)
{
monitor_name: monitor.name.demodulize,
result: result ? OK : FAILURE,
message: msg,
value: value
}
end
end
end
end
end
Loading