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)
def user_domain_preload(request, user): """用户刷新""" urls = request.POST.get('urls', '[]') user_id = user.id msg = '' try: urls = handle_list(urls) if urls: urls = detect_domain(user_id, urls) if not urls: msg = af.URL_FORMAT_ERROR assert False body = { 'user_id': user.id, 'urls': urls, } print(body) api_res = APIUrl.post_link('cdn_domain_preload', body) print(api_res) return_code = api_res.get('return_code', 0) if return_code != 0: msg = af.SEND_ERROR assert False except AssertionError: pass return msg
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)
def user_domain_refresh(request, user): """用户刷新""" print(request.POST) urls = request.POST.get('urls', '[]') dirs = request.POST.get('dirs', '[]') user_id = user.id body = { 'user_id': user_id, } msg = '' try: urls = handle_list(urls) if urls: urls = detect_domain(user_id, urls) if not urls: msg = af.URL_FORMAT_ERROR assert False body['urls'] = urls dirs = handle_list(dirs) if dirs: dirs = detect_domain(user_id, dirs) if not dirs: msg = af.URL_FORMAT_ERROR assert False body['dirs'] = dirs api_res = APIUrl.post_link('cdn_domain_refresh', body) return_code = api_res.get('return_code', 0) if return_code != 0: msg = af.SEND_ERROR assert False except AssertionError: pass return msg
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)
def admin_get_domain_list(request): """管理员获取列表""" msg = '' status = False res = {'status': status, 'msg': msg} user = request.user domain = request.POST.get('domain', '') user_id = request.POST.get('user_id', '') cdn_type = request.POST.get('cdn_type', '') domain_status = request.POST.get('domain_status', '[]') domain_query = Domain.get_domain_query_by_user(user) try: domain_status = handle_list(domain_status, dec_int=True) if user_id: user_id = int_check(user_id) if user_id is None: msg = af.PARAM_ERROR assert False domain_query = domain_query.filter(user__id=user_id) if domain: domain_query = domain_query.filter(domain=domain) domain_query = domain_query.order_by('-protocol') check_msg, domain_dict_list, pagination = get_domain_list( request, domain_query, cdn_type, domain_status) if check_msg: res['msg'] = check_msg return json_response(res) res['domain_list'] = domain_dict_list res['page_info'] = pagination status = True except AssertionError: res['msg'] = msg return json_response(res) res['status'] = status return json_response(res)
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)
def client_get_domain_list(request): """客户端获取域名列表""" msg = '' status = False res = {'status': status, 'msg': msg} user = request.user domain = request.POST.get('domain', '') cdn_type = request.POST.get('cdn_type', '') domain_status = request.POST.get('domain_status', '[]') domain_query = Domain.get_domain_query_by_user(user) try: domain_status = handle_list(domain_status, dec_int=True) if domain: domain_query = domain_query.filter(domain=domain) domain_query = domain_query.order_by('-protocol') check_msg, domain_dict_list, pagination = get_domain_list( request, domain_query, cdn_type, domain_status) if check_msg: res['msg'] = check_msg return json_response(res) res['domain_list'] = domain_dict_list res['page_info'] = pagination status = True except AssertionError: res['msg'] = msg return json_response(res) res['status'] = status return json_response(res)
def client_cert_list(request): """客户端证书列表""" msg = '' status = False user = request.user res = { 'status': status, 'msg': msg } cert_name = request.POST.get('cert_name', '') status = request.POST.get('status', '[]') # cert_name = 'xz_test_0009' # status = 1 try: status_list = handle_list(status, dec_int=True) if not status_list: status_list = [ CertConf.CERT_CONDUCT, CertConf.CERT_SUCCESS, CertConf.CERT_FAIL, CertConf.CERT_TIMEOUT, CertConf.CERT_UPDATE ] body = { 'user_id': user.id, 'status': status_list, } if cert_name: body['cert_name'] = cert_name api_res = APIUrl.post_link('ssl_cert_query', body) print(11111111, api_res) return_code = api_res.get('return_code', 0) if return_code != 0: assert False result_list = api_res.get('cert_list', []) cert_list = [] for cert_info in result_list: # cert_info['cert_from'] = CertConf.cert_from_name( # cert_info['cert_from']) # # cert_info['status'] = CertConf.cert_status_name( # cert_info['status']) cert_list.append(cert_info) result_list = sorted( cert_list, key=lambda x: x['create_time'], reverse=True) check_msg, result_list, pagination = data_pagination( request, result_list) if check_msg: res['msg'] = check_msg return json_response(res) res['cert_list'] = result_list res['page_info'] = pagination status = True except Exception as e: print(e) res['msg'] = _(msg) res['status'] = status print(res) return json_response(res)
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)
def user_domain_create(request, user, opt_user): """用户创建域名""" domain = request.POST.get('domain', '') contract_name = request.POST.get('contract_name', '') cdn_type = request.POST.get('cdn_type', '') protocol = request.POST.get('protocol', '[]') cert_name = request.POST.get('cert_name', '') src_type = request.POST.get('src_type', '') src_value = request.POST.get('src_value', '') src_host = request.POST.get('src_host', '') src_back_type = request.POST.get('src_back_type', '') src_back_value = request.POST.get('src_back_value', '') ignore_cache_control = request.POST.get('ignore_cache_control', '0') ignore_query_string = request.POST.get('ignore_query_string', '0') cache_rule = request.POST.get('cache_rule', '[]') msg = '' status = False try: protocol = handle_list(protocol) if not protocol: msg = af.PROTOCOL_EMPTY assert False cache_rule = handle_list(cache_rule) if CDNConf.HTTPS_TYPE in protocol and not cert_name: msg = af.CERT_NAME_EMPTY assert False check_domain = Domain.objects.filter(domain=domain, protocol__in=protocol).first() if check_domain: msg = af.DOMAIN_EXIST assert False src_ips = [] src_domain = '' if src_type == CDNConf.SRC_IP: src_ips = src_value.split('\n') elif src_type == CDNConf.SRC_DOMAIN: src_domain = src_value if not src_host: src_host = domain src_back_ips = [] src_back_domain = '' if src_back_type == CDNConf.SRC_BACK_IP: src_back_ips = src_back_value.split('\n') elif src_back_type == CDNConf.SRC_BACK_DOMAIN: src_back_domain = src_back_value body = { 'base_template_id': CDNConf.BASE_CC_CMS_TEMPLATE_ID, 'user_id': user.id, 'domain': domain, 'cdn_type': cdn_type, 'contract_name': contract_name, 'protocol': protocol, 'cert_name': cert_name, 'src_type': src_type, 'src_ips': src_ips, 'src_domain': src_domain, 'src_host': src_host, 'src_back_type': src_back_type, 'src_back_ips': src_back_ips, 'src_back_domain': src_back_domain, 'ignore_cache_control': ignore_cache_control, 'ignore_query_string': ignore_query_string, 'cache_rule': cache_rule, } print(body) res = APIUrl.post_link('cdn_domain_create', body) print(res) return_code = res.get('return_code', 0) if return_code != 0: assert False for p in protocol: domain_obj = Domain(domain=domain, protocol=p, user=user) domain_obj.save() log_msg = om.CREATE_CDN_DOMAIN % (opt_user.username, user.username, domain) OperateLog.write_operate_log(request, om.ACCOUNTS, log_msg) status = True except AssertionError: pass return msg, status
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)