1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- from django.contrib import auth
- from rest_framework.views import APIView
- from rest_framework.response import Response
- from rest_framework import status
- from rest_framework.authtoken.models import Token
- from rest_framework.authentication import BasicAuthentication, TokenAuthentication
- from .serializers import UserRegisterSerializer
- from django.middleware.csrf import get_token
- from django.contrib.auth import login
- from api.utils import *
- from api.models import File, Mission
- import requests
- import json
- class CalculateAPI(APIView):
- def post(self, request):
- user = request.user
- try:
- mission = Mission.objects.get(id=request.data.get('mission'))
- except Mission.DoesNotExist:
- return failed(message="处理任务控制失败,未找到处理任务")
- except Exception as error:
- print("处理任务控制失败", error)
- return failed(message="处理任务控制失败,未找到处理任务")
-
- command = request.data.get('command')
- try:
- assert command in ['start', 'pause', 'stop']
- except Exception as error:
- print("处理任务控制代码错误")
- return failed(message="处理任务控制失败,控制代码错误")
- if command == 'start':
- # 如任务已经启动,则不操作
- if not mission.state in ['init', 'pause']:
- return success(message="任务正在进行中")
- if mission.state in ['done']:
- return success(message="任务已完成")
- else:
- # 非启动任务需要检查任务是否已经开始
- if not mission.state == 'calculating':
- return failed(message="任务没有在运行,无法暂停或停止")
- # 向调度程序提交计算任务
- # mission = request.json['mission']
- # plans = request.json['plans']
- calculateData = {
- 'mission': {
- 'id': mission.id,
- },
- 'plans': []
- }
- rootPlan = mission.own_plans.get(parent=None)
- calculateData['plans'].append({
- 'id': rootPlan.id,
- 'nodes': mission.nodeFile.toJson(),
- 'edges': mission.edgeFile.toJson(),
- 'children': list(mission.own_plans.filter(parent=rootPlan).values_list('id', flat=True)),
- })
- rootPlans = [ child for child in mission.own_plans.filter(parent=rootPlan)]
- while rootPlans:
- tempPlans = rootPlans.copy()
- rootPlans = []
- for p in tempPlans:
- children = [ child for child in mission.own_plans.filter(parent=p)]
- # 判断是否父节点存在计算结果,有则作为子节点输入
- if hasattr(p.parent, 'own_result'):
- calculateData['plans'].append({
- 'id': p.id,
- 'algorithm': p.algorithm.name,
- 'nodes': p.parent.own_result.nodeFile.toJson(),
- 'edges': p.parent.own_result.edgeFile.toJson(),
- 'children': [child.id for child in children],
- })
- else:
- calculateData['plans'].append({
- 'id': p.id,
- 'algorithm': p.algorithm.name,
- 'nodes': None,
- 'edges': None,
- 'children': [child.id for child in children],
- })
- rootPlans.extend(children)
- response = requests.post(SCHEDULER_BASE_URL + '/addMission', json=calculateData)
- print(response.json())
- if response.json()['code'] == 'OK':
- # 更新mission的运行状态
- mission.state = 'calculating'
- mission.save()
- return success(message="成功启动计算任务")
- else:
- return failed(message="启动计算任务失败")
|