Esempio n. 1
0
def client_download_status_code_trend(request, start_time, end_time,
                                      domain_ids):
    """下载计费数据"""

    msg = ''
    status = False

    base_code = CDNConf.STATUS_CODE_TREND

    try:
        user = request.user

        msg, start_time, end_time = handle_req_time(start_time, end_time)
        if msg:
            assert False

        if domain_ids == '-':
            domain_query = Domain.objects.filter(user=user)
        else:
            domain_ids = domain_ids.split(',')
            domain_query = Domain.objects.filter(id__in=domain_ids)

        domain_list = [i.domain for i in domain_query]

        opts = []

        __, __, __, all_trend_data, __ = get_domain_status_code(
            user.id, domain_list, start_time, end_time, opts)

        excel_name = '%s-status_code_trend.xls' % user.username
        sheet_name = 'status_code_trend'

        start_time = timestamp_to_str(start_time)
        end_time = timestamp_to_str(end_time)

        row, excel_path, worksheet, workbook = make_base_excel(
            excel_name, sheet_name, domain_list, start_time, end_time)

        base_row = row + 2
        worksheet.write(base_row, 0, label='status_code')
        worksheet.write(base_row, 1, label='total')
        worksheet.write(base_row, 2, label='Percentage')

        row += 3
        """
        {'2xx': 2528195, '3xx': 41, '4xx': 1, '5xx': 0}
        {'CC': {'2xx': 2528195, '3xx': 41, '4xx': 1, '5xx': 0}}
        """

        sum_req = 0
        for i in all_trend_data:
            sum_req += all_trend_data[i]

        for code in base_code:
            num = all_trend_data[code]
            ratio = '%.4f' % (num / sum_req * 100)

            worksheet.write(row, 0, label=code)
            worksheet.write(row, 1, label=num)
            worksheet.write(row, 2, label='{}%'.format(ratio))

            row += 1

        workbook.save(excel_path)

    except AssertionError:
        excel_name = 'error_documents'
        excel_path = make_error_file(excel_name, _(msg))

    response = StreamingHttpResponse(file_iterator(excel_path))
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="{0}"'.format(
        excel_name)

    return response
Esempio n. 2
0
def admin_cdn_status_code_data(request):
    """管理员查看状态码统计数据"""

    msg = ''
    status = False

    res = {'status': status, 'msg': msg}

    start_time = request.POST.get('start_time', 0)
    end_time = request.POST.get('end_time', 0)
    user_id = request.POST.get('user_id', '')
    domain_ids = request.POST.get('domain_list', '[]')
    opts = request.POST.get('opts', '[]')

    all_status_code = []  # 全部状态码数据
    all_trend_result = []
    status_code_table = []
    trend_table = []
    try:
        user_id = int_check(user_id)
        if user_id is None:
            msg = af.PARAME_ERROR
            assert False

        msg, start_time, end_time = handle_req_time(start_time, end_time)
        if msg:
            assert False

        domain_ids = handle_list(domain_ids, dec_int=True)
        if domain_ids:
            opts = handle_list(opts)

            domain_query = Domain.objects.filter(id__in=domain_ids)
            domain_list = [i.domain for i in domain_query]

            (all_status_code, opt_status_code, all_trend_result,
             all_trend_data,
             opt_trend_data) = get_domain_status_code(user_id, domain_list,
                                                      start_time, end_time,
                                                      opts)

            all_opt_status_code = copy.deepcopy(all_status_code)
            all_opt_status_code['opt'] = 'all'
            status_code_table.append(all_opt_status_code)

            for opt in opt_status_code:
                opt_data = copy.deepcopy(opt_status_code[opt])
                opt_data['opt'] = opt
                status_code_table.append(opt_data)

                opt_ratio_data = {}
                for code in opt_status_code[opt]:
                    opt_num = opt_status_code[opt][code]
                    base_num = all_status_code.get(code, 0)

                    opt_ratio = '%.4f' % (opt_num / base_num *
                                          100 if base_num else 0)

                    opt_ratio_data[code] = opt_ratio

                opt_ratio_data['opt'] = opt
                status_code_table.append(opt_ratio_data)

            trend_table = []

            all_opt_trend_data = copy.deepcopy(all_trend_data)
            all_opt_trend_data['opt'] = 'all'
            trend_table.append(all_opt_trend_data)

            for opt in opt_trend_data:
                opt_data = copy.deepcopy(opt_trend_data[opt])
                opt_data['opt'] = opt
                trend_table.append(opt_data)

                opt_ratio_data = {}
                for code in opt_trend_data[opt]:
                    opt_num = opt_trend_data[opt][code]
                    base_num = all_trend_data.get(code, 0)

                    opt_ratio = '%.4f' % (opt_num / base_num *
                                          100 if base_num else 0)

                    opt_ratio_data[code] = opt_ratio

                opt_ratio_data['opt'] = opt
                trend_table.append(opt_ratio_data)

        status = True

    except Exception as e:
        print(e)
        res['msg'] = _(msg)

    res['all_status_code'] = all_status_code
    res['status_code_table'] = status_code_table

    res['all_trend_result'] = all_trend_result
    res['trend_table'] = trend_table

    res['status'] = status

    return json_response(res)
