def handle(self, *args, **options): path = options['path'] month = str(options['date']) if not month: self.stdout.write("month invalid.") return month_obj = datetime.datetime.strptime(month, "%Y%m") res_data = seafevents_api.get_all_users_traffic_by_month( month_obj, -1, -1) data_list = [] head = [_("Time"), _("User"), _("Web Download") + ('(MB)'), \ _("Sync Download") + ('(MB)'), _("Link Download") + ('(MB)'), \ _("Web Upload") + ('(MB)'), _("Sync Upload") + ('(MB)'), \ _("Link Upload") + ('(MB)')] for data in res_data: web_download = byte_to_mb(data['web_file_download']) sync_download = byte_to_mb(data['sync_file_download']) link_download = byte_to_mb(data['link_file_download']) web_upload = byte_to_mb(data['web_file_upload']) sync_upload = byte_to_mb(data['sync_file_upload']) link_upload = byte_to_mb(data['link_file_upload']) row = [month, data['user'], web_download, sync_download, \ link_download, web_upload, sync_upload, link_upload] data_list.append(row) excel_name = "User-Traffic-%s" % month wb = write_xls(excel_name, head, data_list) wb.save(posixpath.join(path, '%s.xlsx' % excel_name)) if path else wb.save('%s.xlsx' % excel_name)
def get(self, request): if not request.user.admin_permissions.can_view_statistic(): return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') month = request.GET.get("month", "") if not month: error_msg = "month invalid." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: month_obj = datetime.datetime.strptime(month, "%Y%m") except: error_msg = "Month %s invalid" % month return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: res_data = seafevents_api.get_all_users_traffic_by_month( month_obj, -1, -1) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) data_list = [] head = [_("Time"), _("User"), _("Web Download") + ('(MB)'), \ _("Sync Download") + ('(MB)'), _("Link Download") + ('(MB)'), \ _("Web Upload") + ('(MB)'), _("Sync Upload") + ('(MB)'), \ _("Link Upload") + ('(MB)')] for data in res_data: web_download = byte_to_mb(data['web_file_download']) sync_download = byte_to_mb(data['sync_file_download']) link_download = byte_to_mb(data['link_file_download']) web_upload = byte_to_mb(data['web_file_upload']) sync_upload = byte_to_mb(data['sync_file_upload']) link_upload = byte_to_mb(data['link_file_upload']) row = [month, data['user'], web_download, sync_download, \ link_download, web_upload, sync_upload, link_upload] data_list.append(row) excel_name = "User Traffic %s" % month try: wb = write_xls(excel_name, head, data_list) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) response = HttpResponse(content_type='application/ms-excel') response[ 'Content-Disposition'] = 'attachment; filename="%s.xlsx"' % excel_name wb.save(response) return response
def get(self, request): if not request.user.admin_permissions.can_view_statistic(): return api_error(status.HTTP_403_FORBIDDEN, 'Permission denied.') month = request.GET.get("month", "") if not month: error_msg = "month invalid." return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: month_obj = datetime.datetime.strptime(month, "%Y%m") except Exception as e: logger.error(e) error_msg = "month %s invalid" % month return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: page = int(request.GET.get('page', '1')) per_page = int(request.GET.get('per_page', '25')) except ValueError: page = 1 per_page = 25 start = (page - 1) * per_page # get one more item than per_page, to judge has_next_page try: traffics = seafevents_api.get_all_users_traffic_by_month(month_obj, start, start + per_page + 1) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) if len(traffics) == per_page + 1: has_next_page = True traffics = traffics[:per_page] else: has_next_page = False user_monthly_traffic_list = [] for traffic in traffics: info = {} info['email'] = traffic['user'] info['name'] = email2nickname(traffic['user']) info['sync_file_upload'] = traffic['sync_file_upload'] info['sync_file_download'] = traffic['sync_file_download'] info['web_file_upload'] = traffic['web_file_upload'] info['web_file_download'] = traffic['web_file_download'] info['link_file_upload'] = traffic['link_file_upload'] info['link_file_download'] = traffic['link_file_download'] user_monthly_traffic_list.append(info) return Response({ 'user_monthly_traffic_list': user_monthly_traffic_list, 'has_next_page': has_next_page })