Skip to content

Commit

Permalink
Remove region methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ev-sc committed Dec 11, 2024
1 parent 517ce8f commit 84e42e0
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 82 deletions.
27 changes: 7 additions & 20 deletions hub/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,17 @@ def get_analytics_queryset(self) -> BaseManager["GenericData"]:
raise NotImplementedError("Subclasses must implement this method")

# TODO: Rename this because it's a big clash with "region" in the specific geographic sense (EERs)
class RegionCount(TypedDict):
class AreaCount(TypedDict):
label: str
gss: Optional[str]
count: int

# TODO: Rename to ...by_eer
def imported_data_count_by_region(self) -> List[RegionCount]:
return (
self.get_analytics_queryset()
.annotate(
label=F("postcode_data__european_electoral_region"),
gss=F("postcode_data__codes__european_electoral_region"),
)
.values("label", "gss")
.annotate(count=Count("label"))
.order_by("-count")
)

# TODO: Rename to e.g. row_count_by_political_boundary
def imported_data_count_by_area(
self,
postcode_io_key: str = None,
gss: str = None,
) -> QuerySet[RegionCount]:
) -> QuerySet[AreaCount]:
qs = self.get_analytics_queryset()
if postcode_io_key is None:
return []
Expand All @@ -60,7 +47,7 @@ def imported_data_count_by_area(

def imported_data_count_by_constituency(
self, gss: str = None
) -> QuerySet[RegionCount]:
) -> QuerySet[AreaCount]:
qs = self.get_analytics_queryset()

if gss:
Expand All @@ -81,7 +68,7 @@ def imported_data_count_by_constituency(

def imported_data_count_by_constituency_by_source(
self, gss: str = None
) -> List[RegionCount]:
) -> List[AreaCount]:
qs = self.get_analytics_queryset()

if gss:
Expand All @@ -103,7 +90,7 @@ def imported_data_count_by_constituency_by_source(

def imported_data_count_by_constituency_2024(
self, gss: str = None
) -> List[RegionCount]:
) -> List[AreaCount]:
qs = self.get_analytics_queryset()

if gss:
Expand All @@ -124,7 +111,7 @@ def imported_data_count_by_constituency_2024(
.order_by("-count")
)

