def test_calculate_watch_info_with_sufficient_history_values(self): watch_object = init_watch_object() watch_object.val2_history = list(range(57)) watch_info0 = calculate_watch_info(watch_object.val2_history) watch_info1 = calculate_watch_info(watch_object.val2_history, duration=1) watch_info7 = calculate_watch_info(watch_object.val2_history, duration=7) watch_info28 = calculate_watch_info(watch_object.val2_history, duration=28) t0_28 = sum(range(28)) t28_56 = sum(range(28, 56)) self.assertDictEqual(watch_info0, { 'total': 56, 'change': None, 'change_rate': None }) self.assertDictEqual(watch_info1, { 'total': 55, 'change': 55 - 48, 'change_rate': (55 - 48) / 48.0 * 100 }) self.assertDictEqual( watch_info7, { 'total': 364, 'change': 364 - 315, 'change_rate': (364 - 315) / 315.0 * 100 }) self.assertDictEqual( watch_info28, { 'total': t28_56, 'change': t28_56 - t0_28, 'change_rate': float(t28_56 - t0_28) / t0_28 * 100 })
def get_context_data(self, **kwargs): context = super(CCMDashboard, self).get_context_data(**kwargs) service = get_service_instance() CA = service.turnover_history[0] set_counters(service) community_count = Member.objects.filter(is_active=True).count() community_today = calculate_watch_info(service.community_history) community_yesterday = calculate_watch_info(service.community_history, 1) community_last_week = calculate_watch_info(service.community_history, 7) community_last_28_days = calculate_watch_info( service.community_history, 28) community_report = { 'today': community_today, 'yesterday': community_yesterday, 'last_week': community_last_week, 'last_28_days': community_last_28_days } joins = Member.objects.all().order_by('-date_joined')[:5] context['estimated_worth'] = 0 context['community_report'] = community_report context['last_joined'] = joins context['ca'] = CA / community_count return context
def test_calculate_watch_info_with_less_history_values_than_period(self): watch_object = init_watch_object() watch_info0 = calculate_watch_info(watch_object.val2_history) watch_info1 = calculate_watch_info(watch_object.val2_history, duration=1) watch_info7 = calculate_watch_info(watch_object.val2_history, duration=7) watch_info28 = calculate_watch_info(watch_object.val2_history, duration=28) self.assertDictEqual(watch_info0, { 'total': 46, 'change': None, 'change_rate': None }) self.assertDictEqual(watch_info1, { 'total': 23, 'change': None, 'change_rate': None }) self.assertDictEqual(watch_info7, { 'total': 107, 'change': None, 'change_rate': None }) self.assertDictEqual(watch_info28, { 'total': 107, 'change': None, 'change_rate': None })
def get_context_data(self, **kwargs): context = super(Dashboard, self).get_context_data(**kwargs) earnings_today = context['earnings_report']['today'] earnings_yesterday = context['earnings_report']['yesterday'] earnings_last_week = context['earnings_report']['last_week'] earnings_last_28_days = context['earnings_report']['last_28_days'] service = get_service_instance() set_counters(service) orders_count_today = calculate_watch_info( service.transaction_count_history) orders_count_yesterday = calculate_watch_info( service.transaction_count_history, 1) orders_count_last_week = calculate_watch_info( service.transaction_count_history, 7) orders_count_last_28_days = calculate_watch_info( service.transaction_count_history, 28) # AEPO stands for Average Earning Per Order aepo_today = earnings_today['total'] / orders_count_today[ 'total'] if orders_count_today['total'] else 0 aepo_yesterday = earnings_yesterday['total'] / orders_count_yesterday['total']\ if orders_count_yesterday and orders_count_yesterday['total'] else 0 aepo_last_week = earnings_last_week['total'] / orders_count_last_week['total']\ if orders_count_last_week and orders_count_last_week['total'] else 0 aepo_last_28_days = earnings_last_28_days['total'] / orders_count_last_28_days['total']\ if orders_count_last_28_days and orders_count_last_28_days['total'] else 0 orders_report = { 'today': { 'count': orders_count_today['total'] if orders_count_today else 0, 'aepo': '%.2f' % aepo_today, # AEPO: Avg Earning Per Order }, 'yesterday': { 'count': orders_count_yesterday['total'] if orders_count_yesterday else 0, 'aepo': '%.2f' % aepo_yesterday, # AEPO: Avg Earning Per Order }, 'last_week': { 'count': orders_count_last_week['total'] if orders_count_last_week else 0, 'aepo': '%.2f' % aepo_last_week, # AEPO: Avg Earning Per Order }, 'last_28_days': { 'count': orders_count_last_28_days['total'] if orders_count_last_28_days else 0, 'aepo': '%.2f' % aepo_last_28_days, # AEPO: Avg Earning Per Order } } customers_today = slice_watch_objects(Customer) customers_yesterday = slice_watch_objects(Customer, 1) customers_last_week = slice_watch_objects(Customer, 7) customers_last_28_days = slice_watch_objects(Customer, 28) customers_report = { 'today': rank_watch_objects(customers_today, 'turnover_history'), 'yesterday': rank_watch_objects(customers_yesterday, 'turnover_history', 1), 'last_week': rank_watch_objects(customers_last_week, 'turnover_history', 7), 'last_28_days': rank_watch_objects(customers_last_28_days, 'turnover_history', 28) } context['orders_report'] = orders_report context['customers_report'] = customers_report return context
def get_context_data(self, **kwargs): context = super(Dashboard, self).get_context_data(**kwargs) service = get_service_instance() set_counters(service) earnings_today = context['earnings_report']['today'] earnings_yesterday = context['earnings_report']['yesterday'] earnings_last_week = context['earnings_report']['last_week'] earnings_last_28_days = context['earnings_report']['last_28_days'] transaction_count = calculate_watch_info(service.transaction_count_history) transaction_count_yesterday = calculate_watch_info(service.transaction_count_history, 1) transaction_count_last_week = calculate_watch_info(service.transaction_count_history, 7) transaction_count_last_28_days = calculate_watch_info(service.transaction_count_history, 28) # AEPT stands for Average Earning Per Transaction aept_today = earnings_today['total'] / transaction_count['total'] if transaction_count['total'] else 0 aept_yesterday = earnings_yesterday['total'] / transaction_count_yesterday['total']\ if transaction_count_yesterday and transaction_count_yesterday['total'] else 0 aept_last_week = earnings_last_week['total'] / transaction_count_last_week['total']\ if transaction_count_last_week and transaction_count_last_week['total'] else 0 aept_last_28_days = earnings_last_28_days['total'] / transaction_count_last_28_days['total']\ if transaction_count_last_28_days and transaction_count_last_28_days['total'] else 0 transactions_report = { 'today': { 'count': transaction_count['total'] if transaction_count else 0, 'aept': '%.2f' % aept_today, # AEPT: Avg Earning Per Transaction }, 'yesterday': { 'count': transaction_count_yesterday['total'] if transaction_count_yesterday else 0, 'aept': '%.2f' % aept_yesterday, }, 'last_week': { 'count': transaction_count_last_week['total'] if transaction_count_last_week else 0, 'aept': '%.2f' % aept_last_week, }, 'last_28_days': { 'count': transaction_count_last_28_days['total']if transaction_count_last_28_days else 0, 'aept': '%.2f' % aept_last_28_days, } } customers = list(Service.objects.all()) for customer in customers: set_counters(customer) customers_report = { 'today': rank_watch_objects(customers, 'earnings_history'), 'yesterday': rank_watch_objects(customers, 'earnings_history', 1), 'last_week': rank_watch_objects(customers, 'earnings_history', 7), 'last_28_days': rank_watch_objects(customers, 'earnings_history', 28) } apps = list(Application.objects.all()) for app in apps: set_counters(app) apps_report = { 'today': rank_watch_objects(apps, 'earnings_history'), 'yesterday': rank_watch_objects(apps, 'earnings_history', 1), 'last_week': rank_watch_objects(apps, 'earnings_history', 7), 'last_28_days': rank_watch_objects(apps, 'earnings_history', 28) } context['transactions_report'] = transactions_report context['customers_report'] = customers_report context['apps_report'] = apps_report return context
def get_context_data(self, **kwargs): context = super(LogicomDashboard, self).get_context_data(**kwargs) operator_profile = get_service_instance().config set_counters(operator_profile) earnings_today = context['earnings_report']['today'] earnings_yesterday = context['earnings_report']['yesterday'] earnings_last_week = context['earnings_report']['last_week'] earnings_last_28_days = context['earnings_report']['last_28_days'] orders_count_today = calculate_watch_info( operator_profile.orders_count_history) orders_count_yesterday = calculate_watch_info( operator_profile.orders_count_history, 1) orders_count_last_week = calculate_watch_info( operator_profile.orders_count_history, 7) orders_count_last_28_days = calculate_watch_info( operator_profile.orders_count_history, 28) # AEPO stands for Average Earning Per Order aepo_today = earnings_today['total'] / orders_count_today[ 'total'] if orders_count_today['total'] else 0 aepo_yesterday = earnings_yesterday['total'] / orders_count_yesterday['total']\ if orders_count_yesterday and orders_count_yesterday['total'] else 0 aepo_last_week = earnings_last_week['total'] / orders_count_last_week['total']\ if orders_count_last_week and orders_count_last_week['total'] else 0 aepo_last_28_days = earnings_last_28_days['total'] / orders_count_last_28_days['total']\ if orders_count_last_28_days and orders_count_last_28_days['total'] else 0 orders_report = { 'today': { 'count': orders_count_today['total'] if orders_count_today else 0, 'aepo': '%.2f' % aepo_today, # AEPO: Avg Earning Per Order }, 'yesterday': { 'count': orders_count_yesterday['total'] if orders_count_yesterday else 0, 'aepo': '%.2f' % aepo_yesterday, # AEPO: Avg Earning Per Order }, 'last_week': { 'count': orders_count_last_week['total'] if orders_count_last_week else 0, 'aepo': '%.2f' % aepo_last_week, # AEPO: Avg Earning Per Order }, 'last_28_days': { 'count': orders_count_last_28_days['total'] if orders_count_last_28_days else 0, 'aepo': '%.2f' % aepo_last_28_days, # AEPO: Avg Earning Per Order } } providers = list( OperatorProfile.objects.filter( business_type=OperatorProfile.PROVIDER)) for provider in providers: set_counters(provider) providers_report = { 'today': rank_watch_objects(providers, 'earnings_history'), 'yesterday': rank_watch_objects(providers, 'earnings_history', 1), 'last_week': rank_watch_objects(providers, 'earnings_history', 7), 'last_28_days': rank_watch_objects(providers, 'earnings_history', 28) } context['orders_report'] = orders_report context['providers_report'] = providers_report return context
def get_context_data(self, **kwargs): context = super(KakocaseDashboardBase, self).get_context_data(**kwargs) operator_profile = get_service_instance().config set_counters(operator_profile) earnings_today = calculate_watch_info( operator_profile.earnings_history) earnings_yesterday = calculate_watch_info( operator_profile.earnings_history, 1) earnings_last_week = calculate_watch_info( operator_profile.earnings_history, 7) earnings_last_28_days = calculate_watch_info( operator_profile.earnings_history, 28) orders_count_today = calculate_watch_info( operator_profile.orders_count_history) orders_count_yesterday = calculate_watch_info( operator_profile.orders_count_history, 1) orders_count_last_week = calculate_watch_info( operator_profile.orders_count_history, 7) orders_count_last_28_days = calculate_watch_info( operator_profile.orders_count_history, 28) # AEPO stands for Average Earning Per Order aepo_today = earnings_today['total'] / orders_count_today[ 'total'] if orders_count_today['total'] else 0 aepo_yesterday = earnings_yesterday['total'] / orders_count_yesterday['total']\ if orders_count_yesterday and orders_count_yesterday['total'] else 0 aepo_last_week = earnings_last_week['total'] / orders_count_last_week['total']\ if orders_count_last_week and orders_count_last_week['total'] else 0 aepo_last_28_days = earnings_last_28_days['total'] / orders_count_last_28_days['total']\ if orders_count_last_28_days and orders_count_last_28_days['total'] else 0 earnings_report = { 'today': earnings_today, 'yesterday': earnings_yesterday, 'last_week': earnings_last_week, 'last_28_days': earnings_last_28_days } orders_report = { 'today': { 'count': orders_count_today['total'] if orders_count_today else 0, 'aepo': '%.2f' % aepo_today, # AEPO: Avg Earning Per Order }, 'yesterday': { 'count': orders_count_yesterday['total'] if orders_count_yesterday else 0, 'aepo': '%.2f' % aepo_yesterday, # AEPO: Avg Earning Per Order }, 'last_week': { 'count': orders_count_last_week['total'] if orders_count_last_week else 0, 'aepo': '%.2f' % aepo_last_week, # AEPO: Avg Earning Per Order }, 'last_28_days': { 'count': orders_count_last_28_days['total'] if orders_count_last_28_days else 0, 'aepo': '%.2f' % aepo_last_28_days, # AEPO: Avg Earning Per Order } } categories = list(ProductCategory.objects.all()) for category in categories: set_counters(category) categories_report = { 'today': rank_watch_objects(categories, 'earnings_history'), 'yesterday': rank_watch_objects(categories, 'earnings_history', 1), 'last_week': rank_watch_objects(categories, 'earnings_history', 7), 'last_28_days': rank_watch_objects(categories, 'earnings_history', 28) } products = list(Product.objects.filter(visible=True, in_trash=False)) for product in products: set_counters(product) products_report = { 'today': rank_watch_objects(products, 'units_sold_history'), 'yesterday': rank_watch_objects(products, 'units_sold_history', 1), 'last_week': rank_watch_objects(products, 'units_sold_history', 7), 'last_28_days': rank_watch_objects(products, 'units_sold_history', 28) } context['earnings_report'] = earnings_report context['orders_report'] = orders_report context['categories_report'] = categories_report context['products_report'] = products_report context['earnings_history'] = operator_profile.earnings_history[-30:] context[ 'earnings_history_previous_month'] = operator_profile.earnings_history[ -60:-30] context[ 'transaction_count_history'] = operator_profile.orders_count_history[ -30:] return context
def get_context_data(self, **kwargs): context = super(DashboardBase, self).get_context_data(**kwargs) service = self.get_service(**kwargs) set_counters(service) earnings_today = calculate_watch_info(service.earnings_history) earnings_yesterday = calculate_watch_info(service.earnings_history, 1) earnings_last_week = calculate_watch_info(service.earnings_history, 7) earnings_last_28_days = calculate_watch_info(service.earnings_history, 28) earnings_report = { 'today': earnings_today, 'yesterday': earnings_yesterday, 'last_week': earnings_last_week, 'last_28_days': earnings_last_28_days } tx_count_today = calculate_watch_info( service.transaction_count_history) tx_count_yesterday = calculate_watch_info( service.transaction_count_history, 1) tx_count_last_week = calculate_watch_info( service.transaction_count_history, 7) tx_count_last_28_days = calculate_watch_info( service.transaction_count_history, 28) # AEPT stands for Average Earning Per Transaction aept_today = earnings_today['total'] / tx_count_today[ 'total'] if tx_count_today['total'] else 0 aept_yesterday = earnings_yesterday['total'] / tx_count_yesterday['total']\ if tx_count_yesterday and tx_count_yesterday['total'] else 0 aept_last_week = earnings_last_week['total'] / tx_count_last_week['total']\ if tx_count_last_week and tx_count_last_week['total'] else 0 aept_last_28_days = earnings_last_28_days['total'] / tx_count_last_28_days['total']\ if tx_count_last_28_days and tx_count_last_28_days['total'] else 0 transactions_report = { 'today': { 'count': tx_count_today['total'] if tx_count_today else 0, 'aepo': '%.2f' % aept_today, # AEPO: Avg Earning Per Order }, 'yesterday': { 'count': tx_count_yesterday['total'] if tx_count_yesterday else 0, 'aepo': '%.2f' % aept_yesterday, # AEPO: Avg Earning Per Order }, 'last_week': { 'count': tx_count_last_week['total'] if tx_count_last_week else 0, 'aepo': '%.2f' % aept_last_week, # AEPO: Avg Earning Per Order }, 'last_28_days': { 'count': tx_count_last_28_days['total'] if tx_count_last_28_days else 0, 'aepo': '%.2f' % aept_last_28_days, # AEPO: Avg Earning Per Order } } qs = CashOutRequest.objects.using('wallets').filter( service_id=service.id, status=CashOutRequest.PAID).order_by('-id') last_cash_out = qs[0] if qs.count() >= 1 else None if last_cash_out: # Re-transform created_on into a datetime object try: last_cash_out.created_on = datetime(*strptime( last_cash_out.created_on[:19], '%Y-%m-%d %H:%M:%S')[:6]) except TypeError: pass if last_cash_out.amount_paid: last_cash_out.amount = last_cash_out.amount_paid try: balance = 0 for wallet in OperatorWallet.objects.using('wallets').filter( nonrel_id=service.id): balance += wallet.balance context['balance'] = balance except: pass context['earnings_report'] = earnings_report context['transactions_report'] = transactions_report context['transactions_count_title'] = self.transactions_count_title context[ 'transactions_avg_revenue_title'] = self.transactions_avg_revenue_title context['last_cash_out'] = last_cash_out context['earnings_history'] = service.earnings_history[-30:] context['earnings_history_previous_month'] = service.earnings_history[ -60:-30] context[ 'transaction_count_history'] = service.transaction_count_history[ -30:] context['CRNCY'] = Currency.active.base() return context
def send_ccm_report(): app = Application.objects.get(slug='kakocase') service_list = Service.objects.select_related('member').filter(app=app) connection = mail.get_connection() try: connection.open() except: logger.error("CCM Report: Connexion error", exc_info=True) for service in service_list: try: db = service.database add_database(db) try: service_original = Service.objects.using(db).get(pk=service.id) except Service.DoesNotExist: continue set_counters(service_original) ccm_watch = calculate_watch_info( service_original.community_history, 15) member = service.member if member.language: activate(member.language) else: activate('en') try: last_mail = CCMMonitoringMail.objects.filter( service=service).order_by('-id')[0] except IndexError: last_mail = None if last_mail: now = datetime.now() diff = now - last_mail.created_on if diff.days <= 14: if ccm_watch['change_rate'] < 100: continue ccm_watch['total_member_count'] = Member.objects.using( db).all().count() ccm_watch['last_mail'] = last_mail ccm_watch['service_name'] = service.project_name subject = _("Community progression report") html_content = get_mail_content( subject, template_name='monitoring/mails/community_progression.html', extra_context=ccm_watch) cc = [ sudo.email for sudo in Member.objects.using(db).filter( is_superuser=True).exclude(pk=member.id) ] sender = 'ikwen <*****@*****.**>' msg = EmailMessage(subject, html_content, sender, [member.email]) msg.content_subtype = "html" msg.cc = cc if not msg.send(): logger.error("CCM Report not sent to %s for service %s" % (member.email, service), exc_info=True) CCMMonitoringMail.objects.create(service=service, subject=subject) except: logger.error("Could not process CCM Monitoring for service %s" % service, exc_info=True) continue try: connection.close() except: pass
def get_context_data(self, **kwargs): context = super(Dashboard, self).get_context_data(**kwargs) discipline_report = [] school_year = get_school_year(self.request) for item in DisciplineItem.objects.filter(is_active=True): discipline_report_obj, update = DisciplineReport.objects\ .get_or_create(discipline_item=item, level=None, classroom=None, school_year=school_year) set_counters(discipline_report_obj) students_yesterday = slice_watch_objects(StudentDisciplineReport, 1, 'last_add_on') students_last_week = slice_watch_objects(StudentDisciplineReport, 7, 'last_add_on') students_last_28_days = slice_watch_objects(StudentDisciplineReport, 28, 'last_add_on') item_report_obj = { 'item': _(item.name), 'report': { 'yesterday': { 'summary': calculate_watch_info(discipline_report_obj.total_history, 1), 'student_list': rank_watch_objects(students_yesterday, 'count_history') }, 'last_week': { 'summary': calculate_watch_info(discipline_report_obj.total_history, 7), 'student_list': rank_watch_objects(students_last_week, 'count_history') }, 'last_28_days': { 'summary': calculate_watch_info(discipline_report_obj.total_history, 28), 'student_list': rank_watch_objects(students_last_28_days, 'count_history') } } } discipline_report.append(item_report_obj) # Classes report classes_report = { 'today': {'count': 0, 'hours': 0}, 'yesterday': {'count': 0, 'hours': 0}, 'last_week': {'count': 0, 'hours': 0}, 'last_28_days': {'count': 0, 'hours': 0} } for report in LessonReport.objects.filter(school_year=school_year): set_counters(report) classes_report['today']['count'] += calculate_watch_info(report.count_history)['total'] classes_report['today']['hours'] += calculate_watch_info(report.hours_count_history)['total'] for report in LessonReport.objects.filter(school_year=school_year): set_counters(report) classes_report['yesterday']['count'] += calculate_watch_info(report.count_history, 1)['total'] classes_report['yesterday']['hours'] += calculate_watch_info(report.hours_count_history, 1)['total'] for report in LessonReport.objects.filter(school_year=school_year): set_counters(report) classes_report['last_week']['count'] += calculate_watch_info(report.count_history, 7)['total'] classes_report['last_week']['hours'] += calculate_watch_info(report.hours_count_history, 7)['total'] for report in LessonReport.objects.filter(school_year=school_year): set_counters(report) classes_report['last_28_days']['count'] += calculate_watch_info(report.count_history, 28)['total'] classes_report['last_28_days']['hours'] += calculate_watch_info(report.hours_count_history, 28)['total'] classroom_count = Classroom.objects.filter(school_year=school_year).count() for val in classes_report.values(): val['count'] = round(float(val['count'])/classroom_count, 2) if classroom_count else 0 val['hours'] = round(float(val['hours'])/classroom_count, 2) if classroom_count else 0 # Session scores report session_queryset = Session.objects.filter(Q(is_active=False) | Q(is_current=True), school_year=school_year) all_session_list = [] for session in session_queryset: if Score.objects.filter(session=session).count() > 0: all_session_list.append(session) session_group_list = list(SessionGroup.objects.filter(school_year=school_year)) session_group_count = 0 if len(all_session_list) >= 2: if len(session_group_list) >= 1: all_session_list.insert(2, session_group_list[0]) session_group_count += 1 if len(all_session_list) >= 5: if len(session_group_list) >= 2: all_session_list.insert(5, session_group_list[1]) session_group_count += 1 if len(all_session_list) >= 8: if len(session_group_list) >= 3: all_session_list.append(session_group_list[2]) session_group_count += 1 session_report, update = SessionReport.objects\ .get_or_create(level=None, classroom=None, subject=None, school_year=school_year) session_group_report, update = SessionGroupReport.objects\ .get_or_create(level=None, classroom=None, subject=None, school_year=school_year) # Billing report pending_invoice_count = Invoice.objects.filter(status=Invoice.PENDING, school_year=school_year).count() try: aggr = Invoice.objects.filter(status=Invoice.PENDING, school_year=school_year).aggregate(Sum('amount')) pending_invoice_amount = aggr['amount__sum'] except IndexError: pending_invoice_amount = 0 pending_invoice_data_list = [] for classroom in Classroom.objects.filter(school_year=school_year): student_list = list(classroom.student_set.filter(is_excluded=False)) count = Invoice.objects.filter(student__in=student_list, status=Invoice.PENDING).count() if count > 0: obj = { 'classroom': classroom, 'count': Invoice.objects.filter(student__in=student_list, status=Invoice.PENDING).count() } pending_invoice_data_list.append(obj) pending_invoice_data_list.sort(cmp=lambda x, y: 1 if x['count'] < y['count'] else -1) pending_invoice_data = { 'count': pending_invoice_count, 'amount': pending_invoice_amount, 'list': pending_invoice_data_list } context['discipline_report'] = discipline_report context['all_session_list'] = all_session_list try: context['selected_session'] = all_session_list[-1] context['selected_session_order_number'] = all_session_list[-1].order_number except: pass context['session_report'] = session_report context['session_group_report'] = session_group_report context['classes_report'] = classes_report context['range_session_group'] = range(session_group_count) context['range_session'] = range(session_queryset.count()) context['pending_invoice_data'] = pending_invoice_data return context