mercy 1.6.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +74 -12
  3. data/.claude/agents/architect.md +48 -0
  4. data/.claude/agents/infra.md +36 -0
  5. data/.claude/agents/ruby.md +45 -0
  6. data/.claude/configuration/arcanum-repo-config.json +3 -0
  7. data/.claude/scripts/check_infra.sh +6 -0
  8. data/.claude/scripts/check_ruby.sh +7 -0
  9. data/.claude/settings.json +10 -0
  10. data/.github/commit_message_template-2.0.md +32 -0
  11. data/.github/commit_message_template.md +10 -0
  12. data/.github/copilot-instructions.md +1 -0
  13. data/.github/pull_request_template.md +17 -0
  14. data/.gitignore +3 -0
  15. data/.rubocop.yml +38 -1
  16. data/.rubocop_todo.yml +6 -317
  17. data/AGENTS.md +58 -0
  18. data/CLAUDE.md +1 -0
  19. data/Dockerfile +18 -3
  20. data/Gemfile +26 -1
  21. data/README.md +8 -4
  22. data/Rakefile +3 -0
  23. data/config/check_specs.yml +7 -0
  24. data/config/yardstick.yml +11 -3
  25. data/docs/agents/arcanum-split-issue.md +9 -0
  26. data/docs/agents/architecture.md +61 -0
  27. data/docs/agents/contributing.md +195 -0
  28. data/docs/agents/flow.md +5 -0
  29. data/docs/agents/folder-structure.md +26 -0
  30. data/docs/agents/issue-enhancement.md +10 -0
  31. data/docs/agents/issues/.gitkeep +1 -0
  32. data/docs/agents/plans/.gitkeep +1 -0
  33. data/lib/arstotzka/type_cast_ext.rb +52 -2
  34. data/lib/mercy/class_methods.rb +77 -7
  35. data/lib/mercy/concern.rb +105 -1
  36. data/lib/mercy/period_parser.rb +46 -3
  37. data/lib/mercy/report/active_record.rb +20 -16
  38. data/lib/mercy/report/error.rb +6 -4
  39. data/lib/mercy/report/multiple.rb +26 -22
  40. data/lib/mercy/report/range.rb +6 -4
  41. data/lib/mercy/report.rb +38 -7
  42. data/lib/mercy/report_builder.rb +2 -0
  43. data/lib/mercy/report_config.rb +5 -0
  44. data/lib/mercy/status.rb +7 -1
  45. data/lib/mercy/status_builder.rb +2 -0
  46. data/lib/mercy/version.rb +3 -1
  47. data/lib/mercy.rb +2 -0
  48. data/mercy.gemspec +14 -27
  49. data/scripts/bump_version.sh +62 -0
  50. data/spec/dummy/.ruby-version +1 -0
  51. data/spec/dummy/Rakefile +10 -0
  52. data/spec/dummy/app/channels/application_cable/channel.rb +6 -0
  53. data/spec/dummy/app/channels/application_cable/connection.rb +6 -0
  54. data/spec/dummy/app/controllers/application_controller.rb +4 -0
  55. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  56. data/spec/dummy/app/controllers/document_report_controller.rb +34 -0
  57. data/spec/dummy/app/controllers/example_report_controller.rb +27 -0
  58. data/spec/dummy/app/helpers/application_helper.rb +4 -0
  59. data/spec/dummy/app/jobs/application_job.rb +4 -0
  60. data/spec/dummy/app/mailers/application_mailer.rb +6 -0
  61. data/spec/dummy/app/models/application_record.rb +5 -0
  62. data/spec/dummy/app/models/concerns/.keep +0 -0
  63. data/spec/dummy/app/models/document.rb +9 -0
  64. data/spec/dummy/app/models/dummy_report.rb +6 -0
  65. data/spec/dummy/app/models/mercy/report/doc_type_error.rb +20 -0
  66. data/spec/dummy/app/models/mercy/report/dummy1.rb +19 -0
  67. data/spec/dummy/app/models/mercy/report/dummy_report.rb +11 -0
  68. data/spec/dummy/app/models/mercy/report/multiple/dummy.rb +28 -0
  69. data/spec/dummy/app/models/mercy/report/multiple/successes.rb +28 -0
  70. data/spec/dummy/app/models/mercy/report/success.rb +18 -0
  71. data/spec/dummy/app/views/layouts/application.html.erb +15 -0
  72. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  73. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  74. data/spec/dummy/bin/bundle +5 -0
  75. data/spec/dummy/bin/rails +6 -0
  76. data/spec/dummy/bin/rake +6 -0
  77. data/spec/dummy/bin/setup +38 -0
  78. data/spec/dummy/bin/update +33 -0
  79. data/spec/dummy/bin/yarn +11 -0
  80. data/spec/dummy/config/application.rb +24 -0
  81. data/spec/dummy/config/boot.rb +7 -0
  82. data/spec/dummy/config/cable.yml +10 -0
  83. data/spec/dummy/config/database.yml +20 -0
  84. data/spec/dummy/config/environment.rb +7 -0
  85. data/spec/dummy/config/environments/development.rb +55 -0
  86. data/spec/dummy/config/environments/production.rb +95 -0
  87. data/spec/dummy/config/environments/test.rb +52 -0
  88. data/spec/dummy/config/initializers/application_controller_renderer.rb +10 -0
  89. data/spec/dummy/config/initializers/backtrace_silencers.rb +9 -0
  90. data/spec/dummy/config/initializers/content_security_policy.rb +27 -0
  91. data/spec/dummy/config/initializers/cookies_serializer.rb +7 -0
  92. data/spec/dummy/config/initializers/filter_parameter_logging.rb +6 -0
  93. data/spec/dummy/config/initializers/inflections.rb +18 -0
  94. data/spec/dummy/config/initializers/mime_types.rb +6 -0
  95. data/spec/dummy/config/initializers/wrap_parameters.rb +16 -0
  96. data/spec/dummy/config/locales/en.yml +33 -0
  97. data/spec/dummy/config/puma.rb +36 -0
  98. data/spec/dummy/config/routes.rb +16 -0
  99. data/spec/dummy/config/spring.rb +8 -0
  100. data/spec/dummy/config/storage.yml +34 -0
  101. data/spec/dummy/config.ru +7 -0
  102. data/spec/dummy/db/migrate/20190508170435_create_documents.rb +13 -0
  103. data/spec/dummy/db/schema.rb +27 -0
  104. data/spec/dummy/log/.keep +0 -0
  105. data/spec/dummy/package.json +5 -0
  106. data/spec/dummy/public/404.html +67 -0
  107. data/spec/dummy/public/422.html +67 -0
  108. data/spec/dummy/public/500.html +66 -0
  109. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  110. data/spec/dummy/public/apple-touch-icon.png +0 -0
  111. data/spec/dummy/public/favicon.ico +0 -0
  112. data/spec/dummy/storage/.keep +0 -0
  113. data/spec/dummy/tmp/.keep +0 -0
  114. data/spec/dummy/tmp/storage/.keep +0 -0
  115. data/spec/integration/controller/error_report_controller_spec.rb +196 -0
  116. data/spec/integration/controller/multiple_report_spec.rb +86 -0
  117. data/spec/integration/controller/range_report_spec.rb +194 -0
  118. data/spec/integration/yard/arstotzka/type_cast_spec.rb +43 -0
  119. data/spec/integration/yard/controller/example_report_controller_spec.rb +95 -0
  120. data/spec/integration/yard/mercy/period_parser_spec.rb +21 -0
  121. data/spec/lib/arstotzka/type_cast_spec.rb +10 -9
  122. data/spec/lib/mercy/period_parser_spec.rb +10 -9
  123. data/spec/lib/mercy/report/error_spec.rb +93 -70
  124. data/spec/lib/mercy/report/multiple_spec.rb +9 -57
  125. data/spec/lib/mercy/report/range_spec.rb +70 -41
  126. data/spec/lib/mercy/report_builder_spec.rb +34 -17
  127. data/spec/lib/mercy/report_config_spec.rb +48 -0
  128. data/spec/lib/mercy/report_spec.rb +5 -15
  129. data/spec/lib/mercy/status_builder_spec.rb +33 -22
  130. data/spec/lib/mercy/status_spec.rb +69 -49
  131. data/spec/spec_helper.rb +18 -7
  132. data/spec/support/fixture_helpers.rb +4 -2
  133. data/spec/support/models/arstotzka/type_cast/dummy.rb +9 -0
  134. data/spec/support/shared_contexts/document_setup.rb +31 -0
  135. data/spec/support/shared_examples/time_parser.rb +2 -0
  136. metadata +113 -191
  137. data/spec/lib/mercy/report/report_config_spec.rb +0 -39
  138. data/spec/support/models/document.rb +0 -6
  139. data/spec/support/report/dummy.rb +0 -15
  140. data/spec/support/schema.rb +0 -11
