60 lines
2.3 KiB
Python
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_status import GovcTaskStatus
|
|
from paste.util import udict
|
|
from paste.core.logging import echo_log
|
|
|
|
|
|
async def get_fetch_status_request(pviguid: str, cguid: str):
|
|
"""
|
|
获取市12345的工单状态信息,请求响应是单条工单的数据
|
|
|
|
:param pviguid: 工单列表请求返回的pviguid
|
|
:param cguid: 工单列表请求返回的cguid
|
|
"""
|
|
api_url = '/rest/sztaskworkordercommonrest/getCinfoLink'
|
|
headers = {
|
|
'Referer': f'{govc_api.ApiUrl}/rest/sztaskworkordercommonrest/getCinfoLink'
|
|
}
|
|
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_fetch_status_request(response: HTTPResponse, retry_queue: asyncio.Queue[HTTPRequest]):
|
|
"""
|
|
任务请求响应后的处理程序。
|
|
|
|
:param response: 响应对象
|
|
:param retry_queue: 重试队列
|
|
"""
|
|
response_body = response.body.decode()
|
|
response_data = json.loads(response_body)
|
|
status_dict = udict.get_by_path(response_data, 'params.statuslink')
|
|
if status_dict:
|
|
mapped_df = pd.DataFrame([status_dict])
|
|
# 更换映射方向,用于将源数据列名改为与数据库表对应
|
|
forward_mapping = {dict_f: table_f for table_f, dict_f in GovcTaskStatus.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, GovcTaskStatus.task_id.key)
|
|
mapped_df[GovcTaskStatus.task_id.key] = task_id
|
|
# 筛选数据状态
|
|
_created, _updated = await GovcTaskStatus.save_batch(mapped_df)
|
|
echo_log(f"成功创建工单状态信息:{_created}条,更新:{_updated}条.")
|
|
else:
|
|
echo_log('未获取到工单状态信息')
|
|
if retry_queue:
|
|
echo_log(f"工单状态信息重试队列中有:{retry_queue.qsize()} 个请求在等待.")
|