123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #!/usr/bin/python
- # -*- coding=utf-8 -*-
- # @Create Time: 2024-01-08 15:46:48
- # @Last Modified time: 2024-01-23 16:31:07
- import threading
- import pandas as pd
- from typing import Optional
- from datetime import datetime
- from pydantic import BaseModel
- from flask import request, jsonify
- from flask_pydantic import validate
- from . import main
- from .preprocess import parse_url
- from ..models import *
- from ..tasks import tasks
- g = dict()
- class RecodModel(BaseModel):
- record_Time: datetime
- current_Url: str
- full_Url: Optional[str] = None
- sim_Url: Optional[str] = None
- user_Id: str
- device_Id: str
- request_Method: str
- event_Type: str
- form_Data: Optional[str] = None
- json_Data: Optional[str] = None
- value_Data: Optional[str] = None
- status_Code: Optional[int] = None
- target_Url: Optional[str] = None
- iframe_Url: Optional[str] = None
- button_Text: Optional[str] = None
- @main.route('/record', methods=['GET', 'POST'])
- @validate()
- def record(body: RecodModel):
- """接收采集数据,记录临时表
- """
- try:
- # --- 采集原始表 --- #
- record = Record()
- record.Record_Time = body.record_Time
- record.Current_Url = body.current_Url
- record.Full_Url = body.full_Url
- record.User_Id = body.user_Id
- record.Request_Method = body.request_Method
- record.Event_Type = body.event_Type
- record.Target_Url = body.target_Url
- record.Button_Text = body.button_Text
- db.session.add(record)
- db.session.commit()
- # print(body.dict())
- tasks.predict(g, item=body.dict())
- # # --- 采集总量 +1 --- #
- # try:
- # tc = TotalCount.query.filter_by(Name = '采集量').one()
- # tc.add_one()
- # db.session.add(tc)
- # db.session.commit()
- # except Exception as e:
- # db.session.rollback()
- # print('--->', e)
- # # --- 登录人数 +1 --- #
- # pl = PersonLogin.query.filter_by(User_Id = body.user_Id)
- # if pl.count() == 0:
- # try:
- # db.session.add(PersonLogin(user_id = body.user_Id))
- # db.session.commit()
- # except Exception as e:
- # db.session.rollback()
- # print('--->', e)
- # # --- 解析访问域 --- #
- # df = pd.DataFrame([body.dict()])
- # result = parser.process(df)
- # try:
- # if result.get("domain") == "人资域":
- # PersonLogin.query.filter_by(User_Id = body.user_Id).update({"RZ_Visit": True})
- # elif result.get("domain") == "资产域":
- # PersonLogin.query.filter_by(User_Id = body.user_Id).update({"ZC_Visit": True})
- # elif result.get("domain") == "财务域":
- # PersonLogin.query.filter_by(User_Id = body.user_Id).update({"CW_Visit": True})
- # elif result.get("domain") == "营销域":
- # PersonLogin.query.filter_by(User_Id = body.user_Id).update({"YX_Visit": True})
- # db.session.commit()
- # except Exception as e:
- # db.session.rollback()
- # print('--->', e)
- except Exception as e:
- db.session.rollback()
- print('--->', e)
- return jsonify({"status": "failed"})
- return jsonify({"status": "success"})
- @main.route('/screen1', methods=['GET', 'POST'])
- def screen1():
- # tc = db.session.execute(db.select(TotalCount.Count).where(TotalCount.Name == '采集总量')).one()
- result = {
- "table_1": [i.to_json() for i in db.session.query(TotalCount).filter().all()],
- "table_2": {},
- "table_3": {},
- "table_4": {},
- "table_5": [i.to_json() for i in db.session.query(PersonLogin).filter().all()],
- "table_6": {},
- "table_7": {}
- }
- return jsonify(result)
- @main.route('/screen2', methods=['GET', 'POST'])
- def screen2():
- result = {
- "table_1": {},
- "table_2": {},
- "table_3": {},
- "table_4": {}
- }
- return jsonify({})
- @main.route('/screen3', methods=['GET', 'POST'])
- def screen3():
- for item in g['fuli@mz.gd.csg.cn'].getActionByDateRange().itertuples():
- print(f"""
- 当前时间: {item[0]}
- 起始URL:{item.cur_url[:50]}
- 终点URL:{item.tar_url[:50]}
- """)
- result = {
- "table_1": {},
- "table_2": {},
- "table_3": {},
- "table_4": {},
- "table_5": {},
- "table_6": {}
- }
- return jsonify({})
- @main.route('/show')
- def show():
- print(g)
- record = db.session.query(Record).filter().all()
- # record = db.session.execute(db.select(Record).where(Record.Request_Method == 'GET')).all()
- print(record)
- return [r.to_json() for r in record]
|