Skip to content

Commit

Permalink
Remove unused agency contraband_hit_rate view (#300)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeanette O'Brien <jeanetteobr@users.noreply.github.com>
  • Loading branch information
copelco and jeanetteobr authored Jun 7, 2024
1 parent 75b1027 commit 4a23655
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 148 deletions.
4 changes: 2 additions & 2 deletions frontend/src/Components/Charts/Contraband/Contraband.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import useMetaTags from '../../../Hooks/useMetaTags';
import useTableModal from '../../../Hooks/useTableModal';

// State
import useDataset, { AGENCY_DETAILS, CONTRABAND_HIT_RATE } from '../../../Hooks/useDataset';
import useDataset, { AGENCY_DETAILS } from '../../../Hooks/useDataset';

// Children
import { P, WEIGHTS } from '../../../styles/StyledComponents/Typography';
Expand All @@ -37,7 +37,7 @@ function Contraband(props) {
const { agencyId, yearRange, year } = props;

const officerId = useOfficerId();
const [chartState] = useDataset(agencyId, CONTRABAND_HIT_RATE);
const [chartState] = useDataset(agencyId, AGENCY_DETAILS);

useEffect(() => {
if (window.location.hash) {
Expand Down
1 change: 0 additions & 1 deletion nc/prime_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"nc:agency-api-stops-by-reason",
"nc:agency-api-searches",
"nc:agency-api-searches-by-type",
"nc:agency-api-contraband-hit-rate",
"nc:agency-api-use-of-force",
"nc:stops-by-percentage",
"nc:stops-by-count",
Expand Down
68 changes: 0 additions & 68 deletions nc/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,73 +272,5 @@ def test_searches_by_reason(self):
self.assertEqual(searches[1]["native_american"], 1)
self.assertEqual(searches[1]["search_type"], type_label)

def test_contraband_hit_rate(self):
agency = factories.AgencyFactory()
# Create the following racial data for 2010:
# 1 black, 1 native american, 3 hispanic
p1 = factories.PersonFactory(race="B", stop__agency=agency, ethnicity="N", stop__year=2010)
p2 = factories.PersonFactory(race="B", stop__agency=agency, ethnicity="H", stop__year=2010)
p3 = factories.PersonFactory(race="I", stop__agency=agency, ethnicity="N", stop__year=2010)
p4 = factories.PersonFactory(race="I", stop__agency=agency, ethnicity="H", stop__year=2010)
p5 = factories.PersonFactory(race="I", stop__agency=agency, ethnicity="H", stop__year=2010)
# Create the following racial data for 2012: 1 black
p6 = factories.PersonFactory(race="B", stop__agency=agency, ethnicity="N", stop__year=2012)
s1 = factories.SearchFactory(stop=p1.stop)
factories.SearchFactory(stop=p2.stop)
s3 = factories.SearchFactory(stop=p3.stop)
s4 = factories.SearchFactory(stop=p4.stop)
s5 = factories.SearchFactory(stop=p5.stop)
s6 = factories.SearchFactory(stop=p6.stop)
# p1 has both drugs and weapons, p5 also has weapons
factories.ContrabandFactory(search=s1, person=p1, stop=p1.stop, ounces=1.0, weapons=2.0)
factories.ContrabandFactory(search=s3, person=p3, stop=p3.stop, pints=1.0)
factories.ContrabandFactory(search=s4, person=p4, stop=p4.stop, money=1.0)
factories.ContrabandFactory(search=s5, person=p5, stop=p5.stop, weapons=1.0)
factories.ContrabandFactory(search=s6, person=p6, stop=p6.stop, dollar_amount=1.0)
url = reverse("nc:agency-api-contraband-hit-rate", args=[agency.pk])
response = self.client.get(url, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data.keys()), 3)

searches = response.data["searches"]
# The expected search data matches the created data, since each of the
# people were searched
self.assertEqual(searches[0]["year"], 2010)
self.assertEqual(searches[0]["black"], 1)
self.assertEqual(searches[0]["native_american"], 1)
self.assertEqual(searches[0]["hispanic"], 3)
self.assertEqual(searches[1]["year"], 2012)
self.assertEqual(searches[1]["black"], 1)

contraband = response.data["contraband"]
# Everyone had contraband, except for p2, so the expected contraband data
# for 2010 are: 1 black, 1 native american, 2 hispanic, and for 2012
# are: 1 black
self.assertEqual(contraband[0]["year"], 2010)
self.assertEqual(contraband[0]["black"], 1)
self.assertEqual(contraband[0]["native_american"], 1)
self.assertEqual(contraband[0]["hispanic"], 2)
self.assertEqual(contraband[1]["year"], 2012)
self.assertEqual(contraband[1]["black"], 1)

contraband = response.data["contraband_types"]
ctype_index = {}
for index, item in enumerate(contraband):
ctype_index[contraband[index]["contraband_type"]] = index

# check the drugs for p1 are noted
i = ctype_index["Drugs"]
self.assertEqual(contraband[i]["contraband_type"], "Drugs")
self.assertEqual(contraband[i]["black"], 1)
self.assertEqual(contraband[i]["native_american"], 0)
self.assertEqual(contraband[i]["hispanic"], 0)

# weapons for both p1 and p5 are noted
i = ctype_index["Weapons"]
self.assertEqual(contraband[i]["contraband_type"], "Weapons")
self.assertEqual(contraband[i]["black"], 1)
self.assertEqual(contraband[i]["native_american"], 0)
self.assertEqual(contraband[i]["hispanic"], 1)

def test_use_of_force(self):
pass
1 change: 0 additions & 1 deletion nc/tests/test_prime_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.test import TestCase

from nc.models import Agency
from nc.prime_cache import run
from nc.tests import factories

Expand Down
77 changes: 1 addition & 76 deletions nc/views/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from dateutil import relativedelta
from django.conf import settings
from django.core.mail import send_mail
from django.db.models import Case, Count, F, Q, Sum, Value, When
from django.db.models import Count, Q, Sum
from django.db.models.functions import ExtractYear
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page, never_cache
Expand All @@ -29,7 +29,6 @@
from nc.models import SEARCH_TYPE_CHOICES as SEARCH_TYPE_CHOICES_TUPLES
from nc.models import (
Agency,
Contraband,
ContrabandSummary,
Person,
Resource,
Expand Down Expand Up @@ -260,80 +259,6 @@ def searches_by_type(self, request, pk=None):
)
return Response(results.flatten())

@action(detail=True, methods=["get"])
@cache_response(key_func=query_cache_key_func)
def contraband_hit_rate(self, request, pk=None):
response = {}
# searches
results = GroupedData(by="year", defaults=GROUP_DEFAULTS)
q = Q(search_type__isnull=False)
self.query(results, group_by=("year", "driver_race", "driver_ethnicity"), filter_=q)
response["searches"] = results.flatten()

# contraband
results = GroupedData(by="year", defaults=GROUP_DEFAULTS)
q = Q(contraband_found=True)
self.query(results, group_by=("year", "driver_race", "driver_ethnicity"), filter_=q)
response["contraband"] = results.flatten()

# # contraband types
qs = Contraband.objects.filter(stop__agency=self.get_object(), person__type="D")
# # filter down by officer if supplied
officer = self.request.query_params.get("officer", None)
if officer:
qs = qs.filter(stop__officer_id=officer)
qs = qs.annotate(
year=ExtractYear("stop__date"),
driver_race=F("person__race"),
driver_ethnicity=F("person__ethnicity"),
drugs_found=Case(
When(
Q(ounces__gt=0)
| Q(pounds__gt=0)
| Q(dosages__gt=0)
| Q(grams__gt=0)
| Q(kilos__gt=0),
then=Value(True),
),
default=Value(False),
),
alcohol_found=Case(
When(Q(pints__gt=0) | Q(gallons__gt=0), then=Value(True)), default=Value(False)
),
money_found=Case(When(Q(money__gt=0), then=Value(True)), default=Value(False)),
weapons_found=Case(When(Q(weapons__gt=0), then=Value(True)), default=Value(False)),
other_found=Case(When(Q(dollar_amount__gt=0), then=Value(True)), default=Value(False)),
)

results = GroupedData(by=("contraband_type", "year"), defaults=GROUP_DEFAULTS)
# group by specified fields and order by year
group_by = ("year", "driver_ethnicity", "driver_race")
for contraband_type in CONTRABAND_CHOICES.values():
field_name = f"{contraband_type.lower()}_found"
type_qs = (
qs.filter(**{field_name: True})
.values(*group_by)
.order_by("year")
.annotate(contraband_type_count=Count(field_name))
)
for contraband in type_qs:
data = {
"year": contraband["year"],
"contraband_type": contraband_type,
}
if "driver_race" in group_by:
# The 'Hispanic' ethnicity option is now being aggregated into its
# own race category, and its count excluded from the other counts.
if contraband["driver_ethnicity"] == "H":
race = GROUPS.get("H", "H")
else:
race = GROUPS.get(contraband["driver_race"], contraband["driver_race"])
data.setdefault(race, 0)
data[race] += contraband["contraband_type_count"]
results.add(**data)
response["contraband_types"] = results.flatten()
return Response(response)


class DriverStopsViewSet(viewsets.ReadOnlyModelViewSet):
queryset = (
Expand Down

0 comments on commit 4a23655

Please sign in to comment.