|
| 1 | +module SummaryTestResultsHelper |
| 2 | + class SummaryTestResults |
| 3 | + class << self |
| 4 | + def fetch(test_groups:, latest:, student_run:, instructor_run:) |
| 5 | + query = base_query(latest:) |
| 6 | + |
| 7 | + query = query.student_run if student_run && !instructor_run |
| 8 | + query = query.instructor_run if !student_run && instructor_run |
| 9 | + |
| 10 | + test_results = fetch_with_query(test_groups:, query:) |
| 11 | + |
| 12 | + SummaryTestResult.new(test_results:) |
| 13 | + end |
| 14 | + |
| 15 | + private |
| 16 | + |
| 17 | + def base_query(latest:) |
| 18 | + if latest |
| 19 | + TestRun.group('grouping_id').select('MAX(created_at) as test_runs_created_at', 'grouping_id') |
| 20 | + else |
| 21 | + TestRun.select('created_at as test_runs_created_at', 'grouping_id') |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + def fetch_with_query(test_groups:, query:) |
| 26 | + latest_test_runs = TestRun |
| 27 | + .joins(grouping: :group) |
| 28 | + .joins("INNER JOIN (#{query.to_sql}) latest_test_run_by_grouping \ |
| 29 | + ON latest_test_run_by_grouping.grouping_id = test_runs.grouping_id \ |
| 30 | + AND latest_test_run_by_grouping.test_runs_created_at = test_runs.created_at") |
| 31 | + .select('id', 'test_runs.grouping_id', 'groups.group_name') |
| 32 | + .to_sql |
| 33 | + |
| 34 | + test_groups.joins(test_group_results: :test_results) |
| 35 | + .joins("INNER JOIN (#{latest_test_runs}) latest_test_runs \ |
| 36 | + ON test_group_results.test_run_id = latest_test_runs.id") |
| 37 | + .select('test_groups.name', |
| 38 | + 'test_groups.id as test_groups_id', |
| 39 | + 'latest_test_runs.group_name', |
| 40 | + 'test_results.name as test_result_name', |
| 41 | + 'test_results.status', |
| 42 | + 'test_results.marks_earned', |
| 43 | + 'test_results.marks_total', |
| 44 | + :output, :extra_info, :error_type) |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + class SummaryTestResult |
| 50 | + def initialize(test_results:) |
| 51 | + @test_results = test_results |
| 52 | + end |
| 53 | + |
| 54 | + def as_csv |
| 55 | + results = {} |
| 56 | + headers = Set.new |
| 57 | + |
| 58 | + summary_test_results = @test_results.as_json |
| 59 | + |
| 60 | + summary_test_results.each do |test_result| |
| 61 | + header = "#{test_result['name']}:#{test_result['test_result_name']}" |
| 62 | + |
| 63 | + if results.key?(test_result['group_name']) |
| 64 | + results[test_result['group_name']][header] = test_result['status'] |
| 65 | + else |
| 66 | + results[test_result['group_name']] = { header => test_result['status'] } |
| 67 | + end |
| 68 | + |
| 69 | + headers << header |
| 70 | + end |
| 71 | + headers = headers.sort |
| 72 | + |
| 73 | + CSV.generate do |csv| |
| 74 | + csv << [nil, *headers] |
| 75 | + |
| 76 | + results.sort_by(&:first).each do |(group_name, _test_group)| |
| 77 | + row = [group_name] |
| 78 | + |
| 79 | + headers.each do |header| |
| 80 | + if results[group_name].key?(header) |
| 81 | + row << results[group_name][header] |
| 82 | + else |
| 83 | + row << nil |
| 84 | + end |
| 85 | + end |
| 86 | + csv << row |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + def as_json |
| 92 | + @test_results.group_by(&:group_name).transform_values do |grouping| |
| 93 | + grouping.group_by(&:name) |
| 94 | + end.to_json |
| 95 | + end |
| 96 | + end |
| 97 | +end |
0 commit comments