Class: RSpec::FlakeClassifier::Store::JSONLStore

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ JSONLStore

Returns a new instance of JSONLStore.

Parameters:

  • path (String)


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

#pathString (readonly)

Returns the value of attribute path.

Returns:

  • (String)


13
14
15
# File 'lib/rspec/flake/classifier/store/jsonl_store.rb', line 13

def path
  @path
end

Instance Method Details

#allHash[String, Hash[String, untyped]]

Returns:

  • (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

#entriesArray[Hash[String, untyped]]

Returns:

  • (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]?

Parameters:

  • digest (String)

Returns:

  • (Hash[String, untyped], nil)


46
47
48
# File 'lib/rspec/flake/classifier/store/jsonl_store.rb', line 46

def find(digest)
  all[digest]
end

#known_flake?(digest) ⇒ Boolean

Parameters:

  • digest (String)

Returns:

  • (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]

Parameters:

  • signature (Signature)
  • classification: (Object) (defaults to: nil)
  • metadata: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (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]?

Parameters:

  • digest (String)
  • classification: (Object)
  • metadata: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (Hash[String, untyped], nil)


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