summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTimothy Andrew <mail@timothyandrew.net>2016-09-21 00:47:37 +0530
committerTimothy Andrew <mail@timothyandrew.net>2016-09-21 00:47:37 +0530
commit918e589c2b29c18d9fe3a8e6c93a3f490c86beb1 (patch)
tree4b354c178952b4369beb16af43c6b75f9d882c49 /lib
parenta4a0ce95001b93c2ed65a18946542f3eecdf564e (diff)
downloadgitlab-ce-918e589c2b29c18d9fe3a8e6c93a3f490c86beb1.tar.gz
Implement a second round of review comments from @DouweM.
- Don't use `TableReferences` - using `.arel_table` is shorter! - Move some database-related code to `Gitlab::Database` - Remove the `MergeRequest#issues_closed` and `Issue#closed_by_merge_requests` associations. They were either shadowing or were too similar to existing methods. They are not being used anywhere, so it's better to remove them to reduce confusion. - Use Rails 3-style validations - Index for `MergeRequest::Metrics#first_deployed_to_production_at` - Only include `CycleAnalyticsHelpers::TestGeneration` for specs that need it. - Other minor refactorings.
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/database/median.rb35
-rw-r--r--lib/gitlab/database/util.rb12
2 files changed, 33 insertions, 14 deletions
diff --git a/lib/gitlab/database/median.rb b/lib/gitlab/database/median.rb
index 94f060bd2c7..9adacd64934 100644
--- a/lib/gitlab/database/median.rb
+++ b/lib/gitlab/database/median.rb
@@ -3,11 +3,15 @@ module Gitlab
module Database
module Median
def median_datetime(arel_table, query_so_far, column_sym)
- if Gitlab::Database.postgresql?
- pg_median_datetime(arel_table, query_so_far, column_sym)
- elsif Gitlab::Database.mysql?
- mysql_median_datetime(arel_table, query_so_far, column_sym)
- end
+ median_queries =
+ if Gitlab::Database.postgresql?
+ pg_median_datetime_sql(arel_table, query_so_far, column_sym)
+ elsif Gitlab::Database.mysql?
+ mysql_median_datetime_sql(arel_table, query_so_far, column_sym)
+ end
+
+ results = Array.wrap(median_queries).map { |query| Util.run_query(query) }
+ extract_median(results).presence
end
def extract_median(results)
@@ -22,7 +26,7 @@ module Gitlab
end
end
- def mysql_median_datetime(arel_table, query_so_far, column_sym)
+ def mysql_median_datetime_sql(arel_table, query_so_far, column_sym)
query = arel_table.
from(arel_table.project(Arel.sql('*')).order(arel_table[column_sym]).as(arel_table.table_name)).
project(average([arel_table[column_sym]], 'median')).
@@ -44,7 +48,7 @@ module Gitlab
]
end
- def pg_median_datetime(arel_table, query_so_far, column_sym)
+ def pg_median_datetime_sql(arel_table, query_so_far, column_sym)
# Create a CTE with the column we're operating on, row number (after sorting by the column
# we're operating on), and count of the table we're operating on (duplicated across) all rows
# of the CTE. For example, if we're looking to find the median of the `projects.star_count`
@@ -59,10 +63,11 @@ module Gitlab
cte = Arel::Nodes::As.new(
cte_table,
arel_table.
- project(arel_table[column_sym].as(column_sym.to_s),
- Arel::Nodes::Over.new(Arel::Nodes::NamedFunction.new("row_number", []),
- Arel::Nodes::Window.new.order(arel_table[column_sym])).as('row_id'),
- arel_table.project("COUNT(1)").as('ct')).
+ project(
+ arel_table[column_sym].as(column_sym.to_s),
+ Arel::Nodes::Over.new(Arel::Nodes::NamedFunction.new("row_number", []),
+ Arel::Nodes::Window.new.order(arel_table[column_sym])).as('row_id'),
+ arel_table.project("COUNT(1)").as('ct')).
# Disallow negative values
where(arel_table[column_sym].gteq(zero_interval)))
@@ -70,9 +75,11 @@ module Gitlab
# by 'where cte.row_id between cte.ct / 2.0 AND cte.ct / 2.0 + 1'). Find the average of the
# selected rows, and this is the median value.
cte_table.project(average([extract_epoch(cte_table[column_sym])], "median")).
- where(Arel::Nodes::Between.new(cte_table[:row_id],
- Arel::Nodes::And.new([(cte_table[:ct] / Arel.sql('2.0')),
- (cte_table[:ct] / Arel.sql('2.0') + 1)]))).
+ where(Arel::Nodes::Between.new(
+ cte_table[:row_id],
+ Arel::Nodes::And.new(
+ [(cte_table[:ct] / Arel.sql('2.0')),
+ (cte_table[:ct] / Arel.sql('2.0') + 1)]))).
with(query_so_far, cte)
end
diff --git a/lib/gitlab/database/util.rb b/lib/gitlab/database/util.rb
new file mode 100644
index 00000000000..12b68deae89
--- /dev/null
+++ b/lib/gitlab/database/util.rb
@@ -0,0 +1,12 @@
+module Gitlab
+ module Database
+ module Util
+ class << self
+ def run_query(query)
+ query = query.to_sql unless query.is_a?(String)
+ ActiveRecord::Base.connection.execute(query)
+ end
+ end
+ end
+ end
+end