Esempio n. 3
0
def client_cdn_status_code_data(request):
    """客户端查看状态码统计数据"""
    import time
    start = time.time()
    msg = ''
    status = False

    res = {'status': status, 'msg': msg}

    user = request.user

    start_time = request.POST.get('start_time', 0)
    end_time = request.POST.get('end_time', 0)
    domain_ids = request.POST.get('domain_list', '[]')

    base_code = CDNConf.STATUS_CODE

    all_status_code = []  # 全部状态码数据
    status_code_table = []  # 状态码表格数据

    all_trend_result = []

    trend_table_data = []

    try:
        msg, start_time, end_time = handle_req_time(start_time, end_time)
        if msg:
            assert False

        domain_ids = handle_list(domain_ids, dec_int=True)

        if domain_ids:
            domain_query = Domain.objects.filter(id__in=domain_ids)
        else:
            domain_query = Domain.objects.filter(user=user)

        domain_list = [i.domain for i in domain_query]

        if domain_list:

            (all_status_code, __, all_trend_result, all_trend_data,
             __) = get_domain_status_code(user.id, domain_list, start_time,
                                          end_time, [])

            sum_req = 0
            for code in all_status_code:
                sum_req += all_status_code[code]

            for code in base_code:
                num = all_status_code[code]
                ratio = (num / sum_req) * 100 if sum_req else 0

                code_dict = {'code': code, 'num': num, 'ratio': '%.4f' % ratio}
                status_code_table.append(code_dict)

            trend_key = list(all_trend_data.keys())
            trend_key.sort()

            sum_cnt = 0
            for i in all_trend_data:
                sum_cnt += all_trend_data[i]

            for key in trend_key:
                code_cnt = all_trend_data[key]

                ratio = code_cnt / sum_cnt * 100 if sum_cnt else 0

                temp_dict = {
                    'code': key,
                    'count': code_cnt,
                    'ratio': '%.4f' % ratio
                }
                trend_table_data.append(temp_dict)

        status = True

    except Exception as e:
        print(e)
        res['msg'] = _(msg)

    print(444444444, time.time() - start)
    res['all_status_code'] = all_status_code
    res['status_code_table'] = status_code_table

    res['all_trend_result'] = all_trend_result
    res['all_trend_data'] = trend_table_data
    res['status'] = status

    return json_response(res)
Esempio n. 4
0
def admin_download_status_code_trend(request, start_time, end_time, user_id,
                                     domain_ids, opts):
    """下载计费数据"""

    msg = ''
    status = False

    base_code = CDNConf.STATUS_CODE_TREND

    try:
        user_id = int_check(user_id)
        if user_id is None:
            msg = af.PARAME_ERROR
            assert False
        user = UserProfile.objects.filter(id=user_id).first()

        msg, start_time, end_time = handle_req_time(start_time, end_time)
        if msg:
            assert False

        domain_ids = domain_ids.split(',')
        domain_query = Domain.objects.filter(id__in=domain_ids)
        domain_list = [i.domain for i in domain_query]

        opts = opts.split(',')

        _, _, _, all_trend_data, opt_trend_data = get_domain_status_code(
            user_id, domain_list, start_time, end_time, opts)

        excel_name = '%s-status_code_trend.xls' % user.username
        sheet_name = 'status_code_trend'

        start_time = timestamp_to_str(start_time)
        end_time = timestamp_to_str(end_time)

        row, excel_path, worksheet, workbook = make_base_excel(
            excel_name, sheet_name, domain_list, start_time, end_time)

        base_row = row + 2
        worksheet.write(base_row, 0, label='status_code')
        worksheet.write(base_row, 1, label='total')
        worksheet.write(base_row, 2, label='Percentage')

        opt_col = 3
        opt_list = []
        for opt in opt_trend_data:
            worksheet.write(base_row, opt_col, label='{} total'.format(opt))
            worksheet.write(base_row,
                            opt_col + 1,
                            label='{} percentage'.format(opt))

            opt_list.append(opt)

            opt_col += 1

        row += 3
        """
        {'2xx': 2528195, '3xx': 41, '4xx': 1, '5xx': 0}
        {'CC': {'2xx': 2528195, '3xx': 41, '4xx': 1, '5xx': 0}}
        """

        sum_req = 0
        for i in all_trend_data:
            sum_req += all_trend_data[i]

        for code in base_code:
            num = all_trend_data[code]
            ratio = '%.4f' % (num / sum_req * 100)

            worksheet.write(row, 0, label=code)
            worksheet.write(row, 1, label=num)
            worksheet.write(row, 2, label='{}%'.format(ratio))

            col = 3
            for opt in opt_list:
                opt_num = opt_trend_data[opt][code]
                worksheet.write(row, col, label=opt_num)

                opt_ratio = '%.4f' % (opt_num / num * 100 if num else 0)
                worksheet.write(row, col + 1, label='{}%'.format(opt_ratio))

                col += 1

            row += 1

        workbook.save(excel_path)

    except AssertionError:
        excel_name = 'error_documents'
        excel_path = make_error_file(excel_name, _(msg))

    response = StreamingHttpResponse(file_iterator(excel_path))
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="{0}"'.format(
        excel_name)

    return response