Module: Bitfab::ReplayCli

Defined in:
lib/bitfab/replay_registry.rb

Overview

Argument parsing and output for the SDK-owned replay command.

Class Method Summary collapse

Class Method Details

.comma_separated(flag, value) ⇒ Object

Raises:

  • (OptionParser::InvalidArgument)


219
220
221
222
223
224
# File 'lib/bitfab/replay_registry.rb', line 219

def comma_separated(flag, value)
  values = value.split(",").map(&:strip).reject(&:empty?)
  raise OptionParser::InvalidArgument, "#{flag} must contain at least one value" if values.empty?

  values
end

.load_code_change(path) ⇒ Object



226
227
228
229
230
231
232
233
# File 'lib/bitfab/replay_registry.rb', line 226

def load_code_change(path)
  value = JSON.parse(File.read(path))
  unless value.is_a?(Hash) && value["description"].is_a?(String) && value["files"].is_a?(Array)
    raise ArgumentError, "Invalid --code-change file '#{path}': expected { description, files }."
  end

  value
end

.load_parameters(path, raw_parameters) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/bitfab/replay_registry.rb', line 251

def load_parameters(path, raw_parameters)
  params = {}
  if path
    value = JSON.parse(File.read(path))
    unless value.is_a?(Hash)
      raise ArgumentError, "Invalid --params file '#{path}': expected a JSON object."
    end
    params.merge!(value)
  end
  raw_parameters.each do |raw|
    key, value = parse_parameter(raw)
    params[key] = value
  end
  params.freeze
end

.parse(registry, argv) ⇒ Object

Raises:

  • (OptionParser::MissingArgument)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/bitfab/replay_registry.rb', line 171

def parse(registry, argv)
  if argv.include?("--db-branch") && argv.include?("--no-db-branch")
    raise OptionParser::InvalidArgument, "--db-branch and --no-db-branch cannot be used together"
  end
  args = {}
  parser = OptionParser.new do |options|
    options.banner = "Usage: bitfab-replay <#{registry.names.join("|")}> [options]"
    options.on("--limit N", Integer) { |value| args[:limit] = positive_integer("--limit", value) }
    options.on("--trace-ids IDS") { |value| args[:trace_ids] = comma_separated("--trace-ids", value) }
    options.on("--name NAME") { |value| args[:name] = value }
    options.on("--concurrency N", Integer) do |value|
      args[:max_concurrency] = positive_integer("--concurrency", value)
    end
    options.on("--max-concurrency N", Integer) do |value|
      args[:max_concurrency] = positive_integer("--max-concurrency", value)
    end
    options.on("--code-change PATH") { |value| args[:code_change] = value }
    options.on("--experiment-group-id UUID") { |value| args[:experiment_group_id] = value }
    options.on("--dataset-id UUID") { |value| args[:dataset_id] = value }
    options.on("--grader-ids IDS") { |value| args[:grader_ids] = comma_separated("--grader-ids", value) }
    options.on("--mock STRATEGY", %w[none all marked]) { |value| args[:mock] = value }
    options.on("--db-branch") { args[:db_branch] = true }
    options.on("--no-db-branch") { args[:db_branch] = false }
    options.on("--no-code-change") { args[:no_code_change] = true }
    options.on("--params PATH") { |value| args[:params] = value }
    options.on("--param NAME=VALUE") { |value| (args[:param] ||= []) << value }
  end
  parser.parse!(argv)
  if args[:trace_ids] && args[:dataset_id]
    raise OptionParser::InvalidArgument,
      "--trace-ids and --dataset-id select different replay sources and cannot be used together"
  end
  if args[:code_change] && args[:no_code_change]
    raise OptionParser::InvalidArgument, "--code-change and --no-code-change cannot be used together"
  end
  pipeline = argv.shift
  raise OptionParser::MissingArgument, parser.banner unless pipeline && registry.names.include?(pipeline)
  raise OptionParser::InvalidArgument, "unexpected arguments: #{argv.join(" ")}" unless argv.empty?

  args.merge(pipeline:)
end

.parse_parameter(raw) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/bitfab/replay_registry.rb', line 235

