def test_statis_overview(self): start_date = timezone.localtime().replace(day=self.days[0], hour=0, minute=0, second=0) end_date = timezone.localtime().replace(day=self.days[-1], hour=23, minute=59, second=59) res = dict() res.update(ChatBotSession.statis_overview(start_date, end_date)) res.update(StudentChatHistory.statis_overview(start_date, end_date)) print('Overview:') print(res) self.assertEqual(res['total_access_count'], 70) self.assertEqual(res['access_office_hr_count'], 27) self.assertEqual(res['polyu_student_count'], 70 / 2) self.assertEqual(res['non_polyu_student_count'], 70 / 2) self.assertListEqual(list(res.keys()), [ 'total_access_count', 'access_office_hr_count', 'polyu_student_count', 'non_polyu_student_count', 'score_green_count', 'score_yellow_count', 'score_red_count', 'mh101_access_count', 'poss_access_count', 'online_chat_access_count', 'successful_chat_count' ]) self.assertEqual( res['score_green_count'] + res['score_yellow_count'] + res['score_red_count'], 70)
def get_statistics(request): data = json.loads(request.body) from_date = data.get('fromDate') to_date = data.get('toDate') f = datetime.strptime(from_date, '%Y-%m-%d') t = datetime.strptime(to_date, '%Y-%m-%d') res = { **ChatBotSession.statis_overview(f, t), **StudentChatHistory.statis_overview(f, t) } return JsonResponse(res, status=200)
def export_statistics(request): data = json.loads(request.body) from_date = data.get('fromDate') to_date = data.get('toDate') f = datetime.strptime(from_date, '%Y-%m-%d') t = datetime.strptime(to_date, '%Y-%m-%d') logger.info('Query from corresponding tables.') chatbot_stat_data = ChatBotSession.objects \ .filter(start_time__range=[f, t]) \ .order_by('start_time') \ .all() online_chat_stat_data = StudentChatHistory.objects \ .filter(chat_request_time__range=[f, t]) \ .order_by('chat_request_time') \ .all() overall_stat = { **ChatBotSession.statis_overview(f, t), **StudentChatHistory.statis_overview(f, t) } chatbot_stat_file = write_chatbot_stat(list(chatbot_stat_data)) online_chat_stat_file = write_online_chat_stat(list(online_chat_stat_data)) overall_stat_file = write_overall_stat(overall_stat, f, t) logger.info('Zip all files.') output = write_zip_files({ 'chatbot_statistics.xlsx': chatbot_stat_file, 'online_chat_statistics.xlsx': online_chat_stat_file, 'overall_statistics.xlsx': overall_stat_file }) response = HttpResponse(output, content_type='application/zip') response['Content-Disposition'] = 'attachment; filename="statistics.zip"' return response