hospital 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hospital/diagnosis.rb +7 -10
- data/lib/hospital/formatter.rb +19 -0
- data/lib/hospital/tasks/checkup.rake +14 -3
- data/lib/hospital/version.rb +1 -1
- data/lib/hospital.rb +92 -28
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c2d250697eb93225f8674b4374369688af80a6d32efcbfc70406ae2130d0bba
|
4
|
+
data.tar.gz: bb30e0f93629c922d9723c943061e9dddad28fcab7406ab24496cd5f893a5b6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ca63659e83bd3a662612b41b2a645776ca06132241fd5033c169f8fb4be1547445fb34e2160731adced86f7ba6a8995f92a6d1fa573751002ce6d2c85869c70
|
7
|
+
data.tar.gz: 91f8b05eddcbfc0fbbc1cb44b7d9a0c340312207267bd4589237480f9fd95a5473af493d0c274d4231c6aaa914fb2820b7e3985039b1ad089eb523764d5ce89c
|
data/lib/hospital/diagnosis.rb
CHANGED
@@ -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
|
5
|
+
@name = name.to_s
|
6
6
|
reset
|
7
7
|
end
|
8
8
|
|
@@ -27,6 +27,12 @@ class Hospital::Diagnosis
|
|
27
27
|
"[#{vars.map{|v| "'#{v}'"}.join(', ')}]"
|
28
28
|
end
|
29
29
|
|
30
|
+
def hide_value value
|
31
|
+
"#{value}"
|
32
|
+
.gsub(/(?<=.{1}).(?=.{2})/, '*')
|
33
|
+
.gsub(/\*{10,}/, '*****...*****')
|
34
|
+
end
|
35
|
+
|
30
36
|
class Result
|
31
37
|
attr_reader :message, :prefix
|
32
38
|
|
@@ -74,15 +80,6 @@ class Hospital::Diagnosis
|
|
74
80
|
end
|
75
81
|
|
76
82
|
def put_results
|
77
|
-
put_header "Checking #{name}:"
|
78
83
|
results.each &:put
|
79
84
|
end
|
80
|
-
|
81
|
-
private
|
82
|
-
|
83
|
-
def put_header message
|
84
|
-
puts ''
|
85
|
-
puts "### #{message}"
|
86
|
-
end
|
87
|
-
|
88
85
|
end
|
@@ -3,13 +3,24 @@
|
|
3
3
|
require_relative '../../hospital'
|
4
4
|
|
5
5
|
desc 'Check system setup sanity.'
|
6
|
-
task :doctor => :environment do
|
6
|
+
task :doctor, [:verbose] => :environment do |t, args|
|
7
7
|
# at_exit { Rake::Task['doctor:summary'].invoke if $!.nil? }
|
8
8
|
|
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
|
+
|
9
18
|
# silence warnings about double constant definitions
|
10
19
|
Kernel::silence_warnings do
|
20
|
+
p "eager load all classes" if verbose
|
11
21
|
Rails.application.eager_load!
|
12
22
|
end
|
13
|
-
|
14
|
-
|
23
|
+
|
24
|
+
p "start checkup" if verbose
|
25
|
+
Hospital.do_checkup_all verbose: verbose
|
15
26
|
end
|
data/lib/hospital/version.rb
CHANGED
data/lib/hospital.rb
CHANGED
@@ -2,49 +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
|
-
|
12
|
-
|
14
|
+
class Checkup
|
15
|
+
attr_reader :code, :condition, :diagnosis, :group, :skipped, :klass
|
13
16
|
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
19
|
-
|
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
|
-
|
26
|
-
|
27
|
-
end
|
29
|
+
def check verbose: false
|
30
|
+
diagnosis.reset
|
28
31
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
58
|
+
end
|
59
|
+
|
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
|
42
103
|
end
|
43
104
|
|
44
|
-
|
105
|
+
def group_header group
|
106
|
+
"### #{group.to_s.capitalize.gsub(/_/, ' ')} checks".h1
|
107
|
+
end
|
108
|
+
end
|
45
109
|
|
46
|
-
|
47
|
-
|
48
|
-
END
|
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')
|
49
112
|
end
|
113
|
+
|
50
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.
|
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-
|
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
|