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

Simplify Enterprise OData Report #35520

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 10 additions & 10 deletions corehq/apps/enterprise/api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ class DomainResource(ODataEnterpriseReportResource):
num_web_users = fields.IntegerField()
num_sms_last_30_days = fields.IntegerField()
last_form_submission = fields.DateTimeField()
num_odata_feeds_used = fields.IntegerField(null=True)
num_odata_feeds_available = fields.IntegerField(null=True)

REPORT_SLUG = EnterpriseReport.DOMAINS

Expand All @@ -230,6 +232,8 @@ def dehydrate(self, bundle):
bundle.data['num_web_users'] = bundle.obj[3]
bundle.data['num_sms_last_30_days'] = bundle.obj[4]
bundle.data['last_form_submission'] = self.convert_datetime(bundle.obj[5])
bundle.data['num_odata_feeds_used'] = bundle.obj[6]
bundle.data['num_odata_feeds_available'] = bundle.obj[7]

return bundle

Expand Down Expand Up @@ -365,10 +369,8 @@ class ODataFeedResource(ODataEnterpriseReportResource):
Currently includes summary rows as well as individual reports
'''

domain = fields.CharField(null=True)
num_feeds_used = fields.IntegerField(null=True)
num_feeds_available = fields.IntegerField(null=True)
report_name = fields.CharField(null=True)
domain = fields.CharField()
report_name = fields.CharField()
report_rows = fields.IntegerField(null=True)

REPORT_SLUG = EnterpriseReport.ODATA_FEEDS
Expand All @@ -382,16 +384,14 @@ def get_report_task(self, request):
)

def dehydrate(self, bundle):
bundle.data['num_feeds_used'] = bundle.obj[0]
bundle.data['num_feeds_available'] = bundle.obj[1]
bundle.data['report_name'] = bundle.obj[2]
bundle.data['report_rows'] = bundle.obj[3]
bundle.data['domain'] = bundle.obj[5] if len(bundle.obj) >= 5 else None
bundle.data['domain'] = bundle.obj[0]
bundle.data['report_name'] = bundle.obj[1]
bundle.data['report_rows'] = bundle.obj[2]

return bundle

def get_primary_keys(self):
return ('report_name',) # very odd report that makes coming up with an actual key challenging
return ('domain', 'report_name',)


class FormSubmissionResource(ODataEnterpriseReportResource):
Expand Down
58 changes: 18 additions & 40 deletions corehq/apps/enterprise/enterprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,16 @@ class EnterpriseDomainReport(EnterpriseReport):
title = gettext_lazy('Project Spaces')
total_description = gettext_lazy('# of Project Spaces')

def __init__(self, account, couch_user):
super().__init__(account, couch_user)
self.export_fetcher = ODataExportFetcher()

@property
def headers(self):
headers = super().headers
return [_('Created On [UTC]'), _('# of Apps'), _('# of Mobile Users'), _('# of Web Users'),
_('# of SMS (last 30 days)'), _('Last Form Submission [UTC]')] + headers
_('# of SMS (last 30 days)'), _('Last Form Submission [UTC]'),
_('OData Feeds Used'), _('OData Feeds Available')] + headers

def rows_for_domain(self, domain_obj):
return [[
Expand All @@ -144,6 +149,8 @@ def rows_for_domain(self, domain_obj):
get_web_user_count(domain_obj.name, include_inactive=False),
sms_in_last(domain_obj.name, 30),
self.format_date(get_last_form_submission_received(domain_obj.name)),
self.export_fetcher.get_export_count(domain_obj.name),
domain_obj.get_odata_feed_limit(),
] + self.domain_properties(domain_obj)]

def total_for_domain(self, domain_obj):
Expand Down Expand Up @@ -351,56 +358,27 @@ def __init__(self, account, couch_user):

@property
def headers(self):
headers = super().headers
return [_('Odata feeds used'), _('Odata feeds available'), _('Report Names'),
_('Number of rows')] + headers
return [_('Project Space'), _('Name'), _('Number of Rows')]

def total_for_domain(self, domain_obj):
return self.export_fetcher.get_export_count(domain_obj.name)

def rows_for_domain(self, domain_obj):
export_count = self.total_for_domain(domain_obj)
if export_count == 0 or export_count > self.MAXIMUM_EXPECTED_EXPORTS:
return [self._get_domain_summary_line(domain_obj, export_count)]

exports = self.export_fetcher.get_exports(domain_obj.name)

export_line_counts = self._get_export_line_counts(exports)

domain_summary_line = self._get_domain_summary_line(domain_obj, export_count, export_line_counts)
individual_export_rows = self._get_individual_export_rows(exports, export_line_counts)

rows = [domain_summary_line]
rows.extend(individual_export_rows)
return rows

def _get_export_line_counts(self, exports):
return {export._id: export.get_count() for export in exports}

def _get_domain_summary_line(self, domain_obj, export_count, export_line_counts={}):
if export_count > self.MAXIMUM_EXPECTED_EXPORTS:
total_line_count = _('ERROR: Too many exports. Please contact customer service')
else:
total_line_count = sum(export_line_counts.values())

return [
export_count,
domain_obj.get_odata_feed_limit(),
None, # Report Name
total_line_count
] + self.domain_properties(domain_obj)
return [
[
domain_obj.name,
_('ERROR: Too many exports. Please contact customer service'),
None,
]
]

def _get_individual_export_rows(self, exports, export_line_counts):
exports = self.export_fetcher.get_exports(domain_obj.name)
rows = []

for export in exports:
count = export_line_counts[export._id]
rows.append([
None, # OData feeds used
None, # OData feeds available
export.name,
count]
)
rows.append([domain_obj.name, export.name, export.get_count()])

return rows

Expand Down
27 changes: 10 additions & 17 deletions corehq/apps/enterprise/tests/test_enterprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class EnterpriseODataReportTests(SimpleTestCase):
def test_headers(self):
report = self._create_report_for_domains()
self.assertEqual(report.headers, [
'Odata feeds used', 'Odata feeds available', 'Report Names', 'Number of rows',
'Project Space Name', 'Project Name', 'Project URL'
'Project Space', 'Name', 'Number of Rows',
])

def test_total_number_display_odata_reports_across_enterprise(self):
Expand Down Expand Up @@ -48,9 +47,8 @@ def test_full_report_for_single_domain(self):

report = self._create_report_for_domains(domain_one)
self.assertEqual(report.rows, [
[2, 10, None, 12, 'domain_one', None, 'http://localhost:8000/a/domain_one/settings/project/'],
[None, None, 'ExportOne', 5],
[None, None, 'ExportTwo', 7]
['domain_one', 'ExportOne', 5],
['domain_one', 'ExportTwo', 7]
])

def test_full_report_for_multiple_domains(self):
Expand All @@ -63,35 +61,30 @@ def test_full_report_for_multiple_domains(self):
report = self._create_report_for_domains(domain_one, domain_two)

self.assertEqual(report.rows, [
[1, 25, None, 9, 'domain_one', None, 'http://localhost:8000/a/domain_one/settings/project/'],
[None, None, 'ExportOne', 9],
[1, 50, None, 15, 'domain_two', None, 'http://localhost:8000/a/domain_two/settings/project/'],
[None, None, 'ExportTwo', 15]
['domain_one', 'ExportOne', 9],
['domain_two', 'ExportTwo', 15]
])

@patch.object(ODataExportFetcher, 'get_export_count')
def test_report_replaces_row_count_with_error_when_too_many_exports(self, mock_export_count):
def test_report_includes_error_when_too_many_exports(self, mock_export_count):
mock_export_count.return_value = 151
domain_one = self._create_domain(name='domain_one', max_exports=200)

report = self._create_report_for_domains(domain_one)

self.assertEqual(report.rows, [
[151, 200, None,
'ERROR: Too many exports. Please contact customer service',
'domain_one', None, 'http://localhost:8000/a/domain_one/settings/project/']
['domain_one', 'ERROR: Too many exports. Please contact customer service', None]
])

@patch.object(ODataExportFetcher, 'get_export_count')
def test_no_exports_for_domain_shows_0_rowcount(self, mock_export_count):
def test_no_exports_for_domain_includes_no_rows(self, mock_export_count):
mock_export_count.return_value = 0
domain_one = self._create_domain(name='domain_one', max_exports=25)
self.domain_to_export_map['domain_one'] = []

report = self._create_report_for_domains(domain_one)

self.assertEqual(report.rows, [
[0, 25, None, 0, 'domain_one', None, 'http://localhost:8000/a/domain_one/settings/project/']
])
self.assertEqual(report.rows, [])

# setup / helpers

Expand Down
Loading