#coding=utf-8 ''' Created on 2017年11月14日 @author: bailiangjun ''' def change_dict_p2m(item,p2m,ms=[],ems=[]): """ @attention: 把指定信息的关键字换成对应的模型字段m @param item: 默认为 {p:值} @note: {p:值} ==> {m:值} 如果传入指定序列ps,则只取序列中的值 如果传入指定序列eps,则取除eps以外的值 """ if not item: return {} if not ms: ms = set(p2m.itervalues()) temp = {p:m for p,m in p2m.iteritems() if (m in ms) and (m not in ems)} return {m:item.get(p,"") for p,m in temp.iteritems()} def change_dict_m2p(item,p2m,ms=[],ems=[]): """ @attention: 把指定信息的关键字换成对应的api协议字段 @param item: 默认为 {m:值} @note: {m:值} ==> {p:值} 如果传入指定序列ps,则只取序列中的值 如果传入指定序列eps,则取除eps以外的值 """ if not item: return {} if not ms: ms = set(p2m.itervalues()) temp = {p:m for p,m in p2m.iteritems() if (m in ms) and (m not in ems)} return {p:item.get(m,"") for p,m in temp.iteritems()} def change_list_p2m(item,p2m,ms=[],ems=[]): """ @attention: 把指定信息的关键字换成对应的模型字段m,针对列表 @param item: 默认为 {p:值} @note: {p:值} ==> {m:值} 如果传入指定序列ps,则只取序列中的值 如果传入指定序列eps,则取除eps以外的值 """ if not item: return [] if not ms: ms = set(p2m.itervalues()) temp = {p:m for p,m in p2m.iteritems() if (m in ms) and (m not in ems)} return map(lambda x:{m:x.get(p,"") for p,m in temp.iteritems()}, item) def change_list_m2p(item,p2m,ms=[],ems=[]): """ @attention: 把指定信息的关键字换成对应的api协议字段,针对列表 @param item: 默认为 {m:值} @note: {m:值} ==> {p:值} 如果传入指定序列ps,则只取序列中的值 如果传入指定序列eps,则取除eps以外的值 """ if not item: return [] if not ms: ms = set(p2m.itervalues()) temp = {p:m for p,m in p2m.iteritems() if (m in ms) and (m not in ems)} return map(lambda x:{p:x.get(m,"") for p,m in temp.iteritems()}, item) def change_node_m2p(item, p2m, ms=[], ems=[]): """ @attention: 把指定信息的关键字换成对应的api协议字段,针对列表 @param item: 默认为 {m:值} @note: {m:值} ==> {p:值} 如果传入指定序列ps,则只取序列中的值 如果传入指定序列eps,则取除eps以外的值 """ if not item: return [] if not ms: ms = set(p2m.itervalues()) temp = {p: m for p, m in p2m.iteritems() if (m in ms) and (m not in ems)} return map(lambda x: {p: x.get(m, "") for p, m in temp.iteritems()}, item) def transfer_top_info(info, lkey, tkey): """ @attention: 转换top数据给前端 @note: 由 二维数组[[标签,值],[]]---> [{label:标签,"times":值}] """ return [{"name": item[lkey], "value": item[tkey]} for item in info] def transfer_list_info(info, params): """ @attention: 转换趋势数据给前端 """ if not info: return {"axis_x": [], "axis_y": {}} if isinstance(params, list): params = {item: [item] for item in params} print params pkeys = params.keys() if len(pkeys) == 1: key = pkeys[0] inkeys = params[key] inkeys = inkeys if inkeys else [key] v = info[key] axis_x = [item["dt"] for item in v] axis_y = {} for ik in inkeys: axis_y[ik] = [item[ik] for item in v] else: axis_y = {} for key in pkeys: inkeys = params[key] inkeys = inkeys if inkeys else [key] v = info[key] axis_x = [item["dt"] for item in v] for ik in inkeys: axis_y[ik] = [item[ik] for item in v] return {"axis_x": axis_x, "axis_y": axis_y} def classify_access_statistic_info(info): """ @attention: 区分access统计数据项 """ total_values = [] list_values = [] top_values = {} if info.has_key("total_hit_flow"): total_values.append("hit_flow") if info.has_key("total_hit_num"): total_values.append("hit_total") if info.has_key("total_req_flow"): total_values.append("req_flow") if info.has_key("total_req_num"): total_values.append("req_total") if info.has_key("tendency_pv"): list_values.append("pageview") if info.has_key("tendency_quote"): list_values.append("search") if info.has_key("tendency_req_flow"): list_values.append("req_flow") list_values.append("hit_flow") if info.has_key("tendency_req_num"): list_values.append("req_total") list_values.append("hit_total") if info.has_key("tendency_ip"): list_values.append("ip_num") if info.get("top_location"): top_values["location"] = int(info["top_location"]) if info.get("top_location_flow"): top_values["location"] = int(info["top_location_flow"]) return total_values, list_values, top_values def classify_waf_statistic_info(info): """ @attention: 区分waf统计数据项 """ total_values = [] list_values = [] top_values = {} if info.has_key("level"): total_values.append("level") if info.has_key("total_cc") or info.has_key("total_web"): total_values.append("att_num") if info.has_key("total_ip"): total_values.append("ip_num") if info.has_key("tendency_attack"): list_values.append("att_num") if info.get("top_ip"): top_values["src_ip"] = int(info["top_ip"]) if info.get("top_location"): top_values["location"] = int(info["top_location"]) if info.get("top_type"): top_values["rule"] = int(info["top_type"]) if info.get("top_site"): top_values["domain"] = int(info["top_site"]) return total_values, list_values, top_values def dict_unicode2str(**params): item = {} for k in params: item[str(k)] = str(params[k]) return str(item)