def test_caching(self, mock): user_case = (CaseAccessors( self.cc_domain.name).get_case_by_domain_hq_user_id( self.cc_user.get_id, CASE_TYPE)) expected_indicators = {'a': 1, 'b': 2} cached_data = CachedIndicators(user_id=self.cc_user.get_id, case_id=user_case.case_id, domain=self.cc_domain.name, indicators=expected_indicators) indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache) locmem_cache.set( cache_key(self.cc_user.get_id, indicator_set.reference_date), cached_data.to_json()) self.assertEqual( set(indicator_set.user_to_case_map), set([self.cc_user.get_id, self.cc_user_no_data.get_id])) self.assertEquals(indicator_set.users_needing_data, set([self.cc_user_no_data.get_id])) self.assertEqual(indicator_set.owners_needing_data, set([self.cc_user_no_data.get_id])) self.check_cc_indicators(indicator_set.get_data(), expected_indicators)
def test_caching(self): user_case = CaseAccessors(self.cc_domain.name).get_case_by_domain_hq_user_id(self.cc_user.get_id, CASE_TYPE) expected_indicators = {'a': 1, 'b': 2} cached_data = CachedIndicators( user_id=self.cc_user.get_id, case_id=user_case.case_id, domain=self.cc_domain.name, indicators=expected_indicators ) indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache ) locmem_cache.set(cache_key(self.cc_user.get_id, indicator_set.reference_date), cached_data.to_json()) self.assertEqual( set(indicator_set.user_to_case_map.keys()), set([self.cc_user.get_id, self.cc_user_no_data.get_id]) ) self.assertEquals(indicator_set.users_needing_data, set([self.cc_user_no_data.get_id])) self.assertEqual(indicator_set.owners_needing_data, set([self.cc_user_no_data.get_id])) self.check_cc_indicators(indicator_set.get_data(), expected_indicators)
def test_standard_indicators_case_dog_only(self): config = CallCenterIndicatorConfig.default_config(include_legacy=False) config.forms_submitted.enabled = False def dog_only(conf): conf.totals.enabled = False conf.all_types = False conf.by_type = [TypedIndicator(enabled=True, date_ranges={WEEK0, MONTH0}, type='dog')] dog_only(config.cases_total) dog_only(config.cases_opened) dog_only(config.cases_closed) dog_only(config.cases_active) indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache, indicator_config=config ) self._test_indicators( self.cc_user, indicator_set.get_data(), expected_standard_indicators( include_legacy=False, include_totals=False, case_types=['dog'], limit_ranges=[WEEK0, MONTH0]) )
def test_custom_indicators(self, mock): mock.return_value = set() expected = {'totalCases': 0} expected.update( get_indicators('formsSubmitted', [3, 3, 9, 0], is_legacy=True)) expected.update(get_indicators('forms_submitted', [3, 3, 9, 0])) expected.update( get_indicators('casesUpdated', [0, 0, 0, 0], is_legacy=True)) expected.update(get_indicators('cases_total', [0, 0, 0, 0])) expected.update(get_indicators('cases_opened', [0, 0, 0, 0])) expected.update(get_indicators('cases_closed', [0, 0, 0, 0])) expected.update(get_indicators('cases_active', [0, 0, 0, 0])) # custom expected.update( get_indicators('motherForms', [3, 3, 9, 0], is_legacy=True)) expected.update( get_indicators('childForms', [0, 0, 0, 0], is_legacy=True)) expected.update( get_indicators('motherDuration', [3, 4, 4, 0], is_legacy=True)) indicator_set = CallCenterIndicators( self.aarohi_domain.name, self.aarohi_domain.default_timezone, self.aarohi_domain.call_center_config.case_type, self.aarohi_user, custom_cache=locmem_cache, ) self._test_indicators(self.aarohi_user, indicator_set.get_data(), expected)
def test_custom_indicators_limited(self, mock): expected = {} # custom expected.update( get_indicators('motherForms', [3, None, 9, None], is_legacy=True, limit_ranges=[WEEK0, MONTH0]) ) expected.update( get_indicators('childForms', [None, 0, None, None], is_legacy=True, limit_ranges=[WEEK1]) ) expected.update( get_indicators('motherDuration', [None, None, None, 0], is_legacy=True, limit_ranges=[MONTH1]) ) indicator_config = CallCenterIndicatorConfig() indicator_config.custom_form = [ TypedIndicator(type='motherForms', date_range=WEEK0), TypedIndicator(type='motherForms', date_range=MONTH0), TypedIndicator(type='childForms', date_range=WEEK1), TypedIndicator(type='motherDuration', date_range=MONTH1), ] indicator_set = CallCenterIndicators( self.aarohi_domain.name, self.aarohi_domain.default_timezone, self.aarohi_domain.call_center_config.case_type, self.aarohi_user, custom_cache=locmem_cache, indicator_config=indicator_config ) self._test_indicators( self.aarohi_user, indicator_set.get_data(), expected )
def test_standard_indicators_case_dog_only(self, mock): config = CallCenterIndicatorConfig.default_config(include_legacy=False) config.forms_submitted.enabled = False def dog_only(conf): conf.totals.enabled = False conf.all_types = False conf.by_type = [ TypedIndicator(enabled=True, date_ranges={WEEK0, MONTH0}, type='dog') ] dog_only(config.cases_total) dog_only(config.cases_opened) dog_only(config.cases_closed) dog_only(config.cases_active) indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache, indicator_config=config) self._test_indicators( self.cc_user, indicator_set.get_data(), expected_standard_indicators(include_legacy=False, include_totals=False, case_types=['dog'], limit_ranges=[WEEK0, MONTH0]))
def test_custom_indicators_limited(self, mock): expected = {} # custom expected.update( get_indicators('motherForms', [3, None, 9, None], is_legacy=True, limit_ranges=[WEEK0, MONTH0])) expected.update( get_indicators('childForms', [None, 0, None, None], is_legacy=True, limit_ranges=[WEEK1])) expected.update( get_indicators('motherDuration', [None, None, None, 0], is_legacy=True, limit_ranges=[MONTH1])) indicator_config = CallCenterIndicatorConfig() indicator_config.custom_form = [ TypedIndicator(type='motherForms', date_range=WEEK0), TypedIndicator(type='motherForms', date_range=MONTH0), TypedIndicator(type='childForms', date_range=WEEK1), TypedIndicator(type='motherDuration', date_range=MONTH1), ] indicator_set = CallCenterIndicators( self.aarohi_domain.name, self.aarohi_domain.default_timezone, self.aarohi_domain.call_center_config.case_type, self.aarohi_user, custom_cache=locmem_cache, indicator_config=indicator_config) self._test_indicators(self.aarohi_user, indicator_set.get_data(), expected)
def test_custom_indicators(self, mock): mock.return_value = set() expected = {'totalCases': 0} expected.update(get_indicators('formsSubmitted', [3, 3, 9, 0], is_legacy=True)) expected.update(get_indicators('forms_submitted', [3, 3, 9, 0])) expected.update(get_indicators('casesUpdated', [0, 0, 0, 0], is_legacy=True)) expected.update(get_indicators('cases_total', [0, 0, 0, 0])) expected.update(get_indicators('cases_opened', [0, 0, 0, 0])) expected.update(get_indicators('cases_closed', [0, 0, 0, 0])) expected.update(get_indicators('cases_active', [0, 0, 0, 0])) # custom expected.update(get_indicators('motherForms', [3, 3, 9, 0], is_legacy=True)) expected.update(get_indicators('childForms', [0, 0, 0, 0], is_legacy=True)) expected.update(get_indicators('motherDuration', [3, 4, 4, 0], is_legacy=True)) indicator_set = CallCenterIndicators( self.aarohi_domain.name, self.aarohi_domain.default_timezone, self.aarohi_domain.call_center_config.case_type, self.aarohi_user, custom_cache=locmem_cache, ) self._test_indicators( self.aarohi_user, indicator_set.get_data(), expected )
def test_standard_indicators(self): indicator_set = CallCenterIndicators(self.cc_domain, self.cc_user, custom_cache=locmem_cache) self.assertEqual( set(indicator_set.user_to_case_map.keys()), set([self.cc_user.get_id, self.cc_user_no_data.get_id]) ) self.assertEqual(indicator_set.users_needing_data, set([self.cc_user.get_id, self.cc_user_no_data.get_id])) self.assertEqual(indicator_set.owners_needing_data, set([self.cc_user.get_id, self.cc_user_no_data.get_id])) self.check_cc_indicators(indicator_set.get_data(), expected_standard_indicators())
def test_no_cases_owned_by_user(self): """ Test to verify that only data belonging to users managed by the supervisor is returned. """ indicator_set = CallCenterIndicators(self.cc_domain, self.cc_user_no_data, custom_cache=locmem_cache) self.assertEqual(indicator_set.user_to_case_map.keys(), []) self.assertEqual(indicator_set.users_needing_data, set()) self.assertEqual(indicator_set.owners_needing_data, set()) self.assertEqual(indicator_set.get_data(), {})
def test_users_assigned_via_group(self): """ Ensure that users who are assigned to the supervisor via a group are also included in final data set. """ indicator_set = CallCenterIndicators(self.domain, self.supervisor, custom_cache=locmem_cache) self.assertEqual(indicator_set.user_to_case_map.keys(), [self.user.get_id]) self.assertEqual(indicator_set.users_needing_data, set([self.user.get_id])) self.assertEqual(indicator_set.owners_needing_data, set([self.user.get_id])) self._test_indicators(self.user, indicator_set.get_data(), expected_standard_indicators())
def test_sync_log(self): user_case = get_case_by_domain_hq_user_id(self.cc_domain.name, self.cc_user.get_id, include_docs=True) indicator_set = CallCenterIndicators( self.cc_domain, self.cc_user, custom_cache=locmem_cache, override_cases=[user_case] ) self.assertEqual(indicator_set.user_to_case_map.keys(), [self.cc_user.get_id]) self.assertEqual(indicator_set.users_needing_data, set([self.cc_user.get_id])) self.assertEqual(indicator_set.owners_needing_data, set([self.cc_user.get_id])) self._test_indicators(self.cc_user, indicator_set.get_data(), expected_standard_indicators())
def test_cases_owned_by_group(self): """ Ensure that indicators include cases owned by a case sharing group the user is part of. """ indicator_set = CallCenterIndicators(self.domain, self.supervisor, custom_cache=locmem_cache) self.assertEqual(indicator_set.user_to_case_map.keys(), [self.user.get_id]) self.assertEqual(indicator_set.users_needing_data, set([self.user.get_id])) self.assertEqual(indicator_set.owners_needing_data, set([self.user.get_id, self.group.get_id])) expected = expected_standard_indicators() expected["totalCases"] = 0L # no cases with user_id = self.user.get_id self._test_indicators(self.user, indicator_set.get_data(), expected)
def test_standard_indicators(self): with override_settings(UCR_DATABASE_URL=self.ucr_db_url): load_data(self.cc_domain.name, self.cc_user.user_id) indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache ) self._test_indicators(self.cc_user, indicator_set.get_data(), expected_standard_indicators())
def test_standard_indicators(self, mock): with override_engine('ucr', self.ucr_db_url): load_data(self.cc_domain.name, self.cc_user.user_id) indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache) self._test_indicators(self.cc_user, indicator_set.get_data(), expected_standard_indicators())
def test_opened_closed(self): """ Test that cases_closed and cases_opened indicators count based on the user that opened / closed the case and not the case owner. """ indicator_set = CallCenterIndicators(self.domain, self.supervisor, custom_cache=locmem_cache) expected = expected_standard_indicators() # cases opened / closed by another user so expect 0 for key in expected: if key.startswith("cases_opened") or key.startswith("cases_closed"): expected[key] = 0L self._test_indicators(self.user, indicator_set.get_data(), expected)
def test_standard_indicators_no_legacy(self, mock): config = CallCenterIndicatorConfig.default_config(include_legacy=False) indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache, indicator_config=config) self._test_indicators( self.cc_user, indicator_set.get_data(), expected_standard_indicators(include_legacy=False))
def test_no_cases_owned_by_user(self, mock): """ Test to verify that only data belonging to users managed by the supervisor is returned. """ indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user_no_data, custom_cache=locmem_cache) self.assertEqual(list(indicator_set.user_to_case_map), []) self.assertEqual(indicator_set.users_needing_data, set()) self.assertEqual(indicator_set.owners_needing_data, set()) self.assertEqual(indicator_set.get_data(), {})
def test_users_assigned_via_group(self, mock): # Ensure that users who are assigned to the supervisor via a group are also included # in final data set. indicator_set = CallCenterIndicators( self.domain.name, self.domain.default_timezone, self.domain.call_center_config.case_type, self.supervisor, custom_cache=locmem_cache ) self.assertEqual(list(indicator_set.user_to_case_map), [self.user.get_id]) self.assertEqual(indicator_set.users_needing_data, set([self.user.get_id])) self.assertEqual(indicator_set.owners_needing_data, set([self.user.get_id])) self._test_indicators(self.user, indicator_set.get_data(), expected_standard_indicators())
def test_standard_indicators(self): indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache ) self.assertEqual( set(indicator_set.user_to_case_map.keys()), set([self.cc_user.get_id, self.cc_user_no_data.get_id]) ) self.assertEqual(indicator_set.users_needing_data, set([self.cc_user.get_id, self.cc_user_no_data.get_id])) self.assertEqual(indicator_set.owners_needing_data, set([self.cc_user.get_id, self.cc_user_no_data.get_id])) self.check_cc_indicators(indicator_set.get_data(), expected_standard_indicators())
def test_cases_owned_by_group(self, mock): # Ensure that indicators include cases owned by a case sharing group the user is part of. indicator_set = CallCenterIndicators( self.domain.name, self.domain.default_timezone, self.domain.call_center_config.case_type, self.supervisor, custom_cache=locmem_cache ) self.assertEqual(list(indicator_set.user_to_case_map), [self.user.get_id]) self.assertEqual(indicator_set.users_needing_data, set([self.user.get_id])) self.assertEqual(indicator_set.owners_needing_data, set([self.user.get_id, self.group.get_id])) expected = expected_standard_indicators() expected['totalCases'] = 0 # no cases with user_id = self.user.get_id self._test_indicators(self.user, indicator_set.get_data(), expected)
def test_sync_log(self): user_case = CaseAccessors(self.cc_domain.name).get_case_by_domain_hq_user_id(self.cc_user.get_id, CASE_TYPE) indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache, override_cases=[CallCenterCase.from_case(user_case)] ) self.assertEqual(indicator_set.user_to_case_map.keys(), [self.cc_user.get_id]) self.assertEqual(indicator_set.users_needing_data, set([self.cc_user.get_id])) self.assertEqual(indicator_set.owners_needing_data, set([self.cc_user.get_id])) self._test_indicators(self.cc_user, indicator_set.get_data(), expected_standard_indicators())
def test_no_cases_owned_by_user(self, mock): """ Test to verify that only data belonging to users managed by the supervisor is returned. """ indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user_no_data, custom_cache=locmem_cache ) self.assertEqual(list(indicator_set.user_to_case_map), []) self.assertEqual(indicator_set.users_needing_data, set()) self.assertEqual(indicator_set.owners_needing_data, set()) self.assertEqual(indicator_set.get_data(), {})
def test_standard_indicators_no_legacy(self): config = CallCenterIndicatorConfig.default_config(include_legacy=False) indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache, indicator_config=config ) self._test_indicators( self.cc_user, indicator_set.get_data(), expected_standard_indicators(include_legacy=False))
def __call__(self, user, version, last_sync=None): assert isinstance(user, CommCareUser) domain = user.project fixtures = [] if not domain or not domain.call_center_config.is_active_and_valid( ) or not should_sync(domain, last_sync): return fixtures try: fixtures.append( gen_fixture( user, CallCenterIndicators(domain.name, domain.default_timezone, domain.call_center_config.case_type, user))) except Exception: # blanket exception catching intended notify_exception(None, 'problem generating callcenter fixture', details={ 'user_id': user._id, 'domain': user.domain }) return fixtures
def test_caching(self): user_case = get_case_by_domain_hq_user_id(self.cc_domain.name, self.cc_user._id, include_docs=True) expected_indicators = {'a': 1, 'b': 2} cached_data = CachedIndicators( user_id=self.cc_user.get_id, case_id=user_case.case_id, domain=self.cc_domain.name, indicators=expected_indicators ) locmem_cache.set(cache_key(self.cc_user.get_id), cached_data.to_json()) indicator_set = CallCenterIndicators(self.cc_domain, self.cc_user, custom_cache=locmem_cache) self.assertEqual(indicator_set.all_user_ids, set([self.cc_user.get_id, self.cc_user_no_data.get_id])) self.assertEquals(indicator_set.users_needing_data, set([self.cc_user_no_data.get_id])) self.assertEqual(indicator_set.owners_needing_data, set([self.cc_user_no_data.get_id])) self.check_cc_indicators(indicator_set.get_data(), expected_indicators)
def test_standard_indicators_load_config_from_db(self): config = CallCenterIndicatorConfig.default_config(self.cc_domain.name, include_legacy=False) config.save() self.addCleanup(config.delete) indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache, ) self._test_indicators( self.cc_user, indicator_set.get_data(), expected_standard_indicators(include_legacy=False))
def get_call_center_indicators(self, config): from corehq.apps.callcenter.indicator_sets import CallCenterIndicators return CallCenterIndicators(self.project.name, self.project.default_timezone, self.project.call_center_config.case_type, self._couch_user, indicator_config=config)
def callcenter_test(request): user_id = request.GET.get("user_id") date_param = request.GET.get("date") if not user_id: return render(request, "hqadmin/callcenter_test.html", {}) error = None try: user = CommCareUser.get(user_id) except ResourceNotFound: user = None error = "User Not Found" try: query_date = dateutil.parser.parse(date_param) except ValueError: error = "Unable to parse date, using today" query_date = date.today() def view_data(case_id, indicators): new_dict = SortedDict() key_list = sorted(indicators.keys()) for key in key_list: new_dict[key] = indicators[key] return { 'indicators': new_dict, 'case': CommCareCase.get(case_id), } if user: domain = user.project dummy_cache = cache.get_cache('django.core.cache.backends.dummy.DummyCache') cci = CallCenterIndicators(domain, user, custom_cache=dummy_cache, override_date=query_date) data = {case_id: view_data(case_id, values) for case_id, values in cci.get_data().items()} else: data = {} context = { "error": error, "mobile_user": user, "date": query_date.strftime("%Y-%m-%d"), "data": data, } return render(request, "hqadmin/callcenter_test.html", context)
def test_opened_closed(self, mock): """ Test that cases_closed and cases_opened indicators count based on the user that opened / closed the case and not the case owner. """ indicator_set = CallCenterIndicators( self.domain.name, self.domain.default_timezone, self.domain.call_center_config.case_type, self.supervisor, custom_cache=locmem_cache) expected = expected_standard_indicators() # cases opened / closed by another user so expect 0 for key in expected: if key.startswith('cases_opened') or key.startswith( 'cases_closed'): expected[key] = 0 self._test_indicators(self.user, indicator_set.get_data(), expected)
def test_standard_indicators(self, mock): indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache ) self.assertEqual( set(indicator_set.user_to_case_map), set([self.cc_user.get_id, self.cc_user_no_data.get_id]) ) self.assertEqual( indicator_set.users_needing_data, set([self.cc_user.get_id, self.cc_user_no_data.get_id]) ) self.assertEqual( indicator_set.owners_needing_data, set([self.cc_user.get_id, self.cc_user_no_data.get_id]) ) self.check_cc_indicators(indicator_set.get_data(), expected_standard_indicators())
def test_opened_closed(self, mock): """ Test that cases_closed and cases_opened indicators count based on the user that opened / closed the case and not the case owner. """ indicator_set = CallCenterIndicators( self.domain.name, self.domain.default_timezone, self.domain.call_center_config.case_type, self.supervisor, custom_cache=locmem_cache ) expected = expected_standard_indicators() # cases opened / closed by another user so expect 0 for key in expected: if key.startswith('cases_opened') or key.startswith('cases_closed'): expected[key] = 0 self._test_indicators(self.user, indicator_set.get_data(), expected)
def test_standard_indicators_case_totals_only(self, mock): config = CallCenterIndicatorConfig.default_config(include_legacy=False) config.cases_total.all_types = False config.cases_opened.all_types = False config.cases_closed.all_types = False config.cases_active.all_types = False indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache, indicator_config=config) self._test_indicators( self.cc_user, indicator_set.get_data(), expected_standard_indicators(include_legacy=False, include_totals=True, case_types=[]))
def test_standard_indicators_case_week1_only(self, mock): config = CallCenterIndicatorConfig.default_config(include_legacy=False) config.forms_submitted.date_ranges = {WEEK1} config.cases_total.totals.date_ranges = {WEEK1} config.cases_opened.totals.date_ranges = {WEEK1} config.cases_closed.totals.date_ranges = {WEEK1} config.cases_active.totals.date_ranges = {WEEK1} indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache, indicator_config=config) self._test_indicators( self.cc_user, indicator_set.get_data(), expected_standard_indicators(include_legacy=False, include_totals=True, limit_ranges=[WEEK1]))
def calculate_indicators(): """ Although this task runs every 15 minutes it only re-calculates the indicators for a domain if we're within 15 minutes after midnight in the domain's timezone. """ domains = [ domain for domain in get_call_center_domains() for midnight in domain.midnights if is_midnight_for_domain(midnight, error_margin=20) ] logger.info( "Calculating callcenter indicators for domains:\n{}".format(domains)) for domain in domains: all_cases = get_call_center_cases(domain.name, domain.cc_case_type) indicator_set = CallCenterIndicators(domain.name, domain.default_timezone, domain.cc_case_type, user=None, override_cases=all_cases, override_cache=True) indicator_set.get_data()
def calculate_indicators(): """ Although this task runs every 15 minutes it only re-calculates the indicators for a domain if we're within 15 minutes after midnight in the domain's timezone. """ domains = [ domain for domain in get_call_center_domains() if is_midnight_for_domain(domain.midnight, error_margin=15) ] for domain in domains: all_cases = get_call_center_cases(domain.name, domain.cc_case_type) indicator_set = CallCenterIndicators( domain.name, domain.default_timezone, domain.cc_case_type, user=None, override_cases=all_cases, override_cache=True ) indicator_set.get_data()
def test_standard_indicators_case_totals_only(self): config = CallCenterIndicatorConfig.default_config(include_legacy=False) config.cases_total.all_types = False config.cases_opened.all_types = False config.cases_closed.all_types = False config.cases_active.all_types = False indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache, indicator_config=config ) self._test_indicators( self.cc_user, indicator_set.get_data(), expected_standard_indicators( include_legacy=False, include_totals=True, case_types=[]) )
def test_standard_indicators_case_week1_only(self): config = CallCenterIndicatorConfig.default_config(include_legacy=False) config.forms_submitted.date_ranges = {WEEK1} config.cases_total.totals.date_ranges = {WEEK1} config.cases_opened.totals.date_ranges = {WEEK1} config.cases_closed.totals.date_ranges = {WEEK1} config.cases_active.totals.date_ranges = {WEEK1} indicator_set = CallCenterIndicators( self.cc_domain.name, self.cc_domain.default_timezone, self.cc_domain.call_center_config.case_type, self.cc_user, custom_cache=locmem_cache, indicator_config=config ) self._test_indicators( self.cc_user, indicator_set.get_data(), expected_standard_indicators( include_legacy=False, include_totals=True, limit_ranges=[WEEK1]) )
def callcenter_test(request): user_id = request.GET.get("user_id") date_param = request.GET.get("date") enable_caching = request.GET.get("cache") doc_id = request.GET.get("doc_id") if not user_id and not doc_id: return render(request, "hqadmin/callcenter_test.html", {"enable_caching": enable_caching}) error = None user = None user_case = None domain = None if user_id: try: user = CommCareUser.get(user_id) domain = user.project except ResourceNotFound: error = "User Not Found" elif doc_id: try: doc = CommCareUser.get_db().get(doc_id) doc_type = doc.get("doc_type", None) if doc_type == "CommCareUser": user_case = get_case_by_domain_hq_user_id(doc["domain"], doc["_id"], include_docs=True) elif doc_type == "CommCareCase": if doc.get("hq_user_id"): user_case = CommCareCase.wrap(doc) else: error = "Case ID does does not refer to a Call Center Case" except ResourceNotFound: error = "User Not Found" if user_case: domain = Domain.get_by_name(user_case["domain"]) try: query_date = dateutil.parser.parse(date_param) except ValueError: error = "Unable to parse date, using today" query_date = date.today() def view_data(case_id, indicators): new_dict = SortedDict() key_list = sorted(indicators.keys()) for key in key_list: new_dict[key] = indicators[key] return {"indicators": new_dict, "case": CommCareCase.get(case_id)} if user or user_case: custom_cache = None if enable_caching else cache.get_cache("django.core.cache.backends.dummy.DummyCache") cci = CallCenterIndicators( domain, user, custom_cache=custom_cache, override_date=query_date, override_cases=[user_case] if user_case else None, ) data = {case_id: view_data(case_id, values) for case_id, values in cci.get_data().items()} else: data = {} context = { "error": error, "mobile_user": user, "date": query_date.strftime("%Y-%m-%d"), "enable_caching": enable_caching, "data": data, "doc_id": doc_id, } return render(request, "hqadmin/callcenter_test.html", context)
def callcenter_test(request): user_id = request.GET.get("user_id") date_param = request.GET.get("date") enable_caching = request.GET.get('cache') doc_id = request.GET.get('doc_id') if not user_id and not doc_id: return render(request, "hqadmin/callcenter_test.html", {"enable_caching": enable_caching}) error = None user = None user_case = None domain = None if user_id: try: user = CommCareUser.get(user_id) domain = user.project except ResourceNotFound: error = "User Not Found" elif doc_id: try: doc = CommCareUser.get_db().get(doc_id) domain = Domain.get_by_name(doc['domain']) doc_type = doc.get('doc_type', None) if doc_type == 'CommCareUser': case_type = domain.call_center_config.case_type user_case = get_case_by_domain_hq_user_id(doc['domain'], doc['_id'], case_type) elif doc_type == 'CommCareCase': if doc.get('hq_user_id'): user_case = CommCareCase.wrap(doc) else: error = 'Case ID does does not refer to a Call Center Case' except ResourceNotFound: error = "User Not Found" try: query_date = dateutil.parser.parse(date_param) except ValueError: error = "Unable to parse date, using today" query_date = date.today() def view_data(case_id, indicators): new_dict = SortedDict() key_list = sorted(indicators.keys()) for key in key_list: new_dict[key] = indicators[key] return { 'indicators': new_dict, 'case': CommCareCase.get(case_id), } if user or user_case: custom_cache = None if enable_caching else cache.caches['dummy'] cci = CallCenterIndicators( domain.name, domain.default_timezone, domain.call_center_config.case_type, user, custom_cache=custom_cache, override_date=query_date, override_cases=[user_case] if user_case else None ) data = {case_id: view_data(case_id, values) for case_id, values in cci.get_data().items()} else: data = {} context = { "error": error, "mobile_user": user, "date": json_format_date(query_date), "enable_caching": enable_caching, "data": data, "doc_id": doc_id } return render(request, "hqadmin/callcenter_test.html", context)
def test_custom_indicators(self): expected = {"totalCases": 0L} expected.update(get_indicators("formsSubmitted", [3L, 3L, 9L, 0L], is_legacy=True)) expected.update(get_indicators("forms_submitted", [3L, 3L, 9L, 0L])) expected.update(get_indicators("casesUpdated", [0L, 0L, 0L, 0L], is_legacy=True)) expected.update(get_indicators("cases_total", [0L, 0L, 0L, 0L])) expected.update(get_indicators("cases_opened", [0L, 0L, 0L, 0L])) expected.update(get_indicators("cases_closed", [0L, 0L, 0L, 0L])) expected.update(get_indicators("cases_active", [0L, 0L, 0L, 0L])) # custom expected.update(get_indicators("motherForms", [3L, 3L, 9L, 0L], is_legacy=True)) expected.update(get_indicators("childForms", [0L, 0L, 0L, 0L], is_legacy=True)) expected.update(get_indicators("motherDuration", [3L, 4L, 4L, 0L], is_legacy=True)) indicator_set = CallCenterIndicators(self.aarohi_domain, self.aarohi_user, custom_cache=locmem_cache) self._test_indicators(self.aarohi_user, indicator_set.get_data(), expected) def test_caching(self): user_case = get_case_by_domain_hq_user_id(self.cc_domain.name, self.cc_user._id, include_docs=True) expected_indicators = {"a": 1, "b": 2} cached_data = CachedIndicators( user_id=self.cc_user.get_id, case_id=user_case.case_id, domain=self.cc_domain.name, indicators=expected_indicators, ) indicator_set = CallCenterIndicators(self.cc_domain, self.cc_user, custom_cache=locmem_cache) locmem_cache.set(cache_key(self.cc_user.get_id, indicator_set.reference_date), cached_data.to_json())
expected.update(get_indicators('forms_submitted', [3L, 3L, 9L, 0L])) expected.update(get_indicators('casesUpdated', [0L, 0L, 0L, 0L], is_legacy=True)) expected.update(get_indicators('cases_total', [0L, 0L, 0L, 0L])) expected.update(get_indicators('cases_opened', [0L, 0L, 0L, 0L])) expected.update(get_indicators('cases_closed', [0L, 0L, 0L, 0L])) expected.update(get_indicators('cases_active', [0L, 0L, 0L, 0L])) # custom expected.update(get_indicators('motherForms', [3L, 3L, 9L, 0L], is_legacy=True)) expected.update(get_indicators('childForms', [0L, 0L, 0L, 0L], is_legacy=True)) expected.update(get_indicators('motherDuration', [3L, 4L, 4L, 0L], is_legacy=True)) indicator_set = CallCenterIndicators( self.aarohi_domain.name, self.aarohi_domain.default_timezone, self.aarohi_domain.call_center_config.case_type, self.aarohi_user, custom_cache=locmem_cache, ) self._test_indicators( self.aarohi_user, indicator_set.get_data(), expected ) def test_custom_indicators_limited(self): expected = {} # custom expected.update( get_indicators('motherForms', [3L, None, 9L, None], is_legacy=True, limit_ranges=[WEEK0, MONTH0])
def callcenter_test(request): user_id = request.GET.get("user_id") date_param = request.GET.get("date") enable_caching = request.GET.get('cache') doc_id = request.GET.get('doc_id') if not user_id and not doc_id: return render(request, "hqadmin/callcenter_test.html", {"enable_caching": enable_caching}) error = None user = None user_case = None domain = None if user_id: try: user = CommCareUser.get(user_id) domain = user.project except ResourceNotFound: error = "User Not Found" elif doc_id: try: doc = CommCareUser.get_db().get(doc_id) domain = Domain.get_by_name(doc['domain']) doc_type = doc.get('doc_type', None) if doc_type == 'CommCareUser': case_type = domain.call_center_config.case_type user_case = CaseAccessors(doc['domain']).get_case_by_domain_hq_user_id(doc['_id'], case_type) elif doc_type == 'CommCareCase': if doc.get('hq_user_id'): user_case = CommCareCase.wrap(doc) else: error = 'Case ID does does not refer to a Call Center Case' except ResourceNotFound: error = "User Not Found" try: query_date = dateutil.parser.parse(date_param) except ValueError: error = "Unable to parse date, using today" query_date = date.today() def view_data(case_id, indicators): new_dict = OrderedDict() key_list = sorted(indicators) for key in key_list: new_dict[key] = indicators[key] return { 'indicators': new_dict, 'case': CommCareCase.get(case_id), } if user or user_case: custom_cache = None if enable_caching else cache.caches['dummy'] override_case = CallCenterCase.from_case(user_case) cci = CallCenterIndicators( domain.name, domain.default_timezone, domain.call_center_config.case_type, user, custom_cache=custom_cache, override_date=query_date, override_cases=[override_case] if override_case else None ) data = {case_id: view_data(case_id, values) for case_id, values in cci.get_data().items()} else: data = {} context = { "error": error, "mobile_user": user, "date": json_format_date(query_date), "enable_caching": enable_caching, "data": data, "doc_id": doc_id } return render(request, "hqadmin/callcenter_test.html", context)
expected.update(get_indicators('cases_total', [0L, 0L, 0L, 0L])) expected.update(get_indicators('cases_opened', [0L, 0L, 0L, 0L])) expected.update(get_indicators('cases_closed', [0L, 0L, 0L, 0L])) expected.update(get_indicators('cases_active', [0L, 0L, 0L, 0L])) # custom expected.update( get_indicators('motherForms', [3L, 3L, 9L, 0L], is_legacy=True)) expected.update( get_indicators('childForms', [0L, 0L, 0L, 0L], is_legacy=True)) expected.update( get_indicators('motherDuration', [3L, 4L, 4L, 0L], is_legacy=True)) indicator_set = CallCenterIndicators( self.aarohi_domain.name, self.aarohi_domain.default_timezone, self.aarohi_domain.call_center_config.case_type, self.aarohi_user, custom_cache=locmem_cache) self._test_indicators(self.aarohi_user, indicator_set.get_data(), expected) def test_caching(self): user_case = get_case_by_domain_hq_user_id(self.cc_domain.name, self.cc_user._id, CASE_TYPE) expected_indicators = {'a': 1, 'b': 2} cached_data = CachedIndicators(user_id=self.cc_user.get_id, case_id=user_case.case_id, domain=self.cc_domain.name, indicators=expected_indicators) indicator_set = CallCenterIndicators(