From 2e874e57c19bd581866277a544408ff90c7bc6a7 Mon Sep 17 00:00:00 2001 From: Jing Cheng Date: Fri, 6 Dec 2024 12:26:25 -0500 Subject: [PATCH] Having a util function to handle the commcare version the mobile worker is in use --- corehq/apps/enterprise/enterprise.py | 13 +++-------- corehq/apps/reports/standard/deployments.py | 12 +++++----- corehq/apps/reports/util.py | 26 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/corehq/apps/enterprise/enterprise.py b/corehq/apps/enterprise/enterprise.py index 4aa8db1128f2..2a7ce41df164 100644 --- a/corehq/apps/enterprise/enterprise.py +++ b/corehq/apps/enterprise/enterprise.py @@ -7,7 +7,7 @@ from memoized import memoized -from corehq.apps.reports.standard.deployments import format_commcare_version +from corehq.apps.reports.util import get_commcare_version_and_date_from_last_usage from couchforms.analytics import get_last_form_submission_received from dimagi.utils.dates import DateSpan @@ -418,18 +418,11 @@ def rows_for_domain(self, domain_obj): ])) for user in user_query.run().hits: - version_in_use = None - date_of_use = None - last_submission = user.get('reporting_metadata', {}).get('last_submission_for_user', {}) last_device = user.get('last_device', {}) - if last_submission.get('commcare_version'): - version_in_use = format_commcare_version(last_submission['commcare_version']) - date_of_use = last_submission['submission_date'] - elif last_device.get('commcare_version'): - version_in_use = format_commcare_version(last_device['commcare_version']) - date_of_use = last_device['last_used'] + version_in_use, date_of_use = get_commcare_version_and_date_from_last_usage(last_submission, + last_device) latest_version_at_time_of_use = get_latest_version_at_time(date_of_use) diff --git a/corehq/apps/reports/standard/deployments.py b/corehq/apps/reports/standard/deployments.py index c586078445da..9ca7a05d4c48 100644 --- a/corehq/apps/reports/standard/deployments.py +++ b/corehq/apps/reports/standard/deployments.py @@ -46,7 +46,10 @@ ProjectReport, ProjectReportParametersMixin, ) -from corehq.apps.reports.util import format_datatables_data +from corehq.apps.reports.util import ( + format_datatables_data, + get_commcare_version_and_date_from_last_usage, +) from corehq.apps.users.models import CouchUser from corehq.apps.users.util import user_display_string from corehq.const import USER_DATE_FORMAT @@ -352,11 +355,8 @@ def process_rows(self, users, fmt_for_export=False): if last_build.get('app_id') and device and device.get('app_meta'): device_app_meta = self.get_data_for_app(device.get('app_meta'), last_build.get('app_id')) - if last_sub and last_sub.get('commcare_version'): - commcare_version = format_commcare_version(last_sub.get('commcare_version')) - else: - if device and device.get('commcare_version', None): - commcare_version = format_commcare_version(device['commcare_version']) + commcare_version, last_seen = get_commcare_version_and_date_from_last_usage(last_sub, device) + if last_sub and last_sub.get('submission_date'): last_seen = string_to_utc_datetime(last_sub['submission_date']) if last_sync and last_sync.get('sync_date'): diff --git a/corehq/apps/reports/util.py b/corehq/apps/reports/util.py index 2035d6da0bdc..fa5ae7fa8be8 100644 --- a/corehq/apps/reports/util.py +++ b/corehq/apps/reports/util.py @@ -851,3 +851,29 @@ def domain_copied_cases_by_owner(domain, owner_ids): .owner(owner_ids)\ .NOT(case_property_missing(CaseCopier.COMMCARE_CASE_COPY_PROPERTY_NAME))\ .values_list('_id', flat=True) + + +def get_commcare_version_and_date_from_last_usage(last_submission=None, last_device=None): + """ + Gets CommCare version and date from the last submission, or fall back to the last used device. + + Args: + last_submission: Dictionary containing Last Submission data from ES + last_device: Dictionary containing DeviceIdLastUsed data from ES + """ + from corehq.apps.reports.standard.deployments import format_commcare_version + + version_in_use = None + date_of_use = None + + print(last_submission) + print(last_device) + if last_submission and last_submission.get('commcare_version'): + version_in_use = format_commcare_version(last_submission.get('commcare_version')) + date_of_use = last_submission.get('submission_date') + + elif last_device and last_device.get('commcare_version'): + version_in_use = format_commcare_version(last_device.get('commcare_version')) + date_of_use = last_device.get('last_used') + + return version_in_use, date_of_use