def parse_parameter(raw)
  key, separator, value = raw.partition("=")
  key = key.strip
  if separator.empty? || key.empty?
    raise OptionParser::InvalidArgument,
      "Invalid --param '#{raw}': expected a non-empty name=value pair"
  end

  parsed = begin
    JSON.parse(value)
  rescue JSON::ParserError
    value
  end
  [key, parsed]
end

.positive_integer(flag, value) ⇒ Object

Raises:

  • (OptionParser::InvalidArgument)


213
214
215
216
217
# File 'lib/bitfab/replay_registry.rb', line 213

def positive_integer(flag, value)
  raise OptionParser::InvalidArgument, "#{flag} must be a positive integer" if value < 1

  value
end

.render_summary(pipeline, result, stderr) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/bitfab/replay_registry.rb', line 267

def render_summary(pipeline, result, stderr)
  same = 0
  changed = 0
  errors = 0
  result[:items].each do |item|
    if item[:error]
      errors += 1
    elsif item[:result] == item[:original_output]
      same += 1
    else
      changed += 1
    end
  end

  stderr.puts "\n─── Summary ───"
  stderr.puts "  Pipeline: #{pipeline}"
  stderr.puts "  Replayed: #{result[:items].length}"
  stderr.puts "  Same:     #{same}"
  stderr.puts "  Changed:  #{changed}"
  stderr.puts "  Errors:   #{errors}" if errors > 0
  stderr.puts "\n  #{result[:test_run_url]}"
end

.run(registry, argv: ARGV, stdout: $stdout, stderr: $stderr) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/bitfab/replay_registry.rb', line 86

def run(registry, argv: ARGV, stdout: $stdout, stderr: $stderr)
  args = parse(registry, argv.dup)
  registration = registry.fetch(args.fetch(:pipeline))
  options = registration.options.dup
  params = load_parameters(args[:params], args.fetch(:param, []))
  if registration.options_factory
    dynamic_options = registration.options_factory.call(ReplayRegistryContext.new(params:))
    unless dynamic_options.is_a?(Hash)
      raise ArgumentError, "Replay registry options_factory must return a Hash."
    end
    registry.validate_options(dynamic_options)
    options.merge!(dynamic_options)
  end

  if options[:trace_ids] && options[:dataset_id]
    raise ArgumentError,
      "Replay registry options trace_ids and dataset_id select different sources and cannot be used together."
  end

  if args[:trace_ids]
    options.delete(:limit)
    options.delete(:dataset_id)
    options[:trace_ids] = args[:trace_ids]
  elsif args[:dataset_id]
    options.delete(:trace_ids)
    options.delete(:limit)
  elsif args[:limit]
    options.delete(:trace_ids)
    options.delete(:dataset_id)
    options[:limit] = args[:limit]
  elsif options[:trace_ids] || options[:dataset_id]
    options.delete(:limit)
  else
    options[:limit] = options.fetch(:limit, 10)
  end

  %i[name max_concurrency experiment_group_id dataset_id grader_ids mock].each do |key|
    options[key] = args[key] unless args[key].nil?
  end

  unless args[:db_branch].nil?
    if args[:db_branch]
      configured = options[:db_branch]
      options[:db_branch] = (configured.nil? || configured == false) ? true : configured
    else
      options[:db_branch] = false
    end
  end

  if args[:code_change]
    code_change = load_code_change(args[:code_change])
    options[:code_change_description] = code_change.fetch("description")
    options[:code_change_files] = code_change.fetch("files")
  elsif args[:no_code_change]
    options[:code_change_description] = nil
    options[:code_change_files] = nil
  end

  reporter = Bitfab.method(:report_replay_progress)
  options[:on_item_start] = reporter
  options[:on_item_finish] = reporter

  count = options[:trace_ids]&.length || options[:limit] || "dataset"
  stderr.puts "[replay] Replaying #{count} traces from \"#{registration.trace_function_key}\"..."

  result = begin
    registration.client.replay(
      registration.receiver,
      registration.method_name,
      trace_function_key: registration.trace_function_key,
      **options
    )
  rescue ReplayError => error
    error.items.each do |item|
      item_error = item[:trace_error] || item[:replay_error] || item[:error]
      stderr.puts "#{item[:original_trace_id]}: #{item_error}"
    end
    raise
  end

  render_summary(args.fetch(:pipeline), result, stderr)
  stdout.puts Bitfab.serialize_replay_result(result)
  result
end