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+
2645OFFICER_CYPHER = """
2746CALL (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