Skip to content

Commit c8e43d2

Browse files
authored
Add Most Complaints include option to GET Agency (#535)
1 parent 314b0f8 commit c8e43d2

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

backend/dto/agency.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def validate_include(cls, v):
5656
"officers",
5757
"complaints",
5858
"allegations",
59+
"most_complaints",
5960
}
6061
if v:
6162
invalid = set(v) - allowed_includes

backend/routes/agencies.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@
2323
}
2424
"""
2525

26+
TOP_UNITS_BY_COMPLAINTS_CYPHER = """
27+
CALL (a) {
28+
MATCH (a)-[]-(u:Unit)<-[]-(:Employment)-[]->(o:Officer)
29+
-[:ACCUSED_OF]->(:Allegation)-[:ALLEGED]-(c:Complaint)
30+
WITH
31+
u,
32+
count(DISTINCT c) AS complaint_count,
33+
count(DISTINCT o) AS officer_count
34+
ORDER BY complaint_count DESC, coalesce(u.name, "") ASC
35+
LIMIT 3
36+
RETURN collect({
37+
unit_uid: u.uid,
38+
unit_name: u.name,
39+
complaint_count: complaint_count,
40+
officer_count: officer_count
41+
}) AS top_units_by_complaints
42+
}
43+
"""
44+
2645
OFFICER_CYPHER = """
2746
CALL (a) {
2847
OPTIONAL MATCH (a)-[]-(:Unit)<-[]-(:Employment)-[]->(o:Officer)
@@ -156,6 +175,9 @@ def get_agency(agency_uid: str):
156175
if "allegations" in params.include:
157176
subqueries += ALLEGATION_CYPHER
158177
return_clause += ", collect(type_summary) AS allegation_summary"
178+
if "most_complaints" in params.include:
179+
subqueries += TOP_UNITS_BY_COMPLAINTS_CYPHER
180+
return_clause += ", top_units_by_complaints"
159181
cy = match_clause + subqueries + return_clause
160182

161183
rows, _ = db.cypher_query(cy, {"agency_uid": agency_uid})
@@ -178,6 +200,9 @@ def get_agency(agency_uid: str):
178200
agency_data["allegation_summary"] = format_allegation_summary(
179201
row[idx])
180202
idx += 1
203+
if "most_complaints" in params.include:
204+
agency_data["most_complaints"] = row[idx]
205+
idx += 1
181206
return ordered_jsonify(agency_data)
182207

183208

0 commit comments

Comments
 (0)