Skip to content

Commit

Permalink
metabase: Fix members count in company table
Browse files Browse the repository at this point in the history
  • Loading branch information
tonial committed Sep 3, 2024
1 parent 29149a1 commit e2c1c34
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
27 changes: 21 additions & 6 deletions itou/metabase/management/commands/populate_metabase_emplois.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import tenacity
from django.contrib.postgres.aggregates import ArrayAgg
from django.db.models import Count, F, Max, Min, Prefetch, Q
from django.db.models import Count, F, Max, Prefetch, Q
from django.utils import timezone

from itou.analytics.models import Datum, StatsDashboardVisit
Expand Down Expand Up @@ -131,15 +131,22 @@ def populate_companies(self):
queryset = (
Company.objects.active()
.select_related("convention")
.prefetch_related("convention__siaes", "job_description_through", "members")
.prefetch_related(
"convention__siaes",
"job_description_through",
"members",
Prefetch(
"companymembership_set",
queryset=CompanyMembership.objects.active(),
to_attr="active_memberships",
),
"companymembership_set",
)
.annotate(
last_job_application_transition_date=Max(
"job_applications_received__logs__timestamp",
filter=~Q(job_applications_received__logs__to_state=JobApplicationState.OBSOLETE),
),
first_membership_join_date=Min(
"companymembership__joined_at",
),
total_candidatures=Count(
"job_applications_received",
distinct=True,
Expand Down Expand Up @@ -247,7 +254,15 @@ def populate_organizations(self):
filter=active_user_created_job_applications_filter,
)
queryset = (
PrescriberOrganization.objects.prefetch_related("prescribermembership_set", "members")
PrescriberOrganization.objects.prefetch_related(
Prefetch(
"prescribermembership_set",
queryset=PrescriberMembership.objects.active(),
to_attr="active_memberships",
),
"members",
"prescribermembership_set",
)
.annotate(
job_applications_count=job_applications_count,
accepted_job_applications_count=accepted_job_applications_count,
Expand Down
5 changes: 3 additions & 2 deletions itou/metabase/tables/companies.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
get_column_from_field,
get_establishment_is_active_column,
get_establishment_last_login_date_column,
get_first_membership_join_date,
)


Expand Down Expand Up @@ -83,13 +84,13 @@ def get_parent_company(company):
"name": "date_inscription",
"type": "date",
"comment": "Date inscription du premier compte employeur",
"fn": lambda o: o.first_membership_join_date,
"fn": lambda o: get_first_membership_join_date(o.companymembership_set),
},
{
"name": "total_membres",
"type": "integer",
"comment": "Nombre de comptes employeur rattachés à la structure",
"fn": lambda o: o.members.count(),
"fn": lambda o: len(o.active_memberships),
},
{
"name": "total_candidatures",
Expand Down
2 changes: 1 addition & 1 deletion itou/metabase/tables/organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_org_members_count(org):
if org == ORG_OF_PRESCRIBERS_WITHOUT_ORG:
# Number of prescriber users without org.
return User.objects.filter(kind=UserKind.PRESCRIBER, prescribermembership=None).count()
return org.members.count()
return len(org.active_memberships)


def _get_ja_sent_by_prescribers_without_org():
Expand Down
4 changes: 1 addition & 3 deletions itou/metabase/tables/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ def get_first_membership_join_date(memberships):
memberships = list(memberships.all())
# We have to do all this in python to benefit from prefetch_related.
if len(memberships) >= 1:
memberships.sort(key=lambda o: o.joined_at)
first_membership = memberships[0]
return first_membership.joined_at
return min(m.joined_at for m in memberships)
return None


Expand Down
17 changes: 14 additions & 3 deletions tests/metabase/management/test_populate_metabase_emplois.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,10 @@ def test_populate_companies():
with_jobs=True,
coords="POINT (5.43567 12.123876)",
)
# Add an inactive membership, and a active membership on an inactive user
# both should be ignored in total_members agregation
CompanyMembershipFactory(company=company, is_active=False)
CompanyMembershipFactory(company=company, user__is_active=False)

num_queries = 1 # Count Siaes
num_queries += 1 # COMMIT Queryset counts (autocommit mode)
Expand All @@ -1144,7 +1148,9 @@ def test_populate_companies():
num_queries += 1 # Select siaes with annotations and columns
num_queries += 1 # Select other siaes with the same convention
num_queries += 1 # Prefetch siae job descriptions
num_queries += 1 # Prefecth siae memberships
num_queries += 1 # Prefecth siae active memberships (for total_membres)
num_queries += 1 # Prefecth siae memberships (for first_membership_join_date)
num_queries += 1 # Prefecth siae members (for members last_login)
num_queries += 1 # Prefetch cities
num_queries += 1 # COMMIT (inject_chunk)
num_queries += 1 # COMMIT (rename_table_atomically DROP TABLE)
Expand Down Expand Up @@ -1320,6 +1326,10 @@ def test_populate_organizations():
authorized=True,
post_code="63020",
)
# Add an inactive membership, and a active membership on an inactive user
# both should be ignored in total_members agregation
PrescriberMembershipFactory(organization=first_organisation, is_active=False)
PrescriberMembershipFactory(organization=first_organisation, user__is_active=False)

num_queries = 1 # Select get_active_companies_pks()
num_queries += 1 # Count organization
Expand All @@ -1333,8 +1343,9 @@ def test_populate_organizations():
num_queries += 1 # Select organization IDs
num_queries += 1 # Select one chunk of organization IDs
num_queries += 1 # Select organization with columns
num_queries += 1 # Select prefetch organizations memberships
num_queries += 1 # Select prefetch organizations members
num_queries += 1 # Select prefetch organizations active memberships (for total_membres)
num_queries += 1 # Select prefetch organizations memberships (for first_membership_join_date)
num_queries += 1 # Select prefetch organizations members (for members last_login)
num_queries += 1 # COMMIT (inject_chunk)
num_queries += 1 # COMMIT (rename_table_atomically DROP TABLE)
num_queries += 1 # COMMIT (rename_table_atomically RENAME TABLE)
Expand Down

0 comments on commit e2c1c34

Please sign in to comment.