Beispiel #1
0
def get_domain_log(request, user):
    """获取域名日志"""
    start_time = request.POST.get('start_time', 0)
    end_time = request.POST.get('end_time', 0)
    domain = request.POST.get('domain', '')

    msg = ''
    result_list = []
    pagination = {}

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

        body = {
            'user_id': user.id,
            'start_time': start_time,
            'end_time': end_time,
            'domain': domain,
        }
        api_res = APIUrl.post_link('cdn_domain_log', body)
        return_code = api_res.get('return_code', 0)

        if return_code != 0:
            msg = af.PARAME_ERROR
            assert False

        result_list = api_res.get('result', [])

        all_log_list = []
        for channel in result_list:

            url_info = urlparse(channel)

            protocol = url_info.scheme

            log_list = result_list[channel]
            for log in log_list:
                # print(log['time'])
                """2019 10 14 20"""
                time_str = '%s-%s-%s %s:%s' % (
                    log['time'][:4], log['time'][4:6], log['time'][6:8],
                    log['time'][8:10], '00')

                log['time'] = time_str
                log['protocol'] = protocol

                all_log_list.append(log)

        result_list = sorted(all_log_list,
                             key=lambda x: (x['time'], x['protocol']))

        msg, result_list, pagination = data_pagination(request, result_list)
    except AssertionError:
        pass

    return msg, result_list, pagination
Beispiel #2
0
def admin_cdn_flux_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_flux_list = []  # 计费图表数据
    sum_cdn_flux = 0  # 总计费数据(MB)
    sum_src_flux = 0  # 总回源数据(MB)
    max_cdn = 0  # 峰值计费值(M/bps)

    table_data = []  # 每日表格数据

    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_flux_list, sum_cdn_flux, sum_src_flux, max_cdn, max_src,
             table_data, opt_result) = get_domain_flux(user_id, domain_list,
                                                       start_time, end_time,
                                                       opts)

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

    res['status'] = status
    res['domain_flux'] = all_flux_list
    res['sum_cdn_flux'] = sum_cdn_flux
    res['sum_src_flux'] = sum_src_flux
    res['max_cdn'] = max_cdn
    res['table_data'] = table_data

    return json_response(res)
Beispiel #3
0
def client_cdn_flux_data(request):
    """客户端查看计费状态码统计数据"""
    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', '[]')

    all_flux_list = []  # 计费图表数据
    sum_cdn_flux = 0  # 总计费数据(MB)
    sum_src_flux = 0  # 总回源数据(MB)
    max_cdn = 0  # 峰值计费值(M/bps)

    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:

            opts = []

            (all_flux_list, sum_cdn_flux, sum_src_flux, max_cdn, max_src,
             table_data, __) = get_domain_flux(user.id, domain_list,
                                               start_time, end_time, opts)

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

    res['status'] = status
    res['domain_flux'] = all_flux_list
    res['sum_cdn_flux'] = sum_cdn_flux
    res['sum_src_flux'] = sum_src_flux
    res['max_cdn'] = max_cdn
    res['table_data'] = table_data

    return json_response(res)
Beispiel #4
0
def _get_opt_log_list(request, user_type):

    start_time = request.POST.get('start_time', 0)
    end_time = request.POST.get('end_time', 0)
    username = request.POST.get('username', '')

    size = request.POST.get('size', PAGE_SIZE)
    page = request.POST.get('page', '1')

    msg = ''
    pagination = None
    try:

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

        if start_time and end_time:
            try:
                start_time = timestamp_to_datetime(start_time)
                end_time = timestamp_to_datetime(end_time)
            except TypeError:
                msg = af.PARAME_ERROR
                assert False

        log_list = OperateLog.get_operator_logs(user_type=user_type,
                                                username=username,
                                                start_time=start_time,
                                                end_time=end_time)

        size = int_check(size)
        page = int_check(page)
        if size is None or page is None or page == 0:
            msg = af.PARAME_ERROR
            assert False

        start = (page - 1) * size
        end = page * size

        total = len(log_list)

        pagination = get_pagination(page, total, size)

        result_list = log_list[start:end]
    except AssertionError:
        result_list = []

    return msg, result_list, pagination
Beispiel #5
0
def admin_cdn_request_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', '[]')

    request_ratio = 0  # 请求命中率(%)

    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]

            request_ratio = get_domain_request(user_id, domain_list,
                                               start_time, end_time, opts)

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

    res['status'] = status
    res['request_ratio'] = request_ratio

    return json_response(res)
