跳到主要内容

消息监控

  • 使用装饰器的形式来监控消息的处理数据
  • 异常不捕获而是继续向上层抛出
  • 处理三类数据:
    • 消息适配器数据
      • 仅统计成功、失败、总时间、总数
      • 进入-总数加一-处理- (捕获异常-失败加一)/(成功-成功加一) -记录总时间-更新数据
    • 指令数据(以'/'开头)
      • 统计成功、失败、总时间、总数
      • 函数参数必须包含(msg:Msg)
      • 统计值为:指令名,用户(msg.user),平台(msg.platform),接收内容(msg.content),发送内容(return)
    • 网页数据(以'#'开头)
      • 统计成功、失败、总时间、总数
      • 函数参数必须包含(data:Dict,account: str = Depends(cookie_account_get))
      • 函数返回值必须为R(status="fail/success", data="xxx")
      • 统计值为:指令名,用户(cookie 中获取),平台(web),接收内容(data 字典拼接),返回内容(R 字典拼接)
  • 监控只能处理异步函数
  • 第一类数据示例
  • 一般放在 msg_deal 函数处,不直接放在路由处
@monitor_adapter("LR232")
async def lr232_msg_deal(data):
  • 第二类数据示例
  • 放在指令上面,参数要是 msg
  • logic 中给 msg_process 调用的函数不需要返回的,新增返回值用于结果统计
  • 可以设置成你想要的结果,如发送的内容,格式化的操作结果等等
@monitor_adapter("/基础_帮助")
async def help_show(msg: Msg):
return content
  • 第三类数据示例
  • 需要放在路由和函数之间
  • 捕获参数里的 data 和 account 进行统计
  • data 要是 Dict,所以像 get,delete 方法和传入 formData 的不好进行统计(见下方其他形式)
  • account 是从请求头携带的 cookie 中提取的,即网页进行数据操作必须要先经过身份验证,管理路径下(登陆后)的页面才使用此记录
  • 也可也不带 account,不过就不知道是谁记录了,如果有不记名的统计需求也可以
@router.put("/file/rename")
@monitor_adapter("#内阁_文件重命名")
async def files_rename(data: Dict, account: str = Depends(cookie_account_get)):

其他形式

  • 网页除了 data:Dict 格式,对于 formdata 和 query,适配器先 jsonload 一下然后读取信息

  • 此两种格式经过转换也能解析 data

  • formData

  • 前端:将 path 以 data 形式包装

formData.append('data',JSON.stringify({paths:basePath}))
const res = await http.post('/file', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
  • 后端:解析 data,装饰器自动转换成 json 并提取
router.post("/file/folders")
@monitor_adapter("#内阁_文件夹上传")
async def file_folders_upload(
files: List[UploadFile] = File(...),
data: str = Form(...),
account: str = Depends(cookie_account_get),
):
  • query
  • 前端:将 query 内容以 data 形式包装 const res = await http.delete('/nodes',{params: { data:JSON.stringify({id: currentNode.value.id })}})
  • 后端:将 query 解析为 data
@router.delete("/nodes")
@monitor_adapter("#内阁_节点删除")
async def node_delete(data: str = Query(...), account: str = Depends(cookie_account_get)):