rejections by country?

This commit is contained in:
24c02 2026-01-07 18:22:24 -05:00
parent 9e7c3b7ec6
commit ec35dfc938
2 changed files with 71 additions and 0 deletions

View file

@ -33,6 +33,9 @@ module Backend
# Calculate rejection reason breakdown
@rejection_breakdown = calculate_rejection_breakdown(@verifications.rejected)
# Calculate rejections by country
@rejections_by_country = calculate_rejections_by_country(@verifications.rejected)
# Get leaderboard data
activity_counts = PublicActivity::Activity
.where(key: [ "verification.approve", "verification.reject" ])
@ -94,5 +97,34 @@ module Backend
breakdown.sort_by { |_, data| -data[:count] }.to_h
end
def calculate_rejections_by_country(rejected_verifications)
return {} if rejected_verifications.empty?
by_country = {}
rejected_verifications.includes(:identity).each do |verification|
next unless verification.rejection_reason.present?
next unless verification.identity&.country.present?
country_code = verification.identity.country
country_name = ISO3166::Country[country_code]&.common_name || country_code
by_country[country_name] ||= { total: 0, reasons: {} }
by_country[country_name][:total] += 1
reason_name = verification.try(:rejection_reason_name) || verification.rejection_reason.humanize
by_country[country_name][:reasons][reason_name] ||= 0
by_country[country_name][:reasons][reason_name] += 1
end
# Sort countries by total rejections, and reasons within each country by count
by_country.transform_values do |data|
data[:reasons] = data[:reasons].sort_by { |_, count| -count }.to_h
data[:top_reason] = data[:reasons].first&.first
data[:top_reason_count] = data[:reasons].first&.last || 0
data
end.sort_by { |_, data| -data[:total] }.to_h
end
end
end

View file

@ -101,5 +101,44 @@
</div>
</div>
</div>
<hr>
<div class="section">
<div class="section-header"><h3>🌍 rejections by country</h3></div>
<div class="section-content">
<% if @rejections_by_country.any? %>
<table>
<thead>
<tr>
<th>country</th>
<th>rejections</th>
<th>top reason</th>
<th>breakdown</th>
</tr>
</thead>
<tbody>
<% @rejections_by_country.each do |country, data| %>
<tr>
<td><b><%= country %></b></td>
<td><%= data[:total] %></td>
<td><%= data[:top_reason] %> (<%= data[:top_reason_count] %>)</td>
<td>
<details>
<summary>view all</summary>
<ul style="margin: 0.5rem 0 0 1rem; padding: 0;">
<% data[:reasons].each do |reason, count| %>
<li><%= reason %>: <%= count %></li>
<% end %>
</ul>
</details>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<div class="empty-state">no rejections</div>
<% end %>
</div>
</div>
<% end %>
</div>