Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions django/evaluate_m2/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ def no_previous_bankruptcy_indicators(self):
)

def with_hit_count(self, event):
from evaluate_m2.models import EvaluatorResult
from evaluate_m2.models import EvaluatorResultMaterializedView

# Subquery for the number of evaluators hit for
# Subquery for the number of evaluators hits for
# each account
subquery = (
EvaluatorResult.objects.filter(
result_summary__event=event,
acct_num=OuterRef("cons_acct_num"),
EvaluatorResultMaterializedView.objects.filter(
event_id=event.id,
cons_acct_num=OuterRef("cons_acct_num"),
)
.order_by()
.values("acct_num")
.values("cons_acct_num")
.annotate(
n=Count("id"),
)
Expand All @@ -41,10 +40,9 @@ def with_hit_count(self, event):
def with_record_count(self, event):
subquery = (
self.model._default_manager.filter(
data_file__event=event,
event=event,
cons_acct_num=OuterRef("cons_acct_num"),
)
.order_by()
.values("cons_acct_num")
.annotate(
n=Count("id"),
Expand Down
7 changes: 6 additions & 1 deletion django/evaluate_m2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,12 @@ def materialized_view_exists(cls) -> bool:
CREATE INDEX idx_event_id_evaluator_id
ON mv_all_evaluator_results (event_id, evaluator_id);
""",
# Index on sample results (to improve sample view performance)
# Index on results by consumer account (for acct search performance)
"""
CREATE INDEX idx_event_id_cons_acct_num
ON mv_all_evaluator_results (event_id, cons_acct_num);
""",
# Index on sample results (for sample view performance)
"""
CREATE INDEX idx_mv_eval_results_sample
ON mv_all_evaluator_results (event_id, evaluator_id, sample);
Expand Down
3 changes: 3 additions & 0 deletions django/evaluate_m2/tests/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.test import TestCase

from evaluate_m2.models import EvaluatorResultMaterializedView
from evaluate_m2.tests.evaluator_test_helper import acct_record, evaluator_result_record
from parse_m2.models import AccountActivity, M2DataFile, Metro2Event

Expand Down Expand Up @@ -158,6 +159,7 @@ def test_inconsistency_counts(self):
)
evaluator_result_record(self.event, "Sample-1", acct)
evaluator_result_record(self.event, "Sample-2", acct)
EvaluatorResultMaterializedView.create_or_refresh_materialized_view()

qs = AccountActivity.objects.with_hit_count(self.event)
query_acct = qs.filter(cons_acct_num=acct.cons_acct_num).first()
Expand All @@ -183,6 +185,7 @@ def test_inconsistency_counts_same_eval(self):
# One hit across two months
evaluator_result_record(self.event, "Sample-1", acct1)
evaluator_result_record(self.event, "Sample-1", acct2)
EvaluatorResultMaterializedView.create_or_refresh_materialized_view()

qs = AccountActivity.objects.with_hit_count(self.event)
query_acct = qs.filter(cons_acct_num=acct1.cons_acct_num).first()
Expand Down
1 change: 1 addition & 0 deletions django/evaluate_m2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ def event(self):
def get_queryset(self):
return (
self.event.get_all_account_activity()
.only('cons_acct_num', 'port_type', 'acct_type')
.with_hit_count(self.event)
.with_record_count(self.event)
)
Expand Down
Loading