From f9fd0ff1ca2a559ef7a82337f66e15e0d2d06e16 Mon Sep 17 00:00:00 2001 From: James Lopez Date: Fri, 28 Oct 2016 12:46:38 +0100 Subject: [PATCH] Some refactoring - used class instead of hash for the query configuration --- lib/gitlab/cycle_analytics/events_fetcher.rb | 48 +---------- lib/gitlab/cycle_analytics/events_query.rb | 4 +- lib/gitlab/cycle_analytics/query_config.rb | 89 ++++++++++++++++++++ 3 files changed, 95 insertions(+), 46 deletions(-) create mode 100644 lib/gitlab/cycle_analytics/query_config.rb diff --git a/lib/gitlab/cycle_analytics/events_fetcher.rb b/lib/gitlab/cycle_analytics/events_fetcher.rb index 2686ac353d6..0248ebc7193 100644 --- a/lib/gitlab/cycle_analytics/events_fetcher.rb +++ b/lib/gitlab/cycle_analytics/events_fetcher.rb @@ -3,48 +3,6 @@ module Gitlab class EventsFetcher include MetricsFetcher - EVENTS_CONFIG = { - issue: { - start_time_attrs: issue_table[:created_at], - end_time_attrs: [issue_metrics_table[:first_associated_with_milestone_at], - issue_metrics_table[:first_added_to_board_at]], - projections: [issue_table[:title], issue_table[:iid], issue_table[:created_at], user_table[:name], user_table[:email]] - }, - plan: { - start_time_attrs: issue_metrics_table[:first_associated_with_milestone_at], - end_time_attrs: [issue_metrics_table[:first_added_to_board_at], - issue_metrics_table[:first_mentioned_in_commit_at]], - projections: [mr_diff_table[:st_commits].as('commits'), issue_metrics_table[:first_mentioned_in_commit_at]] - }, - code: { - start_time_attrs: issue_metrics_table[:first_mentioned_in_commit_at], - end_time_attrs: mr_table[:created_at], - projections: [mr_table[:title], mr_table[:iid], mr_table[:created_at], user_table[:name], user_table[:email]], - order: mr_table[:created_at] - }, - test: { - start_time_attrs: mr_metrics_table[:latest_build_started_at], - end_time_attrs: mr_metrics_table[:latest_build_finished_at], - projections: mr_metrics_table[:ci_commit_id], - order: mr_table[:created_at] - }, - review: { - start_time_attrs: mr_table[:created_at], - end_time_attrs: mr_metrics_table[:merged_at], - projections: [mr_table[:title], mr_table[:iid], mr_table[:created_at], user_table[:name], user_table[:email]] - }, - staging: { - start_time_attrs: mr_metrics_table[:merged_at], - end_time_attrs: mr_metrics_table[:first_deployed_to_production_at], - projections: mr_metrics_table[:ci_commit_id] - }, - production: { - start_time_attrs: issue_table[:created_at], - end_time_attrs: mr_metrics_table[:first_deployed_to_production_at], - projections: [issue_table[:title], issue_table[:iid], issue_table[:created_at], user_table[:name], user_table[:email]] - }, - }.freeze - def initialize(project:, from:) @query = EventsQuery.new(project: project, from: from) end @@ -52,11 +10,13 @@ module Gitlab def fetch(stage:) custom_query = "#{stage}_custom_query".to_sym - @query.execute(stage, EVENTS_CONFIG[stage]) do |base_query| - public_send(custom_query, base_query) if self.respond_to?(custom_query) + @query.execute(stage) do |base_query| + send(custom_query, base_query) if self.respond_to?(custom_query) end end + private + def issue_custom_query(base_query) base_query.join(user_table).on(issue_table[:author_id].eq(user_table[:id])) end diff --git a/lib/gitlab/cycle_analytics/events_query.rb b/lib/gitlab/cycle_analytics/events_query.rb index 39687305bc3..f78272a4108 100644 --- a/lib/gitlab/cycle_analytics/events_query.rb +++ b/lib/gitlab/cycle_analytics/events_query.rb @@ -8,9 +8,9 @@ module Gitlab @from = from end - def execute(stage, config, &block) + def execute(stage, &block) @stage = stage - @config = config + @config = QueryConfig.get(stage) query = build_query(&block) ActiveRecord::Base.connection.execute(query.to_sql).to_a diff --git a/lib/gitlab/cycle_analytics/query_config.rb b/lib/gitlab/cycle_analytics/query_config.rb new file mode 100644 index 00000000000..5689c070eb7 --- /dev/null +++ b/lib/gitlab/cycle_analytics/query_config.rb @@ -0,0 +1,89 @@ +module Gitlab + module CycleAnalytics + class QueryConfig + include MetricsFetcher + + def self.get(*args) + new(*args).get + end + + def initialize(stage) + @stage = stage + end + + def get + send(@stage).freeze if self.respond_to?(@stage) + end + + private + + def issue + { start_time_attrs: issue_table[:created_at], + end_time_attrs: [issue_metrics_table[:first_associated_with_milestone_at], + issue_metrics_table[:first_added_to_board_at]], + projections: [issue_table[:title], + issue_table[:iid], + issue_table[:created_at], + user_table[:name], + user_table[:email]] + } + end + + def plan + { start_time_attrs: issue_metrics_table[:first_associated_with_milestone_at], + end_time_attrs: [issue_metrics_table[:first_added_to_board_at], + issue_metrics_table[:first_mentioned_in_commit_at]], + projections: [mr_diff_table[:st_commits].as('commits'), + issue_metrics_table[:first_mentioned_in_commit_at]] + } + end + + def code + { start_time_attrs: issue_metrics_table[:first_mentioned_in_commit_at], + end_time_attrs: mr_table[:created_at], + projections: [mr_table[:title], mr_table[:iid], + mr_table[:created_at], + user_table[:name], + user_table[:email]], + order: mr_table[:created_at] + } + end + + def test + { start_time_attrs: mr_metrics_table[:latest_build_started_at], + end_time_attrs: mr_metrics_table[:latest_build_finished_at], + projections: mr_metrics_table[:ci_commit_id], + order: mr_table[:created_at] + } + end + + def review + { start_time_attrs: mr_table[:created_at], + end_time_attrs: mr_metrics_table[:merged_at], + projections: [mr_table[:title], mr_table[:iid], + mr_table[:created_at], + user_table[:name], + user_table[:email]] + } + end + + def staging + { start_time_attrs: mr_metrics_table[:merged_at], + end_time_attrs: mr_metrics_table[:first_deployed_to_production_at], + projections: mr_metrics_table[:ci_commit_id] + } + end + + def production + { start_time_attrs: issue_table[:created_at], + end_time_attrs: mr_metrics_table[:first_deployed_to_production_at], + projections: [issue_table[:title], + issue_table[:iid], + issue_table[:created_at], + user_table[:name], + user_table[:email]] + } + end + end + end +end