Beispiel #6
0
def client_cdn_request_data(request):
    """管理员查看请求量统计数据"""

    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', '[]')

    request_ratio = 0  # 请求命中率(%)
    opts = []

    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_ids:
            request_ratio = get_domain_request(user.id, domain_list,
                                               start_time, end_time, opts)

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

    res['status'] = status
    res['request_ratio'] = request_ratio

    return json_response(res)
Beispiel #7
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
Beispiel #8
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)
Beispiel #9
0
def client_download_cdn_flux(request, start_time, end_time, domain_ids):
    """下载计费数据"""

    msg = ''
    status = False

    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_flux_list, sum_cdn_flux, sum_src_flux, max_cdn, max_src,
         table_data, opt_result) = get_domain_flux(user.id, domain_list,
                                                   start_time, end_time, opts)

        excel_name = '%s-flux_data.xls' % user.username
        sheet_name = 'Detailed Traffic Bandwidth'

        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)

        row += 1
        worksheet.write(row, 0, label='Peak bandwidth (Mbps)')
        worksheet.write(row, 1, label=max_cdn)

        row += 1
        worksheet.write(row, 0, label='Source peak bandwidth (Mbps)')
        worksheet.write(row, 1, label=max_src)

        row += 1
        worksheet.write(row, 0, label='Total flow (MB)')
        worksheet.write(row, 1, label=sum_cdn_flux)

        row += 1
        worksheet.write(row, 0, label='Total source flow (MB)')
        worksheet.write(row, 1, label=sum_src_flux)

        row += 2
        worksheet.write(row, 0, label='Time')
        worksheet.write(row, 1, label='bandwidth (Mbps)')
        worksheet.write(row, 2, label='flow (MB)')

        row += 1
        for i in all_flux_list:
            time_key = i.get('time_key', '')
            cdn_data = i.get('cdn_data', 0)

            cdn_bandwidth = cdn_data / 300 * 8

            worksheet.write(row, 0, label=time_key)
            worksheet.write(row, 1, label=cdn_bandwidth)
            worksheet.write(row, 2, label=cdn_data)

            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
Beispiel #10
0
def admin_download_cdn_flux(request, start_time, end_time, user_id, domain_ids,
                            opts):
    """下载计费数据"""

    msg = ''
    status = False

    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_flux_list, sum_cdn_flux, sum_src_flux, max_cdn, max_src,
         table_data, opt_result) = get_domain_flux(user_id, domain_list,
                                                   start_time, end_time, opts)

        excel_name = '%s-flux_data.xls' % user.username
        sheet_name = 'Detailed Traffic Bandwidth'

        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)

        row += 1
        worksheet.write(row, 0, label='Peak bandwidth (M/bps)')
        worksheet.write(row, 1, label=max_cdn)

        row += 1
        worksheet.write(row, 0, label='Source peak bandwidth (M/bps)')
        worksheet.write(row, 1, label=max_src)

        row += 1
        worksheet.write(row, 0, label='Total flow (MB)')
        worksheet.write(row, 1, label=sum_cdn_flux)

        row += 1
        worksheet.write(row, 0, label='Total source flow (MB)')
        worksheet.write(row, 1, label=sum_src_flux)

        row += 2
        worksheet.write(row, 0, label='Time')
        worksheet.write(row, 1, label='bandwidth (M/bps)')
        worksheet.write(row, 2, label='flow (MB)')

        base_title_row = row
        base_title_col = 3

        row += 1
        for i in all_flux_list:
            time_key = i.get('time_key', '')
            cdn_data = i.get('cdn_data', 0)

            cdn_bandwidth = cdn_data / 300 * 8

            worksheet.write(row, 0, label=time_key)
            worksheet.write(row, 1, label=cdn_bandwidth)
            worksheet.write(row, 2, label=cdn_data)

            row += 1

        for opt in opt_result:
            title_row = base_title_row

            bandwidth_title = '%s bandwidth' % opt
            bandwidth_col = base_title_col
            worksheet.write(title_row, bandwidth_col, label=bandwidth_title)

            flow_title = '%s flow' % opt
            flow_col = base_title_col + 1
            worksheet.write(title_row, flow_col, label=flow_title)

            for i in opt_result[opt]:
                title_row += 1

                cdn_data = i.get('cdn_data', 0)
                cdn_bandwidth = cdn_data / 300 * 8

                worksheet.write(title_row, bandwidth_col, label=cdn_bandwidth)
                worksheet.write(title_row, flow_col, label=cdn_data)

            base_title_col += 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
Beispiel #11
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)
Beispiel #12
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