Class: ProjectTestsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/project_tests_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#after_sign_in_path_for, #api_authenticate!, #current_project, #expire_revision!, #followed_projects, #no_cache, #read_revision, #require_login, #return_or_cache_revision!, #revision, #save_current_project, #set_current_project, #unfurling?

Methods included from UrlHelper

#feature_path, #github_commit_range_url, #github_commit_url, #github_project_url, #github_url?, #goldmine_case_number_url, #link_to_project_feature, #new_release_url, #releases_path

Instance Method Details

#indexObject



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
# File 'app/controllers/project_tests_controller.rb', line 3

def index
  @project = Project.find_by_slug! params[:slug]
  @title = "#{@project.name} Tests"

  head = params.fetch :at, @project.head_sha
  commits = params.fetch(:limit, 128).to_i

  @commits = Houston.benchmark("[project_tests#index] fetch commits") {
    @project.repo.ancestors(head, including_self: true, limit: commits) }
  shas = @commits.map(&:sha)
  test_run_id_by_sha = Hash[@project.test_runs.where(sha: shas).pluck(:sha, :id)]
  test_run_ids = test_run_id_by_sha.values

  # We're looking at the history of the tests that exist in the
  # most recent commit; ignore tests that didn't exist before this
  test_ids = Houston.benchmark("[project_tests#index] pick tests") do
    latest_test_run_id = @project.test_runs.where(sha: shas)
      .joins("LEFT OUTER JOIN test_results ON test_runs.id=test_results.test_run_id")
      .where.not("test_results.test_run_id" => nil)
      .limit(1)
      .pluck(:id)
      .first
    TestResult.where(test_run_id: latest_test_run_id).pluck("DISTINCT test_id")
  end

  # Get all the results that we're going to graph
  test_results = Houston.benchmark("[project_tests#index] load results") do
    TestResult.where(test_run_id: test_run_ids, test_id: test_ids).pluck(:test_run_id, :test_id, :status, :duration)
  end

  # Now we need to map results to tests
  # and make sure that they're in the same order
  # that the last 200 commits occurred in.
  @tests = Houston.benchmark("[project_tests#index] map results") do
    map = Hash.new { |hash, test_id| hash[test_id] = {} }
    durations = Hash.new { |hash, test_id| hash[test_id] = [] }
    test_results.each do |(test_run_id, test_id, status, duration)|
      map[test_id][test_run_id] = status
      durations[test_id] << duration if duration
    end

    @project.tests
      .where(id: test_ids)
      .order(:suite, :name)
      .pluck("tests.id", :suite, :name)
      .map do |(id, suite, name)|
        status_by_test_run_id = map[id]
        d = durations[id]
        { id: id,
          suite: suite,
          name: name,
          duration_avg: d.mean,
          duration5: d.percentile(5),
          duration95: d.percentile(95),
          results: shas.map { |sha| status_by_test_run_id[test_run_id_by_sha[sha]] } }
      end
  end
end

#showObject



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/project_tests_controller.rb', line 62

def show
  @project = Project.find_by_slug! params[:slug]
  @test = @project.tests.find params[:id]
  @totals = Hash[@test.test_results.group(:status).pluck(:status, "COUNT(*)")]

  begin
    head = params.fetch :at, @project.head_sha
    stop_shas = @test.introduced_in_shas
    @commits = Houston.benchmark("[project_tests#show] fetch commits") {
      @project.repo.ancestors(head, including_self: true, limit: 100, hide: stop_shas) }

    if @commits.any?
      @runs = @project.test_runs.where(sha: @commits.map(&:sha))

      @commits.each do |commit|
        def commit.date
          @date ||= committed_at.to_date
        end
        def commit.time
          committed_at
        end
      end

      @results = @test.test_results.where(test_run_id: @runs.map(&:id))
        .joins(:test_run)
        .select("test_runs.sha", "test_results.*")
        .index_by { |result| result[:sha] }
      @runs = @runs.index_by(&:sha)
    end
  rescue Houston::Adapters::VersionControl::CommitNotFound
    @commits = []
    @exception = $!
  end

  if request.xhr?
    if @commits.any?
      render partial: "project_tests/commits"
    else
      head 204
    end
  end
end