wellness 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,53 @@
1
+ require 'json'
2
+
3
+ module Wellness
4
+ class SimpleReport
5
+ STATUSES = {
6
+ 'HEALTHY' => 200,
7
+ 'DEGRADED' => 200,
8
+ 'UNHEALTHY' => 500
9
+ }
10
+
11
+ def initialize(system)
12
+ @system = system
13
+ end
14
+
15
+ def call
16
+ non_criticals = []
17
+ criticals = []
18
+
19
+ @system.services.each do |name, service|
20
+ result = service.call['status']
21
+
22
+ if service.critical?
23
+ criticals << result
24
+ else
25
+ non_criticals << result
26
+ end
27
+ end
28
+
29
+ healthy = criticals.all? { |s| s == 'HEALTHY' }
30
+ degraded = non_criticals.any? { |s| s == 'UNHEALTHY' }
31
+
32
+ if healthy
33
+ if degraded
34
+ status = 'DEGRADED'
35
+ else
36
+ status = 'HEALTHY'
37
+ end
38
+ else
39
+ status = 'UNHEALTHY'
40
+ end
41
+
42
+ render({json: { 'status' => status }, status: STATUSES[status]})
43
+ end
44
+
45
+ # Behaves similar to Rail's controller render method
46
+ def render(options={})
47
+ status = options[:status] || 200
48
+ response = JSON.dump(options[:json])
49
+
50
+ [status, { 'Content-Type' => 'application/json' }, [response]]
51
+ end
52
+ end
53
+ end
@@ -1,52 +1,23 @@
1
- require 'wellness/services/base'
2
- require 'wellness/report'
1
+ require 'wellness/service'
3
2
 
4
3
  module Wellness
5
4
  # @author Matthew A. Johnston (warmwaffles)
6
5
  class System
7
- attr_reader :name
6
+ attr_reader :name, :services, :details
8
7
 
9
8
  # @param name [String] the name of the system
10
9
  def initialize(name)
11
- @name = name
12
- @services = []
13
- @details = []
14
- @mutex = Mutex.new
10
+ @name = name
11
+ @services = {}
12
+ @details = {}
15
13
  end
16
14
 
17
- def use(klass, *args)
18
- @mutex.synchronize do
19
- factory = Factory.new(klass, *args)
20
- if klass <= Wellness::Services::Base
21
- @services << factory
22
- else
23
- @details << factory
24
- end
25
- end
15
+ def add_service(name, service, options={})
16
+ @services[name] = Wellness::Service.new(service, options)
26
17
  end
27
18
 
28
- def detailed_check
29
- report = build_report
30
- [report.status_code, { 'Content-Type' => 'application/json' }, [report.detailed.to_json]]
31
- end
32
-
33
- def simple_check
34
- report = build_report
35
- [report.status_code, { 'Content-Type' => 'application/json' }, [report.simple.to_json]]
36
- end
37
-
38
- def build_report
39
- @mutex.synchronize do
40
- Wellness::Report.new(services.map(&:call), details.map(&:call))
41
- end
42
- end
43
-
44
- def services
45
- @services.map(&:build)
46
- end
47
-
48
- def details
49
- @details.map(&:build)
19
+ def add_detail(name, detail)
20
+ @details[name] = detail
50
21
  end
51
22
  end
52
23
  end
@@ -1,3 +1,3 @@
1
1
  module Wellness
2
- VERSION = '1.0.2'
3
- end
2
+ VERSION = '2.0.0'
3
+ end
data/lib/wellness.rb CHANGED
@@ -4,9 +4,10 @@ module Wellness
4
4
  end
5
5
 
6
6
  require 'wellness/version'
7
- require 'wellness/services'
7
+ require 'wellness/detail'
8
+ require 'wellness/detailed_report'
8
9
  require 'wellness/middleware'
10
+ require 'wellness/service'
11
+ require 'wellness/services/base'
12
+ require 'wellness/simple_report'
9
13
  require 'wellness/system'
10
- require 'wellness/detail'
11
- require 'wellness/report'
12
- require 'wellness/factory'
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,7 @@
1
- require 'simplecov'
2
- SimpleCov.start do
3
- add_filter('/spec')
4
- add_group('Services', 'wellness/services')
5
- end
1
+ require 'minitest/autorun'
2
+ require 'minitest/spec'
3
+ require 'minitest/pride'
6
4
 
7
- require 'rspec'
8
5
  require 'wellness'
9
6
 