@@ -0,0 +1,195 @@
1
+ # Contributing
2
+
3
+ ## Commit Guidelines
4
+
5
+ - **Atomic and Unitary:** Each commit must represent a single logical change.
6
+ *Example:*
7
+ - Good: `Add minimum option to Mercy::Report::Range`
8
+ - Bad: `Add minimum option to Range and refactor StatusBuilder`
9
+ - **No Unrelated Changes:** Do not mix unrelated changes in the same commit.
10
+ - **Separate Refactoring:** Whenever possible, separate refactoring commits from new feature or bugfix commits.
11
+
12
+ ## Pull Requests
13
+
14
+ - **Descriptive Summary:** Every PR must include a clear and descriptive summary of its purpose and changes.
15
+ - **PR Description Files:** If a description cannot be provided directly in the PR, generate a file with the PR description (e.g., `docs/agents/issues/<pr_number>_description.md`), but do not commit this file.
16
+
17
+ ## Definition of Done for PRs
18
+
19
+ A PR is considered complete when:
20
+
21
+ - The stated objective has been achieved.
22
+ - All tests are passing.
23
+ - Linting passes without errors.
24
+ - Code coverage is as high as reasonably possible.
25
+ - Public API is documented with YARD and the yardstick measurement passes.
26
+ - Code is not overly complex:
27
+ - Classes and methods should have clear, focused responsibilities.
28
+ - If a class or method is taking on too many responsibilities, refactor to simplify.
29
+ - Methods should be small and do exactly one thing. If a method is growing, extract parts into private helper methods or separate classes.
30
+ - *Example:*
31
+
32
+ ```ruby
33
+ # Good: each method does one thing
34
+ class Worker
35
+ def fetch_job; end
36
+
37
+ def process_job(job); end
38
+ end
39
+
40
+ # Bad: method does too much
41
+ class Worker
42
+ def run
43
+ fetch_job
44
+ process_job
45
+ send_metrics
46
+ cleanup
47
+ end
48
+ end
49
+ ```
50
+
51
+ - This requirement applies primarily to source code. For specs, refactor only if there is excessive duplication.
52
+
53
+ ### CI Checks
54
+
55
+ Before a PR is considered complete, all CI checks relevant to the modified parts of the project must pass locally.
56
+
57
+ CircleCI (`.circleci/config.yml`) runs a single `build` job for the whole repository:
58
+
59
+ | Modified folder | CircleCI job | Local commands |
60
+ | --- | --- | --- |
61
+ | any (`lib/`, `spec/`, `config/`, root files) | `build` | `bundle exec rspec` && `rubocop` && `bundle exec rake verify_measurements` && `scripts/check_readme.sh` |
62
+ | `lib/` | `build` (rubycritic step) | `bundle exec rubycritic --format console --branch origin/master -t 0 <changed lib files>` |
63
+
64
+ Inside Docker, `docker-compose run test_all` runs rspec, yard and the yardstick checks.
65
+
66
+ `scripts/check_readme.sh` verifies that the README links the YARD documentation for the current version in `lib/mercy/version.rb`; bump both together.
67
+
68
+ If a new folder or CI job is added in the future, its corresponding test and check commands must be included here before merging changes to that folder.
69
+
70
+ This same process must be followed when **planning how to resolve an issue**: include a final step in the plan that identifies the affected folders and lists the CI commands to run before opening a PR.
71
+
72
+ ## Code Organization
73
+
74
+ ### File Responsibility: Class Declarers vs Scripts
75
+
76
+ Every source file under `lib/` must act as a **class declarer** — it should define one or more classes or modules. Files must not act as **scripts** (i.e., they must not execute logic at require time or perform side effects beyond requiring dependencies and defining constants).
77
+
78
+ This is a library: there are no executable entrypoints. `lib/mercy.rb` is the gem's load entrypoint and only requires dependencies and components. Inside the test suite, `spec/dummy/` is a Rails application whose boot files (`config/`, `bin/`) are exempt.
79
+
80
+ *Example:*
81
+
82
+ ```ruby
83
+ # Good: class declarer — defines a class
84
+ module Mercy
85
+ class PeriodParser
86
+ def to_seconds; end
87
+ end
88
+ end
89
+
90
+ # Bad: script — executes logic at require time
91
+ builder = Mercy::StatusBuilder.new
92
+ builder.add_report_config(:failures, clazz: Document)
93
+ ```
94
+
95
+ Test files are exempt from this rule and may require modules and execute setup code freely.
96
+
97
+ ### File Naming: snake_case Matching the Constant
98
+
99
+ Files must use **snake_case** naming matching the class/module they define, with the directory path mirroring the namespace.
100
+
101
+ *Examples:*
102
+
103
+ - `lib/mercy/status_builder.rb` for `Mercy::StatusBuilder`
104
+ - `lib/mercy/report_config.rb` for `Mercy::ReportConfig`
105
+ - `lib/mercy/report/range.rb` for `Mercy::Report::Range`
106
+
107
+ This applies to both source files and their corresponding spec files:
108
+
109
+ - `lib/mercy/status_builder.rb` → spec: `spec/lib/mercy/status_builder_spec.rb`
110
+ - `lib/mercy/report/range.rb` → spec: `spec/lib/mercy/report/range_spec.rb`
111
+
112
+ Every Ruby file starts with `# frozen_string_literal: true`.
113
+
114
+ ### Method Order: Public Before Private
115
+
116
+ Within a class, **public methods must be declared before private methods**. Private methods serve as implementation helpers and should appear at the end of the class body, after a single `private` keyword.
117
+
118
+ *Example:*
119
+
120
+ ```ruby
121
+ # Good: public methods first, private methods last
122
+ class Worker
123
+ def run
124
+ prepare
125
+ execute
126
+ end
127
+
128
+ def status; end
129
+
130
+ private
131
+
132
+ def prepare; end
133
+
134
+ def execute; end
135
+ end
136
+
137
+ # Bad: private methods mixed in with or before public methods
138
+ class Worker
139
+ private def prepare; end
140
+
141
+ def run; end
142
+
143
+ private
144
+
145
+ def execute; end
146
+ end
147
+ ```
148
+
149
+ ## Dependency Injection
150
+
151
+ Classes must receive their dependencies (data, configuration, collaborators) as constructor arguments. A class must never reach out to load files, read environment variables, or fetch configuration on its own.
152
+
153
+ **The caller is the only place responsible for loading configuration.** In Mercy, configuration comes from the host controller (`status_report` options) and request params, and is passed down through `StatusBuilder` → `ReportBuilder` → `ReportConfig` → report instances.
154
+
155
+ This makes every class independently testable: tests simply instantiate the class with the data they need, without touching the filesystem or environment.
156
+
157
+ *Example:*
158
+
159
+ ```ruby
160
+ # Good: class receives data as an argument — easy to test
161
+ class Mercy::ReportConfig
162
+ def initialize(config)
163
+ @config = config
164
+ end
165
+ end
166
+
167
+ # Bad: class loads its own config — hard to test and couples to the filesystem
168
+ class Mercy::ReportConfig
169
+ def config
170
+ YAML.load_file('config/mercy.yml') # ❌
171
+ end
172
+ end
173
+ ```
174
+
175
+ This principle applies to all classes — including helpers and builders. If a class needs data, it gets it through its constructor.
176
+
177
+ ## Refactoring Guidelines
178
+
179
+ When refactoring, aim to:
180
+
181
+ - **Reduce Code Duplication:**
182
+ *Example:* Move repeated setup code in specs to `let`, shared contexts or helpers under `spec/support/`.
183
+
184
+ ```ruby
185
+ # Good
186
+ shared_context 'with documents' do
187
+ let(:documents) { create_documents(status: :error, count: 3) }
188
+ end
189
+ # In specs:
190
+ include_context 'with documents'
191
+
192
+ # Bad
193
+ let(:documents) { 3.times.map { Document.create(status: :error) } }
194
+ # ...repeated in many spec files
195
+ ```
@@ -0,0 +1,5 @@
1
+ # Flow
2
+
3
+ ## Overview
4
+
5
+ _Describe the main runtime flow of the application here._
@@ -0,0 +1,26 @@
1
+ # Folder Structure
2
+
3
+ ## Project Root
4
+
5
+ | Directory / File | Description |
6
+ |-----------------|-------------|
7
+ | `lib/` | Gem source code (`Mercy` module, reports, builders, Arstotzka type-cast extension). |
8
+ | `spec/` | RSpec test suite: unit specs, integration specs, a dummy Rails app and shared support code. |
9
+ | `config/` | Configuration for code-quality tooling (yardstick thresholds, rubycritic task). |
10
+ | `scripts/` | CI helper scripts (README version check, rubycritic run). |
11
+ | `docs/agents/` | Agent-facing documentation: architecture, flow, issues and plans. |
12
+ | `.circleci/` | CircleCI pipeline configuration. |
13
+ | `.github/` | PR/commit message templates and Copilot instructions. |
14
+ | `mercy.gemspec` | Gem specification: version, runtime and development dependencies. |
15
+ | `Rakefile` | Rake tasks (`spec`, yardstick measurement/verification). |
16
+ | `Dockerfile` / `docker-compose.yml` | Containerized dev/test environment (`mercy` runs rspec, `test_all` runs rspec + yard + yardstick). |
17
+ | `.rubocop.yml` / `.rubocop_todo.yml` | RuboCop configuration and pending offenses. |
18
+
19
+ ## spec/
20
+
21
+ | Subdirectory | Description |
22
+ |--------------|-------------|
23
+ | `lib/` | Unit specs mirroring `lib/` (`mercy/`, `arstotzka/`). |
24
+ | `integration/` | Integration specs: `controller/` (concern inside the dummy Rails app) and `yard/` (examples from YARD docs). |
25
+ | `dummy/` | Minimal Rails application used to exercise the controller concern. |
26
+ | `support/` | Shared contexts/examples, test models and fixture helpers. |
@@ -0,0 +1,10 @@
1
+ # Issue Enhancement
2
+
3
+ A checklist of concerns to consider when fleshing out a vague issue idea (tagged `Idea`/`Writting`) before it reaches the `Created` stage. Not exhaustive — adjust or extend the list for this project's needs.
4
+
5
+ - **Scope boundaries** — what's explicitly in scope and what's explicitly out.
6
+ - **Alternative solutions** — other ways to solve the same problem, and why this one was chosen.
7
+ - **Edge cases** — inputs, states, or timing the happy path doesn't cover.
8
+ - **Backward compatibility** — whether this breaks existing behavior, data, or integrations.
9
+ - **Testing strategy** — how the change will be verified.
10
+ - **Performance & security considerations** — anything relevant to load, latency, or attack surface.
@@ -0,0 +1 @@
1
+
@@ -0,0 +1 @@
1
+
@@ -1,13 +1,63 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Arstotzka
4
+ # @api public
5
+ #
6
+ # Extension of Arstotzka::TypeCast
7
+ #
8
+ # @see https://www.rubydoc.info/gems/arstotzka/Arstotzka/TypeCast
9
+ # Arstotzka::Typecast
2
10
  module TypeCast
