This repository was archived by the owner on Dec 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathweight_query_builder.rb
More file actions
113 lines (107 loc) · 3.82 KB
/
Copy pathweight_query_builder.rb
File metadata and controls
113 lines (107 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
module Rankers
class WeightQueryBuilder
attr_reader :ranking_configuration
def initialize(ranking_configuration)
@ranking_configuration = ranking_configuration
end
def build_weight_query
weight_query = case ranking_configuration[:use_ranking_data]
when 'swiss'
<<-SQL
avg(
(
case when squadrons.swiss_percentile is not null then squadrons.swiss_percentile else 0 end
)
SQL
when 'elimination'
<<-SQL
avg(
(
case when squadrons.elimination_percentile is not null then squadrons.elimination_percentile else 0 end
)
SQL
when 'all'
<<-SQL
avg(
(
case when squadrons.swiss_percentile is not null then squadrons.swiss_percentile else 0 end
+
case when squadrons.elimination_percentile is not null then squadrons.elimination_percentile else 0 end
)
SQL
else
raise 'error'
end
if ranking_configuration[:large_tournament_multiplier]
weight_query += <<-SQL
*
case when tournaments.num_players is not null and tournaments.num_players > 0
then log(tournaments.num_players) else 0 end
SQL
end
weight_query += ') '
if ranking_configuration[:widespread_use_multiplier]
weight_query += <<-SQL
* (log(count(distinct squadrons.id) + 1))
SQL
end
weight_query += ' as weight'
weight_query
end
def build_average_query
case ranking_configuration[:use_ranking_data]
when 'swiss'
<<-SQL
avg(
case when squadrons.swiss_percentile is not null then squadrons.swiss_percentile else 0 end
)
SQL
when 'elimination'
<<-SQL
avg(
case when squadrons.elimination_percentile is not null then squadrons.elimination_percentile else 0 end
)
SQL
when 'all'
<<-SQL
avg(
case when squadrons.swiss_percentile is not null then squadrons.swiss_percentile else 0 end
+
case when squadrons.elimination_percentile is not null then squadrons.elimination_percentile else 0 end
) / 2
SQL
else
raise 'error'
end
end
def build_win_loss_query
case ranking_configuration[:use_ranking_data]
when 'swiss'
<<-SQL
avg(squadrons.win_loss_ratio_swiss)
SQL
when 'elimination'
<<-SQL
avg(squadrons.win_loss_ratio_elimination)
SQL
when 'all'
<<-SQL
avg(
case
when squadrons.win_loss_ratio_swiss is not null and squadrons.win_loss_ratio_elimination is not null
then ((squadrons.win_loss_ratio_swiss + squadrons.win_loss_ratio_elimination) / 2)
when squadrons.win_loss_ratio_swiss is not null and squadrons.win_loss_ratio_elimination is null
then squadrons.win_loss_ratio_swiss
when squadrons.win_loss_ratio_swiss is null and squadrons.win_loss_ratio_elimination is not null
then squadrons.win_loss_ratio_elimination
else
0
end
)
SQL
else
raise 'error'
end
end
end
end