def get(self, algorithm_id): self.algorithm_id = int(algorithm_id) try: db = TinyDB(globals.DATABASE) table = db.table('algorithm_list') # switch to table query = Query() result = table.search(query.id == self.algorithm_id) if not result: raise errors.DBAlgorithmNotExistException() algorithm = EntryPoint(algorithm_url=result[0]['urlapi'], algorithm_config={}, algorithm_id=self.algorithm_id, user=result[0]['auth']['user'], password=result[0]['auth']['password']) # convey data to algorithm through /stop_alg algorithm.stop_alg() logger.info("[Stop API] Algorithm stoped. Code 200 sent") return Response("Algorithm stopped", status=200, mimetype='text/plain') except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError): logger.info( "[Stop API][requests.exceptions.HTTPError, requests.exceptions.ConnectionError] \ Algorithm not reachable in {}. Code 501 sent".format( result[0]['url'])) return Response("Algorithm api not reachable", status=501, mimetype='text/plain') except (errors.AlgorithmBadStatusException, json.decoder.JSONDecodeError): logger.info( "[Stop API][AlgorithmBadStatusException, json.decoder.JSONDecodeError] \ Command sent but algorithm reply ERROR or not a json. Code 502 sent" ) return Response( "Command sent but algorithm reply ERROR or not a json", status=502, mimetype='text/plain') except (errors.DBAlgorithmNotExistException): logger.info("[Stop API][DBAlgorithmNotExistException, Exception] \ Failure in DSS to stop algorithm or algorithm do not exist. Code 500 sent" ) return Response( "Failure in DSS to start algorithm or algorithm do not exist", status=500, mimetype='text/plain') except Exception as e: logger.info("[Stop API][UncaughtException] \ {}".format(e)) return Response( "Failure in DSS to start algorithm or algorithm do not exist. Check log", status=500, mimetype='text/plain')
def post(self, algorithm_id): self.algorithm_id = int(algorithm_id) try: data = request.get_json() if not data: raise json.decoder.JSONDecodeError() db = TinyDB(globals.DATABASE) table = db.table('algorithm_list') # switch to table query = Query() result = table.search(query.id == self.algorithm_id) if not result: raise errors.DBAlgorithmNotExistException() # check json keys check_jsonkeys(data, 'Update') if not table.update({"config": request.get_json()['config']}, query.id == self.algorithm_id): raise errors.DBAlgorithmUpdateException() logger.info("[Update API] Algorithm config updated") return Response("Algorithm config updated", status=200, mimetype='text/plain') except (json.decoder.JSONDecodeError): logger.info( "[Update API][json.decoder.JSONDecodeError] Not a json. Code 502 sent" ) return Response("Not a valid json", status=502, mimetype='text/plain') except (errors.DBAlgorithmNotExistException, errors.DBAlgorithmUpdateException): logger.info( "[Update API][DBAlgorithmNotExistException, Exception] Failure in DSS to update algorithm or algorithm do not exist. Code 500 sent" ) return Response( "Failure in DSS to update algorithm or algorithm do not exist", status=501, mimetype='text/plain') except (jsonschema.exceptions.ValidationError, jsonschema.exceptions.SchemaError) as error: logger.info( '[Update API]Error in schemas validations {}'.format(error)) return Response( "[Update API]Error in schemas validations {}".format( error.message), status=500, mimetype='text/plain') except Exception as e: logger.info("[Update API][UncaughtException] {}".format(e)) return Response( "Failure in DSS to update algorithm or algorithm do not exist. Check log", status=501, mimetype='text/plain')
def get(self, algorithm_name): try: db = TinyDB('./db/db.json') table = db.table('algorithm_list') # switch to table query = Query() result = table.search(query.algorithm_name == algorithm_name) if not result: raise errors.DBAlgorithmNotExistException() algorithm = EntryPoint( algorithm_url=result[0]['url'], algorithm_config=request.get_json()['config']) # convey data to algorithm through /stop_alg algorithm.stop_alg() return Response("Algorithm stopped", status=200, mimetype='text/plain') except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError): return Response("Algorithm api not reachable", status=501, mimetype='text/plain') except (errors.AlgorithmBadStatusException, json.decoder.JSONDecodeError): return Response( "Command sent but algorithm reply ERROR or not a json", status=502, mimetype='text/plain') except (errors.DBAlgorithmNotExistException, Exception): return Response("Failure in DSS to start algorithm", status=500, mimetype='text/plain')
def get(self, algorithm_id): self.algorithm_id = int(algorithm_id) try: db = TinyDB(globals.DATABASE) table = db.table('algorithm_list') # switch to table query = Query() result = table.search(query.id == self.algorithm_id) if not result: raise errors.DBAlgorithmNotExistException() algorithm = EntryPoint(algorithm_url=result[0]['urlapi'], algorithm_config={}, algorithm_id=self.algorithm_id, user=result[0]['auth']['user'], password=result[0]['auth']['password']) # convey data to algorithm through /status_alg response = algorithm.status_alg() logger.info("[Status API] Algorithm sent status. Code 200 sent") # check if status is correct if not response['status'] in ("STARTED", "STOPPED"): raise errors.AlgorithmBadStatusException() # update status if not change_status(id=self.algorithm_id, new_status=response['status'], table=table): raise errors.DBAlgorithmUpdateException() return Response(json.dumps(response), status=200, mimetype='application/json') except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError): logger.info("[Status API][requests.exceptions.HTTPError, requests.exceptions.ConnectionError]" \ "Algorithm not reachable in {}. Code 501 sent".format(result[0]['urlapi'])) return Response("Algorithm api not reachable", status=501, mimetype='text/plain') except (errors.AlgorithmBadStatusException, json.decoder.JSONDecodeError): logger.info("[Status API][AlgorithmBadStatusException, json.decoder.JSONDecodeError]" \ "Command sent but algorithm reply ERROR or not a json. Code 502 sent") return Response( "Command sent but algorithm reply ERROR or not a json", status=502, mimetype='text/plain') except (errors.DBAlgorithmNotExistException, errors.DBAlgorithmUpdateException): logger.info("[Status API][errors.DBAlgorithmNotExistException, errors.DBAlgorithmUpdateException, Exception] " \ "Failure in DSS to update algorithm or algorithm do not exist. Code 500 sent") return Response("Failure in DSS to get status of algorithm", status=500, mimetype='text/plain') except Exception as e: logger.info("[Status API][UncaughtException] {}".format(e)) return Response( "Failure in DSS to get status of algorithm. Check log", status=500, mimetype='text/plain')
def get(self, algorithm_id): self.algorithm_id = int(algorithm_id) request_id = request.args.get('request_id', default=randint(0, 999999), type=int) try: db = TinyDB(globals.DATABASE) table = db.table('algorithm_list') # switch to table query = Query() result = table.search(query.id == self.algorithm_id) if not result: raise errors.DBAlgorithmNotExistException() algorithm = EntryPoint(algorithm_url=result[0]['urlapi'], algorithm_config=result[0]['config'], algorithm_id=self.algorithm_id, user=result[0]['auth']['user'], password=result[0]['auth']['password']) # convey data to algorithm through /run_alg # as user is using API requestId it is fake # MMT wil provide a correct one algorithm.run_alg(request_id) logger.info( "[Start API] Algorithm id {} started. Code 200 sent".format( self.algorithm_id)) # change status in database if not change_status( id=self.algorithm_id, new_status="STARTED", table=table): raise errors.DBAlgorithmUpdateException() logger.info("[Start API] Algorithm status updated") return Response("Algorithm id {} started".format( self.algorithm_id), status=200, mimetype='text/plain') except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError): logger.info("[Start API][requests.exceptions.HTTPError, requests.exceptions.ConnectionError]" \ "Algorithm not reachable in {}. Code 501 sent".format(result[0]['urlapi'])) return Response("Algorithm api not reachable", status=501, mimetype='text/plain') except (errors.AlgorithmBadStatusException, json.decoder.JSONDecodeError): logger.info("[Start API][AlgorithmBadStatusException, json.decoder.JSONDecodeError]" \ "Command sent but algorithm reply ERROR or not a json. Code 502 sent") return Response( "Command sent but algorithm reply ERROR or not a json", status=502, mimetype='text/plain') except (errors.DBAlgorithmNotExistException, errors.DBAlgorithmUpdateException): logger.info("[Start API][DBAlgorithmNotExistException, errors.DBAlgorithmUpdateException, Exception]" \ "Failure in DSS to start algorithm or algorithm do not exist. Code 500 sent") return Response( "Failure in DSS to start algorithm or algorithm do not exist", status=500, mimetype='text/plain') except Exception as e: logger.info("[Start API][UncaughtException] {}".format(e)) return Response( "Failure in DSS to start algorithm or algorithm do not exist. Check logs", status=500, mimetype='text/plain')