11
+ # Converts value to period
12
+ #
13
+ # @param value [String] string to be converted
14
+ #
15
+ # @return [Integer] parsed period
16
+ #
17
+ # @example
18
+ # module Arstotzka
19
+ # module TypeCast
20
+ # class Dummy
21
+ # extend TypeCast
22
+ # end
23
+ # end
24
+ # end
25
+ #
26
+ # Arstotzka::TypeCast::Dummy.to_period('2days')
27
+ # # returns 2.days (2 * 24 * 3600 seconds)
3
28
  def to_period(value)
4
29
  Mercy::PeriodParser.parse(value)
5
30
  end
6
31
 
32
+ # Converts value to float
33
+ #
34
+ # Accepts +"inf"+ and +"-inf"+ return +/- +Float::INFINITY+
35
+ #
36
+ # @param value [String] string to be converted
37
+ #
38
+ # @return [Float] Converted float
39
+ #
40
+ # @example
41
+ # module Arstotzka
42
+ # module TypeCast
43
+ # class Dummy
44
+ # extend TypeCast
45
+ # end
46
+ # end
47
+ # end
48
+ #
49
+ # Arstotzka::TypeCast::Dummy.to_infinity_float('-0.75')
50
+ # # returns -0.75
51
+ #
52
+ # Arstotzka::TypeCast::Dummy.to_infinity_float('inf')
53
+ # # returns Float::INFINITY
54
+ #
55
+ # Arstotzka::TypeCast::Dummy.to_infinity_float('-inf')
56
+ # # returns -Float::INFINITY
7
57
  def to_infinity_float(value)