def imported_data_count_by_council(self) -> List[RegionCount]:
def imported_data_count_by_council(self) -> List[AreaCount]:
return (
self.get_analytics_queryset()
.annotate(
Expand All @@ -136,7 +123,7 @@ def imported_data_count_by_council(self) -> List[RegionCount]:
.order_by("-count")
)

def imported_data_count_by_ward(self) -> List[RegionCount]:
def imported_data_count_by_ward(self) -> List[AreaCount]:
return (
self.get_analytics_queryset()
.annotate(
Expand Down
7 changes: 2 additions & 5 deletions hub/graphql/types/model_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,25 +683,22 @@ class AnalyticalAreaType(Enum):
parliamentary_constituency_2024 = "parliamentary_constituency_2024"
admin_district = "admin_district"
admin_ward = "admin_ward"
european_electoral_region = "european_electoral_region"


postcodeIOKeyAreaTypeLookup = {
AnalyticalAreaType.parliamentary_constituency: "WMC",
AnalyticalAreaType.parliamentary_constituency_2024: "WMC23",
AnalyticalAreaType.admin_district: "DIS",
AnalyticalAreaType.admin_ward: "WD23",
AnalyticalAreaType.european_electoral_region: "EER",
}


@strawberry.interface
class Analytics:
imported_data_count: int = fn_field()

@strawberry_django.field
def imported_data_count_by_region(self) -> List[GroupedDataCount]:
data = self.imported_data_count_by_region()
return [GroupedDataCount(**datum) for datum in data]

@strawberry_django.field
def imported_data_count_by_area(
self, analytical_area_type: AnalyticalAreaType
Expand Down
45 changes: 32 additions & 13 deletions hub/tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def test_aggregate_data_count(self):
query MapReportLayerGeoJSONPoint($sourceId: ID!) {
sharedDataSource(pk: $sourceId) {
id
importedDataCountByRegion {
importedDataCountByArea(
analyticalAreaType: european_electoral_region
) {
gss
count
}
Expand All @@ -122,10 +124,13 @@ def test_aggregate_data_count(self):
)
result = res.json()

print(result)
print(result)

self.assertIsNone(result.get("errors", None))
self.assertEqual(
[{"gss": "XXX", "count": 1}],
result["data"]["sharedDataSource"]["importedDataCountByRegion"],
result["data"]["sharedDataSource"]["importedDataCountByArea"],
)

# Test graphQL query for geojson point
Expand Down Expand Up @@ -241,7 +246,9 @@ def test_aggregate_data_count(self):
query MapReportLayerGeoJSONPoint($sourceId: ID!) {
sharedDataSource(pk: $sourceId) {
id
importedDataCountByRegion {
importedDataCountByArea(
analyticalAreaType: european_electoral_region
) {
gss
count
}
Expand All @@ -262,7 +269,7 @@ def test_aggregate_data_count(self):
self.assertIsNone(result.get("errors", None))
self.assertEqual(
[{"gss": "XXX", "count": 1}],
result["data"]["sharedDataSource"]["importedDataCountByRegion"],
result["data"]["sharedDataSource"]["importedDataCountByArea"],
)

def test_generic_data_visibility(self):
Expand Down Expand Up @@ -376,7 +383,9 @@ def test_aggregate_data_count(self):
query MapReportLayerGeoJSONPoint($sourceId: ID!) {
sharedDataSource(pk: $sourceId) {
id
importedDataCountByRegion {
importedDataCountByArea(
analyticalAreaType: european_electoral_region
) {
gss
count
}
Expand All @@ -397,7 +406,7 @@ def test_aggregate_data_count(self):
self.assertIsNone(result.get("errors", None))
self.assertEqual(
[{"gss": "XXX", "count": 1}],
result["data"]["sharedDataSource"]["importedDataCountByRegion"],
result["data"]["sharedDataSource"]["importedDataCountByArea"],
)

def test_vector_tiles_visibility(self):
Expand Down Expand Up @@ -508,7 +517,9 @@ def test_aggregate_data_count(self):
query MapReportLayerGeoJSONPoint($sourceId: ID!) {
sharedDataSource(pk: $sourceId) {
id
importedDataCountByRegion {
importedDataCountByArea(
analyticalAreaType: european_electoral_region
) {
gss
count
}
Expand All @@ -529,7 +540,7 @@ def test_aggregate_data_count(self):
self.assertIsNone(result.get("errors", None))
self.assertEqual(
[{"gss": "XXX", "count": 1}],
result["data"]["sharedDataSource"]["importedDataCountByRegion"],
result["data"]["sharedDataSource"]["importedDataCountByArea"],
)

def test_vector_tiles_visibility(self):
Expand Down Expand Up @@ -628,7 +639,9 @@ def test_aggregate_data_count(self):
query MapReportLayerGeoJSONPoint($sourceId: ID!) {
sharedDataSource(pk: $sourceId) {
id
importedDataCountByRegion {
importedDataCountByArea(
analyticalAreaType: european_electoral_region
) {
gss
count
}
Expand Down Expand Up @@ -713,7 +726,7 @@ def test_aggregate_data_count(self):
query MapReportLayerGeoJSONPoint($sourceId: ID!) {
sharedDataSource(pk: $sourceId) {
id
importedDataCountByRegion {
importedDataCountByArea {
gss
count
}
Expand Down Expand Up @@ -808,7 +821,9 @@ def test_aggregate_data_count(self):
query MapReportLayerGeoJSONPoint($sourceId: ID!) {
sharedDataSource(pk: $sourceId) {
id
importedDataCountByRegion {
importedDataCountByArea(
analyticalAreaType: european_electoral_region
) {
gss
count
}
Expand Down Expand Up @@ -894,7 +909,9 @@ def test_aggregate_data_count(self):
query MapReportLayerGeoJSONPoint($sourceId: ID!) {
sharedDataSource(pk: $sourceId) {
id
importedDataCountByRegion {
importedDataCountByArea(
analyticalAreaType: european_electoral_region
) {
gss
count
}
Expand Down Expand Up @@ -1019,7 +1036,9 @@ def test_aggregate_data_count(self):
query MapReportLayerGeoJSONPoint($sourceId: ID!) {
sharedDataSource(pk: $sourceId) {
id
importedDataCountByRegion {
importedDataCountByArea(
analyticalAreaType: european_electoral_region
) {
gss
count
}
Expand Down
5 changes: 0 additions & 5 deletions nextjs/src/__generated__/gql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 84e42e0

Please sign in to comment.