Class: RSpec::FlakeClassifier::Store::JSONLStore
- Inherits:
-
Object
- Object
- RSpec::FlakeClassifier::Store::JSONLStore
- Defined in:
- lib/rspec/flake/classifier/store/jsonl_store.rb,
sig/rspec/flake/classifier.rbs
Constant Summary collapse
- FILENAME =
"failures.jsonl"
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #all ⇒ Hash[String, Hash[String, untyped]]
- #entries ⇒ Array[Hash[String, untyped]]
- #find(digest) ⇒ Hash[String, untyped]?
-
#initialize(path) ⇒ JSONLStore
constructor
A new instance of JSONLStore.
- #known_flake?(digest) ⇒ Boolean
- #record(signature, classification: nil, metadata: {}) ⇒ Hash[String, untyped]
- #update_classification(digest, classification:, metadata: {}) ⇒ Hash[String, untyped]?
Constructor Details
#initialize(path) ⇒ JSONLStore
Returns a new instance of JSONLStore.
15 16 17 |
# File 'lib/rspec/flake/classifier/store/jsonl_store.rb', line 15 def initialize(path) @path = path.to_s end |
Instance Attribute Details
#path ⇒ String (readonly)
Returns the value of attribute path.
13 14 15 |
# File 'lib/rspec/flake/classifier/store/jsonl_store.rb', line 13 def path @path end |
Instance Method Details
#all ⇒ Hash[String, Hash[String, untyped]]
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rspec/flake/classifier/store/jsonl_store.rb', line 58 def all return {} unless File.file?(file_path) File.foreach(file_path).each_with_object({}) do |line, entries| next if line.strip.empty? entry = JSON.parse(line) entries[entry.fetch("digest")] = entry rescue JSON::ParserError next end end |
#entries ⇒ Array[Hash[String, untyped]]
71 72 73 |
# File 'lib/rspec/flake/classifier/store/jsonl_store.rb', line 71 def entries all.values.sort_by { |entry| entry.fetch("last_seen", "") }.reverse end |
#find(digest) ⇒ Hash[String, untyped]?
46 47 48 |
# File 'lib/rspec/flake/classifier/store/jsonl_store.rb', line 46 def find(digest) all[digest] end |
#known_flake?(digest) ⇒ Boolean
50 51 52 53 54 55 56 |
# File 'lib/rspec/flake/classifier/store/jsonl_store.rb', line 50 def known_flake?(digest) entry = find(digest) return false unless entry labels = Array(entry["labels"]).map(&:to_s) entry.dig("classification", "status") == "flaky" || labels.any? { |label| label.to_s.include?("flaky") } end |
#record(signature, classification: nil, metadata: {}) ⇒ Hash[String, untyped]
19 20 21 22 23 24 25 26 27 |
# File 'lib/rspec/flake/classifier/store/jsonl_store.rb', line 19 def record(signature, classification: nil, metadata: {}) current = find(signature.digest) now = Time.now.utc.iso8601 entry = build_entry(signature, current, classification, , now) FileUtils.mkdir_p(path) File.open(file_path, "a") { |file| file.puts(JSON.generate(entry)) } entry end |
#update_classification(digest, classification:, metadata: {}) ⇒ Hash[String, untyped]?
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rspec/flake/classifier/store/jsonl_store.rb', line 29 def update_classification(digest, classification:, metadata: {}) current = find(digest) return nil unless current classification_hash = classification_to_hash(classification) labels = labels_from(classification_hash) labels = Array(current.fetch("labels", [])) if labels.empty? entry = current.merge( "classification" => classification_hash, "labels" => labels, "metadata" => current.fetch("metadata", {}).merge(stringify_keys()) ) FileUtils.mkdir_p(path) File.open(file_path, "a") { |file| file.puts(JSON.generate(entry)) } entry end |