views.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/python
  2. # -*- coding=utf-8 -*-
  3. # @Create Time: 2024-01-08 15:46:48
  4. # @Last Modified time: 2024-01-23 16:31:07
  5. import threading
  6. import pandas as pd
  7. from typing import Optional
  8. from datetime import datetime
  9. from pydantic import BaseModel
  10. from flask import request, jsonify
  11. from flask_pydantic import validate
  12. from . import main
  13. from .preprocess import parse_url
  14. from ..models import *
  15. from ..tasks import tasks
  16. g = dict()
  17. class RecodModel(BaseModel):
  18. record_Time: datetime
  19. current_Url: str
  20. full_Url: Optional[str] = None
  21. sim_Url: Optional[str] = None
  22. user_Id: str
  23. device_Id: str
  24. request_Method: str
  25. event_Type: str
  26. form_Data: Optional[str] = None
  27. json_Data: Optional[str] = None
  28. value_Data: Optional[str] = None
  29. status_Code: Optional[int] = None
  30. target_Url: Optional[str] = None
  31. iframe_Url: Optional[str] = None
  32. button_Text: Optional[str] = None
  33. @main.route('/record', methods=['GET', 'POST'])
  34. @validate()
  35. def record(body: RecodModel):
  36. """接收采集数据,记录临时表
  37. """
  38. try:
  39. # --- 采集原始表 --- #
  40. record = Record()
  41. record.Record_Time = body.record_Time
  42. record.Current_Url = body.current_Url
  43. record.Full_Url = body.full_Url
  44. record.User_Id = body.user_Id
  45. record.Request_Method = body.request_Method
  46. record.Event_Type = body.event_Type
  47. record.Target_Url = body.target_Url
  48. record.Button_Text = body.button_Text
  49. db.session.add(record)
  50. db.session.commit()
  51. # print(body.dict())
  52. tasks.predict(g, item=body.dict())
  53. # # --- 采集总量 +1 --- #
  54. # try:
  55. # tc = TotalCount.query.filter_by(Name = '采集量').one()
  56. # tc.add_one()
  57. # db.session.add(tc)
  58. # db.session.commit()
  59. # except Exception as e:
  60. # db.session.rollback()
  61. # print('--->', e)
  62. # # --- 登录人数 +1 --- #
  63. # pl = PersonLogin.query.filter_by(User_Id = body.user_Id)
  64. # if pl.count() == 0:
  65. # try:
  66. # db.session.add(PersonLogin(user_id = body.user_Id))
  67. # db.session.commit()
  68. # except Exception as e:
  69. # db.session.rollback()
  70. # print('--->', e)
  71. # # --- 解析访问域 --- #
  72. # df = pd.DataFrame([body.dict()])
  73. # result = parser.process(df)
  74. # try:
  75. # if result.get("domain") == "人资域":
  76. # PersonLogin.query.filter_by(User_Id = body.user_Id).update({"RZ_Visit": True})
  77. # elif result.get("domain") == "资产域":
  78. # PersonLogin.query.filter_by(User_Id = body.user_Id).update({"ZC_Visit": True})
  79. # elif result.get("domain") == "财务域":
  80. # PersonLogin.query.filter_by(User_Id = body.user_Id).update({"CW_Visit": True})
  81. # elif result.get("domain") == "营销域":
  82. # PersonLogin.query.filter_by(User_Id = body.user_Id).update({"YX_Visit": True})
  83. # db.session.commit()
  84. # except Exception as e:
  85. # db.session.rollback()
  86. # print('--->', e)
  87. except Exception as e:
  88. db.session.rollback()
  89. print('--->', e)
  90. return jsonify({"status": "failed"})
  91. return jsonify({"status": "success"})
  92. @main.route('/screen1', methods=['GET', 'POST'])
  93. def screen1():
  94. # tc = db.session.execute(db.select(TotalCount.Count).where(TotalCount.Name == '采集总量')).one()
  95. result = {
  96. "table_1": [i.to_json() for i in db.session.query(TotalCount).filter().all()],
  97. "table_2": {},
  98. "table_3": {},
  99. "table_4": {},
  100. "table_5": [i.to_json() for i in db.session.query(PersonLogin).filter().all()],
  101. "table_6": {},
  102. "table_7": {}
  103. }
  104. return jsonify(result)
  105. @main.route('/screen2', methods=['GET', 'POST'])
  106. def screen2():
  107. result = {
  108. "table_1": {},
  109. "table_2": {},
  110. "table_3": {},
  111. "table_4": {}
  112. }
  113. return jsonify({})
  114. @main.route('/screen3', methods=['GET', 'POST'])
  115. def screen3():
  116. for item in g['fuli@mz.gd.csg.cn'].getActionByDateRange().itertuples():
  117. print(f"""
  118. 当前时间: {item[0]}
  119. 起始URL:{item.cur_url[:50]}
  120. 终点URL:{item.tar_url[:50]}
  121. """)
  122. result = {
  123. "table_1": {},
  124. "table_2": {},
  125. "table_3": {},
  126. "table_4": {},
  127. "table_5": {},
  128. "table_6": {}
  129. }
  130. return jsonify({})
  131. @main.route('/show')
  132. def show():
  133. print(g)
  134. record = db.session.query(Record).filter().all()
  135. # record = db.session.execute(db.select(Record).where(Record.Request_Method == 'GET')).all()
  136. print(record)
  137. return [r.to_json() for r in record]