-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunner.rb
More file actions
89 lines (76 loc) · 2.39 KB
/
runner.rb
File metadata and controls
89 lines (76 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module QualityMonitoring
class Runner
attr_reader :file, :type, :cutoff, :report_query_below_score, :judge_by, :metric_collector
def initialize(
file,
type,
cutoff: 10,
report_query_below_score: nil,
judge_by: :recall,
metric_collector: nil
)
@file = Pathname.new(file)
@type = type
@cutoff = cutoff
@report_query_below_score = report_query_below_score
@judge_by = judge_by
@metric_collector = metric_collector
end
def run
scores = []
failure_details = []
data.each do |query, expected_links|
query_params = { q: query, page_size: cutoff }
result_set = DiscoveryEngine::Query::Search.new(query_params).result_set
result_links = result_set.results.map(&:link)
judge = Judge.new(result_links, expected_links)
score = judge.public_send(judge_by)
scores << score
next unless report_query_below_score && score < report_query_below_score
missing_links = expected_links - judge.result_links
failure_details << <<~DETAIL
'#{query}' #{judge_by}:#{score} is below #{report_query_below_score}, missing:
• #{missing_links.join("\n • ")}
-> Attribution token: #{result_set.discovery_engine_attribution_token}
DETAIL
rescue StandardError => e
GovukError.notify(e)
end
mean_score = scores.sum / scores.size.to_f
Rails.logger.info(
sprintf(
"[%s] Completed run for %s dataset %s with %s:%f",
self.class.name,
type,
dataset_name,
judge_by,
mean_score,
),
)
if failure_details.any?
Rails.logger.warn(
sprintf(
"[%s] %d failure(s) encountered for %s dataset %s\n%s",
self.class.name,
failure_details.size,
type,
dataset_name,
failure_details.join("\n"),
),
)
end
if metric_collector
metric_collector.record_score(type, dataset_name, mean_score)
metric_collector.record_failure_count(type, dataset_name, failure_details.size)
metric_collector.record_total_count(type, dataset_name, scores.size)
end
end
private
def dataset_name
file.basename(".csv").to_s
end
def data
@data ||= DatasetLoader.new(file).data
end
end
end