8
58
  return value if value.is_a?(Numeric)
9
- return -Float::INFINITY if value.match(/-inf/)
10
- return Float::INFINITY if value.match(/inf/)
59
+ return -Float::INFINITY if value =~ /-inf/
60
+ return Float::INFINITY if value =~ /inf/
11
61
 
12
62
  value.to_f
13
63
  end
@@ -1,15 +1,85 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mercy
4
+ # @api public
5
+ #
6
+ # module exposing methods to register reports
2
7
  module ClassMethods
8
+ # register a report
9
+ #
10
+ # @param id [String,Symbol] report identifier
11
+ # @param options [Hash] report options
12
+ # (see the {Report report} type for more options)
13
+ # @option options type [Symbol,Class] type of the report.
14
+ # When a +Class+ is given, this will be used as report class.
15
+ # When a +Symbol+ is given, cleass will be found as a class
16
+ # defined in {Mercy::Report Mercy::Report}
17
+ #
18
+ # @return [Array<Symbol>] Existing reports for configuration
19
+ #
20
+ # @example
21
+ # class Document < ActiveRecord::Base
22
+ # scope :with_error, -> { where(status: :error) }
23
+ # scope :type_a, -> { where(doc_type: :a) }
24
+ # scope :type_b, -> { where(doc_type: :b) }
25
+ # end
26
+ #
27
+ # class ExampleReportController < ApplicationController
28
+ # include Mercy
29
+ #
30
+ # status_report :errors_a, base_scope: :type_a,
31
+ # scope: :with_error,
32
+ # clazz: Document,
33
+ # external_key: :external_id
34
+ # status_report :errors_b, base_scope: :type_b,
35
+ # scope: :with_error,
36
+ # clazz: Document,
37
+ # external_key: :external_id
38
+ #
39
+ # def status
40
+ # render_status
41
+ # end
42
+ # end
43
+ #
44
+ # Rails.application.routes.draw do
45
+ # resources :example_report, only: [] do
46
+ # get :status, on: :collection
47
+ # end
48
+ # end
49
+ #
50
+ # 49.times { Document.create(doc_type: :a) }
51
+ # 3.times { Document.create(doc_type: :b) }
52
+ # Document.with_error.create(doc_type: :a, external_id: 10)
53
+ # Document.with_error.create(doc_type: :b, external_id: 20)
54
+ #
55
+ # get '/example_reports/status'
56
+ # #returns http_status: 500
57
+ # # json:
58
+ # # {
59
+ # # "status":"error",
60
+ # # "errors_a":{
61
+ # # "ids":[10],
62
+ # # "percentage":0.02,
63
+ # # "status":"ok"
64
+ # # },
65
+ # # "errors_b":{
66
+ # # "ids":[20],
67
+ # # "percentage":0.25,
68
+ # # "status":"error"
69
+ # # }
70
+ # # }
71
+ def status_report(id, **options)
72
+ options[:id] = id
3
73
 
