Skip to content

Commit

Permalink
OTWO-7242 Optimize contributor_monthly_commits_query method (#1786)
Browse files Browse the repository at this point in the history
  • Loading branch information
Priya5 authored Apr 23, 2024
1 parent efa6b2d commit ddeda7a
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions app/models/analysis/query_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def missing_month_commits
end

def contributor_monthly_commits
@contributor_monthly_commits ||= contributor_monthly_commits_query.map do |c|
code_set_ids = AnalysisSlocSet.joins(:sloc_set).where(analysis_id: @analysis.id).pluck(:code_set_id)

@contributor_monthly_commits ||= contributor_monthly_commits_query(code_set_ids).map do |c|
MonthlyCommitHistory.new(month: DateTime.parse(c['month']).in_time_zone, commits: c['count'])
end
end
Expand Down Expand Up @@ -87,15 +89,16 @@ def with_analysis
activity_facts[:on_trunk].eq(true).and(activity_facts[:analysis_id].eq(@analysis.id))
end

def contributor_monthly_commits_query
def contributor_monthly_commits_query(code_set_ids)
commit_name_ids = AnalysisAlias.where(preferred_name_id: @name_id, analysis_id: @analysis.id).pluck(:commit_name_id)
sql = <<-SQL.squish
select to_char(date(C.time),'MON,YYYY') as month, count (*) as count
FROM commits C INNER JOIN code_sets CS ON C.code_set_id = CS.id
INNER JOIN sloc_sets SS ON SS.code_set_id = CS.id INNER JOIN analysis_sloc_sets ASS ON ASS.sloc_set_id = SS.id
INNER JOIN analysis_aliases K on K.commit_name_id = C.name_id
WHERE ASS.analysis_id = #{@analysis.id} AND K.analysis_id = #{@analysis.id} AND C.position <= ASS.as_of AND K.preferred_name_id = #{@name_id}
WHERE ASS.analysis_id = #{@analysis.id} AND C.position <= ASS.as_of AND C.code_set_id in (#{code_set_ids.join(', ')}) AND C.name_id in (#{commit_name_ids.join(', ')})
group by month
SQL
JSON.parse(ApplicationRecord.connection.execute(sql).try(:to_json))
data = commit_name_ids.present? ? ApplicationRecord.connection.execute(sql).try(:to_json) : commit_name_ids.to_s
JSON.parse(data)
end
end

0 comments on commit ddeda7a

Please sign in to comment.