Skip to content

Commit

Permalink
Having a util function to handle the commcare version the mobile work…
Browse files Browse the repository at this point in the history
…er is in use
  • Loading branch information
jingcheng16 committed Dec 6, 2024
1 parent 1700c2f commit 2e874e5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
13 changes: 3 additions & 10 deletions corehq/apps/enterprise/enterprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions corehq/apps/reports/standard/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'):
Expand Down
26 changes: 26 additions & 0 deletions corehq/apps/reports/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2e874e5

Please sign in to comment.