4
- def status_report(*attr_names)
5
- id = attr_names.first
6
- options = {
7
- id: id
8
- }.merge(attr_names.extract_options!)
9
-
10
- self.status_builder.add_report_config(id, options)
74
+ status_builder.add_report_config(id, options)
11
75
  end
12
76
 
77
+ # @private
78
+ # @api private
79
+ #
80
+ # Status report builder for class
81
+ #
82
+ # @return [Mercy::StatusBuilder]
13
83
  def status_builder
14
84
  @status_builder ||= Mercy::StatusBuilder.new
15
85
  end
data/lib/mercy/concern.rb CHANGED
@@ -1,8 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @api public
4
+ #
5
+ # Concern exposing methods needed to build reports
1
6
  module Mercy
2
7
  extend ActiveSupport::Concern
3
8
 
9
+ # Render the status
10
+ #
11
+ # @param key [:Symbol] key of the configured report
12
+ #
13
+ # @return [String] Json rendered
14
+ #
15
+ # @example (see Mercy::ClassMethods#status_report)
16
+ #
17
+ # @example
18
+ # class Document < ActiveRecord::Base
19
+ # scope :with_success, -> { where(status: :success) }
20
+ # end
21
+ #
22
+ # module Mercy
23
+ # class Report
24
+ # class Success < Mercy::Report::Range
25
+ # ALLOWED_PARAMETERS = %i[period minimum maximum].freeze
26
+ # DEFAULT_OPTION = {
27
+ # clazz: Document
28
+ # }.freeze
29
+ #
30
+ # expose :doc_type, case: :snake
31
+ #
32
+ # def base
33
+ # super.with_success.where(doc_type: doc_type)
34
+ # end
35
+ # end
36
+ # end
37
+ # end
38
+ #
39
+ # module Mercy
40
+ # class Report
41
+ # module Multiple
42
+ # class Successes < Mercy::Report
43
+ # include Mercy::Report::Multiple
44
+ # DEFAULT_OPTION = {
45
+ # doc_type: %i[a b]
46
+ # }.freeze
47
+ # expose :doc_type, case: :snake
48
+ #
49
+ # def reports_ids
50
+ # [doc_type].flatten
51
+ # end
52
+ #
53
+ # def sub_report_class
54
+ # Mercy::Report::Success
55
+ # end
56
+ #
57
+ # def key
58
+ # :doc_type
59
+ # end
60
+ # end
61
+ # end
62
+ # end
63
+ # end
64
+ #
65
+ # class ExampleReportController < ApplicationController
66
+ # include Mercy
67
+ #
68
+ # status_report :successes, on: :successes,
69
+ # clazz: Document,
70
+ # type: Mercy::Report::Multiple::Successes,
71
+ # minimum: 10
72
+ #
73
+ # def status
74
+ # render_status
75
+ # end
76
+ #
77
+ # def count_status
78
+ # render_status(:successes)
79
+ # end
80
+ # end
81
+ #
82
+ # Rails.application.routes.draw do
83
+ # resources :example_report, only: [] do
84
+ # get :count_status, on: :collection
85
+ # end
86
+ # end
87
+ #
88
+ # 10.times { Document.with_success.create(doc_type: :a) }
89
+ # 2.times { Document.with_success.create(doc_type: :b) }
90
+ #
91
+ # get '/example_reports/count_status'
92
+ # #returns http_status: 500
93
+ # # json:
94
+ # # {
95
+ # # 'status' => 'error',
96
+ # # 'successes' => {
97
+ # # 'status' => 'error',
98
+ # # 'a' => {
99
+ # # 'count' => 10,
100
+ # # 'status' => 'ok'
101
+ # # },
102
+ # # 'b' => {
103
+ # # 'count' => 2,
104
+ # # 'status' => 'error'
105
+ # # }
106
+ # # }
4
107
  def render_status(key = :default)
