hospital 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1df079afc135900573cf9a989a01d9dbc1ef917ead9e5f6898ea3faa94c0523
4
- data.tar.gz: 0e82ab36be7b2dd80f6ec6212d62074d48746ad54710e6aff78da0e630878fe1
3
+ metadata.gz: 5c2d250697eb93225f8674b4374369688af80a6d32efcbfc70406ae2130d0bba
4
+ data.tar.gz: bb30e0f93629c922d9723c943061e9dddad28fcab7406ab24496cd5f893a5b6d
5
5
  SHA512:
6
- metadata.gz: 4b0a6363c4f08c17514ef1557e7aefe59abb9b04beb2b961c43db1ea9faef80265cbb32e66c5f15fb887632402dfd698b7d74f197a33531122b1b5ab0d229538
7
- data.tar.gz: d441ee4bd337d0820373a19f0f079624d3fc9f62a56d69b6f3211039424bd66187934a885bf1650c3038aef0a4b328fe967300082ff3bd6f49f04a816e221661
6
+ metadata.gz: 6ca63659e83bd3a662612b41b2a645776ca06132241fd5033c169f8fb4be1547445fb34e2160731adced86f7ba6a8995f92a6d1fa573751002ce6d2c85869c70
7
+ data.tar.gz: 91f8b05eddcbfc0fbbc1cb44b7d9a0c340312207267bd4589237480f9fd95a5473af493d0c274d4231c6aaa914fb2820b7e3985039b1ad089eb523764d5ce89c
@@ -2,7 +2,7 @@ class Hospital::Diagnosis
2
2
  attr_reader :infos, :warnings, :errors, :name, :results
3
3
 
4
4
  def initialize name
5
- @name = name.to_s
5
+ @name = name.to_s
6
6
  reset
7
7
  end
8
8
 
@@ -14,8 +14,10 @@ class Hospital::Diagnosis
14
14
  end
15
15
 
16
16
  def require_env_vars env_vars=[]
17
+ success = true
17
18
  if (missing_vars = env_vars.select{|var| ENV[var].nil? || ENV[var].empty? }).any?
18
19
  add_error("These necessary ENV vars are not set: #{variable_list(missing_vars)}.")
20
+ success = false
19
21
  else
20
22
  add_info("All necessary ENV vars are set.")
21
23
  end
@@ -25,6 +27,12 @@ class Hospital::Diagnosis
25
27
  "[#{vars.map{|v| "'#{v}'"}.join(', ')}]"
26
28
  end
27
29
 
30
+ def hide_value value
31
+ "#{value}"
32
+ .gsub(/(?<=.{1}).(?=.{2})/, '*')
33
+ .gsub(/\*{10,}/, '*****...*****')
34
+ end
35
+
28
36
  class Result
29
37
  attr_reader :message, :prefix
30
38
 
@@ -72,15 +80,6 @@ class Hospital::Diagnosis
72
80
  end
73
81
 
74
82
  def put_results
75
- put_header "Checking #{name}:"
76
83
  results.each &:put
77
84
  end
78
-
79
- private
80
-
81
- def put_header message
82
- puts ''
83
- puts "### #{message}"
84
- end
85
-
86
85
  end
@@ -0,0 +1,19 @@
1
+ module Formatter
2
+ refine String do
3
+ def h1
4
+ "\n\e[4m\e[1m#{self}\e[0m"
5
+ end
6
+
7
+ def h2
8
+ "\n\e[4m#{self}\e[0m"
9
+ end
10
+
11
+ def red
12
+ "\e[31m#{self}\e[0m"
13
+ end
14
+
15
+ def yellow
16
+ "\e[33m#{self}\e[0m"
17
+ end
18
+ end
19
+ end
@@ -3,8 +3,24 @@
3
3
  require_relative '../../hospital'
4
4
 
5
5
  desc 'Check system setup sanity.'
6
- task :doctor => [] do
6
+ task :doctor, [:verbose] => :environment do |t, args|
7
7
  # at_exit { Rake::Task['doctor:summary'].invoke if $!.nil? }
8
8
 
9
- Hospital.checkup_all
9
+ ActiveRecord::Base.connection_pool.disconnect!
10
+ ActiveSupport.on_load(:active_record) do
11
+ config = ActiveRecord::Base.configurations[Rails.env]
12
+ config['pool'] = 100
13
+ ActiveRecord::Base.establish_connection(config)
14
+ end
15
+
16
+ verbose = args[:verbose] == "true"
17
+
18
+ # silence warnings about double constant definitions
19
+ Kernel::silence_warnings do
20
+ p "eager load all classes" if verbose
21
+ Rails.application.eager_load!
22
+ end
23
+
24
+ p "start checkup" if verbose
25
+ Hospital.do_checkup_all verbose: verbose
10
26
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hospital
4
- VERSION = "0.3.0"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/hospital.rb CHANGED
@@ -2,48 +2,113 @@
2
2
 
3
3
  require_relative "hospital/version"
4
4
  require_relative "hospital/diagnosis"
5
+ require_relative "hospital/formatter"
6
+
7
+ using Formatter
5
8
 
6
9
  module Hospital
7
10
  require_relative 'railtie' if defined?(Rails)
8
11
 
9
12
  class Error < StandardError; end
10
13
 
