From 02eebeba31c31a379edab6c5813f4e09cc0e079e Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Mon, 5 Jul 2021 20:32:22 +0300 Subject: [PATCH 01/15] using css styles --- README.md | 18 +++++++++ app/cells/ez/status/index/show.slim | 11 +++--- lib/generators/ez/status/install_generator.rb | 26 +++++++++++++ spec/dummy/config/initializers/ez_status.rb | 26 +++++++++++++ spec/features/show_spec.rb | 37 +++++++++++++++++-- .../ez/status/install_generator_spec.rb | 26 +++++++++++++ 6 files changed, 135 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 00648b6..a8d6f1c 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,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-name' => 'content', + 'ez-status-index-check-message' => 'right floated content', + 'ez-status-index-check-value' => 'right floated content', + 'ez-status-index-check-result' => 'right floated content', + 'ez-status-index-check-name-span' => 'ui green label' +} +``` + ## TODO - diff --git a/app/cells/ez/status/index/show.slim b/app/cells/ez/status/index/show.slim index 9160f96..2882a8f 100644 --- a/app/cells/ez/status/index/show.slim +++ b/app/cells/ez/status/index/show.slim @@ -2,25 +2,24 @@ = 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) + div class="#{css_for('index-status')} #{css_for('index-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')}" + span class="#{css_for('index-check-message-span')}" = monitor[:msg] - if monitor[:value] div class="#{css_for('index-check-value')}" - span class="#{css_for('index-check-name-value')}" + span class="#{css_for('index-check-value-span')}" = monitor[:value] div class="#{css_for('index-check-result')}" - span class="#{css_for('index-check-name-result')}" + span class="#{css_for('index-check-result-span')}" = monitor[:result] div class="#{css_for('index-check-name')}" diff --git a/lib/generators/ez/status/install_generator.rb b/lib/generators/ez/status/install_generator.rb index 7649271..ccf9ad0 100644 --- a/lib/generators/ez/status/install_generator.rb +++ b/lib/generators/ez/status/install_generator.rb @@ -51,6 +51,32 @@ def create_initializer_file # Ez::Status::Providers::Sidekiq, # MyCustomProvider ] + + # config.ui_custom_css_map = { + # 'ez-status-index-container' => 'your_css_class', + # 'ez-status-index-inner-container' => 'your_css_class', + # # heder + # '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-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', + # + # 'ez-status-index-check-name' => 'your_css_class', + # 'ez-status-index-check-name-span' => 'your_css_class' + # } end " end diff --git a/spec/dummy/config/initializers/ez_status.rb b/spec/dummy/config/initializers/ez_status.rb index 059f81e..7e8fe64 100644 --- a/spec/dummy/config/initializers/ez_status.rb +++ b/spec/dummy/config/initializers/ez_status.rb @@ -43,4 +43,30 @@ # Ez::Status::Providers::Sidekiq, # MyCustomProvider ] + + # config.ui_custom_css_map = { + # 'ez-status-index-container' => 'your_css_class', + # 'ez-status-index-inner-container' => 'your_css_class', + # # heder + # '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-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', + # + # 'ez-status-index-check-name' => 'your_css_class', + # 'ez-status-index-check-name-span' => 'your_css_class' + # } end diff --git a/spec/features/show_spec.rb b/spec/features/show_spec.rb index c9d1f35..4aeb3cd 100644 --- a/spec/features/show_spec.rb +++ b/spec/features/show_spec.rb @@ -77,13 +77,44 @@ context 'when default' do before { visit '/status' } - it 'decorate DOM with ez-status classes' do + it 'decorate DOM with ez-status classes for container' do expect(page).to have_selector('.ez-status-index-container') + expect(page).to have_selector('.ez-status-index-inner-container') + end + + it 'decorate DOM with ez-status classes for header' do expect(page).to have_selector('.ez-status-index-header') - expect(page).to have_selector('.ez-status-index-main') + expect(page).to have_selector('.ez-status-index-title') + expect(page).to have_selector('.ez-status-index-title-span') + end + + it 'decorate DOM with ez-status classes for monitors' do + expect(page).to have_selector('.ez-status-index-monitors-collection') + end + + it 'decorate DOM with ez-status classes for monitor' do expect(page).to have_selector('.ez-status-index-status') - expect(page).to have_selector('.ez-status-index-check-name') + expect(page).to have_selector('.ez-status-index-failed') + end + + it 'decorate DOM with ez-status classes for message' do + expect(page).to have_selector('.ez-status-index-check-message') + expect(page).to have_selector('.ez-status-index-check-message-span') + end + + it 'decorate DOM with ez-status classes for value' do + expect(page).to have_selector('.ez-status-index-check-value') + expect(page).to have_selector('.ez-status-index-check-value-span') + end + + it 'decorate DOM with ez-status classes for result' do expect(page).to have_selector('.ez-status-index-check-result') + expect(page).to have_selector('.ez-status-index-check-result-span') + end + + it 'decorate DOM with ez-status classes for name' do + expect(page).to have_selector('.ez-status-index-check-name') + expect(page).to have_selector('.ez-status-index-check-name-span') end end diff --git a/spec/lib/generators/ez/status/install_generator_spec.rb b/spec/lib/generators/ez/status/install_generator_spec.rb index 24fb328..44c4e9d 100755 --- a/spec/lib/generators/ez/status/install_generator_spec.rb +++ b/spec/lib/generators/ez/status/install_generator_spec.rb @@ -59,6 +59,32 @@ # Ez::Status::Providers::Sidekiq, # MyCustomProvider ] + + # config.ui_custom_css_map = { + # 'ez-status-index-container' => 'your_css_class', + # 'ez-status-index-inner-container' => 'your_css_class', + # # heder + # '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-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', + # + # 'ez-status-index-check-name' => 'your_css_class', + # 'ez-status-index-check-name-span' => 'your_css_class' + # } end " end From bb665556e5cb64660cb542fde1ef35b0fc879bbd Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Mon, 5 Jul 2021 20:32:59 +0300 Subject: [PATCH 02/15] rubocop --- .rubocop.yml | 3 +++ config/routes.rb | 1 - spec/controllers/ez/status/status_controller_spec.rb | 12 +++--------- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 8fa443c..32eebf9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -47,3 +47,6 @@ Layout/HashAlignment: RSpec/MultipleExpectations: Enabled: false + +RSpec/AnyInstance: + Enabled: false diff --git a/config/routes.rb b/config/routes.rb index 2033ecb..9bd21d6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,4 +5,3 @@ root to: 'status#index' end end - diff --git a/spec/controllers/ez/status/status_controller_spec.rb b/spec/controllers/ez/status/status_controller_spec.rb index bf7adc9..314505a 100755 --- a/spec/controllers/ez/status/status_controller_spec.rb +++ b/spec/controllers/ez/status/status_controller_spec.rb @@ -8,16 +8,12 @@ describe 'Application Layout' do context 'when default layout' do - subject { get :index } - it 'renders page' do - expect(subject).to render_template('layouts/application') + expect(get(:index)).to render_template('layouts/application') end end xcontext 'when custom layout: layouts/custom_application' do - subject { get :index } - around do |spec| Ez::Status.config.layout = 'layouts/custom_application' routes { Ez::Status::Engine.routes } @@ -27,13 +23,11 @@ end xit 'layout' do - expect(subject).not_to render_template(layout: :application) + expect(get(:index)).not_to render_template(layout: :application) end end xcontext 'when missing template layouts/application_not_found' do - subject { get :index } - around do |spec| Ez::Status.config.layout = 'layouts/application_not_found' spec.run @@ -41,7 +35,7 @@ end it 'renders page' do - expect(subject).to render_template('layouts/application_not_found') + expect(get(:index)).to render_template('layouts/application_not_found') end end end From 695553ead4125ad649a28f4eda7210fb0c976404 Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Tue, 6 Jul 2021 17:58:20 +0300 Subject: [PATCH 03/15] add model for status --- Gemfile.lock | 2 + README.md | 41 +++++++++++++++++++ app/models/ez/status/application_record.rb | 9 ++++ app/models/ez/status/status.rb | 14 +++++++ ez-status.gemspec | 1 + lib/ez/status.rb | 2 + .../ez/status/migrations_generator.rb | 37 +++++++++++++++++ spec/dummy/config/database.yml | 8 ++-- spec/dummy/config/initializers/ez_status.rb | 2 + .../ez/status/install_generator_spec.rb | 3 +- .../ez/status/migrations_generator_spec.rb | 39 ++++++++++++++++++ 11 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 app/models/ez/status/application_record.rb create mode 100644 app/models/ez/status/status.rb create mode 100644 lib/generators/ez/status/migrations_generator.rb create mode 100755 spec/lib/generators/ez/status/migrations_generator_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index f643694..2989af4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -296,6 +297,7 @@ DEPENDENCIES generator_spec guard-rspec launchy + pg pry-rails rails-controller-testing redis diff --git a/README.md b/README.md index a8d6f1c..a3422d5 100644 --- a/README.md +++ b/README.md @@ -78,9 +78,50 @@ Ez::Status.configure do |config| # Ez::Status::Providers::Sidekiq, # MyCustomProvider ] + + # config.ui_custom_css_map = { + # 'ez-status-index-container' => 'your_css_class', + # 'ez-status-index-inner-container' => 'your_css_class', + # # heder + # '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-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', + # + # 'ez-status-index-check-name' => 'your_css_class', + # 'ez-status-index-check-name-span' => 'your_css_class' + # } end ``` +### ActiveRecord migrations: + +**If you want save data to DataBase, please change configuration first** + +config/initializers/ez_status.rb +```ruby +config.status_table_name = 'my_model_name' +``` + +And run +```bash +rails generate ez:status:migrations +``` + + ### Basic Authentication ```ruby diff --git a/app/models/ez/status/application_record.rb b/app/models/ez/status/application_record.rb new file mode 100644 index 0000000..3157673 --- /dev/null +++ b/app/models/ez/status/application_record.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Ez + module Status + class ApplicationRecord < ActiveRecord::Base + self.abstract_class = true + end + end +end diff --git a/app/models/ez/status/status.rb b/app/models/ez/status/status.rb new file mode 100644 index 0000000..d66d6e8 --- /dev/null +++ b/app/models/ez/status/status.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Ez + module Status + class Status < ApplicationRecord + self.table_name = Ez::Status.config.status_table_name + + validates :name, presence: true + validates :result, presence: false + validates :message, presence: false + validates :value, presence: false + end + end +end diff --git a/ez-status.gemspec b/ez-status.gemspec index 3068fd8..7e371f9 100644 --- a/ez-status.gemspec +++ b/ez-status.gemspec @@ -44,4 +44,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'sidekiq' spec.add_development_dependency 'simplecov' spec.add_development_dependency 'sqlite3' + spec.add_development_dependency 'pg' end diff --git a/lib/ez/status.rb b/lib/ez/status.rb index 6bcf2e2..d87de69 100644 --- a/lib/ez/status.rb +++ b/lib/ez/status.rb @@ -15,6 +15,8 @@ module Status config.layout = nil config.basic_auth_credentials = {} + config.status_table_name = nil + config.monitors = [] config.ui_header = nil diff --git a/lib/generators/ez/status/migrations_generator.rb b/lib/generators/ez/status/migrations_generator.rb new file mode 100644 index 0000000..b00ebcb --- /dev/null +++ b/lib/generators/ez/status/migrations_generator.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +# rubocop:disable all +module Ez + module Status + class MigrationsGenerator < Rails::Generators::Base + def config + Ez::Status.config + end + + def table_name + raise 'You need setup table name' if config.status_table_name.blank? + config.status_table_name + end + + def create_migration + # TODO: ActiveRecord::Migration[6.0], add automatic detection [5.0], [5.1], [5.2], [6.1], [6.1], [6.2], + create_file "db/migrate/#{Time.current.strftime('%Y%m%d%H%M%S')}_create_ez_status_#{table_name.underscore}.rb", + "# frozen_string_literal: true + +class CreateEzStatus#{table_name.classify} < ActiveRecord::Migration[6.0] + def change + create_table :#{table_name.underscore} do |t| + t.string :name, null: false + t.string :result, null: true + t.string :message, null: true + t.string :value, null: true + + t.timestamps null: false + end + end +end +" + end + end + end +end diff --git a/spec/dummy/config/database.yml b/spec/dummy/config/database.yml index 4a8a1b2..41b8de3 100644 --- a/spec/dummy/config/database.yml +++ b/spec/dummy/config/database.yml @@ -5,21 +5,21 @@ # gem 'sqlite3' # default: &default - adapter: sqlite3 + adapter: postgresql pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> timeout: 5000 development: <<: *default - database: db/development.sqlite3 + database: ez_status_development # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default - database: db/test.sqlite3 + database: ez_status_test production: <<: *default - database: db/production.sqlite3 + database: ez_status_production diff --git a/spec/dummy/config/initializers/ez_status.rb b/spec/dummy/config/initializers/ez_status.rb index 7e8fe64..3ed1a3a 100644 --- a/spec/dummy/config/initializers/ez_status.rb +++ b/spec/dummy/config/initializers/ez_status.rb @@ -35,6 +35,8 @@ # password: 'MyPassword' # } + #config.status_table_name = 'status' + config.monitors = [ Ez::Status::Providers::Database, Ez::Status::Providers::Cache, diff --git a/spec/lib/generators/ez/status/install_generator_spec.rb b/spec/lib/generators/ez/status/install_generator_spec.rb index 44c4e9d..664848e 100755 --- a/spec/lib/generators/ez/status/install_generator_spec.rb +++ b/spec/lib/generators/ez/status/install_generator_spec.rb @@ -11,8 +11,7 @@ prepare_destination run_generator end - - # rubocop:disable RSpec/ExampleLength + # TODO: add frozen_string_literal: true it 'creates a config initializer' do assert_file 'config/initializers/ez_status.rb', "require 'ez/status/providers/database' require 'ez/status/providers/cache' diff --git a/spec/lib/generators/ez/status/migrations_generator_spec.rb b/spec/lib/generators/ez/status/migrations_generator_spec.rb new file mode 100755 index 0000000..007c2bf --- /dev/null +++ b/spec/lib/generators/ez/status/migrations_generator_spec.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require 'generator_spec' +require 'generators/ez/status/migrations_generator' + +describe Ez::Status::MigrationsGenerator, type: :generator do + destination File.expand_path('../../../../../tmp', __dir__) + arguments %w[something] + + around do |spec| + Ez::Status.config.status_table_name = 'status' + spec.run + Ez::Status.config.status_table_name = nil + end + + before do + prepare_destination + run_generator + end + + it 'creates a migration' do + assert_file "# frozen_string_literal: true + +class CreateEzStatusStatus < ActiveRecord::Migration[6.0] + def change + create_table :status do |t| + t.string :name, null: false + t.string :result, null: true + t.string :message, null: true + t.string :value, null: true + + t.timestamps null: false + end + end +end + +" + end +end From 2d7d6b1bf40491b898eb944d6f6024213528d567 Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Mon, 12 Jul 2021 18:07:19 +0300 Subject: [PATCH 04/15] update model for status --- .rubocop.yml | 5 +++ README.md | 5 ++- app/models/ez/status/status.rb | 2 +- ez-status.gemspec | 2 +- lib/ez/status.rb | 4 ++- lib/generators/ez/status/install_generator.rb | 2 -- .../ez/status/migrations_generator.rb | 34 +++++++++++-------- spec/dummy/config/initializers/ez_status.rb | 11 +++++- .../20210705184026_create_ez_status_status.rb | 13 +++++++ .../ez/status/install_generator_spec.rb | 3 +- .../ez/status/migrations_generator_spec.rb | 20 +++++------ 11 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 spec/dummy/db/migrate/20210705184026_create_ez_status_status.rb diff --git a/.rubocop.yml b/.rubocop.yml index 32eebf9..bed8cce 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -50,3 +50,8 @@ RSpec/MultipleExpectations: RSpec/AnyInstance: Enabled: false + +Metrics/MethodLength: + Exclude: + - lib/generators/ez/status/install_generator.rb + - lib/generators/ez/status/migrations_generator.rb diff --git a/README.md b/README.md index a3422d5..0b5ad2b 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,10 @@ end config/initializers/ez_status.rb ```ruby -config.status_table_name = 'my_model_name' +# Backend adapter to store all status +config.backend = Ez::Status::Store.new +# and DB table name that you can change as well +config.active_record_table_name = :ez_status_store ``` And run diff --git a/app/models/ez/status/status.rb b/app/models/ez/status/status.rb index d66d6e8..14c670c 100644 --- a/app/models/ez/status/status.rb +++ b/app/models/ez/status/status.rb @@ -3,7 +3,7 @@ module Ez module Status class Status < ApplicationRecord - self.table_name = Ez::Status.config.status_table_name + self.table_name = Ez::Status.config.active_record_table_name validates :name, presence: true validates :result, presence: false diff --git a/ez-status.gemspec b/ez-status.gemspec index 7e371f9..abce823 100644 --- a/ez-status.gemspec +++ b/ez-status.gemspec @@ -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' @@ -44,5 +45,4 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'sidekiq' spec.add_development_dependency 'simplecov' spec.add_development_dependency 'sqlite3' - spec.add_development_dependency 'pg' end diff --git a/lib/ez/status.rb b/lib/ez/status.rb index d87de69..bafa04d 100644 --- a/lib/ez/status.rb +++ b/lib/ez/status.rb @@ -12,10 +12,12 @@ module Status configure do |config| config.status_base_controller = nil config.status_base_routes = nil + config.layout = nil + config.basic_auth_credentials = {} - config.status_table_name = nil + config.active_record_table_name = nil config.monitors = [] diff --git a/lib/generators/ez/status/install_generator.rb b/lib/generators/ez/status/install_generator.rb index ccf9ad0..9a8d98e 100644 --- a/lib/generators/ez/status/install_generator.rb +++ b/lib/generators/ez/status/install_generator.rb @@ -3,7 +3,6 @@ module Ez module Status class InstallGenerator < Rails::Generators::Base - # rubocop:disable Metrics/MethodLength def create_initializer_file create_file 'config/initializers/ez_status.rb', "require 'ez/status/providers/database' @@ -80,7 +79,6 @@ def create_initializer_file end " end - # rubocop:enable Metrics/MethodLength end end end diff --git a/lib/generators/ez/status/migrations_generator.rb b/lib/generators/ez/status/migrations_generator.rb index b00ebcb..f7a76b4 100644 --- a/lib/generators/ez/status/migrations_generator.rb +++ b/lib/generators/ez/status/migrations_generator.rb @@ -4,23 +4,11 @@ module Ez module Status class MigrationsGenerator < Rails::Generators::Base - def config - Ez::Status.config - end - - def table_name - raise 'You need setup table name' if config.status_table_name.blank? - config.status_table_name - end - def create_migration - # TODO: ActiveRecord::Migration[6.0], add automatic detection [5.0], [5.1], [5.2], [6.1], [6.1], [6.2], - create_file "db/migrate/#{Time.current.strftime('%Y%m%d%H%M%S')}_create_ez_status_#{table_name.underscore}.rb", - "# frozen_string_literal: true - -class CreateEzStatus#{table_name.classify} < ActiveRecord::Migration[6.0] + create_file "db/migrate/#{current_time}_create_#{table_name.to_s.underscore}.rb", + "class Create#{table_name.to_s.classify} < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}] def change - create_table :#{table_name.underscore} do |t| + create_table :#{table_name.to_s.underscore} do |t| t.string :name, null: false t.string :result, null: true t.string :message, null: true @@ -32,6 +20,22 @@ def change end " end + + private + + def config + Ez::Status.config + end + + def table_name + raise 'You need setup active_record_table_name in config file' if config.active_record_table_name.blank? + config.active_record_table_name + end + + def current_time + Time.now.strftime('%Y%m%d%H%M%S') + end + end end end diff --git a/spec/dummy/config/initializers/ez_status.rb b/spec/dummy/config/initializers/ez_status.rb index 3ed1a3a..aaf96aa 100644 --- a/spec/dummy/config/initializers/ez_status.rb +++ b/spec/dummy/config/initializers/ez_status.rb @@ -26,17 +26,24 @@ 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' # } - #config.status_table_name = 'status' + # Adapter to store all date + # config.store = Ez::Status::Store.new + # and DB table name that you can change as well + # config.active_record_table_name = :ez_status_store + # Define your monitors config.monitors = [ Ez::Status::Providers::Database, Ez::Status::Providers::Cache, @@ -46,6 +53,8 @@ # 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', diff --git a/spec/dummy/db/migrate/20210705184026_create_ez_status_status.rb b/spec/dummy/db/migrate/20210705184026_create_ez_status_status.rb new file mode 100644 index 0000000..81c7889 --- /dev/null +++ b/spec/dummy/db/migrate/20210705184026_create_ez_status_status.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true +class CreateEzStatusStatus < ActiveRecord::Migration[6.0] + def change + create_table :status do |t| + t.string :name, null: false + t.string :result, null: true + t.string :message, null: true + t.string :value, null: true + + t.timestamps null: false + end + end +end diff --git a/spec/lib/generators/ez/status/install_generator_spec.rb b/spec/lib/generators/ez/status/install_generator_spec.rb index 664848e..44c4e9d 100755 --- a/spec/lib/generators/ez/status/install_generator_spec.rb +++ b/spec/lib/generators/ez/status/install_generator_spec.rb @@ -11,7 +11,8 @@ prepare_destination run_generator end - # TODO: add frozen_string_literal: true + + # rubocop:disable RSpec/ExampleLength it 'creates a config initializer' do assert_file 'config/initializers/ez_status.rb', "require 'ez/status/providers/database' require 'ez/status/providers/cache' diff --git a/spec/lib/generators/ez/status/migrations_generator_spec.rb b/spec/lib/generators/ez/status/migrations_generator_spec.rb index 007c2bf..1d8f0a3 100755 --- a/spec/lib/generators/ez/status/migrations_generator_spec.rb +++ b/spec/lib/generators/ez/status/migrations_generator_spec.rb @@ -7,23 +7,23 @@ destination File.expand_path('../../../../../tmp', __dir__) arguments %w[something] - around do |spec| - Ez::Status.config.status_table_name = 'status' - spec.run - Ez::Status.config.status_table_name = nil - end + let(:delay_in_seconds) { 5 } + let(:current_time) { Time.now.strftime('%Y%m%d%H%M%S') } + let(:table_name) { :ez_status_store } + let(:current_version) { ActiveRecord::Migration.current_version } before do + Ez::Status.config.active_record_table_name = :ez_status_store prepare_destination run_generator end + # rubocop:disable RSpec/ExampleLength it 'creates a migration' do - assert_file "# frozen_string_literal: true - -class CreateEzStatusStatus < ActiveRecord::Migration[6.0] + assert_file "db/migrate/#{current_time}_create_#{table_name.to_s.underscore}.rb", + "class Create#{table_name.to_s.classify} < ActiveRecord::Migration[#{current_version}] def change - create_table :status do |t| + create_table :#{table_name.to_s.underscore} do |t| t.string :name, null: false t.string :result, null: true t.string :message, null: true @@ -33,7 +33,7 @@ def change end end end - " end + # rubocop:enable RSpec/ExampleLength end From bd1470a96f560dcc26ea911ddca7e9af9c3650ce Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Sat, 17 Jul 2021 19:26:30 +0300 Subject: [PATCH 05/15] rename model --- app/models/ez/status/records.rb | 11 +++++++++++ app/models/ez/status/status.rb | 14 -------------- 2 files changed, 11 insertions(+), 14 deletions(-) create mode 100644 app/models/ez/status/records.rb delete mode 100644 app/models/ez/status/status.rb diff --git a/app/models/ez/status/records.rb b/app/models/ez/status/records.rb new file mode 100644 index 0000000..977a669 --- /dev/null +++ b/app/models/ez/status/records.rb @@ -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 diff --git a/app/models/ez/status/status.rb b/app/models/ez/status/status.rb deleted file mode 100644 index 14c670c..0000000 --- a/app/models/ez/status/status.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -module Ez - module Status - class Status < ApplicationRecord - self.table_name = Ez::Status.config.active_record_table_name - - validates :name, presence: true - validates :result, presence: false - validates :message, presence: false - validates :value, presence: false - end - end -end From 160bb85b0325e83d34e51fcadc4ec03d83230f5a Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Sat, 17 Jul 2021 19:31:51 +0300 Subject: [PATCH 06/15] update migrations --- lib/generators/ez/status/migrations_generator.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/generators/ez/status/migrations_generator.rb b/lib/generators/ez/status/migrations_generator.rb index f7a76b4..745cd3f 100644 --- a/lib/generators/ez/status/migrations_generator.rb +++ b/lib/generators/ez/status/migrations_generator.rb @@ -6,15 +6,15 @@ module Status class MigrationsGenerator < Rails::Generators::Base def create_migration create_file "db/migrate/#{current_time}_create_#{table_name.to_s.underscore}.rb", - "class Create#{table_name.to_s.classify} < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}] + "class Create#{table_name.to_s.camelize} < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}] def change create_table :#{table_name.to_s.underscore} do |t| - t.string :name, null: false - t.string :result, null: true - t.string :message, null: true - t.string :value, null: true + t.string :monitor_name, null: false + t.boolean :result, null: true + t.string :message, null: true + t.bigint :value, null: true - t.timestamps null: false + t.datetime :created_at, null: false, default: -> { 'CURRENT_TIMESTAMP' } end end end @@ -28,7 +28,6 @@ def config end def table_name - raise 'You need setup active_record_table_name in config file' if config.active_record_table_name.blank? config.active_record_table_name end From 61e8c5f02e115d674c0eb06e360ade3414e22a99 Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Sun, 18 Jul 2021 21:45:07 +0300 Subject: [PATCH 07/15] add save results to DB --- app/cells/ez/status/application_cell.rb | 5 +++ app/cells/ez/status/index/show.slim | 24 +++-------- app/cells/ez/status/index_cell.rb | 25 ++--------- lib/ez/status.rb | 10 ++++- lib/ez/status/services.rb | 42 +++++++++++++++++++ lib/generators/ez/status/install_generator.rb | 38 ++++++++++++----- 6 files changed, 92 insertions(+), 52 deletions(-) create mode 100644 lib/ez/status/services.rb diff --git a/app/cells/ez/status/application_cell.rb b/app/cells/ez/status/application_cell.rb index 382962f..c7d3088 100755 --- a/app/cells/ez/status/application_cell.rb +++ b/app/cells/ez/status/application_cell.rb @@ -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) + end + def custom_css_map @custom_css_map ||= Ez::Status.config.ui_custom_css_map || {} end diff --git a/app/cells/ez/status/index/show.slim b/app/cells/ez/status/index/show.slim index 2882a8f..f6ec133 100644 --- a/app/cells/ez/status/index/show.slim +++ b/app/cells/ez/status/index/show.slim @@ -6,22 +6,8 @@ div class="#{css_for('index-monitors-collection')}" - monitors.each do |monitor| - div class="#{css_for('index-status')} #{css_for('index-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-message-span')}" - = monitor[:msg] - - - if monitor[:value] - div class="#{css_for('index-check-value')}" - span class="#{css_for('index-check-value-span')}" - = monitor[:value] - - div class="#{css_for('index-check-result')}" - span class="#{css_for('index-check-result-span')}" - = 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] diff --git a/app/cells/ez/status/index_cell.rb b/app/cells/ez/status/index_cell.rb index 7f4e3f8..19ac3f9 100644 --- a/app/cells/ez/status/index_cell.rb +++ b/app/cells/ez/status/index_cell.rb @@ -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 diff --git a/lib/ez/status.rb b/lib/ez/status.rb index bafa04d..247001b 100644 --- a/lib/ez/status.rb +++ b/lib/ez/status.rb @@ -2,13 +2,19 @@ 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] + 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 @@ -17,7 +23,9 @@ module Status config.basic_auth_credentials = {} - config.active_record_table_name = nil + config.columns = DEFAULT_COLUMNS + + config.active_record_table_name = :ez_status_records config.monitors = [] diff --git a/lib/ez/status/services.rb b/lib/ez/status/services.rb new file mode 100644 index 0000000..86b314c --- /dev/null +++ b/lib/ez/status/services.rb @@ -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 diff --git a/lib/generators/ez/status/install_generator.rb b/lib/generators/ez/status/install_generator.rb index 9a8d98e..a1e948d 100644 --- a/lib/generators/ez/status/install_generator.rb +++ b/lib/generators/ez/status/install_generator.rb @@ -33,15 +33,25 @@ def create_initializer_file 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, @@ -51,30 +61,36 @@ def create_initializer_file # 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', - # # heder + # + # # 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-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-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-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', + # 'ez-status-index-check-result' => 'your_css_class', + # 'ez-status-index-check-result-span' => 'your_css_class', # - # 'ez-status-index-check-name' => 'your_css_class', - # 'ez-status-index-check-name-span' => 'your_css_class' # } end " From 4970e75313d555d6618d90016122fb09c57baac6 Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Mon, 19 Jul 2021 22:36:31 +0300 Subject: [PATCH 08/15] update demmy --- spec/dummy/config/initializers/ez_status.rb | 35 +++++++++++-------- .../20210705184026_create_ez_status_status.rb | 13 ------- ...20210721003815_create_ez_status_records.rb | 12 +++++++ spec/dummy/db/schema.rb | 13 ++++++- 4 files changed, 44 insertions(+), 29 deletions(-) delete mode 100644 spec/dummy/db/migrate/20210705184026_create_ez_status_status.rb create mode 100644 spec/dummy/db/migrate/20210721003815_create_ez_status_records.rb diff --git a/spec/dummy/config/initializers/ez_status.rb b/spec/dummy/config/initializers/ez_status.rb index aaf96aa..50fe10a 100644 --- a/spec/dummy/config/initializers/ez_status.rb +++ b/spec/dummy/config/initializers/ez_status.rb @@ -38,10 +38,11 @@ # password: 'MyPassword' # } - # Adapter to store all date - # config.store = Ez::Status::Store.new - # and DB table name that you can change as well - # config.active_record_table_name = :ez_status_store + # 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 = [ @@ -58,26 +59,30 @@ # config.ui_custom_css_map = { # 'ez-status-index-container' => 'your_css_class', # 'ez-status-index-inner-container' => 'your_css_class', - # # heder + # + # # 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-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-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-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', + # 'ez-status-index-check-result' => 'your_css_class', + # 'ez-status-index-check-result-span' => 'your_css_class', # - # 'ez-status-index-check-name' => 'your_css_class', - # 'ez-status-index-check-name-span' => 'your_css_class' # } end diff --git a/spec/dummy/db/migrate/20210705184026_create_ez_status_status.rb b/spec/dummy/db/migrate/20210705184026_create_ez_status_status.rb deleted file mode 100644 index 81c7889..0000000 --- a/spec/dummy/db/migrate/20210705184026_create_ez_status_status.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true -class CreateEzStatusStatus < ActiveRecord::Migration[6.0] - def change - create_table :status do |t| - t.string :name, null: false - t.string :result, null: true - t.string :message, null: true - t.string :value, null: true - - t.timestamps null: false - end - end -end diff --git a/spec/dummy/db/migrate/20210721003815_create_ez_status_records.rb b/spec/dummy/db/migrate/20210721003815_create_ez_status_records.rb new file mode 100644 index 0000000..96fb069 --- /dev/null +++ b/spec/dummy/db/migrate/20210721003815_create_ez_status_records.rb @@ -0,0 +1,12 @@ +class CreateEzStatusRecords < ActiveRecord::Migration[6.1] + def change + create_table :ez_status_records do |t| + t.string :monitor_name, null: false + t.boolean :result, null: true + t.string :message, null: true + t.bigint :value, null: true + + t.datetime :created_at, null: false, default: -> { 'CURRENT_TIMESTAMP' } + end + end +end diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 2e759b1..1f71f2a 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -10,7 +10,10 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_06_15_064829) do +ActiveRecord::Schema.define(version: 2021_07_21_003815) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" create_table "delayed_jobs", force: :cascade do |t| t.integer "priority", default: 0, null: false @@ -27,4 +30,12 @@ t.index ["priority", "run_at"], name: "delayed_jobs_priority" end + create_table "ez_status_records", force: :cascade do |t| + t.string "monitor_name", null: false + t.boolean "result" + t.string "message" + t.bigint "value" + t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" }, null: false + end + end From be5db1da277b2bbc04f9356023d02745106c18f2 Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Mon, 19 Jul 2021 22:38:46 +0300 Subject: [PATCH 09/15] add spec for columns --- spec/features/show_spec.rb | 77 +++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/spec/features/show_spec.rb b/spec/features/show_spec.rb index 4aeb3cd..b9d6310 100644 --- a/spec/features/show_spec.rb +++ b/spec/features/show_spec.rb @@ -45,7 +45,7 @@ end end - describe 'header' do + describe 'config.header' do context 'when custom' do around do |spec| Ez::Status.config.ui_header = custom_text @@ -73,7 +73,74 @@ end end - describe 'css_selectors' do + describe 'config.columns' do + context 'when default' do + before { visit '/status' } + + xit 'should default order' do + # order = /#{item1.name}.*#{item2.name}/m + # monitors = Ez::Status.check_all + # Ez::Status::DEFAULT_COLUMNS.map{|col| "ez-status-index-check-monitor-name" monitors monitors} + # expect(page.body).to match order + end + + it 'expect monitor-name column' do + expect(page).to have_selector('.ez-status-index-check-monitor-name') + expect(page).to have_selector('.ez-status-index-check-monitor-name-span') + end + + it 'expect result column' do + expect(page).to have_selector('.ez-status-index-check-result') + expect(page).to have_selector('.ez-status-index-check-result-span') + end + + it 'expect message column' do + expect(page).to have_selector('.ez-status-index-check-message') + expect(page).to have_selector('.ez-status-index-check-message-span') + end + + it 'expect value column' do + expect(page).to have_selector('.ez-status-index-check-value') + expect(page).to have_selector('.ez-status-index-check-value-span') + end + end + + context 'when custom' do + around do |spec| + Ez::Status.config.columns = %i[result monitor_name] + spec.run + Ez::Status.config.columns = Ez::Status::DEFAULT_COLUMNS + end + + before { visit '/status' } + + xit 'should correct custom order' do + # expect(page.body).to match order + end + + it 'expect monitor-name column' do + expect(page).to have_selector('.ez-status-index-check-monitor-name') + expect(page).to have_selector('.ez-status-index-check-monitor-name-span') + end + + it 'expect result column' do + expect(page).to have_selector('.ez-status-index-check-result') + expect(page).to have_selector('.ez-status-index-check-result-span') + end + + it 'don\'t expect message column' do + expect(page).not_to have_selector('.ez-status-index-check-message') + expect(page).not_to have_selector('.ez-status-index-check-message-span') + end + + it 'don\'t expect value column' do + expect(page).not_to have_selector('.ez-status-index-check-value') + expect(page).not_to have_selector('.ez-status-index-check-value-span') + end + end + end + + describe 'config.ui_custom_css_map' do context 'when default' do before { visit '/status' } @@ -92,7 +159,7 @@ expect(page).to have_selector('.ez-status-index-monitors-collection') end - it 'decorate DOM with ez-status classes for monitor' do + xit 'decorate DOM with ez-status classes for monitor' do expect(page).to have_selector('.ez-status-index-status') expect(page).to have_selector('.ez-status-index-failed') end @@ -113,8 +180,8 @@ end it 'decorate DOM with ez-status classes for name' do - expect(page).to have_selector('.ez-status-index-check-name') - expect(page).to have_selector('.ez-status-index-check-name-span') + expect(page).to have_selector('.ez-status-index-check-monitor-name') + expect(page).to have_selector('.ez-status-index-check-monitor-name-span') end end From 33f500bafb3458dbc882dff19e1dd7d92dad88bb Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Mon, 19 Jul 2021 22:39:10 +0300 Subject: [PATCH 10/15] add spec for monitors and store --- spec/lib/ez/status_spec.rb | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 spec/lib/ez/status_spec.rb diff --git a/spec/lib/ez/status_spec.rb b/spec/lib/ez/status_spec.rb new file mode 100644 index 0000000..2c5d628 --- /dev/null +++ b/spec/lib/ez/status_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Ez::Status do + let(:result) do + [ + { monitor_name: 'Database', result: 'OK', message: nil, value: nil }, + { monitor_name: 'Cache', result: 'FAILURE', message: nil, value: nil } + ] + end + + describe 'Monitors' do + let(:monitors) { described_class.check_all } + + context 'when check_all' do + it 'is Array' do + expect(monitors).to be_a Array + end + + it 'is not empty' do + expect(monitors).not_to be_empty + end + + it 'all monitors be Hash' do + expect(monitors).to all be_a(Hash) + end + + it 'matches include result' do + expect(monitors).to match_array(result) + end + end + end + + describe 'Store' do + let(:capture) { described_class.capture! } + + describe 'capture!' do + it 'saves results to DB' do + expect(Ez::Status::Records.count).to be 0 + capture + expect(Ez::Status::Records.count).to be 2 + end + end + end +end From 6df19430a0a2c170614ff326c78dd040350101c5 Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Mon, 19 Jul 2021 22:39:32 +0300 Subject: [PATCH 11/15] update install generator --- .../ez/status/install_generator_spec.rb | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/spec/lib/generators/ez/status/install_generator_spec.rb b/spec/lib/generators/ez/status/install_generator_spec.rb index 44c4e9d..32aa32f 100755 --- a/spec/lib/generators/ez/status/install_generator_spec.rb +++ b/spec/lib/generators/ez/status/install_generator_spec.rb @@ -42,15 +42,25 @@ 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, @@ -60,30 +70,36 @@ # 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', - # # heder + # + # # 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-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-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-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', + # 'ez-status-index-check-result' => 'your_css_class', + # 'ez-status-index-check-result-span' => 'your_css_class', # - # 'ez-status-index-check-name' => 'your_css_class', - # 'ez-status-index-check-name-span' => 'your_css_class' # } end " From b84f8209544963f014489316c4640ff1111ad22c Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Mon, 19 Jul 2021 22:39:49 +0300 Subject: [PATCH 12/15] update migrations generator --- .../ez/status/migrations_generator_spec.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/spec/lib/generators/ez/status/migrations_generator_spec.rb b/spec/lib/generators/ez/status/migrations_generator_spec.rb index 1d8f0a3..e557098 100755 --- a/spec/lib/generators/ez/status/migrations_generator_spec.rb +++ b/spec/lib/generators/ez/status/migrations_generator_spec.rb @@ -9,11 +9,10 @@ let(:delay_in_seconds) { 5 } let(:current_time) { Time.now.strftime('%Y%m%d%H%M%S') } - let(:table_name) { :ez_status_store } + let(:table_name) { :ez_status_records } # Default table name lib/ez/status.rb let(:current_version) { ActiveRecord::Migration.current_version } before do - Ez::Status.config.active_record_table_name = :ez_status_store prepare_destination run_generator end @@ -21,15 +20,15 @@ # rubocop:disable RSpec/ExampleLength it 'creates a migration' do assert_file "db/migrate/#{current_time}_create_#{table_name.to_s.underscore}.rb", - "class Create#{table_name.to_s.classify} < ActiveRecord::Migration[#{current_version}] + "class Create#{table_name.to_s.camelize} < ActiveRecord::Migration[#{current_version}] def change create_table :#{table_name.to_s.underscore} do |t| - t.string :name, null: false - t.string :result, null: true - t.string :message, null: true - t.string :value, null: true + t.string :monitor_name, null: false + t.boolean :result, null: true + t.string :message, null: true + t.bigint :value, null: true - t.timestamps null: false + t.datetime :created_at, null: false, default: -> { 'CURRENT_TIMESTAMP' } end end end From 00c7d8e05c8ccbfc1a2384214af327d9e051bd31 Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Wed, 21 Jul 2021 13:40:32 +0300 Subject: [PATCH 13/15] update README.md --- README.md | 112 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 85 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 0b5ad2b..624a55b 100644 --- a/README.md +++ b/README.md @@ -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' @@ -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, @@ -79,51 +90,91 @@ Ez::Status.configure do |config| # 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', - # # heder + # + # # 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-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-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-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', + # 'ez-status-index-check-result' => 'your_css_class', + # 'ez-status-index-check-result-span' => 'your_css_class', # - # 'ez-status-index-check-name' => 'your_css_class', - # 'ez-status-index-check-name-span' => 'your_css_class' # } end ``` ### ActiveRecord migrations: -**If you want save data to DataBase, please change configuration first** +**If you want save results to DB (PostgreSQL, etc), please change configuration first** -config/initializers/ez_status.rb +Uncomment DB table name: ```ruby -# Backend adapter to store all status -config.backend = Ez::Status::Store.new -# and DB table name that you can change as well -config.active_record_table_name = :ez_status_store +# config/initializers/ez_status.rb + +# DB table name, that you can change as well +config.active_record_table_name = :ez_status_records ``` -And run +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 @@ -144,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,] +``` + ## Examples ```ruby @@ -204,15 +262,15 @@ end ```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-name' => 'content', - 'ez-status-index-check-message' => 'right floated content', - 'ez-status-index-check-value' => 'right floated content', - 'ez-status-index-check-result' => 'right floated content', - 'ez-status-index-check-name-span' => 'ui green label' + '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' } ``` From a59a0c3522d43f9b093fe98f9642aa2ac0f40062 Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Wed, 21 Jul 2021 15:42:09 +0300 Subject: [PATCH 14/15] fix css --- spec/dummy/app/assets/stylesheets/application.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/dummy/app/assets/stylesheets/application.css b/spec/dummy/app/assets/stylesheets/application.css index bdb9380..f87e3cb 100644 --- a/spec/dummy/app/assets/stylesheets/application.css +++ b/spec/dummy/app/assets/stylesheets/application.css @@ -116,7 +116,7 @@ /* box-shadow: 0 0 0 10px #ebeef3, 0 0 0 22px #f3f4f6;*/ /*}*/ -/*.ez-status-index-check-name {*/ +/*.ez-status-index-check-monitor-name {*/ /* color: #979cb0;*/ /* font-weight: 600;*/ /* font-size: 20px;*/ From 3696f61eeba265c87771a78de8f1ba83ea164db1 Mon Sep 17 00:00:00 2001 From: Vasyl Shevchenko Date: Wed, 21 Jul 2021 15:43:02 +0300 Subject: [PATCH 15/15] fix rubocop --- lib/ez/status.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ez/status.rb b/lib/ez/status.rb index 247001b..5a64d9d 100644 --- a/lib/ez/status.rb +++ b/lib/ez/status.rb @@ -8,7 +8,7 @@ module Ez module Status - DEFAULT_COLUMNS = %i[monitor_name message value result] + DEFAULT_COLUMNS = %i[monitor_name message value result].freeze include Ez::Configurator