5
108
  status = self.class.status_builder.build(key, params)
6
- render json: status.as_json.stringify_keys.to_deep_hash, status: status.status
109
+ status_hash = status.as_json.stringify_keys.to_deep_hash
110
+ render json: status_hash, status: status.http_status
7
111
  end
8
112
  end
@@ -1,9 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mercy
4
+ # @api private
5
+ #
6
+ # Class responsible for parsing period string into period
2
7
  class PeriodParser
3
8
  class << self
9
+ # Parses string to period
10
+ #
11
+ # @param period [String] period string
12
+ #
13
+ # @return [Integer]
14
+ #
15
+ # @example
16
+ # Mercy::PeriodParser.parse('3days')
17
+ #
18
+ # # returns 3.days (259200)
4
19
  def parse(period)
5
20
  return unless period
6
21
  return period if period.is_a?(Integer)
22
+
7
23
  new(period).to_seconds
8
24
  end
9
25
  end
@@ -12,8 +28,17 @@ module Mercy
12
28
  @period = period
13
29
  end
14
30
 
31
+ # Parses period string to integer period
32
+ #
33
+ # @return [Integer]
34
+ #
35
+ # @example
36
+ # parser = Mercy::PeriodParser.new('30minutes')
37
+ # parser.to_period
38
+ # # returns 30.minutes (108000)
15
39
  def to_seconds
