Files
d3i-szct/dock/govc/govc_scrape_finish_info.py
2026-06-02 17:46:38 +08:00

60 lines
2.3 KiB
Python

import asyncio
import json
import pandas as pd
from tornado.httpclient import HTTPResponse, HTTPRequest
from dock.govc import govc_api
import models
from models.govc_task_finish import GovcTaskFinish
from paste.util import udict
from paste.core.logging import echo_log
async def get_finish_info_request(pviguid: str, cguid: str):
"""
获取市12345的工单的办结信息,请求响应是单条工单的数据
:param pviguid: 工单列表请求返回的pviguid
:param cguid: 工单列表请求返回的cguid
"""
api_url = '/rest/sztaskworkordercommonrest/getFinishInfo'
headers = {
'Referer': f'{govc_api.ApiUrl}/rest/sztaskworkordercommonrest/getFinishInfo'
}
request_body = {
"ProcessVersionInstanceGuid": pviguid,
'caseguid': cguid,
'yearflag': 'undefined'
}
# 构造 API 请求
return await govc_api.new_api_request(api_url, request_body, headers=headers)
async def after_finish_info_request(response: HTTPResponse, retry_queue: asyncio.Queue[HTTPRequest]):
"""
任务请求响应后的处理程序。
:param response: 响应对象
:param retry_queue: 重试队列
"""
response_body = response.body.decode()
response_data = json.loads(response_body)
result_list = udict.get_by_path(response_data, 'params.finishinfo')
if result_list:
mapped_df = pd.DataFrame(result_list)
# 更换映射方向,用于将源数据列名改为与数据库表对应
forward_mapping = {dict_f: table_f for table_f, dict_f in GovcTaskFinish.FieldMapping.items()}
mapped_df = mapped_df.rename(columns=forward_mapping)
# 这里把空数据都换成 None,以便存入数据库时是 null
mapped_df.replace(models.EmptyInDF + models.EmptyDatetimeInDF, None, inplace=True)
task_id = getattr(response.request, GovcTaskFinish.task_id.key)
mapped_df[GovcTaskFinish.task_id.key] = task_id
# 筛选数据状态
_created, _updated = await GovcTaskFinish.save_batch(mapped_df)
echo_log(f"成功创建办结信息:{_created}条,更新:{_updated}条.")
else:
echo_log('未获取到办结信息')
if retry_queue:
echo_log(f"办结信息重试队列中有:{retry_queue.qsize()} 个请求在等待.")