Improve performance for pipeline charts

Achieved by using another table, which both has better indexes and
is smaller. Now the data provided for the user is more valueable too.
This commit is contained in:
Z.J. van de Weg 2017-06-22 14:37:59 +02:00
parent 9c7bf12356
commit 13d39971f3
5 changed files with 21 additions and 19 deletions

View file

@ -136,6 +136,11 @@ class Projects::PipelinesController < Projects::ApplicationController
@charts[:month] = Ci::Charts::MonthChart.new(project) @charts[:month] = Ci::Charts::MonthChart.new(project)
@charts[:year] = Ci::Charts::YearChart.new(project) @charts[:year] = Ci::Charts::YearChart.new(project)
@charts[:build_times] = Ci::Charts::BuildTime.new(project) @charts[:build_times] = Ci::Charts::BuildTime.new(project)
@counts = {}
@counts[:total] = @project.pipelines.count(:all)
@counts[:success] = @project.pipelines.success.count(:all)
@counts[:failed] = @project.pipelines.failed.count(:all)
end end
private private

View file

@ -17,13 +17,10 @@ module GraphHelper
ids.zip(parent_spaces) ids.zip(parent_spaces)
end end
def success_ratio(success_builds, failed_builds) def success_ratio(success:, failed:)
failed_builds = failed_builds.count(:all) return 100 if failed.zero?
success_builds = success_builds.count(:all)
return 100 if failed_builds.zero? ratio = (success.to_f / (success + failed)) * 100
ratio = (success_builds.to_f / (success_builds + failed_builds)) * 100
ratio.to_i ratio.to_i
end end
end end

View file

@ -2,18 +2,14 @@
%ul %ul
%li %li
Total: Total:
%strong= pluralize @project.builds.count(:all), 'job' %strong= pluralize @counts[:total], 'job'
%li %li
Successful: Successful:
%strong= pluralize @project.builds.success.count(:all), 'job' %strong= pluralize @counts[:success], 'job'
%li %li
Failed: Failed:
%strong= pluralize @project.builds.failed.count(:all), 'job' %strong= pluralize @counts[:failed], 'job'
%li %li
Success ratio: Success ratio:
%strong %strong
#{success_ratio(@project.builds.success, @project.builds.failed)}% #{success_ratio(@counts)}%
%li
Commits covered:
%strong
= @project.pipelines.count(:all)

View file

@ -0,0 +1,4 @@
---
title: Improve performance of the pipeline charts page
merge_request: 12378
author:

View file

@ -3,7 +3,7 @@ module Ci
module DailyInterval module DailyInterval
def grouped_count(query) def grouped_count(query)
query query
.group("DATE(#{Ci::Build.table_name}.created_at)") .group("DATE(#{Ci::Pipeline.table_name}.created_at)")
.count(:created_at) .count(:created_at)
.transform_keys { |date| date.strftime(@format) } .transform_keys { |date| date.strftime(@format) }
end end
@ -17,12 +17,12 @@ module Ci
def grouped_count(query) def grouped_count(query)
if Gitlab::Database.postgresql? if Gitlab::Database.postgresql?
query query
.group("to_char(#{Ci::Build.table_name}.created_at, '01 Month YYYY')") .group("to_char(#{Ci::Pipeline.table_name}.created_at, '01 Month YYYY')")
.count(:created_at) .count(:created_at)
.transform_keys(&:squish) .transform_keys(&:squish)
else else
query query
.group("DATE_FORMAT(#{Ci::Build.table_name}.created_at, '01 %M %Y')") .group("DATE_FORMAT(#{Ci::Pipeline.table_name}.created_at, '01 %M %Y')")
.count(:created_at) .count(:created_at)
end end
end end
@ -46,8 +46,8 @@ module Ci
end end
def collect def collect
query = project.builds query = project.pipelines
.where("? > #{Ci::Build.table_name}.created_at AND #{Ci::Build.table_name}.created_at > ?", @to, @from) .where("? > #{Ci::Pipeline.table_name}.created_at AND #{Ci::Pipeline.table_name}.created_at > ?", @to, @from)
totals_count = grouped_count(query) totals_count = grouped_count(query)
success_count = grouped_count(query.success) success_count = grouped_count(query.success)