def get_prediction(request, pk1, pk2, pk3): models = [] jobs = [] pk1 = int(pk1) pk2 = int(pk2) pk3 = int(pk3) log = Log.objects.get(pk=pk1) split, created = Split.objects.get_or_create(type='single', original_log=log) try: if pk2 > 0: model = PredModels.objects.get(pk=pk2) models.append(model) if pk3 > 0: model = PredModels.objects.get(pk=pk3) models.append(model) except PredModels.DoesNotExist: return Response({'error': 'not in database'}, status=status.HTTP_404_NOT_FOUND) for model in models: job = generate_run(pk1, model, model.id, split) # django_rq.enqueue(training, jobrun, predictive_model) django_rq.enqueue(runtime_task, job, model) # os.system('python3 manage.py rqworker --burst') serializer = JobSerializer(jobs, many=True) return Response(serializer.data, status=201)
def create_multiple(request): """No request validation""" payload = json.loads(request.body.decode('utf-8')) try: split = Split.objects.get(pk=payload['split_id']) except Split.DoesNotExist: return Response({'error': 'not in database'}, status=status.HTTP_404_NOT_FOUND) # detect either or not a predictive_model to update has been specified otherwise train a new one. if 'incremental_train' in payload['config'] and payload['config'][ 'incremental_train']['base_model'] is not None: jobs = update(split, payload) elif payload['type'] in [e.value for e in PredictiveModels]: jobs = generate(split, payload) elif payload['type'] == JobTypes.LABELLING.value: jobs = generate_labelling(split, payload) else: return Response( {'error': 'type not supported'.format(payload['type'])}, status=status.HTTP_422_UNPROCESSABLE_ENTITY) for job in jobs: django_rq.enqueue(tasks.prediction_task, job.id) serializer = JobSerializer(jobs, many=True) return Response(serializer.data, status=201)
def post_replay(request): """ Post request to start a demo of a log arriving to server :param request: json :return: Response """ jobs = [] data = request.data split_id = int(data['splitId']) job_id = int(data['jobId']) split = Split.objects.get(pk=split_id) try: training_initial_job = Job.objects.get(pk=job_id) new_job = duplicate_orm_row(training_initial_job) new_job.type = JobTypes.REPLAY.value new_job.status = JobStatuses.CREATED.value new_job.split = split new_job.save() except Job.DoesNotExist: return Response({'error': 'Job ' + str(job_id) + ' not in database'}, status=status.HTTP_404_NOT_FOUND) django_rq.enqueue(replay_task, new_job, training_initial_job) serializer = JobSerializer(jobs, many=True) return Response(serializer.data, status=status.HTTP_201_CREATED)
def post_replay_prediction(request): """ Post request to have a single prediction during the replay of a log :param request: json :return: Response """ jobs = [] job_id = int(request.query_params['jobId']) training_initial_job_id = int(request.query_params['training_job']) logger.info("Creating replay_prediction task") try: training_initial_job = Job.objects.get(pk=training_initial_job_id) replay_job = Job.objects.filter(pk=job_id)[0] replay_prediction_job = duplicate_orm_row(replay_job) replay_prediction_job.parent_job = Job.objects.filter(pk=job_id)[0] replay_prediction_job.type = JobTypes.REPLAY_PREDICT.value replay_prediction_job.status = JobStatuses.CREATED.value replay_prediction_job.save() except Job.DoesNotExist: return Response({'error': 'Job ' + str(job_id) + ' not in database'}, status=status.HTTP_404_NOT_FOUND) logger.info("Enqueuing replay_prediction task ID {}".format( replay_prediction_job.id)) log = import_log_from_string(request.data.decode('utf-8')) django_rq.enqueue(replay_prediction_task, replay_prediction_job, training_initial_job, log) serializer = JobSerializer(jobs, many=True) return Response(serializer.data, status=status.HTTP_201_CREATED)
def post_prediction(request): """ Post request to have a single static prediction :param request: json :return: Response """ jobs = [] data = request.data job_id = int(data['jobId']) split_id = int(data['splitId']) split = Split.objects.get(pk=split_id) try: job = Job.objects.get(pk=job_id) new_job = duplicate_orm_row(job) new_job.type = JobTypes.RUNTIME.value new_job.status = JobStatuses.CREATED.value new_job.split = split new_job.save() except Job.DoesNotExist: return Response({'error': 'Job ' + str(job_id) + ' not in database'}, status=status.HTTP_404_NOT_FOUND) django_rq.enqueue(runtime_task, new_job) serializer = JobSerializer(jobs, many=True) return Response(serializer.data, status=status.HTTP_201_CREATED)
def _serializer(object): """Assumed to be Django models""" name = object.__class__.__name__ if name == 'Log': data = LogSerializer(object).data elif name == 'Job': data = JobSerializer(object).data elif name == 'XTrace': data = TraceSerializer(object).data else: raise NotImplementedError("Websocket not implemented for class ".format(name)) return JSONRenderer().render({'type': name, 'data': data})
def post_replay_prediction(request): """ Post request to have a single prediction during the replay of a log :param request: json :return: Response """ jobs = [] job_id = int(request.query_params['jobId']) training_initial_job_id = int(request.query_params['training_job']) logger.info("Creating replay_prediction task") try: training_initial_job = Job.objects.get(pk=training_initial_job_id) replay_job = Job.objects.filter(pk=job_id)[0] # replay_prediction_job = duplicate_orm_row(replay_job) #todo: replace with simple CREATE replay_prediction_job = Job.objects.create( created_date=replay_job.created_date, modified_date=replay_job.modified_date, error=replay_job.error, status=replay_job.status, type=replay_job.type, create_models=replay_job.create_models, case_id=replay_job.case_id, event_number=replay_job.event_number, gold_value=replay_job.gold_value, results=replay_job.results, parent_job=replay_job.parent_job, split=replay_job.split, encoding=replay_job.encoding, labelling=replay_job.labelling, clustering=replay_job.clustering, predictive_model=replay_job.predictive_model, evaluation=replay_job.evaluation, hyperparameter_optimizer=replay_job.hyperparameter_optimizer, incremental_train=replay_job.incremental_train) replay_prediction_job.parent_job = Job.objects.filter(pk=job_id)[0] replay_prediction_job.type = JobTypes.REPLAY_PREDICT.value replay_prediction_job.status = JobStatuses.CREATED.value replay_prediction_job.save() except Job.DoesNotExist: return Response({'error': 'Job ' + str(job_id) + ' not in database'}, status=status.HTTP_404_NOT_FOUND) logger.info("Enqueuing replay_prediction task ID {}".format( replay_prediction_job.id)) log = import_log_from_string(request.data.decode('utf-8')) django_rq.enqueue(replay_prediction_task, replay_prediction_job, training_initial_job, log) serializer = JobSerializer(jobs, many=True) return Response(serializer.data, status=status.HTTP_201_CREATED)
def post_replay(request): """ Post request to start a demo of a log arriving to server :param request: json :return: Response """ jobs = [] data = request.data split_id = int(data['splitId']) job_id = int(data['jobId']) split = Split.objects.get(pk=split_id) try: training_initial_job = Job.objects.get(pk=job_id) # new_job = duplicate_orm_row(training_initial_job) #todo: replace with simple CREATE new_job = Job.objects.create( created_date=training_initial_job.created_date, modified_date=training_initial_job.modified_date, error=training_initial_job.error, status=training_initial_job.status, type=training_initial_job.type, create_models=training_initial_job.create_models, case_id=training_initial_job.case_id, event_number=training_initial_job.event_number, gold_value=training_initial_job.gold_value, results=training_initial_job.results, parent_job=training_initial_job.parent_job, split=training_initial_job.split, encoding=training_initial_job.encoding, labelling=training_initial_job.labelling, clustering=training_initial_job.clustering, predictive_model=training_initial_job.predictive_model, evaluation=training_initial_job.evaluation, hyperparameter_optimizer=training_initial_job. hyperparameter_optimizer, incremental_train=training_initial_job.incremental_train) new_job.type = JobTypes.REPLAY.value new_job.status = JobStatuses.CREATED.value new_job.split = split new_job.save() except Job.DoesNotExist: return Response({'error': 'Job ' + str(job_id) + ' not in database'}, status=status.HTTP_404_NOT_FOUND) django_rq.enqueue(replay_task, new_job, training_initial_job) serializer = JobSerializer(jobs, many=True) return Response(serializer.data, status=status.HTTP_201_CREATED)
def modelList(request): completed_jobs = Job.objects.filter(status=JobStatuses.COMPLETED.value, type=JobTypes.PREDICTION.value, create_models=True) serializer = JobSerializer(completed_jobs, many=True) return Response(serializer.data, status=200)
def post(request): serializer = JobSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=400)