def update_graph(graph_id): app.logger.debug(request.data) data = json.loads(request.data) if not data: return make_fail_response('Expected json data'), 400 graph_dict = graph_collection_manager.get_db_graph(graph_id, g.user._id) if not graph_dict: return make_fail_response('Graph was not found'), 404 if graph_dict['_readonly']: return make_fail_response('Permission denied'), 403 if graph_dict['graph_running_status'] != GraphRunningStatus.CREATED: return make_fail_response('Cannot save graph with status `{}`'.format(graph_dict['graph_running_status'])) update_dict_recursively(graph_dict, data) graph = Graph.from_dict(graph_dict) graph.save() return JSONEncoder().encode({ 'status': 'success', 'message': 'Successfully updated', 'graph': graph.to_dict(), })
def create_demo_graphs(user): res = [] for graph_id in DemoUserManager.demo_config.graph_ids: graph = Graph.load(graph_id) graph._id = ObjectId() graph.author = user._id graph.save() res.append(graph._id) return res
def get_graphs(graph_running_status): """Find all the Graphs with a given graph_running_status. Args: graph_running_status (str): Graph Running Status """ db_graphs = get_db_connector().graphs.find( {'graph_running_status': graph_running_status}) graphs = [] for db_graph in db_graphs: graphs.append(Graph.from_dict(db_graph)) return graphs
def get_graph(graph_id=None): if graph_id == 'new': return JSONEncoder().encode({ 'data': Graph().to_dict(), 'status': 'success', 'resources_dict': resource_manager.resources_dict, }) else: graph = graph_collection_manager.get_db_graph(graph_id) if graph: return JSONEncoder().encode({ 'data': graph, 'status': 'success', 'resources_dict': resource_manager.resources_dict, }) else: return make_fail_response('Graph was not found'), 404
def __init__(self, graph, node_collection=None): if isinstance(graph, Graph): self.graph_id = graph._id self.graph = graph else: self.graph_id = graph self.graph = Graph.load(self.graph_id) self.node_id_to_node = {node._id: node for node in self.graph.nodes} # number of dependencies to ids self.dependency_index_to_node_ids = defaultdict(lambda: set()) self.node_id_to_dependents = defaultdict(lambda: set()) self.node_id_to_dependency_index = defaultdict(lambda: 0) self.uncompleted_nodes_count = 0 if node_collection: self.node_collection = node_collection else: self.node_collection = NodeCollection() for node in self.graph.nodes: # ignore nodes in finished statuses if NodeRunningStatus.is_finished(node.node_running_status): continue node_id = node._id dependency_index = 0 for node_input in node.inputs: for input_value in node_input.values: parent_node_id = to_object_id(input_value.node_id) self.node_id_to_dependents[parent_node_id].add(node_id) if not NodeRunningStatus.is_finished( self.node_id_to_node[parent_node_id]. node_running_status): dependency_index += 1 if not NodeRunningStatus.is_finished(node.node_running_status): self.uncompleted_nodes_count += 1 self.dependency_index_to_node_ids[dependency_index].add(node_id) self.node_id_to_dependency_index[node_id] = dependency_index
def post_graph_node_action(graph_id, action): graph_dict = graph_collection_manager.get_db_graph(graph_id, g.user._id) if not graph_dict: return make_fail_response('Graph was not found'), 404 if graph_dict['_readonly']: return make_fail_response('Permission denied'), 403 if not request.data: return make_fail_response('Empty body'), 400 data = json.loads(request.data) graph = Graph.from_dict(graph_dict) if action == GraphNodePostAction.INSERT_NODE: node_id = data.get('node_id', None) x, y = int(data.get('x', 0)), int(data.get('y', 0)) node_dict = node_collection_manager.get_db_node(node_id) if not node_dict: return make_fail_response('Node was not found'), 404 node = Node.from_dict(node_dict) node.x, node.y = x, y node.parent_node = node._id node._id = ObjectId() graph.nodes.append(node) graph.save() return make_success_response(node=node.to_dict()) elif action == GraphNodePostAction.REMOVE_NODE: node_id = ObjectId(data.get('node_id', None)) node_index = -1 for index, node in enumerate(graph.nodes): for input in node.inputs: input.values = [value for value in input.values if ObjectId(value.node_id) != node_id] if ObjectId(node._id) == node_id: node_index = index if node_index < 0: return make_fail_response('Node was not found'), 404 del graph.nodes[node_index] graph.save() return make_success_response('Node removed') elif action == GraphNodePostAction.CHANGE_PARAMETER: node_id = data.get('node_id', None) parameter_name = data.get('parameter_name', None) parameter_value = data.get('parameter_value', None) if parameter_name is None: return make_fail_response('No parameter name'), 400 if parameter_value is None: return make_fail_response('No parameter value'), 400 node, = _find_nodes(graph, node_id) if not node: return make_fail_response('Node was not found'), 404 for parameter in node.parameters: if parameter.name == parameter_name: parameter_dict = parameter.to_dict() parameter_dict['value'] = parameter_value parameter.value = Parameter(obj_dict=parameter_dict).value graph.save() return make_success_response('Parameter updated') elif action in (GraphNodePostAction.CREATE_LINK, GraphNodePostAction.REMOVE_LINK): for field in ['from', 'to']: for sub_field in ['node_id', 'resource']: if field not in data: return make_fail_response('`{}` is missing'.format(field)), 400 if sub_field not in data[field]: return make_fail_response('`{}.{}` is missing'.format(field, sub_field)), 400 from_node_id = data['from']['node_id'] from_resource = data['from']['resource'] to_node_id = data['to']['node_id'] to_resource = data['to']['resource'] from_node, to_node = _find_nodes(graph, from_node_id, to_node_id) if not from_node or not to_node: return make_fail_response('Node was not found'), 404 from_output = None to_input = None for output in from_node.outputs: if output.name == from_resource: from_output = output break for input in to_node.inputs: if input.name == to_resource: to_input = input break if not from_output or not to_input: return make_fail_response('Input or output not found'), 404 if action == GraphNodePostAction.CREATE_LINK: # TODO graph.validate() it if from_output.file_type not in to_input.file_types and 'file' not in to_input.file_types: return make_fail_response('Incompatible types'), 400 # TODO graph.validate() it if to_input.max_count > 0 and len(to_input.values) >= to_input.max_count: return make_fail_response('Number of inputs reached the limit'), 400 new_input_value = InputValue() new_input_value.node_id = from_node_id new_input_value.output_id = from_resource # TODO graph.validate() it for value in to_input.values: if value.node_id == from_node_id and value.output_id == from_resource: return make_fail_response('Link already exists'), 400 to_input.values.append(new_input_value) elif action == GraphNodePostAction.REMOVE_LINK: rm_index = -1 # TODO graph.validate() it for index, value in enumerate(to_input.values): if value.node_id == from_node_id and value.output_id == from_resource: rm_index = index break if rm_index < 0: return make_fail_response('Link not found'), 404 del to_input.values[rm_index] graph.save() return make_success_response('Completed') else: return make_fail_response('Unknown action `{}`'.format(action)), 400 return 'ok'
def post_graph_action(graph_id, action): graph_dict = graph_collection_manager.get_db_graph(graph_id) if not graph_dict: return make_fail_response('Graph was not found'), 404 return _perform_graph_actions(Graph.from_dict(graph_dict), [action.upper()])
def post_graph(): app.logger.debug(request.data) data = json.loads(request.data) graph = Graph.from_dict(data['graph']) actions = data['actions'] return _perform_graph_actions(graph, actions)