BigW Consortium Gitlab

cycle_analytics_controller.rb 2.19 KB
Newer Older
1
class Projects::CycleAnalyticsController < Projects::ApplicationController
2
  include ActionView::Helpers::DateHelper
3
  include ActionView::Helpers::TextHelper
4

5 6
  before_action :authorize_read_cycle_analytics!

7
  def show
8
    @cycle_analytics = CycleAnalytics.new(@project, from: parse_start_date)
9 10 11

    respond_to do |format|
      format.html
12
      format.json { render json: cycle_analytics_json }
13
    end
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  end

  private

  def parse_start_date
    case cycle_analytics_params[:start_date]
    when '30' then 30.days.ago
    when '90' then 90.days.ago
    else 90.days.ago
    end
  end

  def cycle_analytics_params
    return {} unless params[:cycle_analytics].present?

    { start_date: params[:cycle_analytics][:start_date] }
30
  end
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

  def cycle_analytics_json
    cycle_analytics_view_data = [[:issue, "Issue", "Time before an issue gets scheduled"],
                                 [:plan, "Plan", "Time before an issue starts implementation"],
                                 [:code, "Code", "Time until first merge request"],
                                 [:test, "Test", "Total test time for all commits/merges"],
                                 [:review, "Review", "Time between merge request creation and merge/close"],
                                 [:staging, "Staging", "From merge request merge until deploy to production"],
                                 [:production, "Production", "From issue creation until deploy to production"]]

    stats = cycle_analytics_view_data.reduce([]) do |stats, (stage_method, stage_text, stage_description)|
      value = @cycle_analytics.send(stage_method).presence

      stats << {
        title: stage_text,
        description: stage_description,
        value: value && !value.zero? ? distance_of_time_in_words(value) : nil
      }
      stats
    end

52 53 54 55
    issues = @cycle_analytics.summary.new_issues
    commits = @cycle_analytics.summary.commits
    deploys = @cycle_analytics.summary.deploys

56
    summary = [
57 58 59
      { title: "New Issue".pluralize(issues), value: issues },
      { title: "Commit".pluralize(commits), value: commits },
      { title: "Deploy".pluralize(deploys), value: deploys }
60 61 62 63 64 65 66
    ]

    {
      summary: summary,
      stats: stats
    }
  end
67
end