Skip to content

Commit

Permalink
feat: metric completion
Browse files Browse the repository at this point in the history
  • Loading branch information
benzend committed Oct 16, 2024
1 parent 6dd129c commit 9a90080
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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/
Expand Down
56 changes: 56 additions & 0 deletions lib/glare/ux-metrics/ux_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/glare/ux-metrics/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Glare
module UxMetrics
VERSION = "0.2.1"
VERSION = "0.2.2"
end
end
22 changes: 21 additions & 1 deletion sig/glare/ux_metrics/ux_metrics.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -129,4 +149,4 @@ module Glare

VERSION: String
end
end
end
33 changes: 33 additions & 0 deletions spec/glare/ux-metrics/ux_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9a90080

Please sign in to comment.