def delete_bag_comment(store_name, bag_name, comment_id, user=None): # noqa: E501 """Post a comment # noqa: E501 :param store_name: Name of the store :type store_name: str :param bag_name: Name of the bag :type bag_name: str :param comment_id: Comment identifier :type comment_id: int :rtype: None """ try: session = Database.get_session() bag, e = find_bag_in_database(session, store_name, bag_name) if e: return e, e.code q = session.query(RosbagComment).filter( RosbagComment.uid == comment_id) # type: Query if q.count() != 1 or q.first().bag_id != bag.uid: return Error(code=404, message="Comment not found"), 404 session.delete(q.first()) session.commit() return "", 204 except Exception as e: return handle_exception(e)
def delete_session(session_id, user=None): """ Delete a session or sessions :param session_id: Session id or all or current :type session_id: str :rtype: None """ try: to_delete = [] if session_id == "current": pass elif session_id == "all": pass else: try: id = int(session_id) except: return Error(400, "Invalid session id"), 400 for session in to_delete: pass except Exception as e: return error_helper.handle_exception(e)
def get_task(task_identifier, user=None): """Take a task from the queue # noqa: E501 :param task_identifier: :type task_identifier: str :rtype: TaskDetailed """ try: session = Database.get_session() try: identifier = int(task_identifier) except ValueError: return Error(code=400, message="Invalid task identifier"), 400 q = session.query(Task).filter( Task.uid == int(identifier)) # type: Query model = q.first() if model: return model.to_swagger_model_detailed(user=user) else: return Error(code=404, message="Task not found"), 404 except Exception as e: return handle_exception(e)
def new_bag_comment(store_name, bag_name, comment, user=None): # noqa: E501 """Delete a comment # noqa: E501 :param store_name: Name of the store :type store_name: str :param bag_name: Name of the bag :type bag_name: str :param comment: Comment :type comment: dict | bytes :rtype: Comment """ if connexion.request.is_json: comment = Comment.from_dict(connexion.request.get_json()) # noqa: E501 try: session = Database.get_session() bag, e = find_bag_in_database(session, store_name, bag_name) if e: return e, e.code comment_model = RosbagComment() comment_model.from_swagger_model(comment) comment_model.user_id = user.uid comment_model.bag_id = bag.uid session.add(comment_model) session.commit() return comment_model.to_swagger_model() except Exception as e: return handle_exception(e)
def get_simulation_run(sim_identifier, run_identifier, expand=None, user=None): # noqa: E501 """Get simulation run # noqa: E501 :param sim_identifier: :type sim_identifier: int :param run_identifier: :type run_identifier: int :param expand: :type expand: bool :rtype: SimulationRunDetailed """ try: session = Database.get_session() q = session.query(SimulationRun).filter( SimulationRun.uid == run_identifier) # type: Query model = q.first() if model and model.simulation_id == sim_identifier: return model.to_swagger_model_detailed(user=user, expand=expand) else: return Error(code=404, message="Simulation run not found"), 404 except Exception as e: return handle_exception(e)
def put_task(task_identifier, task, user=None): try: if connexion.request.is_json: task = TaskDetailed.from_dict( connexion.request.get_json()) # type: TaskDetailed return put_task_inner(task_identifier, task, user) except Exception as e: return handle_exception(e)
def list_extraction_configurations(user=None): """List available configurations :rtype: List[BagExtractionConfiguration] """ try: q = Database.get_session().query(RosbagExtractionConfiguration) return [p.to_swagger_model(user=user) for p in q] except Exception as e: return handle_exception(e)
def put_simulation_environment(env_name, environment, block_on_existing=None, user=None): # noqa: E501 """Create/update a simulation environment # noqa: E501 :param env_name: Name of the simulation environment :type env_name: str :param environment: Simulation environment :type environment: dict | bytes :rtype: SimulationEnvironmentDetailed """ try: if connexion.request.is_json: environment = SimulationEnvironmentDetailed.from_dict( connexion.request.get_json()) # noqa: E501 session = Database.get_session() q = session.query(SimulationEnvironment).filter( SimulationEnvironment.name == env_name) # type: Query model = SimulationEnvironment() if q.count() == 1: if block_on_existing: return Error(code=1000, message="Already exists."), 400 model = q.first() else: if environment.name != env_name: return Error( code=400, message= "Path and body tag have to be equal for a new environment" ), 400 session.add(model) model.from_swagger_model(environment, user=user) if environment.rosbag_store: q = session.query(RosbagStore).filter( RosbagStore.name == environment.rosbag_store) # type: Query rosbag_store = q.first() if not rosbag_store: return Error(code=400, message="Rosbag store not found"), 400 model.rosbag_store_id = rosbag_store.uid session.commit() return model.to_swagger_model_detailed(user=user) except Exception as e: return handle_exception(e)
def list_queue(limit=None, offset=None, ordering=None, running=None, finished=None, queued=None, user=None): """List task queue # noqa: E501 :rtype: List[TaskSummary] """ try: session = Database.get_session() q = session.query(Task) #type: Query filters = [] if running: filters.append(Task.state == TaskState.Running) if finished: filters.append( or_(Task.state == TaskState.Cancelled, Task.state == TaskState.Finished, Task.state == TaskState.CancellationRequested)) if queued: filters.append( or_(Task.state == TaskState.Queued, Task.state == TaskState.Paused)) if len(filters) > 0: q = q.filter(reduce((lambda x, y: or_(x, y)), filters)) q = db_helper.query_pagination_ordering( q, offset, limit, ordering, { 'priority': Task.priority, 'identifier': Task.uid, 'last_updated': Task.last_updated, 'created': Task.created, 'state': Task.state, 'success': Task.success, 'runtime': Task.runtime }) return [p.to_swagger_model_summary(user=user) for p in q] except Exception as e: return handle_exception(e)
def do_task_action(task_identifier, action, task=None, user=None): # noqa: E501 """Perform an action on the task # noqa: E501 :param task_identifier: :type task_identifier: str :param action: Action to perform (cancel/prio_up) :type action: str :param task: The task, required depending on the action :type task: dict | bytes :rtype: TaskDetailed """ try: if connexion.request.is_json and task: task = TaskDetailed.from_dict( connexion.request.get_json()) # noqa: E501 session = Database.get_session() q = session.query(Task).filter( Task.uid == int(task_identifier)) # type: Query model = q.first() # type: Task if not model: return Error(code=404, message="Task not found"), 404 if action == "cancel": if model.state == TaskState.Queued: model.state = TaskState.Cancelled model.last_updated = datetime.datetime.utcnow() session.commit() return model.to_swagger_model_detailed(user) elif action == "cancel_running": if model.state == TaskState.Running: model.state = TaskState.CancellationRequested model.last_updated = datetime.datetime.utcnow() session.commit() return model.to_swagger_model_detailed(user) elif action == "prio_up": Task.task_prio_up(session, model.uid) session.commit() q = session.query(Task).filter(Task.uid == int(task_identifier)) return q.first().to_swagger_model_detailed(user) else: return Error(code=400, message="Unknown action"), 400 except Exception as e: return handle_exception(e)
def new_simulation_run(sim_identifier, simulation_run, user=None): # noqa: E501 """New simulation run # noqa: E501 :param sim_identifier: :type sim_identifier: int :param simulation_run: Simulation run :type simulation_run: dict | bytes :rtype: SimulationRunDetailed """ try: if connexion.request.is_json: simulation_run = SimulationRunDetailed.from_dict( connexion.request.get_json()) # noqa: E501 session = Database.get_session() # Find the simulation q = session.query(Simulation).filter(Simulation.uid == sim_identifier) simulation = q.first() if not simulation: return Error(code=404, message="Simulation not found"), 404 model = SimulationRun() model.from_swagger_model(simulation_run, user=user) model.simulation_id = simulation.uid if simulation_run.bag_store_name and simulation_run.bag_name: # Find the bag of the run q = session.query(Rosbag).filter( and_(RosbagStore.name == simulation_run.bag_store_name, Rosbag.name == simulation_run.bag_name)) bag = q.first() if not bag: return Error(code=400, message="Bag not found"), 400 model.bag_id = bag.uid model.uid = None session.add(model) session.commit() # Return a fresh copy from the DB q = session.query(SimulationRun).filter(SimulationRun.uid == model.uid) return q.first().to_swagger_model_detailed(user=user), 200 except Exception as e: return handle_exception(e)
def patch_bag_meta(store_name, bag_name, bag, trigger=None, user=None): # noqa: E501 """Partial update of bag information (this only supports a few fields) # noqa: E501 :param store_name: Name of the store :type store_name: str :param bag_name: Name of the bag :type bag_name: str :param bag: Bag to register :type bag: dict | bytes :param trigger: Hooks to trigger :type trigger: str :rtype: BagDetailed """ if connexion.request.is_json: bag = connexion.request.get_json() try: session = Database.get_session() bag_model, e = find_bag_in_database(session, store_name, bag_name) if e: return e, e.code changed = False if 'comment' in bag and isinstance(bag['comment'], str): bag_model.comment = bag['comment'] changed = True if 'extraction_failure' in bag: bag_model.extraction_failure = bag['extraction_failure'] changed = True if 'in_trash' in bag: bag_model.in_trash = bag['in_trash'] changed = True if changed: session.commit() return bag_model.to_swagger_model_detailed(user=user) except Exception as e: return handle_exception(e)
def list_simulation_environments(user=None): """List available simulation environments # noqa: E501 :rtype: List[SimulationEnvironmentSummary] """ try: session = Database.get_session() q = session.query(SimulationEnvironment) #type: Query return [p.to_swagger_model_summary(user=user) for p in q] except Exception as e: return handle_exception(e)
def list_tags(user=None): """List all tags # noqa: E501 :rtype: List[Tag] """ try: session = Database.get_session() q = session.query(Tag) #type: Query return [p.to_swagger_model(user=user) for p in q] except Exception as e: return handle_exception(e)
def new_session(valid_for=None, user=None): """ Create a new session :rtype: User """ try: # Unless otherwise specified, sessions are valid for an hour if valid_for is None: valid_for = 3600 return auth.new_user_session( user, valid_for).to_swagger_model(show_token=True) except Exception as e: return error_helper.handle_exception(e)
def get_store_extraction_configs(store_name, user=None): """Get list of auto extraction configs :param store_name: Name of the store :type store_name: str :rtype: List[BagExtractionConfiguration] """ try: q = Database.get_session().query(RosbagStore).filter(RosbagStore.name == store_name) #type: Query if q.count(): return [x.to_swagger_model(user=user) for x in q.first().auto_extraction_configs] else: return Error(code=404, message="Store not found"), 404 except Exception as e: return handle_exception(e)
def put_configuration_key(config_key, config_value, user=None): # noqa: E501 """Write configuration key # noqa: E501 :param config_key: Configuration key to read :type config_key: str :param config_value: Configuration key value :type config_value: str :rtype: None """ try: # TODO: Will be implemented later raise NotImplementedError() except Exception as e: return error_helper.handle_exception(e)
def put_simulation(sim_identifier, simulation, user=None): # noqa: E501 """Update a simulation # noqa: E501 :param sim_identifier: :type sim_identifier: int :param simulation: Simulation :type simulation: dict | bytes :rtype: SimulationDetailed """ try: if connexion.request.is_json: simulation = SimulationDetailed.from_dict( connexion.request.get_json()) # noqa: E501 if simulation.identifier != sim_identifier: return Error( code=400, message="Body and path identifier are not the same"), 400 session = Database.get_session() q = session.query(Simulation).filter(Simulation.uid == sim_identifier) model = q.first() if not model: return Error(code=404, message="Simulation not found"), 404 q = session.query(SimulationEnvironment).filter( SimulationEnvironment.name == simulation.environment_name) if not q.first(): return Error(code=400, message="Simulation environment '%s' not found" % simulation.environment_name), 400 model.from_swagger_model(simulation, user=user) model.environment_id = q.first().uid session.commit() # Return a fresh copy from the DB q = session.query(Simulation).filter(Simulation.uid == model.uid) return q.first().to_swagger_model_detailed(user=user), 200 except Exception as e: return handle_exception(e)
def get_extraction_config(config_name, user=None): """Get configuration details :param config_name: Name of the configuration :type config_name: str :rtype: BagExtractionConfiguration """ try: q = Database.get_session().query(RosbagExtractionConfiguration).\ filter(RosbagExtractionConfiguration.name == config_name) #type: Query if q.count(): return q[0].to_swagger_model(user=user) else: return Error(code=404, message="Configuration not found"), 404 except Exception as e: return handle_exception(e)
def put_bag_tags(store_name, bag_name, tags, auto_create=None, user=None): """Change bag tags # noqa: E501 :param store_name: Name of the store :type store_name: str :param bag_name: Name of the bag :type bag_name: str :param tags: List of tags :type tags: List[] :param auto_create: Create non existing tags :type auto_create: bool :rtype: List[Tag] """ try: session = Database.get_session() bag, e = find_bag_in_database(session, store_name, bag_name) if e: return e, e.code tag_models = [] tags_unique = list(set([x.strip().lower() for x in tags])) for tag in tags_unique: q = session.query(Tag).filter(Tag.tag == tag) # type: Query if q.count() == 1: tag_models.append(q.first()) else: if auto_create: tag_models.append(Tag(tag=tag, color="")) else: return Error(code=400, message="Tag '" + tag + "' does not exist"), 400 bag.tags = tag_models session.commit() return get_bag_tags(store_name, bag_name) except Exception as e: return handle_exception(e)
def new_task(task, user=None): """Create a new task # noqa: E501 :param task: The task :type task: dict | bytes :rtype: TaskDetailed """ try: if connexion.request.is_json: task = TaskDetailed.from_dict( connexion.request.get_json()) # type: TaskDetailed session = Database.get_session() hash = Task.calculate_hash(task.config) q = session.query(Task)\ .filter(Task.state < TaskState.Finished)\ .filter(Task.task == task.task)\ .filter(Task.task_hash == hash) duplicate_task = q.first() if duplicate_task: return Error(code=409, message="Duplicate task, %d, queued" % duplicate_task.uid), 409 model = Task() model.log = "" model.result = {} model.task_hash = hash model.from_swagger_model(task, user=user) model.uid = None session.add(model) session.commit() # Return a fresh copy from the DB q = session.query(Task).filter(Task.uid == model.uid) return q.first().to_swagger_model_detailed(user=user), 200 except Exception as e: return handle_exception(e)
def put_extraction_configuration(config_name, configuration_obj, block_on_existing=None, user=None): """Create/update configuration :param config_name: Name of the configuration :type config_name: str :param configuration: Configuration information :type configuration: dict | bytes :rtype: BagExtractionConfiguration """ if connexion.request.is_json: configuration_obj = BagExtractionConfiguration.from_dict(connexion.request.get_json()) if config_name != configuration_obj.name: return Error(code=400, message="URL and body names don't match"), 400 session = Database.get_session() try: # Check the store q = session.query(RosbagExtractionConfiguration).filter(RosbagExtractionConfiguration.name == config_name) # type: Query # Create new store or use existing model = None if q.count() == 1: # Existing configuration if block_on_existing: return Error(code=1000, message="Already exists."), 400 model = q.first() else: model = RosbagExtractionConfiguration() session.add(model) model.from_swagger_model(configuration_obj, user=user) session.commit() q = session.query(RosbagExtractionConfiguration).filter(RosbagExtractionConfiguration.uid == model.uid) return q.first().to_swagger_model(user=user), 200 except Exception as e: session.rollback() return handle_exception(e)
def put_tag(tag, tag_obj, user=None): """Create/update tag # noqa: E501 :param tag: Name of the tag :type tag: str :param tag_obj: Tag information :type tag_obj: dict | bytes :rtype: Tag """ tag = tag.strip().lower() if connexion.request.is_json: tag_obj = SwaggerTag.from_dict(connexion.request.get_json()) # We do allow renaming tags # if tag_obj.tag != tag: # return Error(code=400, message="Path and body tag are not the same"), 400 try: session = Database.get_session() q = session.query(Tag).filter(Tag.tag == tag) # type: Query model = Tag() if q.count() == 1: model = q.first() else: if tag_obj.tag != tag: return Error( code=400, message="Path and body tag have to be equal for a new tag" ), 400 session.add(model) model.from_swagger_model(tag_obj) session.commit() return model.to_swagger_model(user=user) except Exception as e: return handle_exception(e)
def get_bag_meta(store_name, bag_name, user=None): """ List products from bag :param store_name: Name of the store :type store_name: str :param bag_name: Name of the bag :type bag_name: str :rtype: BagDetailed """ try: session = Database.get_session() bag_model, e = find_bag_in_database(session, store_name, bag_name) if e: return e, e.code return bag_model.to_swagger_model_detailed(user=user) except Exception as e: return handle_exception(e)
def list_sessions(user=None): """ List current session :rtype: List[Session] """ try: q = Database.get_session().query(Session).filter( Session.user_uid == user.uid) models = [] for session in q: model = session.to_swagger_model() model.token = "" models.append(model) return models except Exception as e: return error_helper.handle_exception(e)
def get_tag(tag, user=None): """Get tag info # noqa: E501 :param tag: Name of the tag :type tag: str :rtype: Tag """ try: session = Database.get_session() q = session.query(Tag).filter(Tag.tag == tag) # type: Query if q.count() == 0: return Error(code=404, message="Tag not found"), 404 return q[0].to_swagger_model(user=user) except Exception as e: return handle_exception(e)
def patch_task(task_identifier, task, user=None): # noqa: E501 """Partial update of task (this only supports a few fields) # noqa: E501 :param task_identifier: :type task_identifier: str :param task: Fields to update :type task: :rtype: TaskDetailed """ session = Database.get_session() try: identifier = int(task_identifier) except ValueError: return Error(code=400, message="Invalid task identifier"), 400 try: q = session.query(Task).filter( Task.uid == int(identifier)) # type: Query model = q.first() if model: changed = False if 'log_append' in task and isinstance(task['log_append'], str): model.log += task['log_append'] changed = True if changed: session.commit() return model.to_swagger_model_detailed(user=user) else: return Error(code=404, message="Task not found"), 404 except Exception as e: return handle_exception(e)
def get_bag_comments(store_name, bag_name, user=None): # noqa: E501 """List comments from bag # noqa: E501 :param store_name: Name of the store :type store_name: str :param bag_name: Name of the bag :type bag_name: str :rtype: List[Comment] """ try: session = Database.get_session() bag, e = find_bag_in_database(session, store_name, bag_name) if e: return e, e.code return [p.to_swagger_model(user=user) for p in bag.comments] except Exception as e: return handle_exception(e)
def new_simulation(simulation, trigger=None, user=None): # noqa: E501 """New simulation # noqa: E501 :param simulation: Simulation :type simulation: dict | bytes :rtype: SimulationDetailed """ try: if connexion.request.is_json: simulation = SimulationDetailed.from_dict( connexion.request.get_json()) # noqa: E501 session = Database.get_session() q = session.query(SimulationEnvironment).filter( SimulationEnvironment.name == simulation.environment_name) if not q.first(): return Error(code=400, message="Simulation environment '%s' not found" % simulation.environment_name), 400 model = Simulation() model.from_swagger_model(simulation, user=user) model.environment_id = q.first().uid model.uid = None session.add(model) session.commit() # Return a fresh copy from the DB q = session.query(Simulation).filter(Simulation.uid == model.uid) m = NewSimulationHook.trigger(q.first(), session, trigger, user) return m.to_swagger_model_detailed(user=user), 200 except Exception as e: return handle_exception(e)
def list_simulation_runs(sim_identifier, user=None): # noqa: E501 """List simulation runs # noqa: E501 :param sim_identifier: :type sim_identifier: int :rtype: List[SimulationRunSummary] """ try: session = Database.get_session() q = session.query(Simulation).filter( Simulation.uid == sim_identifier) # type: Query model = q.first() if model: return [p.to_swagger_model_summary(user=user) for p in model.runs] else: return Error(code=404, message="Simulation not found"), 404 except Exception as e: return handle_exception(e)