10
- Dir[Dir.pwd.concat('/spec/support/**/*.rb')].each { |f| require f }
7
+ Dir.glob('./support/**/*.rb') { |f| require f }
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ require 'minitest/autorun'
5
+ require 'minitest/unit'
6
+ require 'minitest/pride'
7
+
8
+ Bundler.require
9
+
10
+ Dir.glob('./support/**/*.rb') { |f| require f }
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ require 'wellness/detail'
4
+
5
+ module Wellness
6
+ class DetailTest < Minitest::Unit::TestCase
7
+ def setup
8
+ @options = {
9
+ a: 'foo'
10
+ }
11
+ @detail = Detail.new(@options)
12
+ end
13
+
14
+ def test_initialize
15
+ assert_equal(@options, @detail.options)
16
+ end
17
+
18
+ def test_check
19
+ assert_equal({}, @detail.check)
20
+ end
21
+
22
+ def test_call
23
+ assert_equal({}, @detail.call)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,127 @@
1
+ require 'test_helper'
2
+ require 'wellness/system'
3
+ require 'wellness/detailed_report'
4
+
5
+ module Wellness
6
+ class DetailedReportTest < Minitest::Unit::TestCase
7
+ def build_report(system)
8
+ Wellness::DetailedReport.new(system)
9
+ end
10
+
11
+ def test_call
12
+ system = Wellness::System.new('Healthy System')
13
+ system = System.new('Test System')
14
+ system.add_service('foo', healthy, { critical: true })
15
+ system.add_service('bar', healthy, { critical: true })
16
+ system.add_detail('something', some_detail)
17
+
18
+ result = build_report(system).call
19
+
20
+ status = result[0]
21
+ headers = result[1]
22
+ content = JSON.parse(result[2].last)
23
+
24
+ assert_equal(200, status)
25
+ assert_equal('HEALTHY', content['status'])
26
+
27
+ # ###################################################
28
+
29
+ system = Wellness::System.new('Unhealthy System')
30
+ system = System.new('Test System')
31
+ system.add_service('foo', unhealthy, { critical: true })
32
+ system.add_service('bar', healthy, { critical: true })
33
+ system.add_detail('something', some_detail)
34
+
35
+ result = build_report(system).call
36
+
37
+ status = result[0]
38
+ headers = result[1]
39
+ content = JSON.parse(result[2].last)
40
+
41
+ assert_equal(500, status)
42
+ assert_equal('UNHEALTHY', content['status'])
43
+
44
+ # ###################################################
45
+
46
+ system = Wellness::System.new('Degraded System')
47
+ system = System.new('Test System')
48
+ system.add_service('foo', unhealthy, { critical: false })
49
+ system.add_service('bar', healthy, { critical: true })
50
+ system.add_detail('something', some_detail)
51
+
52
+ result = build_report(system).call
53
+
54
+ status = result[0]
55
+ headers = result[1]
56
+ content = JSON.parse(result[2].last)
57
+
58
+ assert_equal(500, status)
59
+ assert_equal('DEGRADED', content['status'])
60
+
61
+ # ###################################################
62
+
63
+ system = Wellness::System.new('Unhealthy System')
64
+ system = System.new('Test System')
65
+ system.add_service('foo', unhealthy, { critical: true })
66
+ system.add_service('bar', unhealthy, { critical: true })
67
+ system.add_detail('something', some_detail)
68
+
69
+ result = build_report(system).call
70
+
71
+ status = result[0]
72
+ headers = result[1]
73
+ content = JSON.parse(result[2].last)
74
+
75
+ assert_equal(500, status)
76
+ assert_equal('UNHEALTHY', content['status'])
77
+
78
+ # ###################################################
79
+
80
+ system = Wellness::System.new('Degraded System')
81
+ system = System.new('Test System')
82
+ system.add_service('foo', unhealthy, { critical: false })
83
+ system.add_service('bar', unhealthy, { critical: false })
84
+ system.add_detail('something', some_detail)
85
+
86
+ result = build_report(system).call
87
+
88
+ status = result[0]
89
+ headers = result[1]
90
+ content = JSON.parse(result[2].last)
91
+
92
+ assert_equal(500, status)
93
+ assert_equal('DEGRADED', content['status'])
94
+ end
95
+
96
+ def unhealthy
97
+ Proc.new do
98
+ {
99
+ 'status' => 'UNHEALTHY',
100
+ 'details' => {
101
+ 'something' => 1
102
+ }
103
+ }
104
+ end
105
+ end
106
+
107
+ def healthy
108
+ Proc.new do
109
+ {
110
+ 'status' => 'HEALTHY',
111
+ 'details' => {
112
+ 'something' => 0
113
+ }
114
+ }
115
+ end
116
+ end
117
+
118
+ def some_detail
119
+ Proc.new do
120
+ {
121
+ 'foo' => 1,
122
+ 'bar' => 0
123
+ }
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,127 @@
1
+ require 'test_helper'
2
+ require 'wellness/system'
3
+ require 'wellness/simple_report'
4
+
5
+ module Wellness
6
+ class SimpleReportTest < Minitest::Unit::TestCase
7
+ def build_report(system)
8
+ Wellness::SimpleReport.new(system)
9
+ end
10
+
11
+ def test_call
12
+ system = Wellness::System.new('Healthy System')
13
+ system = System.new('Test System')
14
+ system.add_service('foo', healthy, { critical: true })
15
+ system.add_service('bar', healthy, { critical: true })
16
+ system.add_detail('something', some_detail)
17
+
18
+ result = build_report(system).call
19
+
20
+ status = result[0]
21
+ headers = result[1]
22
+ content = JSON.parse(result[2].last)
23
+
24
+ assert_equal(200, status)
25
+ assert_equal('HEALTHY', content['status'])
26
+
27
+ # ###################################################
28
+
29
+ system = Wellness::System.new('Unhealthy System')
30
+ system = System.new('Test System')
31
+ system.add_service('foo', unhealthy, { critical: true })
32
+ system.add_service('bar', healthy, { critical: true })
33
+ system.add_detail('something', some_detail)
34
+
35
+ result = build_report(system).call
36
+
37
+ status = result[0]
38
+ headers = result[1]
39
+ content = JSON.parse(result[2].last)
40
+
41
+ assert_equal(500, status)
42
+ assert_equal('UNHEALTHY', content['status'])
43
+
44
+ # ###################################################
45
+
46
+ system = Wellness::System.new('Degraded System')
47
+ system = System.new('Test System')
48
+ system.add_service('foo', unhealthy, { critical: false })
49
+ system.add_service('bar', healthy, { critical: true })
50
+ system.add_detail('something', some_detail)
51
+
52
+ result = build_report(system).call
53
+
54
+ status = result[0]
55
+ headers = result[1]
56
+ content = JSON.parse(result[2].last)
57
+
58
+ assert_equal(200, status)
59
+ assert_equal('DEGRADED', content['status'])
60
+
61
+ # ###################################################
62
+
63
+ system = Wellness::System.new('Unhealthy System')
64
+ system = System.new('Test System')
65
+ system.add_service('foo', unhealthy, { critical: true })
66
+ system.add_service('bar', unhealthy, { critical: true })
67
+ system.add_detail('something', some_detail)
68
+
69
+ result = build_report(system).call
70
+
71
+ status = result[0]
72
+ headers = result[1]
73
+ content = JSON.parse(result[2].last)
74
+
75
+ assert_equal(500, status)
76
+ assert_equal('UNHEALTHY', content['status'])
77
+
78
+ # ###################################################
79
+
80
+ system = Wellness::System.new('Degraded System')
81
+ system = System.new('Test System')
82
+ system.add_service('foo', unhealthy, { critical: false })
83
+ system.add_service('bar', unhealthy, { critical: false })
84
+ system.add_detail('something', some_detail)
85
+
86
+ result = build_report(system).call
87
+
88
+ status = result[0]
89
+ headers = result[1]
90
+ content = JSON.parse(result[2].last)
91
+
92
+ assert_equal(200, status)
93
+ assert_equal('DEGRADED', content['status'])
94
+ end
95
+
96
+ def unhealthy
97
+ Proc.new do
98
+ {
99
+ 'status' => 'UNHEALTHY',
100
+ 'details' => {
101
+ 'something' => 1
102
+ }
103
+ }
104
+ end
105
+ end
106
+
107
+ def healthy
108
+ Proc.new do
109
+ {
110
+ 'status' => 'HEALTHY',
111
+ 'details' => {
112
+ 'something' => 0
113
+ }
114
+ }
115
+ end
116
+ end
117
+
118
+ def some_detail
119
+ Proc.new do
120
+ {
121
+ 'foo' => 1,
122
+ 'bar' => 0
123
+ }
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ require 'wellness/system'
4
+
5
+ module Wellness
6
+ class SystemTest < Minitest::Unit::TestCase
7
+ def test_add_service
8
+ system = System.new('Test System')
9
+ service = Proc.new do
10
+ raise(RuntimeError, 'called when it should not be')
11
+ end
12
+ system.add_service('test', service)
13
+ end
14
+
15
+ def test_add_detail
16
+ system = System.new('Test System')
17
+ detail = Proc.new do
18
+ raise(RuntimeError, 'called when it should not be')
19
+ end
20
+ system.add_detail('test', detail)
21
+ end
22
+
23
+ def unhealthy
24
+ Proc.new do
25
+ {
26
+ 'status' => 'UNHEALTHY',
27
+ 'details' => {
28
+ 'something' => 1
29
+ }
30
+ }
31
+ end
32
+ end
33
+
34
+ def healthy
35
+ Proc.new do
36
+ {
37
+ 'status' => 'HEALTHY',
38
+ 'details' => {
39
+ 'something' => 0
40
+ }
41
+ }
42
+ end
43
+ end
44
+
45
+ def some_detail
46
+ Proc.new do
47
+ {
48
+ 'foo' => 1,
49
+ 'bar' => 0
50
+ }
51
+ end
52
+ end
53
+ end
54
+ end
data/wellness.gemspec CHANGED
@@ -20,8 +20,6 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency('bundler', '~> 1.3')
22
22
  spec.add_development_dependency('rake')
23
- spec.add_development_dependency('rspec')
24
- spec.add_development_dependency('simplecov')
25
23
 
26
24
  # service dependencies
27
25
  spec.add_development_dependency('pg')