Example #1
0
def predict(request):
    if request.method == "POST":
        try:
            vo = ValueObject(json.loads(request.body))
            if not isinstance(vo.timeout, int):
                vo.timeout = 1
            if not isinstance(vo.time_interval, int):
                vo.time_interval = 1
            job_num = None
            if vo.name is not None and vo.fetches is not None:
                job_num = MW.predict(vo.name, vo.fetches, vo.feed_dict)
            result = None
            request_result = False
            if job_num is not None:
                request_result = True
                result = __check_job__(vo.name, job_num, vo.timeout,
                                       vo.time_interval)
            if result is None:
                result = {
                    "job": job_num,
                    "finish": False,
                    "result": None,
                    "time": vo.timeout
                }
            response_json = {
                "request_result": request_result,
                "result": result
            }
        except Exception as e:
            print(e)
            response_json = {"request_result": False, "result": str(e)}
        return JsonResponse(response_json)
    else:
        return HttpResponse(__RESPONSE_HTTP__)
Example #2
0
def clear(request):
    if request.method == "POST":
        try:
            vo = ValueObject(json.loads(request.body))
            if not isinstance(vo.timeout, int):
                vo.timeout = 1
            if not isinstance(vo.time_interval, int):
                vo.time_interval = 1
            job_num = None
            if vo.name is not None:
                job_num = MW.clear(vo.name)
            result = None
            request_result = False
            if job_num is not None:
                request_result = True
                start_time = time.time()
                response_time = 0.0
                while response_time < vo.timeout:
                    job_finish = MW.get_finish(vo.name, job_num)
                    if job_finish:
                        job_result = MW.get_result(vo.name, job_num)
                        result = {
                            "job": job_num,
                            "finish": job_finish,
                            "result": job_result,
                            "time": response_time
                        }
                        break
                    else:
                        time.sleep(vo.time_interval)
                        response_time = round(time.time() - start_time, 2)
            if result is None:
                result = {
                    "job": job_num,
                    "finish": False,
                    "result": None,
                    "time": vo.timeout
                }
            response_json = {
                "request_result": request_result,
                "result": result
            }
        except Exception as e:
            print(e)
            response_json = {"request_result": False, "result": str(e)}
        return JsonResponse(response_json)
    else:
        return HttpResponse(__RESPONSE_HTTP__)
Example #3
0
def get_variable(request):
    if request.method == "POST":
        try:
            vo = ValueObject(json.loads(request.body))
            result = None
            if isinstance(vo.variable, list):
                variable = MW.get_variable(vo.name)
                if variable is not None:
                    result = {}
                    for var in vo.variable:
                        value = None
                        if var in variable:
                            value = variable[var]
                        result[var] = value
            else:
                result = MW.get_variable(vo.name, vo.variable)
            result = VariableEncoder(result, encoder_func)
            request_result = result is not None
            response_json = {
                "request_result": request_result,
                "result": result
            }
        except Exception as e:
            print(e)
            response_json = {"request_result": False, "result": str(e)}
        return JsonResponse(response_json)
    else:
        return HttpResponse(__RESPONSE_HTTP__)
Example #4
0
def create_workspace(request):
    if request.method == "POST":
        try:
            vo = ValueObject(json.loads(request.body))
            if not isinstance(vo.type, int):
                vo.type = 0
            if not isinstance(vo.time_interval, int):
                vo.time_interval = 1
            result = MW.create_workspace(vo.name, vo.type, vo.time_interval)
            request_result = result is not None
            response_json = {
                "request_result": request_result,
                "result": result
            }
        except Exception as e:
            print(e)
            response_json = {"request_result": False, "result": str(e)}
        return JsonResponse(response_json)
    else:
        return HttpResponse(__RESPONSE_HTTP__)
Example #5
0
def push_job(request):
    if request.method == "POST":
        try:
            vo = ValueObject(json.loads(request.body))
            if not isinstance(vo.type, int):
                vo.type = 0
            if not isinstance(vo.timeout, int):
                vo.timeout = 1
            if not isinstance(vo.time_interval, int):
                vo.time_interval = 1
            if not isinstance(vo.clear, bool):
                vo.clear = False
            job_num = None
            if vo.name is not None and vo.job is not None:
                job_num = MW.push_job(vo.name, vo.job, vo.type, vo.clear)
            result = None
            request_result = False
            if job_num is not None:
                request_result = True
                start_time = time.time()
                response_time = 0.0
                while response_time < vo.timeout:
                    job_finish = MW.get_finish(vo.name, job_num)
                    if job_finish:
                        job_result = MW.get_result(vo.name, job_num)
                        variable = VariableEncoder(MW.get_variable(vo.name),
                                                   encoder_func)
                        result = {
                            "job": job_num,
                            "finish": job_finish,
                            "result": job_result,
                            "time": response_time,
                            "variable": variable
                        }
                        break
                    else:
                        time.sleep(vo.time_interval)
                        response_time = round(time.time() - start_time, 2)
            if result is None:
                result = {
                    "job": job_num,
                    "finish": False,
                    "result": None,
                    "time": vo.timeout,
                    "variable": None
                }
            response_json = {
                "request_result": request_result,
                "result": result
            }
        except Exception as e:
            print(e)
            response_json = {"request_result": False, "result": str(e)}
        return JsonResponse(response_json)
    else:
        return HttpResponse(__RESPONSE_HTTP__)
Example #6
0
def get_finish(request):
    if request.method == "POST":
        try:
            vo = ValueObject(json.loads(request.body))
            result = MW.get_finish(vo.name, vo.job)
            request_result = result is not None
            response_json = {
                "request_result": request_result,
                "result": result
            }
        except Exception as e:
            print(e)
            response_json = {"request_result": False, "result": str(e)}
        return JsonResponse(response_json)
    else:
        return HttpResponse(__RESPONSE_HTTP__)
Example #7
0
def push_job(request):
    if request.method == "POST":
        try:
            vo = ValueObject(json.loads(request.body))
            if not isinstance(vo.type, int):
                vo.type = 0
            if not isinstance(vo.timeout, int):
                vo.timeout = 1
            if not isinstance(vo.time_interval, int):
                vo.time_interval = 1
            if not isinstance(vo.clear, bool):
                vo.clear = False
            job_num = None
            if vo.name is not None and vo.job is not None:
                job_num = MW.push_job(vo.name, vo.job, vo.type, vo.clear)
            result = None
            request_result = False
            if job_num is not None:
                request_result = True
                result = __check_job__(vo.name, job_num, vo.timeout,
                                       vo.time_interval)
                if isinstance(result, dict):
                    result["variable"] = VariableEncoder(
                        MW.get_variable(vo.name), encoder_func)
            if result is None:
                result = {
                    "job": job_num,
                    "finish": False,
                    "result": None,
                    "time": vo.timeout,
                    "variable": None
                }
            response_json = {
                "request_result": request_result,
                "result": result
            }
        except Exception as e:
            print(e)
            response_json = {"request_result": False, "result": str(e)}
        return JsonResponse(response_json)
    else:
        return HttpResponse(__RESPONSE_HTTP__)