From 9a900809405f6e5e3d776ef0e9947de2315a8b02 Mon Sep 17 00:00:00 2001 From: Benjamin Scott Date: Tue, 15 Oct 2024 18:32:23 -0600 Subject: [PATCH] feat: metric completion --- Gemfile.lock | 2 +- lib/glare/ux-metrics/ux_metrics.rb | 56 +++++++++++++++++++++++++++++ lib/glare/ux-metrics/version.rb | 2 +- sig/glare/ux_metrics/ux_metrics.rbs | 22 +++++++++++- spec/glare/ux-metrics/ux_metrics.rb | 33 +++++++++++++++++ 5 files changed, 112 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 6fceea8..4a5ea74 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - glare-ux-metrics-rb (0.2.1) + glare-ux-metrics-rb (0.2.2) GEM remote: https://rubygems.org/ diff --git a/lib/glare/ux-metrics/ux_metrics.rb b/lib/glare/ux-metrics/ux_metrics.rb index ea7e033..33203d2 100644 --- a/lib/glare/ux-metrics/ux_metrics.rb +++ b/lib/glare/ux-metrics/ux_metrics.rb @@ -445,6 +445,62 @@ def correct_data end end + module Completion + class Parser + def initialize(direct_success:, indirect_success:, failed:) + @direct_success = direct_success + @indirect_success = indirect_success + @failed = failed + end + + attr_reader :direct_success, :indirect_success, :failed + + def valid? + return false unless direct_success.is_a?(Float) && indirect_success.is_a?(Float) && failed.is_a?(Float) + + true + end + + def parse + result = direct_success + indirect_success + + label = if result > 0.9 + "Successful" + elsif result <= 90 && result >= 75 + "Avg" + else + "Failed" + end + + threshold = if label == "Successful" + "positive" + elsif label == "Avg" + "neutral" + else + "negative" + end + + Result.new(result: result, threshold: threshold, label: label) + end + + class InvalidDataError < Error + def initialize(msg = "Data not valid. Correct data format is: \n\n#{correct_data}") + super(msg) + end + + def correct_data + { + very_satisfied: "string|integer|float", + somewhat_satisfied: "string|integer|float", + neutral: "string|integer|float", + somewhat_dissatisfied: "string|integer|float", + very_dissatisfied: "string|integer|float", + }.to_json + end + end + end + end + class Result def initialize(result:, threshold:, label:) @result = result diff --git a/lib/glare/ux-metrics/version.rb b/lib/glare/ux-metrics/version.rb index 767d85b..0b104e0 100644 --- a/lib/glare/ux-metrics/version.rb +++ b/lib/glare/ux-metrics/version.rb @@ -2,6 +2,6 @@ module Glare module UxMetrics - VERSION = "0.2.1" + VERSION = "0.2.2" end end \ No newline at end of file diff --git a/sig/glare/ux_metrics/ux_metrics.rbs b/sig/glare/ux_metrics/ux_metrics.rbs index 4b77387..66147bd 100644 --- a/sig/glare/ux_metrics/ux_metrics.rbs +++ b/sig/glare/ux_metrics/ux_metrics.rbs @@ -119,6 +119,26 @@ module Glare end end + module Completion + class Parser + attr_reader direct_success: Float + attr_reader indirect_success: Float + attr_reader failed: Float + + def initialize: ( + direct_success: Float, indirect_success: Float, failed: Float + ) -> void + + def valid?: -> bool + + def parse: -> Result + + class InvalidDataError < Error + def correct_data: -> String + end + end + end + class Result attr_reader result: Float attr_reader label: String @@ -129,4 +149,4 @@ module Glare VERSION: String end -end +end \ No newline at end of file diff --git a/spec/glare/ux-metrics/ux_metrics.rb b/spec/glare/ux-metrics/ux_metrics.rb index c0def38..64c93ef 100644 --- a/spec/glare/ux-metrics/ux_metrics.rb +++ b/spec/glare/ux-metrics/ux_metrics.rb @@ -206,4 +206,37 @@ expect(data.result.is_a?(Float) && data.label.is_a?(String) && data.threshold.is_a?(String)).to eq(true) end end + + describe Glare::UxMetrics::Completion do + let(:completion_data) do + { + direct_success: 0.5, + indirect_success: 0.3, + failed: 0.2 + } + end + + it "validates valid completion data" do + data = Glare::UxMetrics::Completion::Parser.new( + direct_success: completion_data[:direct_success], + indirect_success: completion_data[:indirect_success], + failed: completion_data[:failed], + ) + expect(data.valid?).to eq(true) + end + + it "invalidates invalid completion data" do + data = Glare::UxMetrics::Completion::Parser.new(direct_success: "yooo", indirect_success: "yooo", failed: "yooo") + expect(data.valid?).to eq(false) + end + + it "returns valid data" do + data = Glare::UxMetrics::Completion::Parser.new( + direct_success: completion_data[:direct_success], + indirect_success: completion_data[:indirect_success], + failed: completion_data[:failed], + ).parse + expect(data.result.is_a?(Float) && data.label.is_a?(String) && data.threshold.is_a?(String)).to eq(true) + end + end end