Skip to content

Commit 711df45

Browse files
committed
Fix ratings count
1 parent e116506 commit 711df45

2 files changed

Lines changed: 30 additions & 20 deletions

File tree

lib/plexus/apps.ex

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,28 +114,38 @@ defmodule Plexus.Apps do
114114
end
115115

116116
defp with_scores(query) do
117-
micro_g = from Rating, where: [rating_type: :micro_g]
118-
native = from Rating, where: [rating_type: :native]
117+
ratings_agg =
118+
from r in Rating,
119+
group_by: [r.app_package, r.rating_type],
120+
select: %{
121+
app_package: r.app_package,
122+
rating_type: r.rating_type,
123+
avg_score: fragment("CAST(ROUND(AVG(?)::numeric, 2) AS FLOAT)", r.score),
124+
total_count: count(r.id)
125+
}
119126

120127
query
121-
|> join(:left, [a], r_micro_g in subquery(micro_g), on: a.package == r_micro_g.app_package)
122-
|> join(:left, [a, _], r_native in subquery(native), on: a.package == r_native.app_package)
123-
|> group_by([a, r_micro_g, r_native], [a.package, r_micro_g.rating_type, r_native.rating_type])
124-
|> select_merge([a, r_micro_g, r_native], %{
128+
|> join(:left, [a], agg in subquery(ratings_agg),
129+
on: a.package == agg.app_package and agg.rating_type == :native,
130+
as: :native_agg
131+
)
132+
|> join(:left, [a], agg in subquery(ratings_agg),
133+
on: a.package == agg.app_package and agg.rating_type == :micro_g,
134+
as: :micro_g_agg
135+
)
136+
|> select_merge([a, native_agg: n, micro_g_agg: m], %{
125137
scores: %{
126138
native: %Score{
127139
app_package: a.package,
128140
rating_type: :native,
129-
numerator:
130-
fragment("CAST(ROUND(AVG(COALESCE(?, 0))::numeric, 2) AS FLOAT)", r_native.score),
131-
total_count: count(r_native.id, :distinct)
141+
numerator: coalesce(n.avg_score, 0.0),
142+
total_count: coalesce(n.total_count, 0)
132143
},
133144
micro_g: %Score{
134145
app_package: a.package,
135146
rating_type: :micro_g,
136-
numerator:
137-
fragment("CAST(ROUND(AVG(COALESCE(?, 0))::numeric, 2) AS FLOAT)", r_micro_g.score),
138-
total_count: count(r_micro_g.id, :distinct)
147+
numerator: coalesce(m.avg_score, 0.0),
148+
total_count: coalesce(m.total_count, 0)
139149
}
140150
}
141151
})

lib/plexus/ratings.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,56 +88,56 @@ defmodule Plexus.Ratings do
8888

8989
def gold_de_googled_count do
9090
Rating
91-
|> where([rating], rating.score >= 3.5)
91+
|> where([rating], rating.score == 4)
9292
|> where([rating], rating.rating_type == :native)
9393
|> Repo.aggregate(:count)
9494
end
9595

9696
def gold_micro_g_count do
9797
Rating
98-
|> where([rating], rating.score >= 3.5)
98+
|> where([rating], rating.score == 4)
9999
|> where([rating], rating.rating_type == :micro_g)
100100
|> Repo.aggregate(:count)
101101
end
102102

103103
def silver_de_googled_count do
104104
Rating
105-
|> where([rating], rating.score >= 3 and rating.score < 3.5)
105+
|> where([rating], rating.score == 3)
106106
|> where([rating], rating.rating_type == :native)
107107
|> Repo.aggregate(:count)
108108
end
109109

110110
def silver_micro_g_count do
111111
Rating
112-
|> where([rating], rating.score >= 3 and rating.score < 3.5)
112+
|> where([rating], rating.score == 3)
113113
|> where([rating], rating.rating_type == :micro_g)
114114
|> Repo.aggregate(:count)
115115
end
116116

117117
def bronze_de_googled_count do
118118
Rating
119-
|> where([rating], rating.score >= 2 and rating.score < 3)
119+
|> where([rating], rating.score == 2)
120120
|> where([rating], rating.rating_type == :native)
121121
|> Repo.aggregate(:count)
122122
end
123123

124124
def bronze_micro_g_count do
125125
Rating
126-
|> where([rating], rating.score >= 2 and rating.score < 3)
126+
|> where([rating], rating.score == 2)
127127
|> where([rating], rating.rating_type == :micro_g)
128128
|> Repo.aggregate(:count)
129129
end
130130

131131
def broken_de_googled_count do
132132
Rating
133-
|> where([rating], rating.score < 2)
133+
|> where([rating], rating.score == 1)
134134
|> where([rating], rating.rating_type == :native)
135135
|> Repo.aggregate(:count)
136136
end
137137

138138
def broken_micro_g_count do
139139
Rating
140-
|> where([rating], rating.score < 2)
140+
|> where([rating], rating.score == 1)
141141
|> where([rating], rating.rating_type == :micro_g)
142142
|> Repo.aggregate(:count)
143143
end

0 commit comments

Comments
 (0)