metricdeck 0.1.4

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f7f573a7a2e2bb3711b03137e5fb248eed220a12175d15d9e040d0844d69bbc1
4
+ data.tar.gz: fc0d9ef9f8b3d918ef3ddd6868a829d472a9145d0cd95af841f5b857d8f81ca1
5
+ SHA512:
6
+ metadata.gz: 7c3840a0e01c4944a39e7c71693d408b2d258a691c5c70d0574863707b863ed9a693db4aec4ccd86bd00a88a7f7310113be428e52f23a32676b4da553f6e54b3
7
+ data.tar.gz: b8a712c7d62e113f4577e71ce8599cef4bc3854038024d49c14e838b02d61e5ce57b630942ccdcf139e81e83c5ce0aa18324e5b9981b1555f84be3dc2acb5b73
data/CHANGELOG.md ADDED
@@ -0,0 +1,40 @@
1
+ # Changelog
2
+
3
+ ## [0.1.4](https://github.com/shqear93/metricdeck/compare/metricdeck/v0.1.3...metricdeck/v0.1.4) (2026-05-31)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * update gemspec homepage to correct repo URL ([2b9ea5b](https://github.com/shqear93/metricdeck/commit/2b9ea5bb9d65ea270d9914165c11dd476ded6285))
9
+
10
+ ## [0.1.3](https://github.com/shqear93/metricdeck/compare/metricdeck/v0.1.2...metricdeck/v0.1.3) (2026-05-29)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * verify end-to-end automated release pipeline ([2ea3800](https://github.com/shqear93/metricdeck/commit/2ea3800a9e37c299f43d6c4fc18d226f5034a184))
16
+
17
+ ## [0.1.2](https://github.com/shqear93/metricdeck/compare/metricdeck/v0.1.1...metricdeck/v0.1.2) (2026-05-29)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * bound rails dependency to avoid open-ended warning ([ff74091](https://github.com/shqear93/metricdeck/commit/ff740911cf843e1c3024db98face4c631101547d))
23
+
24
+ ## [0.1.1](https://github.com/shqear93/metricdeck/compare/metricdeck-v0.1.0...metricdeck/v0.1.1) (2026-05-29)
25
+
26
+
27
+ ### Bug Fixes
28
+
29
+ * include release-please manifest in gem files ([6aff160](https://github.com/shqear93/metricdeck/commit/6aff1609e0fce806f1af6dfd392b521dd0a4dac5))
30
+
31
+ ## [0.1.0] - 2026-05-27
32
+
33
+ - Initial release as **metricdeck**
34
+ - Extracted metric card framework from Athar EMS people service
35
+ - MetricCard value object with trend/comparison support
36
+ - BaseCalculator module with validate + perform interface
37
+ - CardHelpers with percentage_change, determine_trend, create_metric_card
38
+ - DateHelpers with parse_date, calculate_previous_period, generate_comparison_text
39
+ - MetricCardService with auto-discovery of calculators by namespace
40
+ - Rails generator for new calculators
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Athar Association
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # metricdeck
2
+
3
+ [![CI](https://github.com/shqear93/metricdeck/actions/workflows/ci.yml/badge.svg)](https://github.com/shqear93/metricdeck/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/metricdeck.svg)](https://rubygems.org/gems/metricdeck)
5
+
6
+ A pluggable metric card framework for Ruby on Rails applications.
7
+
8
+ ## Installation
9
+
10
+ Add to your Gemfile:
11
+
12
+ ```ruby
13
+ gem 'metricdeck'
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ### 1. Create a calculator
19
+
20
+ ```ruby
21
+ # app/metrics/calculators/users_calculator.rb
22
+ module Metrics
23
+ module Calculators
24
+ class UsersCalculator
25
+ include Metricdeck::Calculators::BaseCalculator
26
+ include Metricdeck::Helpers::CardHelpers
27
+
28
+ def validate_context(context)
29
+ # optional validation
30
+ end
31
+
32
+ def perform_calculation(context)
33
+ current = User.active.count
34
+ previous = User.active.where('created_at <= ?', 1.month.ago).count
35
+
36
+ create_metric_card(
37
+ card_id,
38
+ value: current,
39
+ current: current,
40
+ previous: previous
41
+ )
42
+ end
43
+ end
44
+ end
45
+ end
46
+ ```
47
+
48
+ ### 2. Fetch metric cards
49
+
50
+ ```ruby
51
+ service = Metricdeck::MetricCardService.new(namespace: Metrics::Calculators)
52
+ cards = service.get_cards({ employee_id: 42 })
53
+ ```
54
+
55
+ ### 3. Generate a calculator
56
+
57
+ ```bash
58
+ rails g metricdeck:calculator users
59
+ ```
60
+
61
+ ## I18n
62
+
63
+ Add translations under:
64
+
65
+ ```yaml
66
+ en:
67
+ metricdeck:
68
+ cards:
69
+ users:
70
+ title: "Active Users"
71
+ unit: "users"
72
+ comparisons:
73
+ previous_period: "vs previous period"
74
+ last_month: "vs last month"
75
+ last_year: "vs last year"
76
+ ```
77
+
78
+ ## Migrating from Athar EMS People Service
79
+
80
+ If you're migrating from the internal `Statistics` module in the Athar EMS people service:
81
+
82
+ 1. Replace `Statistics::Calculators::BaseCalculator` with `Metricdeck::Calculators::BaseCalculator`
83
+ 2. Replace `Statistics::Helpers::CardHelpers` with `Metricdeck::Helpers::CardHelpers`
84
+ 3. Replace `Statistics::Helpers::DateHelpers` with `Metricdeck::Helpers::DateHelpers`
85
+ 4. Replace `Statistics::MetricCardService` with `Metricdeck::MetricCardService`
86
+ 5. Replace `Statistics::MetricCard` with `Metricdeck::MetricCard`
87
+ 6. Update i18n keys from `statistics.*` to `metricdeck.*`
88
+ 7. Pass your calculator namespace explicitly:
89
+ ```ruby
90
+ service = Metricdeck::MetricCardService.new(namespace: Statistics::Calculators)
91
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Metricdeck
6
+ module Generators
7
+ class CalculatorGenerator < Rails::Generators::NamedBase
8
+ source_root File.expand_path('templates', __dir__)
9
+
10
+ desc 'Creates a new metric calculator'
11
+
12
+ def create_calculator_file
13
+ template 'calculator.rb.tt', "app/metrics/calculators/#{file_name}_calculator.rb"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metrics
4
+ module Calculators
5
+ class <%= class_name %>Calculator
6
+ include Metricdeck::Calculators::BaseCalculator
7
+ include Metricdeck::Helpers::CardHelpers
8
+
9
+ def validate_context(context)
10
+ # Add validation logic here
11
+ # Example:
12
+ # raise ArgumentError, "Missing required context: employee_id" unless context[:employee_id].present?
13
+ end
14
+
15
+ def perform_calculation(context)
16
+ # Calculate current and previous values
17
+ current = 0
18
+ previous = 0
19
+
20
+ create_metric_card(
21
+ card_id,
22
+ value: current,
23
+ current: current,
24
+ previous: previous
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metricdeck
4
+ module Calculators
5
+ module BaseCalculator
6
+ def calculate(context)
7
+ validate_context(context)
8
+ perform_calculation(context)
9
+ end
10
+
11
+ def perform_calculation(_context)
12
+ raise NotImplementedError, "#{self.class} must implement #perform_calculation"
13
+ end
14
+
15
+ def card_id
16
+ self.class.name.demodulize.underscore.sub(/_calculator$/, '')
17
+ end
18
+
19
+ protected
20
+
21
+ def validate_context(_context)
22
+ # Override in subclasses
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metricdeck
4
+ module Helpers
5
+ module CardHelpers
6
+ def calculate_percentage_change(current, previous)
7
+ return 0 if previous.to_f.zero?
8
+
9
+ ((current.to_f - previous.to_f) / previous.to_f * 100).round(1)
10
+ end
11
+
12
+ def determine_trend(current, previous)
13
+ return 'neutral' if previous.to_f.zero? || current == previous
14
+
15
+ current > previous ? 'up' : 'down'
16
+ end
17
+
18
+ def create_metric_card(id, value:, current:, previous:, title_key: nil, unit_key: nil,
19
+ comparison_key: nil, comparison_text: nil)
20
+ card_key = id.to_s
21
+
22
+ title = resolve_title(card_key, title_key)
23
+ unit = resolve_unit(card_key, unit_key)
24
+ comparison_text ||= resolve_comparison_text(card_key, comparison_key)
25
+
26
+ percentage_change = calculate_percentage_change(current, previous)
27
+ trend = determine_trend(current, previous)
28
+
29
+ Metricdeck::MetricCard.new(
30
+ id: id,
31
+ title: title,
32
+ value: value.to_s,
33
+ unit: unit,
34
+ comparison_percentage: percentage_change,
35
+ trend: trend,
36
+ comparison_text: comparison_text
37
+ )
38
+ end
39
+
40
+ private
41
+
42
+ def resolve_title(card_key, title_key)
43
+ key = title_key || "metricdeck.cards.#{card_key}.title"
44
+ I18n.t(key, default: card_key.humanize)
45
+ end
46
+
47
+ def resolve_unit(card_key, unit_key)
48
+ if unit_key.is_a?(Symbol)
49
+ I18n.t("metricdeck.units.#{unit_key}", default: '')
50
+ elsif unit_key.present?
51
+ I18n.t(unit_key, default: '')
52
+ else
53
+ I18n.t("metricdeck.cards.#{card_key}.unit", default: '')
54
+ end
55
+ end
56
+
57
+ def resolve_comparison_text(card_key, comparison_key)
58
+ default = I18n.t('metricdeck.comparisons.previous_period', default: 'vs previous period')
59
+ if comparison_key.is_a?(Symbol)
60
+ I18n.t("metricdeck.comparisons.#{comparison_key}", default: default)
61
+ elsif comparison_key.present?
62
+ I18n.t(comparison_key, default: default)
63
+ else
64
+ I18n.t("metricdeck.cards.#{card_key}.comparison", default: default)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metricdeck
4
+ module Helpers
5
+ module DateHelpers
6
+ def parse_date_with_formats(date_str)
7
+ return nil if date_str.blank?
8
+ return date_str if date_str.is_a?(Date)
9
+
10
+ formats = ['%d-%m-%Y', '%Y-%m-%d', '%m/%d/%Y', '%d/%m/%Y']
11
+
12
+ formats.each do |format|
13
+ return Date.strptime(date_str.to_s, format)
14
+ rescue Date::Error
15
+ next
16
+ end
17
+
18
+ begin
19
+ Date.parse(date_str.to_s)
20
+ rescue Date::Error
21
+ nil
22
+ end
23
+ end
24
+
25
+ def calculate_previous_period(start_date, end_date, comparison_period = :previous)
26
+ period_length = (end_date - start_date).to_i + 1
27
+
28
+ case comparison_period.to_sym
29
+ when :month, :last_month
30
+ prev_start_date = start_date.prev_month
31
+ prev_end_date = [end_date.prev_month, prev_start_date.end_of_month].min
32
+ when :year, :last_year
33
+ prev_start_date = start_date.prev_year
34
+ prev_end_date = end_date.prev_year
35
+ else
36
+ prev_start_date = start_date - period_length.days
37
+ prev_end_date = end_date - period_length.days
38
+ end
39
+
40
+ [prev_start_date, prev_end_date]
41
+ end
42
+
43
+ def generate_comparison_text(comparison_period)
44
+ period = comparison_period.to_sym if comparison_period.respond_to?(:to_sym)
45
+
46
+ comparison_key = case period
47
+ when :month, :last_month
48
+ :last_month
49
+ when :year, :last_year
50
+ :last_year
51
+ else
52
+ :previous_period
53
+ end
54
+
55
+ I18n.t("metricdeck.comparisons.#{comparison_key}")
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metricdeck
4
+ class MetricCard
5
+ include ActiveModel::Model
6
+ include ActiveModel::Attributes
7
+ include ActiveModel::Serialization
8
+
9
+ attribute :id, :string
10
+ attribute :title, :string
11
+ attribute :value, :string
12
+ attribute :unit, :string
13
+ attribute :comparison_percentage, :float
14
+ attribute :trend, :string
15
+ attribute :comparison_text, :string
16
+
17
+ def initialize(attributes = {})
18
+ super
19
+ end
20
+
21
+ def comparison
22
+ {
23
+ 'percentage' => comparison_percentage,
24
+ 'trend' => trend,
25
+ 'text' => comparison_text
26
+ }
27
+ end
28
+
29
+ def as_json(_options = nil)
30
+ attributes.compact.merge('comparison' => comparison)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metricdeck
4
+ class MetricCardService
5
+ attr_reader :namespace
6
+
7
+ def initialize(namespace: nil)
8
+ @namespace = namespace || default_namespace
9
+ end
10
+
11
+ def get_cards(context, card_types = nil)
12
+ calculators = available_calculators
13
+
14
+ calculators = calculators.select { |c| card_types.include?(c.card_id.to_sym) } if card_types.present?
15
+
16
+ calculators.map { |calculator| calculator.calculate(context) }
17
+ end
18
+
19
+ def available_calculators
20
+ calculators = []
21
+
22
+ namespace.constants.each do |const_name|
23
+ const = namespace.const_get(const_name)
24
+
25
+ next unless const.is_a?(Class) &&
26
+ const.included_modules.include?(Metricdeck::Calculators::BaseCalculator) &&
27
+ const_name.to_s.end_with?('Calculator')
28
+
29
+ calculators << const.new
30
+ end
31
+
32
+ calculators
33
+ end
34
+
35
+ def available_card_types
36
+ available_calculators.map { |c| c.card_id.to_sym }
37
+ end
38
+
39
+ private
40
+
41
+ def default_namespace
42
+ if Object.const_defined?(:Metrics) && ::Metrics.const_defined?(:Calculators)
43
+ ::Metrics::Calculators
44
+ else
45
+ raise ArgumentError,
46
+ 'No calculator namespace provided and Metrics::Calculators is not defined. ' \
47
+ 'Pass namespace: MyApp::Metrics::Calculators to MetricCardService.new'
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metricdeck
4
+ VERSION = '0.1.4'
5
+ end
data/lib/metricdeck.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model'
4
+ require 'active_support/all'
5
+
6
+ module Metricdeck
7
+ class Error < StandardError; end
8
+
9
+ require_relative 'metricdeck/version'
10
+ require_relative 'metricdeck/metric_card'
11
+ require_relative 'metricdeck/calculators/base_calculator'
12
+ require_relative 'metricdeck/helpers/card_helpers'
13
+ require_relative 'metricdeck/helpers/date_helpers'
14
+ require_relative 'metricdeck/metric_card_service'
15
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metricdeck
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Athar Team
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2026-05-31 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rails
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.0'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '9.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '7.0'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '9.0'
32
+ description: A calculator registry that produces standardized metric cards with trends,
33
+ comparisons, and i18n labels.
34
+ email:
35
+ - info@athar.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - CHANGELOG.md
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - lib/generators/metricdeck/calculator/calculator_generator.rb
45
+ - lib/generators/metricdeck/calculator/templates/calculator.rb.tt
46
+ - lib/metricdeck.rb
47
+ - lib/metricdeck/calculators/base_calculator.rb
48
+ - lib/metricdeck/helpers/card_helpers.rb
49
+ - lib/metricdeck/helpers/date_helpers.rb
50
+ - lib/metricdeck/metric_card.rb
51
+ - lib/metricdeck/metric_card_service.rb
52
+ - lib/metricdeck/version.rb
53
+ homepage: https://github.com/shqear93/metricdeck
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ allowed_push_host: https://rubygems.org
58
+ homepage_uri: https://github.com/shqear93/metricdeck
59
+ source_code_uri: https://github.com/shqear93/metricdeck
60
+ changelog_uri: https://github.com/shqear93/metricdeck/blob/main/CHANGELOG.md
61
+ rubygems_mfa_required: 'true'
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 3.4.0
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.6.2
77
+ specification_version: 4
78
+ summary: Pluggable metric card framework for Rails
79
+ test_files: []