tasks.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #!/usr/bin/python
  2. # -*- coding=utf-8 -*-
  3. # @Create Time: 2024-01-18 09:47:28
  4. # @Last Modified time: 2024-02-23 13:25:42
  5. import os, re, json
  6. import datetime
  7. import requests
  8. import pandas as pd
  9. from pandas import DataFrame
  10. import logging
  11. from .UserActivateDegreeAnalyze import UserActivateDegreeAnalyzeSpace
  12. from ..preprocess import parse_url
  13. class ResultJson:
  14. def __init__(self, time, user_id, domain, title1, title2, title3, device_id=None, text=None, is_op=True, is_bus=False, is_bias=False, status="正常"):
  15. # 执行时间
  16. self.time = time.strftime('%Y-%m-%d %H:%M:%S')
  17. # 执行用户
  18. self.user_id = user_id
  19. # 执行域
  20. self.domain = domain
  21. self.title1 = title1
  22. self.title2 = title2
  23. # 业务项
  24. self.title3 = title3
  25. # 设备
  26. self.device_id = device_id
  27. # 操作步骤
  28. self.text = text if text else ""
  29. # 是否用户操作
  30. self.is_op = is_op
  31. # 是否业务
  32. self.is_bus = is_bus
  33. # 是否偏离
  34. self.is_bias = is_bias
  35. # 执行结果
  36. self.status = status
  37. def to_json(self) -> dict:
  38. return {
  39. "real": { # 实时信息
  40. "time": self.time, # 记录时间
  41. "userId": self.user_id, # 用户ID
  42. "deviceId": self.device_id, # 设备ID
  43. "domain": self.domain, # 访问域
  44. "business": self.title3, # 业务类型
  45. "option": self.text, # 执行操作
  46. "isOp": self.is_op, # 是否是一条操作
  47. "isBus": self.is_bus, # 是否是一项业务
  48. "isBias": self.is_bias, # 是否偏离
  49. "status": self.status # 执行结果
  50. },
  51. "base": { # 定时信息
  52. "用户ID": self.user_id,
  53. "常用时段": "",
  54. "所在部门": "",
  55. "使用设备": "",
  56. "操作系统": "",
  57. "常用业务": ""
  58. }
  59. }
  60. def postback(json_data: dict) -> None:
  61. url = os.environ.get('BKURL')
  62. response = requests.post(url, json={'parameter': json_data})
  63. if response.status_code == 200:
  64. print('success')
  65. else:
  66. print(response.text)
  67. class UrlParser(object):
  68. def __init__(self):
  69. self.rzdf = pd.read_json(path_or_buf='/root/mzinfo/V2/app/resources/人资域.json', orient='records', lines=True, encoding='utf-8')
  70. self.zcdf = pd.read_json(path_or_buf='/root/mzinfo/V2/app/resources/资产域.json', orient='records', lines=True, encoding='utf-8')
  71. self.cwdf = pd.read_json(path_or_buf='/root/mzinfo/V2/app/resources/财务域.json', orient='records', lines=True, encoding='utf-8')
  72. self.yxdf = pd.read_json(path_or_buf='/root/mzinfo/V2/app/resources/营销域.json', orient='records', lines=True, encoding='utf-8')
  73. def process(self, row: dict):
  74. row['Current_Url'] = parse_url(row['Current_Url'])
  75. row['Target_Url'] = parse_url(row['Target_Url'])
  76. return self.process_record(row)
  77. def process_record(self, item: dict):
  78. i = item['Current_Url']
  79. result = None
  80. if i[1] == '10.10.21.23':
  81. result = {'domain': '人资域'}
  82. if i[2] == '/gmp/login.html':
  83. result = {
  84. 'time': item['Record_Time'],
  85. 'user': item['User_Id'],
  86. 'method': item['Request_Method'],
  87. 'hasform': True if item['Form_Data'] else False,
  88. 'domain': '人资域',
  89. '一级标题': '人资域登录',
  90. '二级标题': '人资域登录',
  91. '三级标题': '人资域登录',
  92. 'text': item['Button_Text'] if item['Button_Text'] else None
  93. }
  94. elif i[2] == '/gmp/static/gmpweb/hrIndex.html':
  95. result = {
  96. 'time': item['Record_Time'],
  97. 'user': item['User_Id'],
  98. 'method': item['Request_Method'],
  99. 'hasform': True if item['Form_Data'] else False,
  100. 'domain': '人资域',
  101. '一级标题': '人资域首页',
  102. '二级标题': '人资域首页',
  103. '三级标题': '人资域首页',
  104. 'text': item['Button_Text'] if item['Button_Text'] else None
  105. }
  106. elif i[2] == '/gmp/static/gmpweb/workbench/todo/TodoCenter.html':
  107. result = {
  108. 'time': item['Record_Time'],
  109. 'user': item['User_Id'],
  110. 'method': item['Request_Method'],
  111. 'hasform': True if item['Form_Data'] else False,
  112. 'domain': '人资域',
  113. '一级标题': '人资域',
  114. '二级标题': '公共应用',
  115. '三级标题': '代办中心',
  116. 'text': item['Button_Text'] if item['Button_Text'] else None
  117. }
  118. elif i[2] == 'hrcore_web/labormanagement/contractagreementview/myAgreement.html':
  119. result = {
  120. 'time': item['Record_Time'],
  121. 'user': item['User_Id'],
  122. 'method': item['Request_Method'],
  123. 'hasform': True if item['Form_Data'] else False,
  124. 'domain': '人资域',
  125. '一级标题': '人资域',
  126. '二级标题': '个人应用',
  127. '三级标题': '我的协议',
  128. 'text': item['Button_Text'] if item['Button_Text'] else None
  129. }
  130. elif i[2] == '/gmp/static/hr/peixun/user/index.html':
  131. result = {
  132. 'time': item['Record_Time'],
  133. 'user': item['User_Id'],
  134. 'method': item['Request_Method'],
  135. 'hasform': True if item['Form_Data'] else False,
  136. 'domain': '人资域',
  137. '一级标题': '人资域',
  138. '二级标题': '培训管理',
  139. '三级标题': '培训管理',
  140. 'text': item['Button_Text'] if item['Button_Text'] else None
  141. }
  142. elif i[5].get("appCode"):
  143. task = self.rzdf.query(f'''path == "{i[2]}" & appCode == "{i[5]['appCode'][0]}"''')
  144. try:
  145. result = {
  146. 'time': item['Record_Time'],
  147. 'user': item['User_Id'],
  148. 'method': item['Request_Method'],
  149. 'hasform': True if item['Form_Data'] else False,
  150. 'domain': '人资域',
  151. '一级标题': task['一级标题'].values[0],
  152. '二级标题': task['二级标题'].values[0],
  153. '三级标题': task['三级标题'].values[0],
  154. 'text': item['Button_Text'] if item['Button_Text'] else None
  155. }
  156. except Exception as e:
  157. print(f"\033[0;33;40m 错误 code {i[5].get('appCode')} {task}\033[0m")
  158. else: # 后续使用 iframe 处理
  159. pass
  160. elif i[1] == '10.10.21.28':
  161. result = {'domain': '资产域'}
  162. if i[2] == '/gmp/static/gmpweb/index.html':
  163. result = {
  164. 'time': item['Record_Time'],
  165. 'user': item['User_Id'],
  166. 'method': item['Request_Method'],
  167. 'hasform': True if item['Form_Data'] else False,
  168. 'domain': '资产域',
  169. '一级标题': '资产域首页',
  170. '二级标题': '资产域首页',
  171. '三级标题': '资产域首页',
  172. 'text': item['Button_Text'] if item['Button_Text'] else None
  173. }
  174. elif i[5].get('appCode'):
  175. task = self.zcdf.query(f'''path == "{i[2]}" & appCode == "{i[5]['appCode'][0]}"''')
  176. try:
  177. result = {
  178. 'time': item['Record_Time'],
  179. 'user': item['User_Id'],
  180. 'method': item['Request_Method'],
  181. 'hasform': True if item['Form_Data'] else False,
  182. 'domain': '资产域',
  183. '一级标题': task['一级标题'].values[0],
  184. '二级标题': task['二级标题'].values[0],
  185. '三级标题': task['三级标题'].values[0],
  186. 'text': item['Button_Text'] if item['Button_Text'] else None
  187. }
  188. except Exception as e:
  189. print(f"\033[0;33;40m 错误 code {i[5].get('appCode')} {task}\033[0m")
  190. elif i[1] == 'fms.gmp.cloud.hq.iv.csg':
  191. result = {'domain': '财务域'}
  192. if i[2] == '/gmp/login.html':
  193. result = {
  194. 'time': item['Record_Time'],
  195. 'user': item['User_Id'],
  196. 'method': item['Request_Method'],
  197. 'hasform': True if item['Form_Data'] else False,
  198. 'domain': '财务域',
  199. '一级标题': '登录财务域',
  200. '二级标题': '登录财务域',
  201. '三级标题': '登录财务域',
  202. 'text': item['Button_Text']
  203. }
  204. elif i[2] == '/gmp/index.html':
  205. result = {
  206. 'time': item['Record_Time'],
  207. 'user': item['User_Id'],
  208. 'method': item['Request_Method'],
  209. 'hasform': True if item['Form_Data'] else False,
  210. 'domain': '财务域',
  211. '一级标题': '财务域首页',
  212. '二级标题': '财务域首页',
  213. '三级标题': '财务域首页',
  214. 'text': item['Button_Text']
  215. }
  216. elif i[2] == '/gmp/': # 待确认
  217. result = {
  218. 'time': item['Record_Time'],
  219. 'user': item['User_Id'],
  220. 'method': item['Request_Method'],
  221. 'hasform': True if item['Form_Data'] else False,
  222. 'domain': '财务域',
  223. '一级标题': '财务域认证',
  224. '二级标题': '财务域认证',
  225. '三级标题': '财务域认证',
  226. 'text': item['Button_Text']
  227. }
  228. elif i[5].get('appCode'):
  229. try:
  230. task = self.cwdf.query(f'''path == "{i[2]}" & appCode == "{i[5]['appCode'][0]}"''')
  231. result = {
  232. 'time': item['Record_Time'],
  233. 'user': item['User_Id'],
  234. 'method': item['Request_Method'],
  235. 'hasform': True if item['Form_Data'] else False,
  236. 'domain': '财务域',
  237. '一级标题': task['一级标题'].values[0],
  238. '二级标题': task['二级标题'].values[0],
  239. '三级标题': task['三级标题'].values[0],
  240. 'text': item['Button_Text']
  241. }
  242. except Exception as e:
  243. print(f"\033[0;33;40m 错误 code {i[5].get('appCode')} {task}\033[0m")
  244. elif i[1] == '10.150.23.1:8010':
  245. result = {'domain': '营销管理'}
  246. fd = item['Form_Data']
  247. if item['Form_Data'] and item['Form_Data'] != 'None':
  248. try:
  249. if item['Form_Data'][0] != "{":
  250. item["Params_Data"] = item['Form_Data']
  251. form_data = None
  252. else:
  253. item['Form_Data'] = item['Form_Data'].replace("\"remark\":\"[", "\"remark\":\"\"[")
  254. item['Form_Data'] = item['Form_Data'].replace("\"[", "[").replace("]\"", "]").replace("\"{", "{").replace("}\"", "}")
  255. item['Form_Data'] = item['Form_Data'].replace("object HTMLInputElement", "").replace("[null]", "[]").replace("\"null\"", "\"\"")
  256. item['Form_Data'] = item['Form_Data'].replace("\n", "")
  257. form_data = json.loads(item['Form_Data'])
  258. except Exception as e:
  259. logging.error(item['Form_Data'])
  260. logging.error(fd)
  261. form_data = None
  262. raise e
  263. else:
  264. form_data = None
  265. if form_data and '_INVOKE_FUNC_TITLE_' in form_data:
  266. title = form_data['_INVOKE_FUNC_TITLE_'][0]
  267. appcontext = form_data['_INVOKE_FUNC_URL_'][0].split('/')[1]
  268. print(title, appcontext)
  269. task = self.yxdf.query(f'''四级标题 == "{title}" & appcontext == "{appcontext}"''')
  270. if task.empty:
  271. task = self.yxdf.query(f'''三级标题 == "{title}" & appcontext == "{appcontext}"''')
  272. if task.empty:
  273. task = self.yxdf.query(f'''四级标题 == "{title}"''')
  274. if task.empty:
  275. task = self.yxdf.query(f'''三级标题 == "{title}"''')
  276. try:
  277. result = {
  278. 'time': item['Record_Time'],
  279. 'user': item['User_Id'],
  280. 'method': item['Request_Method'],
  281. 'hasform': True,
  282. 'domain': '营销管理',
  283. '一级标题': task['一级标题'].values[0],
  284. '二级标题': task['二级标题'].values[0],
  285. '三级标题': task['三级标题'].values[0],
  286. '四级标题': task['四级标题'].values[0],
  287. 'text': item['Button_Text'] if item['Button_Text'] else None
  288. }
  289. except Exception as e:
  290. logging.error(task)
  291. logging.error(item['Form_Data'])
  292. logging.error(title)
  293. logging.error(e)
  294. logging.error(form_data['_INVOKE_FUNC_URL_'][0])
  295. else:
  296. result = {
  297. 'time': item['Record_Time'],
  298. 'user': item['User_Id'],
  299. 'method': item['Request_Method'],
  300. 'hasform': False,
  301. 'domain': '营销管理',
  302. '一级标题': '登录门户',
  303. '二级标题': '登录门户',
  304. '三级标题': '登录门户',
  305. 'text': item['Button_Text'] if item['Button_Text'] else None
  306. }
  307. elif i[1] == '4a.gd.csg.local':
  308. result = {
  309. 'time': item['Record_Time'],
  310. 'user': item['User_Id'],
  311. 'method': item['Request_Method'],
  312. 'hasform': True if item['Form_Data'] else False,
  313. 'domain': '登录门户',
  314. '一级标题': '登录门户',
  315. '二级标题': '登录门户',
  316. '三级标题': '登录门户',
  317. 'text': item['Button_Text'] if item['Button_Text'] else None
  318. }
  319. return result
  320. # 字典键首字母大写
  321. # def capital_to_higher(dict_info: dict) -> dict:
  322. # new_dict = {}
  323. # for i, j in dict_info.items():
  324. # new_dict[re.sub("([a-zA-Z])", lambda x: x.groups()[0].upper(), i, 1)] = j
  325. # return new_dict
  326. # 字典键全改小写
  327. def capital_to_lower(dict_info: dict) -> dict:
  328. new_dict = {}
  329. for i, j in dict_info.items():
  330. new_dict[i.lower()] = j
  331. return new_dict
  332. def predict(g: dict, item: dict):
  333. # 字典键全改小写
  334. item = capital_to_lower(item)
  335. # 放入全局
  336. if g.get(item['user_id']):
  337. g[item['user_id']].push(item)
  338. else:
  339. g[item['user_id']] = UserActivateDegreeAnalyzeSpace(item['user_id'])
  340. g[item['user_id']].push(item)
  341. # if g.get('BusinessClickRealTimeSpace'):
  342. # result = g['BusinessClickRealTimeSpace'].calculateRealTimeStat(frag)
  343. # else:
  344. # g['BusinessClickRealTimeSpace'] = BusinessClickRealTimeSpace()
  345. # result = g['BusinessClickRealTimeSpace'].calculateRealTimeStat(frag)
  346. # print(result)