def by_domain(cls, domain, stale_query=False): key = [domain] stale_kwargs = {} if stale_query: stale_kwargs['stale'] = stale_ok() if cls.__name__ in get_all_repeater_types(): key.append(cls.__name__) elif cls.__name__ == Repeater.__name__: # In this case the wrap function delegates to the # appropriate sub-repeater types. pass else: # Any repeater type can be posted to the API, and the installed apps # determine whether we actually know about it. # But if we do not know about it, then may as well return nothing now return [] raw_docs = cls.view('repeaters/repeaters', startkey=key, endkey=key + [{}], include_docs=True, reduce=False, wrap_doc=False, **stale_kwargs) return [ cls.wrap(repeater_doc['doc']) for repeater_doc in raw_docs if cls.get_class_from_doc_type(repeater_doc['doc']['doc_type']) ]
def get_exports_by_form(domain): from corehq.apps.app_manager.models import Application rows = Application.get_db().view('exports_forms_by_app/view', startkey=[domain], endkey=[domain, {}], group=True, stale=stale_ok()).all() form_count_breakdown = get_form_count_breakdown_for_domain(domain) for row in rows: key = tuple(row['key']) if key in form_count_breakdown: row['value']['submissions'] = form_count_breakdown.pop(key) for key, value in form_count_breakdown.items(): rows.append({ 'key': list(key), 'value': { 'xmlns': key[2], 'submissions': value } }) rows.sort(key=lambda row: row['key']) return rows
def users_have_locations(domain): from corehq.apps.users.models import CouchUser return bool(CouchUser.get_db().view( 'users_extra/users_by_location_id', startkey=[domain], endkey=[domain, {}], reduce=True, stale=stale_ok(), ).one())
def get_exports_by_application(domain): from .models import Application return Application.get_db().view( 'exports_forms_by_app/view', startkey=[domain, {}], endkey=[domain, {}, {}], reduce=False, stale=stale_ok(), ).all()
def get_number_of_forms_in_domain(domain): from corehq.apps.reports.util import make_form_couch_key key = make_form_couch_key(domain) row = XFormInstance.get_db().view( "reports_forms/all_forms", startkey=key, endkey=key + [{}], stale=stale_ok(), ).one() return row["value"] if row else 0
def _get_count_of_commcare_users_in_domain(active_flag, domain): result = CommCareUser.get_db().view( 'users/by_domain', startkey=[active_flag, domain, 'CommCareUser'], endkey=[active_flag, domain, 'CommCareUser', {}], group=True, group_level=2, stale=stale_ok(), ).one() return result['value'] if result else 0
def get_form_details_for_xmlns(domain, xmlns): startkey = ["xmlns", domain, xmlns] return [ _row_to_form_details(row) for row in Application.get_db().view('forms_by_app_info/view', startkey=startkey, endkey=startkey + [{}], reduce=False, stale=stale_ok(), ).all() ]
def get_number_of_forms_in_domain(domain): from corehq.apps.reports.util import make_form_couch_key key = make_form_couch_key(domain) row = XFormInstance.get_db().view( "all_forms/view", startkey=key, endkey=key + [{}], stale=stale_ok(), ).one() return row["value"] if row else 0
def get_form_details_for_app_and_module(domain, app_id, module_id, deleted=False): status = 'deleted' if deleted else 'active' startkey = ["status app module form", domain, status, app_id, module_id] return [ _row_to_form_details(row) for row in Application.get_db().view('forms_by_app_info/view', startkey=startkey, endkey=startkey + [{}], reduce=False, stale=stale_ok(), ).all() ]
def get_user_id_by_username(username): result = CouchUser.view( 'users/by_username', key=username, include_docs=False, reduce=False, stale=stale_ok(), ) row = result.one() if row: return row["id"] return None
def get_last_form_submission_by_xmlns(domain, xmlns): from corehq.apps.reports.util import make_form_couch_key key = make_form_couch_key(domain, xmlns=xmlns) return XFormInstance.view( "all_forms/view", reduce=False, endkey=key, startkey=key + [{}], descending=True, limit=1, include_docs=True, stale=stale_ok(), ).first()
def get_number_of_forms_per_domain(): key = ['submission'] return { row["key"][1]: row["value"] for row in XFormInstance.get_db().view( "all_forms/view", group=True, group_level=2, startkey=key, endkey=key + [{}], stale=stale_ok(), ).all() }
def get_sync_logs_for_user(user_id, limit): rows = synclog_view( "phone/sync_logs_by_user", startkey=[user_id, {}], endkey=[user_id], descending=True, reduce=False, limit=limit, include_docs=True, stale=stale_ok() ) sync_log_jsons = (row['doc'] for row in rows) return [properly_wrap_sync_log(sync_log_json) for sync_log_json in sync_log_jsons]
def _get_commcare_users_in_domain(active_flag, domain, start_at, limit): extra_args = {} if start_at != 0: extra_args['skip'] = start_at if limit is not None: extra_args['limit'] = limit return CommCareUser.view("users/by_domain", reduce=False, include_docs=True, startkey=[active_flag, domain, 'CommCareUser'], endkey=[active_flag, domain, 'CommCareUser', {}], stale=stale_ok(), **extra_args).all()
def get_number_of_forms_by_type(domain, type_): # todo: this is only used to display totals in the "raw forms" report and can probably be removed assert type_ in doc_types() startkey = [domain, type_] endkey = startkey + [{}] submissions = XFormInstance.view( "couchforms/all_submissions_by_domain", startkey=startkey, endkey=endkey, reduce=True, stale=stale_ok(), ).one() return submissions['value'] if submissions else 0
def get_form_analytics_metadata(domain, app_id, xmlns): """ Returns metadata about the form, or None if no info found. Here is an example structure: { "xmlns": "http://openrosa.org/formdesigner/5D563904-4038-4070-A0D4-CC421003E862", "form": { "name": { "en": "Brogramming Project", "es": "Projecto de brogramming" }, "id": 1 }, "app": { "langs": [ "es", "en", "fra" ], "name": "brogrammino", "id": "10257bd886ba423eea19a562e95cec07" }, "module": { "name": { "en": "Dimagi", "es": "Brogramminos" }, "id": 0 }, "app_deleted": false, "submissions": 15 } """ # todo: wrap this return value in a class/stucture from corehq.apps.app_manager.models import Application view_results = Application.get_db().view( 'exports_forms_by_app/view', key=[domain, app_id, xmlns], stale=stale_ok(), group=True ).one() form_count = get_form_count_for_domain_app_xmlns(domain, app_id, xmlns) if view_results: result = view_results['value'] result['submissions'] = form_count return result elif form_count: return {'xmlns': xmlns, 'submissions': form_count} else: return None
def _get_commcare_users_in_domain(active_flag, domain, start_at, limit): extra_args = {} if start_at != 0: extra_args['skip'] = start_at if limit is not None: extra_args['limit'] = limit return CommCareUser.view("users/by_domain", reduce=False, include_docs=True, startkey=[active_flag, domain, 'CommCareUser'], endkey=[active_flag, domain, 'CommCareUser', {}], stale=stale_ok(), **extra_args ).all()
def get_number_of_forms_per_domain(): from corehq.apps.reports.util import make_form_couch_key key = make_form_couch_key(None) return { row["key"][1]: row["value"] for row in XFormInstance.get_db().view( "reports_forms/all_forms", group=True, group_level=2, startkey=key, endkey=key + [{}], stale=stale_ok(), ).all() }
def get_all_form_definitions_grouped_by_app_and_xmlns(domain): def _row_to_form_info(row): return SimpleFormInfo(app_id=row['key'][3], xmlns=row['key'][2]) startkey = ["xmlns app", domain] return [ _row_to_form_info(r) for r in Application.get_db().view( 'forms_by_app_info/view', startkey=startkey, endkey=startkey + [{}], group=True, stale=stale_ok(), ).all() ]
def get_number_of_submissions(domain, user_id, xmlns, app_id, start, end, by_submission_time=True): from corehq.apps.reports.util import make_form_couch_key key = make_form_couch_key(domain, user_id=user_id, xmlns=xmlns, by_submission_time=by_submission_time, app_id=app_id) data = XFormInstance.get_db().view( 'reports_forms/all_forms', reduce=True, startkey=key + [json_format_datetime(start)], endkey=key + [json_format_datetime(end)], stale=stale_ok(), ).first() return data['value'] if data else 0
def app_has_been_submitted_to_in_last_30_days(domain, app_id): now = datetime.datetime.utcnow() _30_days = datetime.timedelta(days=30) then = json_format_datetime(now - _30_days) now = json_format_datetime(now) key = ['submission app', domain, app_id] row = XFormInstance.get_db().view( "reports_forms/all_forms", startkey=key + [then], endkey=key + [now], limit=1, stale=stale_ok(), ).all() return True if row else False
def app_has_been_submitted_to_in_last_30_days(domain, app_id): now = datetime.datetime.utcnow() _30_days = datetime.timedelta(days=30) then = json_format_datetime(now - _30_days) now = json_format_datetime(now) key = ['submission app', domain, app_id] row = XFormInstance.get_db().view( "all_forms/view", startkey=key + [then], endkey=key + [now], limit=1, stale=stale_ok(), ).all() return True if row else False
def get_number_of_forms_of_all_types(domain): """ Gets a count of all form-like things in a domain (including errors and duplicates) """ # todo: this is only used to display the "filtered from __ entries" in the "raw forms" report # and can probably be removed startkey = [domain] endkey = startkey + [{}] submissions = XFormInstance.view( "couchforms/all_submissions_by_domain", startkey=startkey, endkey=endkey, reduce=True, stale=stale_ok(), ).one() return submissions['value'] if submissions else 0
def get_first_form_submission_received(domain): from corehq.apps.reports.util import make_form_couch_key key = make_form_couch_key(domain) row = XFormInstance.get_db().view( "reports_forms/all_forms", reduce=False, startkey=key, endkey=key + [{}], limit=1, stale=stale_ok(), ).first() if row: submission_time = iso_string_to_datetime(row["key"][2]) else: submission_time = None return submission_time
def get_first_form_submission_received(domain): from corehq.apps.reports.util import make_form_couch_key key = make_form_couch_key(domain) row = XFormInstance.get_db().view( "all_forms/view", reduce=False, startkey=key, endkey=key + [{}], limit=1, stale=stale_ok(), ).first() if row: submission_time = iso_string_to_datetime(row["key"][2]) else: submission_time = None return submission_time
def get_form_analytics_metadata(domain, app_id, xmlns): """ Returns metadata about the form, or None if no info found. Here is an example structure: { "xmlns": "http://openrosa.org/formdesigner/5D563904-4038-4070-A0D4-CC421003E862", "form": { "name": { "en": "Brogramming Project", "es": "Projecto de brogramming" }, "id": 1 }, "app": { "langs": [ "es", "en", "fra" ], "name": "brogrammino", "id": "10257bd886ba423eea19a562e95cec07" }, "module": { "name": { "en": "Dimagi", "es": "Brogramminos" }, "id": 0 }, "app_deleted": false, "submissions": 15 } """ # todo: wrap this return value in a class/stucture view_results = XFormInstance.get_db().view( 'exports_forms/by_xmlns', key=[domain, app_id, xmlns], stale=stale_ok(), group=True ).one() if view_results: return view_results['value'] return None
def get_all_form_details(domain, deleted=None): """ deleted = None means include all options. deleted = True/False will only included deleted/non-deleted apps """ if deleted is None: startkey = ["app module form", domain] else: status = 'deleted' if deleted else 'active' startkey = ["status app module form", domain, status] return [ _row_to_form_details(r) for r in Application.get_db().view( 'forms_by_app_info/view', startkey=startkey, endkey=startkey + [{}], reduce=False, stale=stale_ok(), ).all() ]
def get_exports_by_form(domain): from corehq.apps.app_manager.models import Application rows = Application.get_db().view( 'exports_forms_by_app/view', startkey=[domain], endkey=[domain, {}], group=True, stale=stale_ok() ).all() form_count_breakdown = get_form_count_breakdown_for_domain(domain) for row in rows: key = tuple(row['key']) if key in form_count_breakdown: row['value']['submissions'] = form_count_breakdown.pop(key) for key, value in form_count_breakdown.items(): rows.append({'key': list(key), 'value': {'xmlns': key[2], 'submissions': value}}) rows.sort(key=lambda row: row['key']) return rows
def get_form_details_for_app_and_xmlns(domain, app_id, xmlns, deleted=False): status = "deleted" if deleted else "active" startkey = ["status xmlns app", domain, status, xmlns, app_id] return [ _row_to_form_details(row) for row in Application.get_db() .view("forms_by_app_info/view", startkey=startkey, endkey=startkey + [{}], reduce=False, stale=stale_ok()) .all() ]