diff options
author | Timothy Andrew <mail@timothyandrew.net> | 2016-08-25 08:02:27 +0300 |
---|---|---|
committer | Timothy Andrew <mail@timothyandrew.net> | 2016-08-26 13:57:37 +0300 |
commit | f932bb8e41852d7bdcd66fe15a56277074df3aa3 (patch) | |
tree | 1fbdd0aad33f095b543ece238eac5b3f71d311b7 /app/models/cycle_analytics.rb | |
parent | 8ccea81cba048021ed9eded83db15b383ecd4a32 (diff) |
Add the "Plan" Cycle Analytics query.
1. Move from raw SQL to ActiveRecord.
2. Add a non-persisted `CycleAnalytics` model to store all the queries.
Diffstat (limited to 'app/models/cycle_analytics.rb')
-rw-r--r-- | app/models/cycle_analytics.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/app/models/cycle_analytics.rb b/app/models/cycle_analytics.rb new file mode 100644 index 00000000000..d037b1fef12 --- /dev/null +++ b/app/models/cycle_analytics.rb @@ -0,0 +1,40 @@ +class CycleAnalytics + def issue + issues = Issue.includes(:metrics).where("issue_metrics.id IS NOT NULL").references(:issue_metrics).to_a + start_time_fn = -> (issue) { issue.created_at } + end_time_fn = -> (issue) { issue.metrics.first_associated_with_milestone_at.presence || issue.metrics.first_added_to_board_at.presence } + + calculate_metric(issues, start_time_fn, end_time_fn) + end + + def plan + issues = Issue.includes(:metrics).where("issue_metrics.id IS NOT NULL").references(:issue_metrics).to_a + start_time_fn = -> (issue) { issue.metrics.first_associated_with_milestone_at.presence || issue.metrics.first_added_to_board_at.presence } + end_time_fn = lambda do |issue| + merge_requests = issue.closed_by_merge_requests + merge_requests.map(&:created_at).min if merge_requests.present? + end + + calculate_metric(issues, start_time_fn, end_time_fn) + end + + private + + def calculate_metric(data, start_time_fn, end_time_fn) + times = data.map do |data_point| + start_time = start_time_fn[data_point] + end_time = end_time_fn[data_point] + + if start_time.present? && end_time.present? + end_time - start_time + end + end + + median(times.compact) + end + + def median(coll) + size = coll.length + (coll[size / 2] + coll[(size - 1) / 2]) / 2.0 + end +end |