Commit f9fd0ff1 authored by James Lopez's avatar James Lopez

Some refactoring - used class instead of hash for the query configuration

parent ca9ae8bf
...@@ -3,48 +3,6 @@ module Gitlab ...@@ -3,48 +3,6 @@ module Gitlab
class EventsFetcher class EventsFetcher
include MetricsFetcher 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:) def initialize(project:, from:)
@query = EventsQuery.new(project: project, from: from) @query = EventsQuery.new(project: project, from: from)
end end
...@@ -52,11 +10,13 @@ module Gitlab ...@@ -52,11 +10,13 @@ module Gitlab
def fetch(stage:) def fetch(stage:)
custom_query = "#{stage}_custom_query".to_sym custom_query = "#{stage}_custom_query".to_sym
@query.execute(stage, EVENTS_CONFIG[stage]) do |base_query| @query.execute(stage) do |base_query|
public_send(custom_query, base_query) if self.respond_to?(custom_query) send(custom_query, base_query) if self.respond_to?(custom_query)
end end
end end
private
def issue_custom_query(base_query) def issue_custom_query(base_query)
base_query.join(user_table).on(issue_table[:author_id].eq(user_table[:id])) base_query.join(user_table).on(issue_table[:author_id].eq(user_table[:id]))
end end
......
...@@ -8,9 +8,9 @@ module Gitlab ...@@ -8,9 +8,9 @@ module Gitlab
@from = from @from = from
end end
def execute(stage, config, &block) def execute(stage, &block)
@stage = stage @stage = stage
@config = config @config = QueryConfig.get(stage)
query = build_query(&block) query = build_query(&block)
ActiveRecord::Base.connection.execute(query.to_sql).to_a ActiveRecord::Base.connection.execute(query.to_sql).to_a
......
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment