Skip to content

Commit

Permalink
median quality score in month
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 authored and Rup-Narayan-Rajbanshi committed Dec 15, 2023
1 parent 3326e5b commit b9ff5ab
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 6 deletions.
158 changes: 157 additions & 1 deletion apps/assessment_registry/dashboard_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.db.models import Count, Sum, Avg, Case, Value, When
from django.contrib.postgres.aggregates.general import ArrayAgg
from django.db import models
from django.db.models.functions import TruncDay
from django.db.models.functions import TruncDay, TruncMonth
from django.db import connection as django_db_connection
from geo.schema import ProjectGeoAreaType

Expand Down Expand Up @@ -395,15 +395,23 @@ class AssessmentDashboardStatisticsType(graphene.ObjectType):
)
median_quality_score_by_geo_area = graphene.List(graphene.NonNull(MedianQualityScoreByGeographicalAreaDateType))
median_quality_score_over_time = graphene.List(graphene.NonNull(MedianQualityScoreOverTimeDateType))
median_quality_score_over_time_by_month = graphene.List(graphene.NonNull(MedianQualityScoreOverTimeDateType))
median_quality_score_of_each_dimension = graphene.List(graphene.NonNull(MedianScoreOfEachDimensionType))
median_quality_score_of_each_dimension_by_date = graphene.List(graphene.NonNull(MedianScoreOfEachDimensionDateType))
median_quality_score_of_each_dimension_by_date_month = graphene.List(
graphene.NonNull(MedianScoreOfEachDimensionDateType))
median_quality_score_of_analytical_density = graphene.List(graphene.NonNull(MedianScoreOfAnalyticalDensityType))
median_quality_score_by_analytical_density_date = graphene.List(graphene.NonNull(MedianScoreOfAnalyticalDensityDateType))
median_quality_score_by_analytical_density_date_month = graphene.List(
graphene.NonNull(MedianScoreOfAnalyticalDensityDateType))
median_quality_score_by_geoarea_and_sector = graphene.List(graphene.NonNull(MedianScoreOfGeographicalAndSectorDateType))
median_quality_score_by_geoarea_and_sector_by_month = graphene.List(
graphene.NonNull(MedianScoreOfGeographicalAndSectorDateType))
median_quality_score_by_geoarea_and_affected_group = graphene.List(
graphene.NonNull(MedianScoreOfGeoAreaAndAffectedGroupDateType)
)
median_score_by_sector_and_affected_group = graphene.List(graphene.NonNull(MedianScoreOfSectorAndAffectedGroup))
median_score_by_sector_and_affected_group_by_month = graphene.List(graphene.NonNull(MedianScoreOfSectorAndAffectedGroup))

@staticmethod
def custom_resolver(root, info, _filter):
Expand Down Expand Up @@ -775,6 +783,7 @@ def resolve_median_quality_score_by_geo_area(root: AssessmentDashboardStat, info
).exclude(final_score__isnull=True)
return score

# per day data of median_quality_score_over_time
@staticmethod
def resolve_median_quality_score_over_time(root: AssessmentDashboardStat, info):
# TODO final score value should be convert into functions
Expand Down Expand Up @@ -808,6 +817,39 @@ def resolve_median_quality_score_over_time(root: AssessmentDashboardStat, info):
).exclude(final_score__isnull=True)
return score

@staticmethod
def resolve_median_quality_score_over_time_by_month(root: AssessmentDashboardStat, info):
# TODO final score value should be convert into functions
score = (
root.assessment_registry_qs.annotate(date=TruncMonth("created_at"))
.annotate(
score_rating_matrix=(
Case(
When(score_ratings__rating=1, then=(Value(0))),
When(score_ratings__rating=2, then=(Value(0.5))),
When(score_ratings__rating=3, then=(Value(1))),
When(score_ratings__rating=4, then=(Value(1.5))),
When(score_ratings__rating=4, then=(Value(2))),
output_field=models.FloatField(),
)
)
)
.values("date")
.order_by()
.annotate(
final_score=(
Avg(
(
models.F("analytical_density__figure_provided__len") *
models.F("analytical_density__analysis_level_covered__len")
) / models.Value(10)
) + Sum(models.F("score_rating_matrix"))
) / Count("id") * 5
)
.values("final_score", "date")
).exclude(final_score__isnull=True)
return score

