From a4a0ce95001b93c2ed65a18946542f3eecdf564e Mon Sep 17 00:00:00 2001 From: Timothy Andrew Date: Tue, 20 Sep 2016 18:50:48 +0530 Subject: [PATCH] Move JSON generation (cycle analytics) into a controller method. Helper methods are meant for views --- .../projects/cycle_analytics_controller.rb | 36 +++++++++++++++++-- app/helpers/cycle_analytics_helper.rb | 35 ------------------ 2 files changed, 34 insertions(+), 37 deletions(-) delete mode 100644 app/helpers/cycle_analytics_helper.rb diff --git a/app/controllers/projects/cycle_analytics_controller.rb b/app/controllers/projects/cycle_analytics_controller.rb index dd0acc57425..7f0689306be 100644 --- a/app/controllers/projects/cycle_analytics_controller.rb +++ b/app/controllers/projects/cycle_analytics_controller.rb @@ -1,5 +1,5 @@ class Projects::CycleAnalyticsController < Projects::ApplicationController - include CycleAnalyticsHelper + include ActionView::Helpers::DateHelper before_action :authorize_read_cycle_analytics! @@ -8,7 +8,7 @@ class Projects::CycleAnalyticsController < Projects::ApplicationController respond_to do |format| format.html - format.json { render json: cycle_analytics_json(@cycle_analytics) } + format.json { render json: cycle_analytics_json } end end @@ -27,4 +27,36 @@ class Projects::CycleAnalyticsController < Projects::ApplicationController { start_date: params[:cycle_analytics][:start_date] } end + + 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 + + summary = [ + { title: "New Issues", value: @cycle_analytics.summary.new_issues }, + { title: "Commits", value: @cycle_analytics.summary.commits }, + { title: "Deploys", value: @cycle_analytics.summary.deploys } + ] + + { + summary: summary, + stats: stats + } + end end diff --git a/app/helpers/cycle_analytics_helper.rb b/app/helpers/cycle_analytics_helper.rb deleted file mode 100644 index f6f654a639e..00000000000 --- a/app/helpers/cycle_analytics_helper.rb +++ /dev/null @@ -1,35 +0,0 @@ -module CycleAnalyticsHelper - include ActionView::Helpers::DateHelper - - def cycle_analytics_json(cycle_analytics) - 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 - - summary = [ - { title: "New Issues", value: cycle_analytics.summary.new_issues }, - { title: "Commits", value: cycle_analytics.summary.commits }, - { title: "Deploys", value: cycle_analytics.summary.deploys } - ] - - { - summary: summary, - stats: stats - } - end -end