File: average_reports.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (66 lines) | stat: -rwxr-xr-x 1,895 bytes parent folder | download
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
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'json'
require 'optparse'

class AverageReports
  attr_reader :initial_report_file, :initial_report_data, :report_file_to_data_map

  def initialize(initial_report_file:, new_report_files:)
    @initial_report_file = initial_report_file
    @initial_report_data = parse_json_from_report_file(initial_report_file)

    @report_file_to_data_map = new_report_files.each_with_object({}) do |report_file, map|
      next unless File.exist?(report_file)

      map[report_file] ||= parse_json_from_report_file(report_file)
    end
  end

  def execute
    puts "Updating #{initial_report_file} with #{report_file_to_data_map.size} new reports..."

    compound_reports = report_file_to_data_map.keys.each_with_object({}) do |report_file, result|
      report = report_file_to_data_map[report_file]

      report.each do |spec, duration|
        result[spec] ||= [*initial_report_data[spec]]
        result[spec] << duration
      end

      puts "Updated #{report.size} data points from #{report_file}"
    end

    averaged_reports = compound_reports.transform_values do |durations|
      durations.sum.to_f / durations.size
    end

    File.write(initial_report_file, JSON.pretty_generate(averaged_reports))
    puts "Saved #{initial_report_file}."

    averaged_reports
  end

  private

  def parse_json_from_report_file(report_file)
    JSON.parse(File.read(report_file))
  end
end

if $PROGRAM_NAME == __FILE__
  options = {}

  OptionParser.new do |opts|
    opts.on("-i", "--initial-report initial_report_file", String, 'Initial report file name') do |value|
      options[:initial_report_file] = value
    end

    opts.on("-n", "--new-reports new_report_files", Array, 'New report file names delimited by ","') do |values|
      options[:new_report_files] = values
    end
  end.parse!

  AverageReports.new(**options).execute
end