16
- return unless period.match(/^\d+(years|months|days|hours|minutes|seconds)?/)
40
+ return unless period =~ /^\d+(years|months|days|hours|minutes|seconds)?/
41
+
17
42
  type.blank? ? value.seconds : value.public_send(type)
18
43
  end
19
44
 
@@ -21,12 +46,30 @@ module Mercy
21
46
 
22
47
  attr_reader :period
23
48
 
49
+ # @private
50
+ #
51
+ # returns numeric part of period string
52
+ #
53
+ # @return [Integer]
24
54
  def value
25
- @period_value ||= period.gsub(/\D+/, '').to_i
55
+ @value ||= period.gsub(/\D+/, '').to_i
26
56
  end
27
57
 
58
+ # @private
59
+ #
60
+ # Returns the type of the period
61
+ #
62
+ # possible types are:
63
+ # - seconds
64
+ # - minutes
65
+ # - hours
66
+ # - days
67
+ # - months
68
+ # - years
69
+ #
70
+ # @return [String]
28
71
  def type
29
- @period_type ||= period.gsub(/\d+/, '')
72
+ @type ||= period.gsub(/\d+/, '')
30
73
  end
31
74
  end
32
75
  end
@@ -1,26 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mercy
2
- class Report::ActiveRecord < Report
3
- expose :period, type: :period
4
- expose :clazz, :base_scope, case: :snake
4
+ class Report
5
+ class ActiveRecord < Report
6
+ expose :period, type: :period
7
+ expose :clazz, :base_scope, case: :snake
5
8
 
