custom_tools.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #coding=utf-8
  2. '''
  3. Created on 2017年11月14日
  4. @author: bailiangjun
  5. '''
  6. def change_dict_p2m(item,p2m,ms=[],ems=[]):
  7. """
  8. @attention: 把指定信息的关键字换成对应的模型字段m
  9. @param item: 默认为 {p:值}
  10. @note:
  11. {p:值} ==> {m:值}
  12. 如果传入指定序列ps,则只取序列中的值
  13. 如果传入指定序列eps,则取除eps以外的值
  14. """
  15. if not item:
  16. return {}
  17. if not ms:
  18. ms = set(p2m.itervalues())
  19. temp = {p:m for p,m in p2m.iteritems() if (m in ms) and (m not in ems)}
  20. return {m:item.get(p,"") for p,m in temp.iteritems()}
  21. def change_dict_m2p(item,p2m,ms=[],ems=[]):
  22. """
  23. @attention: 把指定信息的关键字换成对应的api协议字段
  24. @param item: 默认为 {m:值}
  25. @note:
  26. {m:值} ==> {p:值}
  27. 如果传入指定序列ps,则只取序列中的值
  28. 如果传入指定序列eps,则取除eps以外的值
  29. """
  30. if not item:
  31. return {}
  32. if not ms:
  33. ms = set(p2m.itervalues())
  34. temp = {p:m for p,m in p2m.iteritems() if (m in ms) and (m not in ems)}
  35. return {p:item.get(m,"") for p,m in temp.iteritems()}
  36. def change_list_p2m(item,p2m,ms=[],ems=[]):
  37. """
  38. @attention: 把指定信息的关键字换成对应的模型字段m,针对列表
  39. @param item: 默认为 {p:值}
  40. @note:
  41. {p:值} ==> {m:值}
  42. 如果传入指定序列ps,则只取序列中的值
  43. 如果传入指定序列eps,则取除eps以外的值
  44. """
  45. if not item:
  46. return []
  47. if not ms:
  48. ms = set(p2m.itervalues())
  49. temp = {p:m for p,m in p2m.iteritems() if (m in ms) and (m not in ems)}
  50. return map(lambda x:{m:x.get(p,"") for p,m in temp.iteritems()}, item)
  51. def change_list_m2p(item,p2m,ms=[],ems=[]):
  52. """
  53. @attention: 把指定信息的关键字换成对应的api协议字段,针对列表
  54. @param item: 默认为 {m:值}
  55. @note:
  56. {m:值} ==> {p:值}
  57. 如果传入指定序列ps,则只取序列中的值
  58. 如果传入指定序列eps,则取除eps以外的值
  59. """
  60. if not item:
  61. return []
  62. if not ms:
  63. ms = set(p2m.itervalues())
  64. temp = {p:m for p,m in p2m.iteritems() if (m in ms) and (m not in ems)}
  65. return map(lambda x:{p:x.get(m,"") for p,m in temp.iteritems()}, item)
  66. def change_node_m2p(item, p2m, ms=[], ems=[]):
  67. """
  68. @attention: 把指定信息的关键字换成对应的api协议字段,针对列表
  69. @param item: 默认为 {m:值}
  70. @note:
  71. {m:值} ==> {p:值}
  72. 如果传入指定序列ps,则只取序列中的值
  73. 如果传入指定序列eps,则取除eps以外的值
  74. """
  75. if not item:
  76. return []
  77. if not ms:
  78. ms = set(p2m.itervalues())
  79. temp = {p: m for p, m in p2m.iteritems() if (m in ms) and (m not in ems)}
  80. return map(lambda x: {p: x.get(m, "") for p, m in temp.iteritems()}, item)
  81. def transfer_top_info(info, lkey, tkey):
  82. """
  83. @attention: 转换top数据给前端
  84. @note:
  85. 由 二维数组[[标签,值],[]]---> [{label:标签,"times":值}]
  86. """
  87. return [{"name": item[lkey], "value": item[tkey]} for item in info]
  88. def transfer_list_info(info, params):
  89. """
  90. @attention: 转换趋势数据给前端
  91. """
  92. if not info:
  93. return {"axis_x": [], "axis_y": {}}
  94. if isinstance(params, list):
  95. params = {item: [item] for item in params}
  96. print params
  97. pkeys = params.keys()
  98. if len(pkeys) == 1:
  99. key = pkeys[0]
  100. inkeys = params[key]
  101. inkeys = inkeys if inkeys else [key]
  102. v = info[key]
  103. axis_x = [item["dt"] for item in v]
  104. axis_y = {}
  105. for ik in inkeys:
  106. axis_y[ik] = [item[ik] for item in v]
  107. else:
  108. axis_y = {}
  109. for key in pkeys:
  110. inkeys = params[key]
  111. inkeys = inkeys if inkeys else [key]
  112. v = info[key]
  113. axis_x = [item["dt"] for item in v]
  114. for ik in inkeys:
  115. axis_y[ik] = [item[ik] for item in v]
  116. return {"axis_x": axis_x, "axis_y": axis_y}
  117. def classify_access_statistic_info(info):
  118. """
  119. @attention: 区分access统计数据项
  120. """
  121. total_values = []
  122. list_values = []
  123. top_values = {}
  124. if info.has_key("total_hit_flow"):
  125. total_values.append("hit_flow")
  126. if info.has_key("total_hit_num"):
  127. total_values.append("hit_total")
  128. if info.has_key("total_req_flow"):
  129. total_values.append("req_flow")
  130. if info.has_key("total_req_num"):
  131. total_values.append("req_total")
  132. if info.has_key("tendency_pv"):
  133. list_values.append("pageview")
  134. if info.has_key("tendency_quote"):
  135. list_values.append("search")
  136. if info.has_key("tendency_req_flow"):
  137. list_values.append("req_flow")
  138. list_values.append("hit_flow")
  139. if info.has_key("tendency_req_num"):
  140. list_values.append("req_total")
  141. list_values.append("hit_total")
  142. if info.has_key("tendency_ip"):
  143. list_values.append("ip_num")
  144. if info.get("top_location"):
  145. top_values["location"] = int(info["top_location"])
  146. if info.get("top_location_flow"):
  147. top_values["location"] = int(info["top_location_flow"])
  148. return total_values, list_values, top_values
  149. def classify_waf_statistic_info(info):
  150. """
  151. @attention: 区分waf统计数据项
  152. """
  153. total_values = []
  154. list_values = []
  155. top_values = {}
  156. if info.has_key("level"):
  157. total_values.append("level")
  158. if info.has_key("total_cc") or info.has_key("total_web"):
  159. total_values.append("att_num")
  160. if info.has_key("total_ip"):
  161. total_values.append("ip_num")
  162. if info.has_key("tendency_attack"):
  163. list_values.append("att_num")
  164. if info.get("top_ip"):
  165. top_values["src_ip"] = int(info["top_ip"])
  166. if info.get("top_location"):
  167. top_values["location"] = int(info["top_location"])
  168. if info.get("top_type"):
  169. top_values["rule"] = int(info["top_type"])
  170. if info.get("top_site"):
  171. top_values["domain"] = int(info["top_site"])
  172. return total_values, list_values, top_values
  173. def dict_unicode2str(**params):
  174. item = {}
  175. for k in params:
  176. item[str(k)] = str(params[k])
  177. return str(item)