api_calculate.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. from django.contrib import auth
  2. from rest_framework.views import APIView
  3. from api.utils import *
  4. from api.models import File, Mission
  5. import requests
  6. import logging
  7. import json
  8. logger = logging.getLogger('calculate')
  9. class CalculateAPI(APIView):
  10. def post(self, request):
  11. user = request.user
  12. try:
  13. if request.data.get('mission'):
  14. missionId = request.data.get('mission')
  15. elif request.data.get('missionId'):
  16. missionId = request.data.get('missionId')
  17. mission = Mission.objects.get(id=int(missionId))
  18. except Mission.DoesNotExist:
  19. return failed(message="处理任务控制失败,未找到处理任务")
  20. except Exception as error:
  21. print("处理任务控制失败", error)
  22. return failed(message="处理任务控制失败,未找到处理任务")
  23. # 检测用户权限
  24. if not mission.user == user and not user.identity == 'admin':
  25. logger.error(f"未授权用户{user.username}尝试执行任务{mission.name}:{mission.id}的计算操作")
  26. return failed(message="处理任务控制失败,用户没有操作权限")
  27. command = request.data.get('command')
  28. try:
  29. assert command in ['start', 'pause', 'stop']
  30. except Exception as error:
  31. print("处理任务控制代码错误")
  32. return failed(message="处理任务控制失败,控制代码错误")
  33. # 进行状态检查
  34. if command == 'start':
  35. # 如任务已经启动,则不操作
  36. if not mission.state in ['init', 'pause']:
  37. return success(message="任务正在进行中")
  38. if mission.state in ['done']:
  39. return success(message="任务已完成")
  40. else:
  41. # 非启动任务需要检查任务是否已经开始
  42. if not mission.state == 'calculating':
  43. return failed(message="任务没有在运行,无法暂停或停止")
  44. # 向调度程序提交计算任务
  45. # mission = request.json['mission']
  46. # plans = request.json['plans']
  47. # 根据控制指令不同,执行不同操作
  48. # 启动计算任务,构造通用数据结构
  49. if command == 'start':
  50. calculateData = {
  51. 'mission': {
  52. 'id': mission.id,
  53. },
  54. 'plans': []
  55. }
  56. rootPlan = mission.own_plans.get(parent=None)
  57. # 如果是恢复历史任务,则需要进行特殊处理未下一步运行提供所有plan的原料数据,因此此时可能scheduler已经终止过,过程数据已丢失
  58. # 如果是从初始状态或停止状态启动
  59. if mission.state == 'pause':
  60. # 如果是暂停中恢复,需要找出当前停在哪里,将父节点的结果作为原料输入
  61. # 找出最新plan
  62. lastPlans = [child for child in mission.own_plans.filter(parent=rootPlan).all()]
  63. latestPlans = []
  64. while lastPlans:
  65. currentPlan = lastPlans.pop()
  66. # 检查当前currentPlan是否已经计算完毕,如果已经计算完毕则继续找它的子节点,直到找到未计算完的
  67. # 注意可能当前plan的result已经被删除
  68. if hasattr(currentPlan, 'own_result') and currentPlan.own_result.progress == 100:
  69. # 计算完毕,将子节点加入寻找列表
  70. lastPlans.extend([child for child in mission.own_plans.filter(parent=currentPlan).all()])
  71. else:
  72. # 没有计算完毕,从这里恢复,需要用父节点结果作为自己的输入
  73. parentPlan = currentPlan.parent
  74. # 判断是否父节点是根节点,是则用mission的数据作为输入
  75. if parentPlan.parent == None:
  76. nodesJson = mission.nodeFile.toJson()
  77. edgesJson = mission.edgeFile.toJson()
  78. else:
  79. nodesJson = parentPlan.own_result.nodeFile.toJson()
  80. edgesJson = parentPlan.own_result.edgeFile.toJson()
  81. latestPlans.append(currentPlan)
  82. calculateData['plans'].append({
  83. 'id': currentPlan.id,
  84. 'algorithm': currentPlan.algorithm.name,
  85. 'nodes': nodesJson,
  86. 'edges': edgesJson,
  87. 'children': list(mission.own_plans.filter(parent=currentPlan).values_list('id', flat=True)),
  88. # 新式控制方法,检测到带有root字段,则为初始节点
  89. 'root': True,
  90. })
  91. # 开始弹栈,将所有后续子节点加入
  92. while latestPlans:
  93. currentPlan = latestPlans.pop()
  94. for child in mission.own_plans.filter(parent=currentPlan).all():
  95. latestPlans.append(child)
  96. # 子节点既没有初始数据也没有root标记
  97. calculateData['plans'].append({
  98. 'id': child.id,
  99. 'algorithm': child.algorithm.name,
  100. 'nodes': None,
  101. 'edges': None,
  102. 'children': list(mission.own_plans.filter(parent=child).values_list('id', flat=True)),
  103. })
  104. response = requests.post(SCHEDULER_BASE_URL + '/resumeMission', json=calculateData)
  105. print(response.json())
  106. if response.json()['code'] == 'OK':
  107. # 更新mission的运行状态
  108. mission.state = 'calculating'
  109. mission.save()
  110. return success(message="恢复计算任务成功")
  111. else:
  112. return failed(message="恢复计算任务失败")
  113. # 如果不是恢复计算任务,则正常计算
  114. calculateData['plans'].append({
  115. 'id': rootPlan.id,
  116. 'nodes': mission.nodeFile.toJson(),
  117. 'edges': mission.edgeFile.toJson(),
  118. 'children': list(mission.own_plans.filter(parent=rootPlan).values_list('id', flat=True)),
  119. })
  120. rootPlans = [ child for child in mission.own_plans.filter(parent=rootPlan)]
  121. while rootPlans:
  122. tempPlans = rootPlans.copy()
  123. rootPlans = []
  124. for p in tempPlans:
  125. children = [ child for child in mission.own_plans.filter(parent=p)]
  126. # 判断是否父节点存在计算结果,有则作为子节点输入
  127. if hasattr(p.parent, 'own_result') and p.parent.own_result.nodeFile and p.parent.own_result.edgeFile:
  128. calculateData['plans'].append({
  129. 'id': p.id,
  130. 'algorithm': p.algorithm.name,
  131. 'nodes': p.parent.own_result.nodeFile.toJson(),
  132. 'edges': p.parent.own_result.edgeFile.toJson(),
  133. 'children': [child.id for child in children],
  134. })
  135. else:
  136. calculateData['plans'].append({
  137. 'id': p.id,
  138. 'algorithm': p.algorithm.name,
  139. 'nodes': None,
  140. 'edges': None,
  141. 'children': [child.id for child in children],
  142. })
  143. rootPlans.extend(children)
  144. response = requests.post(SCHEDULER_BASE_URL + '/addMission', json=calculateData)
  145. print(response.json())
  146. if response.json()['code'] == 'OK':
  147. # 更新mission的运行状态
  148. mission.state = 'calculating'
  149. mission.save()
  150. return success(message="启动计算任务成功")
  151. else:
  152. return failed(message="启动计算任务失败")
  153. # 暂停计算
  154. if command == 'pause':
  155. # 暂停任务时仅需要传递mission的id
  156. calculateData = {
  157. 'mission': {
  158. 'id': mission.id,
  159. }
  160. }
  161. response = requests.post(SCHEDULER_BASE_URL + '/pauseMission', json=calculateData)
  162. if response.json()['code'] == 'OK':
  163. mission.state = 'pause'
  164. mission.save()
  165. # 暂停后,所有当前未完成任务全部变为初始状态,删除其result
  166. for plan in mission.own_plans.all():
  167. if hasattr(plan, 'own_result') and plan.own_result.progress != 100:
  168. result = plan.own_result
  169. result.delete()
  170. return success(message="暂停计算任务成功")
  171. else:
  172. print(response)
  173. return failed(message="暂停计算任务失败", data=response)
  174. # 停止计算
  175. if command == 'stop':
  176. # 停止任务时仅需要传递mission的id
  177. calculateData = {
  178. 'mission': {
  179. 'id': mission.id,
  180. }
  181. }
  182. response = requests.post(SCHEDULER_BASE_URL + '/stopMission', json=calculateData)
  183. if response.json()['code'] == 'OK':
  184. # 停止后所有任务相关数据删除,恢复初始状态
  185. mission.state = 'init'
  186. mission.save()
  187. # 停止后,所有plan的进度需要全部归零,即删除所有result,或将result的进度改为-2
  188. # 尝试删除所有result方案,下次运行时会重新创建result
  189. for plan in mission.own_plans.all():
  190. if hasattr(plan, 'own_result'):
  191. result = plan.own_result
  192. result.delete()
  193. return success(message="暂停计算任务成功")
  194. else:
  195. print(response)
  196. return failed(message="暂停计算任务失败", data=response)