6
- private
9
+ private
7
10
 
8
- def fetch_scoped(base, scope)
9
- if (scope.is_a?(Symbol))
10
- scope.to_s.split('.').inject(base) do |entries, method|
11
- entries.public_send(method)
11
+ def fetch_scoped(base, scope)
12
+ if scope.is_a?(Symbol)
13
+ scope.to_s.split('.').inject(base) do |entries, method|
14
+ entries.public_send(method)
15
+ end
16
+ else
17
+ base.where(scope)
12
18
  end
13
- else
14
- base.where(scope)
15
19
  end
16
- end
17
20
 
18
- def last_entries
19
- @last_entries ||= base.where('updated_at >= ?', period.seconds.ago)
20
- end
21
+ def last_entries
22
+ @last_entries ||= base.where('updated_at >= ?', period.seconds.ago)
23
+ end
21
24
 
22
- def base
23
- fetch_scoped(clazz, base_scope)
25
+ def base
26
+ fetch_scoped(clazz, base_scope)
27
+ end
24
28
  end
25
29
  end
26
30
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mercy
2
4
  class Report
3
5
  class Error < Report::ActiveRecord
4
- ALLOWED_PARAMETERS=[:period, :threshold]
6
+ ALLOWED_PARAMETERS = %i[period threshold].freeze
5
7
  DEFAULT_OPTION = {
6
8
  external_key: :id,
7
9
  threshold: 0.02,
@@ -9,7 +11,7 @@ module Mercy
9
11
  scope: :with_error,
10
12
  base_scope: :all,
11
13
  uniq: false
12
- }
14
+ }.freeze
13
15
 
14
16
  expose :threshold, type: :float
15
17
  expose :scope, :external_key, :uniq, :limit, case: :snake
@@ -49,8 +51,8 @@ module Mercy
49
51
  end
50
52
 
51
53
  def fetch_percentage
52
- if (scope.is_a?(Symbol))
53
- last_entries.percentage(*(scope.to_s.split('.').map(&:to_sym)))
54
+ if scope.is_a?(Symbol)
55
+ last_entries.percentage(*scope.to_s.split('.').map(&:to_sym))
54
56
  else
55
57
  last_entries.percentage(scope)
56
58
  end