@staticmethod
def resolve_median_quality_score_of_each_dimension(root: AssessmentDashboardStat, info):
# TODO final score value should be convert into functions
Expand Down Expand Up @@ -852,6 +894,29 @@ def resolve_median_quality_score_of_each_dimension_by_date(root: AssessmentDashb
.exclude(final_score__isnull=True)
)

@staticmethod
def resolve_median_quality_score_of_each_dimension_by_date_month(root: AssessmentDashboardStat, info):
# TODO final score value should be convert into functions
return (
root.assessment_registry_qs.values(date=TruncMonth("created_at"))
.annotate(
score_rating_matrix=(
Case(
When(score_ratings__rating=1, then=(Value(0))),
When(score_ratings__rating=2, then=(Value(0.5))),
When(score_ratings__rating=3, then=(Value(1))),
When(score_ratings__rating=4, then=(Value(1.5))),
When(score_ratings__rating=4, then=(Value(2))),
output_field=models.FloatField(),
)
)
)
.values("date")
.annotate(final_score=Sum("score_rating_matrix"))
.values("final_score", "date", score_type=models.F("score_ratings__score_type"))
.exclude(final_score__isnull=True)
)

@staticmethod
def resolve_median_quality_score_of_analytical_density(root: AssessmentDashboardStat, info):
return (
Expand Down Expand Up @@ -885,6 +950,22 @@ def resolve_median_quality_score_by_analytical_density_date(root: AssessmentDash
.exclude(analytical_density__sector__isnull=True)
)

@staticmethod
def resolve_median_quality_score_by_analytical_density_date_month(root: AssessmentDashboardStat, info):
return (
root.assessment_registry_qs.values(date=TruncMonth("created_at"))
.annotate(
final_score=(
Avg(
models.F("analytical_density__figure_provided__len") *
models.F("analytical_density__analysis_level_covered__len")
)
) / models.Value(10)
)
.values("final_score", "date", sector=models.F("analytical_density__sector"))
.exclude(analytical_density__sector__isnull=True)
)

@staticmethod
def resolve_median_quality_score_by_geoarea_and_sector(root: AssessmentDashboardStat, info):
return (
Expand All @@ -905,6 +986,26 @@ def resolve_median_quality_score_by_geoarea_and_sector(root: AssessmentDashboard
.values("geo_area", "final_score", "sector", "date")
).exclude(analytical_density__sector__isnull=True, analytical_density__analysis_level_covered__isnull=True)

@staticmethod
def resolve_median_quality_score_by_geoarea_and_sector_by_month(root: AssessmentDashboardStat, info):
return (
root.assessment_registry_qs.filter(locations__admin_level__level=1)
.values(
"locations",
date=TruncMonth("created_at"),
)
.annotate(
final_score=(
Avg(
models.F("analytical_density__figure_provided__len") *
models.F("analytical_density__analysis_level_covered__len")
)
) / models.Value(10)
)
.annotate(geo_area=models.F("locations"), sector=models.F("analytical_density__sector"))
.values("geo_area", "final_score", "sector", "date")
).exclude(analytical_density__sector__isnull=True, analytical_density__analysis_level_covered__isnull=True)

@staticmethod
def resolve_median_quality_score_by_geoarea_and_affected_group(root: AssessmentDashboardStat, info):
score = (
Expand Down Expand Up @@ -939,6 +1040,40 @@ def resolve_median_quality_score_by_geoarea_and_affected_group(root: AssessmentD

return score

@staticmethod
def resolve_median_quality_score_by_geoarea_and_affected_group_by_month(root: AssessmentDashboardStat, info):
score = (
root.assessment_registry_qs.filter(locations__admin_level_id=1)
.values("locations", date=TruncMonth("created_at"))
.annotate(
score_rating_matrix=(
Case(
When(score_ratings__rating=1, then=(Value(0))),
When(score_ratings__rating=2, then=(Value(0.5))),
When(score_ratings__rating=3, then=(Value(1))),
When(score_ratings__rating=4, then=(Value(1.5))),
When(score_ratings__rating=5, then=(Value(2))),
output_field=models.FloatField(),
)
)
)
.values("date")
.annotate(
final_score=(
Avg(
(
models.F("analytical_density__figure_provided__len") *
models.F("analytical_density__analysis_level_covered__len")
) / models.Value(10)
) + Sum(models.F("score_rating_matrix"))
) / Count("id") * 5,
)
.annotate(affected_group=models.Func(models.F("affected_groups"), function="unnest"))
.values("final_score", "date", "affected_group", geo_area=models.F("locations"))
).exclude(final_score__isnull=True)

return score

@staticmethod
def resolve_median_score_by_sector_and_affected_group(root: AssessmentDashboardStat, info):
return (
Expand All @@ -960,6 +1095,27 @@ def resolve_median_score_by_sector_and_affected_group(root: AssessmentDashboardS
.values("affected_group", "final_score", "sector", "date")
).exclude(analytical_density__sector__isnull=True, analytical_density__analysis_level_covered__isnull=True)

@staticmethod
def resolve_median_score_by_sector_and_affected_group_by_month(root: AssessmentDashboardStat, info):
return (
root.assessment_registry_qs.values(
date=TruncMonth("created_at"),
)
.annotate(
final_score=(
Avg(
models.F("analytical_density__figure_provided__len") *
models.F("analytical_density__analysis_level_covered__len")
)
) / models.Value(10)
)
.annotate(
affected_group=models.Func(models.F("affected_groups"), function="unnest"),
sector=models.F("analytical_density__sector"),
)
.values("affected_group", "final_score", "sector", "date")
).exclude(analytical_density__sector__isnull=True, analytical_density__analysis_level_covered__isnull=True)


class Query:
assessment_dashboard_statistics = graphene.Field(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def generate_assessments(self, project: Project, leads: typing.List[Lead]):
geo_areas = list(
GeoArea.objects.filter(
admin_level__region__project=project,
admin_level__level__in=[1,2],
admin_level__level__in=[1, 2],
).annotate(
region_id=models.F('admin_level__region'),
).only('id')
Expand Down
3 changes: 1 addition & 2 deletions apps/assessment_registry/tests/test_dashboard_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ def _query_check(filter=None, **kwargs):

self.force_login(self.member_user)
content = _query_check(filter)["data"]["project"]["assessmentDashboardStatistics"]
print("...........",content)
# assessment dashboard tab 1
self.assertEqual(content["totalAssessment"], 1)
self.assertEqual(content["totalCollectionTechnique"], 2)
Expand Down Expand Up @@ -369,4 +368,4 @@ def _query_check(filter=None, **kwargs):
self.assertEqual(content['medianQualityScoreByAnalyticalDensityDate'][0]['sectorDisplay'], "Food Security")
self.assertEqual(content['medianQualityScoreByAnalyticalDensityDate'][0]['date'], str(date.today()))
self.assertEqual(content['medianQualityScoreByAnalyticalDensityDate'][1]['finalScore'], 0.0)
self.assertEqual(content['medianQualityScoreByGeoArea'][0]['geoArea'],self.geo_area1)
self.assertEqual(content['medianQualityScoreByGeoArea'][0]['geoArea'], self.geo_area1)
3 changes: 1 addition & 2 deletions apps/geo/dataloaders.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from collections import defaultdict
from graphene.types import context

from promise import Promise
from django.utils.functional import cached_property
Expand Down Expand Up @@ -35,7 +34,7 @@ def batch_load_fn(self, keys):

class GeoAreaLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
geo_area_qs = GeoArea.objects.filter(id__in=keys).defer('polygons','centroid','cached_data')
geo_area_qs = GeoArea.objects.filter(id__in=keys).defer('polygons', 'centroid', 'cached_data')
_map = defaultdict()
for geo_area in geo_area_qs:
_map[geo_area.id] = geo_area
Expand Down

0 comments on commit b9ff5ab

Please sign in to comment.