11
- @@checkups = {}
12
- @@diagnosises = {}
14
+ class Checkup
15
+ attr_reader :code, :condition, :diagnosis, :group, :skipped, :klass
13
16
 
14
- def self.included(base)
15
- raise Hospital::Error.new("Cannot include Hospital, please extend instead.")
16
- end
17
+ def initialize klass, code, group: :general, title: nil, condition: -> { true }
18
+ @klass = klass
19
+ @code = code
20
+ @group = group
21
+ @condition = condition
22
+ @diagnosis = Hospital::Diagnosis.new([klass.to_s, title].compact.join(' - '))
23
+ end
17
24
 
18
- def self.extended(base)
19
- @@checkups[base] = ->(diagnosis) do
20
- diagnosis.add_warning("#{base}: No checks defined! Please call checkup with a lambda.")
25
+ def reset_diagnosis
26
+ diagnosis.reset
21
27
  end
22
- @@diagnosises[base] = Hospital::Diagnosis.new(base)
23
- end
24
28
 
25
- def checkup code
26
- @@checkups[self] = code
27
- end
29
+ def check verbose: false
30
+ diagnosis.reset
28
31
 
29
- def self.checkup klass
30
- @@diagnosises[klass].reset
31
- @@checkups[klass].call(@@diagnosises[klass])
32
- @@diagnosises[klass]
32
+ if condition.nil? || condition.call
33
+ @skipped = false
34
+ code.call(diagnosis)
35
+ diagnosis
36
+ else
37
+ @skipped = true
38
+ nil
39
+ end
40
+ rescue StandardError => e
41
+ diagnosis.add_error "Unrescued exception in #{klass}.checkup:\n#{e.inspect}.\nThis is a bug inside the checkup method that should be fixed!"
42
+ end
33
43
  end
34
44
 
35
- def self.checkup_all
36
- errcount = 0
37
- @@checkups.keys.each do |klass|
38
- checkup(klass)
39
- diagnosis = @@diagnosises[klass]
40
- diagnosis.put_results
41
- errcount += diagnosis.errors.count
45
+ @@checkups = {}
46
+
47
+ class << self
48
+
49
+ def included(klass)
50
+ raise Hospital::Error.new("Cannot include Hospital, please extend instead.")
51
+ end
52
+
53
+ def extended(klass)
54
+ # only relevant if the class does not yet define a real checkup method
55
+ @@checkups[klass] = Checkup.new klass, -> (diagnosis) do
56
+ diagnosis.add_warning("#{klass}: No checks defined! Please call checkup with a lambda.")
57
+ end, group: :general
42
58
  end
43
59
 
44
- puts <<~END
45
- Summary:"
46
- Errors: #{errcount}
47
- END
60
+ def do_checkup_all verbose: false
61
+ errcount = 0
62
+ warcount = 0
63
+
64
+ threads = @@checkups.map do |klass, checkup|
65
+ Thread.new do
66
+ Thread.current.report_on_exception = false
67
+ checkup.check(verbose: verbose)
68
+ end
69
+ end
70
+
71
+ threads.each &:join
72
+
73
+ @@checkups.group_by{|klass, checkup| checkup.group}.map do |group, checkups|
74
+ puts group_header(group)
75
+ first = false
76
+
77
+ checkups.each do |klass, checkup|
78
+ if diagnosis = checkup.diagnosis
79
+ errcount += diagnosis.errors.count
80
+ warcount += diagnosis.warnings.count
81
+
82
+ if !checkup.skipped
83
+ puts "Checking #{diagnosis.name}:".h2
84
+ diagnosis.put_results
85
+ elsif verbose
86
+ puts "Skipped #{diagnosis.name}.".h2
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ puts <<~END
93
+
94
+ #{"Summary:".h1}
95
+ #{"Errors: #{errcount}".red}
96
+ #{"Warnings: #{warcount}".yellow}
97
+ END
98
+ end
99
+
100
+ # used to call the checkup for a specific class directly (in specs)
101
+ def do_checkup(klass)
102
+ @@checkups[klass].check
103
+ end
104
+
105
+ def group_header group
106
+ "### #{group.to_s.capitalize.gsub(/_/, ' ')} checks".h1
107
+ end
48
108
  end
109
+
110
+ def checkup if: -> { true }, group: :general, title: nil, &code
111
+ @@checkups[self] = Checkup.new self, code, group: group, title: title, condition: binding.local_variable_get('if')
112
+ end
113
+
49
114
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hospital
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-19 00:00:00.000000000 Z
11
+ date: 2023-12-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Imagine a team of little doctors creating diagnoses and creating a useful
14
14
  report.
@@ -22,6 +22,7 @@ files:
22
22
  - README.md
23
23
  - lib/hospital.rb
24
24
  - lib/hospital/diagnosis.rb
25
+ - lib/hospital/formatter.rb
25
26
  - lib/hospital/tasks/checkup.rake
26
27
  - lib/hospital/version.rb
27
28
  - lib/railtie.rb
@@ -47,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
48
  - !ruby/object:Gem::Version
48
49
  version: '0'
49
50
  requirements: []
50
- rubygems_version: 3.2.32
51
+ rubygems_version: 3.1.6
51
52
  signing_key:
52
53
  specification_version: 4
53
54
  summary: A framwork for app self-checks