Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture missing payments on the dashboard #4430

Merged
merged 3 commits into from
Nov 13, 2024
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
77 changes: 43 additions & 34 deletions src/hct_mis_api/apps/dashboard/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from typing import Any, Dict, Optional

from django.core.cache import cache
from django.db.models import Count, F, Sum
from django.db.models.functions import ExtractMonth, ExtractYear
from django.db.models import Count, F, Sum, Value
from django.db.models.functions import Coalesce, ExtractMonth, ExtractYear

from rest_framework.utils.serializer_helpers import ReturnDict

Expand All @@ -16,16 +16,16 @@
CACHE_TIMEOUT = 60 * 60 * 24 # 24 hours

pwdSum = Sum(
F("household__female_age_group_0_5_disabled_count")
+ F("household__female_age_group_6_11_disabled_count")
+ F("household__female_age_group_12_17_disabled_count")
+ F("household__female_age_group_18_59_disabled_count")
+ F("household__female_age_group_60_disabled_count")
+ F("household__male_age_group_0_5_disabled_count")
+ F("household__male_age_group_6_11_disabled_count")
+ F("household__male_age_group_12_17_disabled_count")
+ F("household__male_age_group_18_59_disabled_count")
+ F("household__male_age_group_60_disabled_count"),
Coalesce(F("household__female_age_group_0_5_disabled_count"), 0)
+ Coalesce(F("household__female_age_group_6_11_disabled_count"), 0)
+ Coalesce(F("household__female_age_group_12_17_disabled_count"), 0)
+ Coalesce(F("household__female_age_group_18_59_disabled_count"), 0)
+ Coalesce(F("household__female_age_group_60_disabled_count"), 0)
+ Coalesce(F("household__male_age_group_0_5_disabled_count"), 0)
+ Coalesce(F("household__male_age_group_6_11_disabled_count"), 0)
+ Coalesce(F("household__male_age_group_12_17_disabled_count"), 0)
+ Coalesce(F("household__male_age_group_18_59_disabled_count"), 0)
+ Coalesce(F("household__male_age_group_60_disabled_count"), 0),
default=0,
)

