def handle(self, *args, **options): report_date = options.get('reportdate', None) if not report_date: print "Please specify report date" sys.exit() report_date = self.get_reportdate(report_date) print report_date stories = Story.objects.filter(created_at__gte=report_date) survey_stories = stories.filter(group__source__name='mobile') survey_count = survey_stories.count() survey_devices = survey_stories.aggregate( Count('telephone', distinct=True))['telephone__count'] survey_schools = survey_stories.aggregate( Count('school_id', distinct=True))['school_id__count'] print 'survey_count: ', survey_count print 'devices_count: ', survey_devices print 'school_count: ', survey_schools user_types = ['CRP', 'BFC'] konnect_stories = stories.filter(group__source__name='konnectsms') konnect_count = konnect_stories.count() konnect_devices = konnect_stories.aggregate( Count('telephone', distinct=True))['telephone__count'] CRP_count = konnect_stories.filter(user__groups__name='CRP').count() BFC_count = konnect_stories.filter(user__groups__name='BFC').count() other_count = konnect_stories.exclude( user__groups__name__in=user_types).count() print 'Konnect_count:', konnect_count print 'konnect_devices_count:', konnect_devices print 'CRPcount:', CRP_count print 'BFCcount:', BFC_count print 'othercount:', other_count author = 'ILP Konnect' emoji = ':memo:' try: post_to_slack( channel='#klp', author=author, message= 'Community Survey: %s Schools, %s Devices and %s Surveys' % (survey_schools, survey_devices, survey_count), emoji=emoji, ) except: pass try: post_to_slack( channel='#klp', author=author, message= 'GKA Monitoring: %s Devices and %s Surveys - %s by CRP, %s by BFC and %s by others' % (konnect_devices, konnect_count, CRP_count, BFC_count, other_count), emoji=emoji, ) except: pass
def handle(self, *args, **options): count = 0 # To post daily updates to slack. from_date = options.get('from', None) to_date = options.get('to', None) error_file = '/var/www/dubdubdub/log/error_' + \ timezone.now().date().strftime("%m%d%Y") + '.log' try: f = open(error_file) self.ivrs_errors = json.loads(f.read()) f.close() except: self.ivrs_errors = [] if from_date or to_date: if from_date and to_date: sane, message = self.sanity_check(from_date, to_date) if sane: print message from_date = self.transform_date(from_date) to_date = self.transform_date(to_date) else: print message return else: print "Please specify either both --from and --to dates or neither" return else: from_date = to_date = timezone.now().date().strftime("%m/%d/%Y") source = Source.objects.get(name="ivrs") success, json_list = self.fetch_data(from_date, to_date) if success: for the_json in json_list: sane, message = self.sanity_check_json(the_json) if sane: created = self.process_json(source, the_json) if created: count += 1 else: print message continue else: print "All jsons processed!" else: return f = open(error_file, 'w') f.write(json.dumps(self.ivrs_errors, indent = 4)) f.close() post_to_slack( channel='#klp', author='Mahiti IVRS', message='%s calls processed' % count, emoji=':phone:' )
def handle(self, *args, **options): count = 0 # To post daily updates to slack. from_date = options.get('from', None) to_date = options.get('to', None) error_file = '/var/www/dubdubdub/log/error_' + \ timezone.now().date().strftime("%m%d%Y") + '.log' try: f = open(error_file) self.ivrs_errors = json.loads(f.read()) f.close() except: self.ivrs_errors = [] if from_date or to_date: if from_date and to_date: sane, message = self.sanity_check(from_date, to_date) if sane: print message from_date = self.transform_date(from_date) to_date = self.transform_date(to_date) else: print message return else: print "Please specify either both --from and --to dates or neither" return else: from_date = to_date = timezone.now().date().strftime("%m/%d/%Y") source = Source.objects.get(name="ivrs") success, json_list = self.fetch_data(from_date, to_date) if success: for the_json in json_list: sane, message = self.sanity_check_json(the_json) if sane: created = self.process_json(source, the_json) if created: count += 1 else: print message continue else: print "All jsons processed!" else: return f = open(error_file, 'w') f.write(json.dumps(self.ivrs_errors, indent=4)) f.close() post_to_slack(channel='#klp', author='Mahiti IVRS', message='%s calls processed' % count, emoji=':phone:')
def process_state(self, qg_type): states = State.objects.filter( qg_type=qg_type, is_processed=False, ) valid_count = states.filter(is_invalid=False).count() invalid_count = states.filter(is_invalid=True).count() for state in states: state.is_processed = True state.save() if state.is_invalid: continue user = state.user school = School.objects.get(id=state.school_id) telephone = state.telephone date = state.date_of_visit akshara_staff = UserType.objects.get_or_create( name=UserType.AKSHARA_STAFF)[0] question_group = qg_type.questiongroup story = Story.objects.create(user=user, school=school, is_verified=True, group=question_group, date_of_visit=date, telephone=telephone, user_type=akshara_staff) for (question_number, answer) in enumerate(state.answers[1:]): if answer != 'NA': question = get_question(question_number + 1, qg_type.questiongroup) answer = Answer.objects.get_or_create(story=story, question=question, text=answer) if qg_type.name == 'gkav4': author = 'GKA SMS' emoji = ':memo:' else: author = None if author: try: post_to_slack( channel='#klp', author=author, message='%s Valid calls & %s Invalid calls' % (valid_count, invalid_count), emoji=emoji, ) except: pass
def handle(self, *args, **options): report_date = options.get('reportdate', None) if not report_date: print "Please specify report date" sys.exit() report_date = self.get_reportdate(report_date) print report_date stories = Story.objects.filter(created_at__gte =report_date) survey_stories = stories.filter(group__source__name='mobile') survey_count = survey_stories.count() survey_devices = survey_stories.aggregate(Count('telephone', distinct = True))['telephone__count'] survey_schools = survey_stories.aggregate(Count('school_id', distinct = True))['school_id__count'] print 'survey_count: ',survey_count print 'devices_count: ',survey_devices print 'school_count: ',survey_schools user_types = ['CRP', 'BFC'] konnect_stories = stories.filter(group__source__name='konnectsms') konnect_count = konnect_stories.count() konnect_devices = konnect_stories.aggregate(Count('telephone', distinct = True))['telephone__count'] CRP_count = konnect_stories.filter(user__groups__name = 'CRP').count() BFC_count = konnect_stories.filter(user__groups__name = 'BFC').count() other_count = konnect_stories.exclude(user__groups__name__in = user_types).count() print 'Konnect_count:',konnect_count print 'konnect_devices_count:',konnect_devices print 'CRPcount:',CRP_count print 'BFCcount:',BFC_count print 'othercount:',other_count author = 'ILP Konnect' emoji = ':memo:' try: post_to_slack( channel='#klp', author=author, message='Community Survey: %s Schools, %s Devices and %s Surveys' %(survey_schools, survey_devices, survey_count), emoji=emoji, ) except: pass try: post_to_slack( channel='#klp', author=author, message='GKA Monitoring: %s Devices and %s Surveys - %s by CRP, %s by BFC and %s by others' %(konnect_devices, konnect_count, CRP_count, BFC_count, other_count ), emoji=emoji, ) except: pass
def process_state(self, qg_type): states = State.objects.filter( qg_type=qg_type, is_processed=False, ) valid_count = states.filter(is_invalid=False).count() invalid_count = states.filter(is_invalid=True).count() for state in states: state.is_processed = True state.save() if state.is_invalid: continue user = state.user institution = Institution.objects.get(id=state.school_id) date = state.date_of_visit mobile = state.telephone akshara_staff = RespondentType.objects.get(name='Akshara Staff') question_group = qg_type.questiongroup status = Status.objects.get(name='Active') if user.first_name == 'Unknown' and user.last_name == 'User': answergroup, created = AnswerGroup_Institution.objects.get_or_create( created_by=user, institution=institution, is_verified=True, questiongroup=question_group, date_of_visit=date, status=status, respondent_type=akshara_staff, mobile=mobile) else: answergroup, created = AnswerGroup_Institution.objects.get_or_create( created_by=user, institution=institution, is_verified=True, questiongroup=question_group, date_of_visit=date, status=status, respondent_type=akshara_staff, defaults={'mobile': mobile}) for (question_number, answer) in enumerate(state.answers[1:]): if answer != 'NA': question = get_question(question_number + 1, qg_type.questiongroup) answerinstitution = AnswerInstitution.objects.get_or_create( answergroup=answergroup, question=question, answer=answer, double_entry=0) if qg_type.name == 'gkav4': author = 'GKA SMS' emoji = ':memo:' else: author = None if author: try: post_to_slack( channel='#klp', author=author, message='In Unified DB, %s Valid calls & %s Invalid calls' % (valid_count, invalid_count), emoji=emoji, ) except: print("could not post to slack") pass
def process_state(self, ivrs_type, version): valid_count = 0 # For posting daily notifications to slack. invalid_count = 0 fifteen_minutes = datetime.now() - timedelta(minutes=15) ivrs_type_number_dict = { 'gka' : GKA_SERVER, 'gka-new' : GKA_SERVER, 'ivrs-pri' : PRI, 'ivrs-pre' : PRE, } states = State.objects.filter( ivrs_type=ivrs_type, is_processed=False, date_of_visit__lte=fifteen_minutes ) if states: for state in states: if not sane_state(state, ivrs_type): state.is_invalid = True invalid_count += 1 else: school = School.objects.get(id=state.school_id) telephone = state.telephone date = state.date_of_visit akshara_staff = UserType.objects.get_or_create( name=UserType.AKSHARA_STAFF )[0] question_group = Questiongroup.objects.get( version=version, source__name='ivrs' ) story = Story.objects.create( school=school, is_verified=True, group=question_group, date_of_visit=date, telephone=telephone, user_type=akshara_staff ) for (question_number, answer) in enumerate(state.answers[1:]): if answer != 'NA': question = get_question( question_number+1, ivrs_type_number_dict[ivrs_type] ) answer = Answer.objects.get_or_create( story=story, question=question, text=answer ) valid_count += 1 state.is_processed = True state.save() if ivrs_type in ['gka-new']: author = 'GKA IVRS' elif ivrs_type == 'ivrs-pri': author = 'Primary School IVRS' else: author = None if author: try: post_to_slack( channel='#klp', author=author, message='%s Valid calls & %s Invalid calls' %(valid_count, invalid_count), emoji=':calling:', ) except: pass
def handle(self, *args, **options): report_date = options.get('reportdate', None) if not report_date: print ("Please specify report date") sys.exit() report_date = self.get_reportdate(report_date) surveytag = SurveyTagMapping.objects.all() konnectsurveys = surveytag.filter(tag_id='konnect') survey_ids = konnectsurveys.values_list('survey_id', flat=True) gka_flag = False for s_id in survey_ids: survey_name = Survey.objects.filter(id=s_id).values('name')[0]['name'] questiongroup = QuestionGroup.objects.filter(survey_id=s_id) questgrp_ids = questiongroup.values_list('id', flat=True) ansgrps = AnswerGroup_Institution.objects.filter(date_of_visit__gte =report_date).filter(questiongroup_id__in=questgrp_ids) assess_survey = Survey.objects.filter(id=s_id) admin_id = assess_survey.values_list('admin0', flat=True) boundary_name = Boundary.objects.filter(id__in=admin_id).values('name')[0]['name'] konnect_devices = ansgrps.aggregate(Count('mobile', distinct = True))['mobile__count'] konnect_schools = ansgrps.aggregate(Count('institution', distinct = True))['institution__count'] print("survey_name:", survey_name) print("boundary_name:",boundary_name ) print("konnect_schools:",konnect_schools ) print("konnect_devices:",konnect_devices) print("Surveys:", ansgrps.count()) if s_id in (11,14): gka_flag = True if gka_flag: user_types = ['CRP', 'AS', 'HM'] CRP_count = ansgrps.filter(respondent_type = 'CRP').count() AS_count = ansgrps.filter(respondent_type = 'AS').count() HM_count = ansgrps.filter(respondent_type = 'HM').count() other_count = ansgrps.exclude(respondent_type__in = user_types).count() print("CRP_count", CRP_count) print("AS_count", AS_count) print("HM_count", HM_count) print("other_count", other_count) author = 'ILP Konnect' emoji = ':memo:' try: post_to_slack( channel='#klp', author=author, message='%s: %s: %s Schools, %s Devices and %s Surveys' %(boundary_name,survey_name, konnect_schools, konnect_devices, ansgrps.count()), emoji=emoji, ) except: print ("could not post to slack") pass if gka_flag: try: post_to_slack( channel='#klp', author=author, message='GKA Monitoring: %s by CRP, %s by AS, %s by HM and %s by others' %(CRP_count, AS_count, HM_count, other_count ), emoji=emoji, ) except: print ("could not post to slack") pass gka_flag = False
def process_state(self, qg_type): valid_count = 0 # For posting daily notifications to slack. invalid_count = 0 states = State.objects.filter( qg_type=qg_type, is_processed=False, ) for state in states: if not sane_state(state, qg_type): state.is_invalid = True invalid_count += 1 else: school = School.objects.get(id=state.school_id) telephone = state.telephone date = state.date_of_visit akshara_staff = UserType.objects.get_or_create( name=UserType.AKSHARA_STAFF )[0] question_group = qg_type.questiongroup story = Story.objects.create( school=school, is_verified=True, group=question_group, date_of_visit=date, telephone=telephone, user_type=akshara_staff ) for (question_number, answer) in enumerate(state.answers[1:]): if answer != 'NA': question = get_question( question_number+1, qg_type.questiongroup ) answer = Answer.objects.get_or_create( story=story, question=question, text=answer ) valid_count += 1 state.is_processed = True state.save() if qg_type.name == 'gkav3': author = 'GKA IVRS' emoji = ':calling:' elif qg_type.name == 'gkav4': author = 'GKA SMS' emoji = ':memo:' elif qg_type.name == 'prischoolv1': author = 'Primary School IVRS' emoji = ':calling:' else: author = None if author: try: post_to_slack( channel='#klp', author=author, message='%s Valid calls & %s Invalid calls' %(valid_count, invalid_count), emoji=emoji, ) except: pass
def process_state(self, qg_type): states = State.objects.filter( qg_type=qg_type, is_processed=False, ) valid_count = states.filter(is_invalid=False).count() invalid_count = states.filter(is_invalid=True).count() for state in states: state.is_processed = True state.save() if state.is_invalid: continue user = state.user school = School.objects.get(id=state.school_id) telephone = state.telephone date = state.date_of_visit akshara_staff = UserType.objects.get_or_create( name=UserType.AKSHARA_STAFF )[0] question_group = qg_type.questiongroup story = Story.objects.create( user=user, school=school, is_verified=True, group=question_group, date_of_visit=date, telephone=telephone, user_type=akshara_staff ) for (question_number, answer) in enumerate(state.answers[1:]): if answer != 'NA': question = get_question( question_number+1, qg_type.questiongroup ) answer = Answer.objects.get_or_create( story=story, question=question, text=answer ) if qg_type.name == 'gkav4': author = 'GKA SMS' emoji = ':memo:' else: author = None if author: try: post_to_slack( channel='#klp', author=author, message='%s Valid calls & %s Invalid calls' %(valid_count, invalid_count), emoji=emoji, ) except: pass