def get_number_of_mobile_workers(domain, year, month): date_start, date_end = get_start_and_end_dates_of_month(year, month) domain_user_history = DomainUserHistory.objects.filter( domain=domain, record_date__gte=date_start, record_date__lte=date_end, ) return (domain_user_history.first().num_users if domain_user_history.exists() else 0)
def get_number_of_web_users(domain, year, month): date_start, date_end = get_start_and_end_dates_of_month(year, month) users_existing_in_domain = set( doc['base_username'] for doc in UserES().domain(domain).web_users().created( lte=date_end).run().hits) users_who_accepted_invites = set( email for email in Invitation.objects.filter( domain=domain, is_accepted=True, invited_on__gte=date_start, invited_on__lt=date_end).values_list('email', flat=True)) return len(users_existing_in_domain.union(users_who_accepted_invites))
def ensure_start_and_end_dates_are_correct_feb_no_leap_year(self): date_start, date_end = get_start_and_end_dates_of_month(2021, 2) self.assertEqual(date_start, date(2021, 2, 1)) self.assertEqual(date_end, date(2021, 2, 28))
def ensure_start_and_end_dates_are_correct_for_31_days(self): date_start, date_end = get_start_and_end_dates_of_month(2020, 7) self.assertEqual(date_start, date(2020, 7, 1)) self.assertEqual(date_end, date(2020, 7, 31))
def setUpClass(cls): super().setUpClass() cls.es = get_es_new() initialize_index_and_mapping(cls.es, USER_INDEX_INFO) initialize_index_and_mapping(cls.es, XFORM_INDEX_INFO) cls.date_start, cls.date_end = get_start_and_end_dates_of_month( 2021, 11) cls.domain = Domain.get_or_create_with_name('test-partner-analytics', is_active=True) # Data for Mobile Workers Tests DomainUserHistory.objects.create(domain=cls.domain.name, record_date=cls.date_end, num_users=3) DomainUserHistory.objects.create(domain=cls.domain.name, record_date=cls.date_start - datetime.timedelta(days=1), num_users=6) DomainUserHistory.objects.create(domain=cls.domain.name, record_date=cls.date_end + datetime.timedelta(days=1), num_users=9) # Data for Web Users tests cls.users = [ WebUser.create(cls.domain.name, '*****@*****.**', 'testpwd', None, None, date=cls.date_start + datetime.timedelta(days=5)), WebUser.create(cls.domain.name, '*****@*****.**', 'testpwd', None, None, date=cls.date_start - datetime.timedelta(days=1)), WebUser.create(cls.domain.name, '*****@*****.**', 'testpwd', None, None, date=cls.date_end + datetime.timedelta(days=1)), WebUser.create(cls.domain.name, '*****@*****.**', 'testpwd', None, None, date=cls.date_start + datetime.timedelta(days=6)), ] for user in cls.users: elastic_user = transform_user_for_elasticsearch(user.to_json()) send_to_elasticsearch('users', elastic_user) invitations = [ Invitation.objects.create( domain=cls.domain.name, email='*****@*****.**', invited_by='*****@*****.**', invited_on=cls.date_start + datetime.timedelta(days=5), ), Invitation.objects.create( domain=cls.domain.name, email='*****@*****.**', invited_by='*****@*****.**', invited_on=cls.date_start - datetime.timedelta(days=1), ), Invitation.objects.create( domain=cls.domain.name, email='*****@*****.**', invited_by='*****@*****.**', invited_on=cls.date_end + datetime.timedelta(days=1), ), ] for invitation in invitations: invitation.is_accepted = True invitation.save() # Data for forms tests forms = [ create_form_for_test(cls.domain.name, received_on=cls.date_start - datetime.timedelta(days=1)), create_form_for_test(cls.domain.name, received_on=cls.date_end + datetime.timedelta(days=1)), create_form_for_test(cls.domain.name, received_on=cls.date_start + datetime.timedelta(days=3)), create_form_for_test(cls.domain.name, received_on=cls.date_start + datetime.timedelta(days=12)), ] for form in forms: elastic_form = transform_xform_for_elasticsearch(form.to_json()) send_to_elasticsearch('forms', elastic_form) cls.es.indices.refresh(USER_INDEX_INFO.alias) cls.es.indices.refresh(XFORM_INDEX_INFO.alias)
def get_number_of_submissions(domain, year, month): date_start, date_end = get_start_and_end_dates_of_month(year, month) return (FormES().fields(['received_on' ]).domain(domain).submitted(gte=date_start, lt=date_end).count())