Expand Down Expand Up @@ -73,16 +73,23 @@ def refresh_data(cls, business_area_slug: str) -> ReturnDict:
for area in list_country:
payments_aggregated = (
Payment.objects.using("read_only")
.select_related("business_area", "household", "program")
.filter(business_area=area)
.select_related(
"business_area",
"household",
"program",
"household__admin1",
"financial_service_provider",
"delivery_type",
)
.filter(business_area=area, household__is_removed=False)
.annotate(
month=ExtractMonth("delivery_date"),
year=ExtractYear("delivery_date"),
programs=F("household__program__name"),
sectors=F("household__program__sector"),
admin1=F("household__admin1__name"),
fsp=F("financial_service_provider__name"),
delivery_types=F("delivery_type__name"),
year=ExtractYear(Coalesce("delivery_date", "entitlement_date", "status_date")),
month=ExtractMonth(Coalesce("delivery_date", "entitlement_date", "status_date")),
programs=Coalesce(F("household__program__name"), Value("Unknown program")),
sectors=Coalesce(F("household__program__sector"), Value("Unknown sector")),
admin1=Coalesce(F("household__admin1__name"), Value("Unknown admin1")),
fsp=Coalesce(F("financial_service_provider__name"), Value("Unknown fsp")),
delivery_types=Coalesce(F("delivery_type__name"), F("delivery_type_choice")),
)
.distinct()
.values(
Expand All @@ -96,8 +103,8 @@ def refresh_data(cls, business_area_slug: str) -> ReturnDict:
"delivery_types",
)
.annotate(
total_usd=Sum("delivered_quantity_usd"),
total_quantity=Sum("delivered_quantity"),
total_usd=Sum("delivered_quantity_usd", default=0),
total_quantity=Sum("delivered_quantity", default=0),
total_payments=Count("id", distinct=True),
individuals=Sum("household__size"),
households=Count("household", distinct=True),
Expand All @@ -108,16 +115,18 @@ def refresh_data(cls, business_area_slug: str) -> ReturnDict:

payment_records_aggregated = (
PaymentRecord.objects.using("read_only")
.select_related("business_area", "household", "program")
.filter(business_area=area)
.select_related(
"business_area", "household", "program", "household__admin1", "service_provider", "delivery_type"
)
.filter(business_area=area, household__is_removed=False)
.annotate(
month=ExtractMonth("delivery_date"),
year=ExtractYear("delivery_date"),
programs=F("household__program__name"),
sectors=F("household__program__sector"),
admin1=F("household__admin1__name"),
fsp=F("service_provider__short_name"),
delivery_types=F("delivery_type__name"),
year=ExtractYear(Coalesce("delivery_date", "status_date")),
month=ExtractMonth(Coalesce("delivery_date", "status_date")),
programs=Coalesce(F("household__program__name"), Value("Unknown program")),
sectors=Coalesce(F("household__program__sector"), Value("Unknown sector")),
admin1=Coalesce(F("household__admin1__name"), Value("Unknown admin1")),
fsp=Coalesce(F("service_provider__short_name"), Value("Unknown fsp")),
delivery_types=Coalesce(F("delivery_type__name"), F("delivery_type_choice")),
)
.distinct()
.values(
Expand All @@ -131,8 +140,8 @@ def refresh_data(cls, business_area_slug: str) -> ReturnDict:
"delivery_types",
)
.annotate(
total_usd=Sum("delivered_quantity_usd"),
total_quantity=Sum("delivered_quantity"),
total_usd=Sum("delivered_quantity_usd", default=0),
total_quantity=Sum("delivered_quantity", default=0),
total_payments=Count("id", distinct=True),
individuals=Sum("household__size"),
households=Count("household", distinct=True),
Expand Down
20 changes: 11 additions & 9 deletions src/hct_mis_api/apps/dashboard/templates/dashboard/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ <h3 class="text-lg font-semibold">
const deliveryGroup = deliveryDim.group().reduceSum(d => d.delivered_quantity_usd);
const sectorGroup = sectorDim.group().reduceSum(d => d.delivered_quantity_usd);
const volumeGroup = volumeDim.group().reduceSum(d => d.delivered_quantity_usd);
const admin1Group = admin1Dim.group().reduceSum(d => d.delivered_quantity_usd);
const admin1Group = admin1Dim.group().reduceSum(d => d.delivered_quantity_usd).order((d) => -d);
const topAdmin1Group = {
all: () => admin1Group.all().slice(0, 20) // Top 20 admin1
};
const monthGroup = monthDim.group().reduceSum(d => d.delivered_quantity_usd);

// Define charts
Expand Down Expand Up @@ -286,15 +289,14 @@ <h3 class="text-lg font-semibold">
.title(d => `${d.key}: ${numberFormatter(d.value)} USD`)
.ordering(d => monthOrder[d.key]);

const admin1Chart = dc.pieChart("#payments-by-admin1")
const admin1Chart = dc.rowChart("#payments-by-admin1")
.dimension(admin1Dim)
.group(admin1Group)
.radius(280)
.innerRadius(40)
.renderLabel(true)
.useViewBoxResizing(true)
.label(d => `${d.key} ${(d.value / admin1Group.all().reduce((sum, g) => sum + g.value, 0) * 100).toFixed(0)}%`)
.title(d => `${d.key}: ${numberFormatter(d.value)} USD`);
.group(topAdmin1Group)
.elasticX(true)
.height(400)
.label(d => `${d.key}: ${numberFormatter(d.value)} USD (${(d.value / admin1Group.all().reduce((sum, g) => sum + g.value, 0) * 100).toFixed(0)}%)`)
.title(d => `${d.key}: ${numberFormatter(d.value)} USD`)
.xAxis().ticks(5);


function updateTopMetrics() {
Expand Down
Loading