def get_graph(graph_id=None): if graph_id == 'new': return JSONEncoder().encode({ 'data': Graph().to_dict(), 'status': 'success' }) elif graph_id: graph = graph_collection_manager.get_db_graph(graph_id) if graph: return JSONEncoder().encode({'data': graph, 'status': 'success'}) else: return 'Graph was not found', 404 else: query = json.loads(request.args.get('query', "{}")) query["author"] = to_object_id(g.user._id) count_query = {k: v for k, v in query.items() if k in COUNT_QUERY_KEYS} graphs_query = { k: v for k, v in query.items() if k in PAGINATION_QUERY_KEYS } return JSONEncoder().encode({ 'graphs': [ graph for graph in graph_collection_manager.get_db_graphs( **graphs_query) ], 'total_count': graph_collection_manager.get_db_graphs_count(**count_query), 'status': 'success' })
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 __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 node.node_running_status in { NodeRunningStatus.SUCCESS, NodeRunningStatus.FAILED, NodeRunningStatus.STATIC, NodeRunningStatus.RESTORED, NodeRunningStatus.CANCELED }: 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 self.node_id_to_node[ parent_node_id].node_running_status not in { NodeRunningStatus.SUCCESS, NodeRunningStatus.FAILED, NodeRunningStatus.STATIC, NodeRunningStatus.RESTORED, NodeRunningStatus.CANCELED }: dependency_index += 1 if node.node_running_status not in { NodeRunningStatus.SUCCESS, NodeRunningStatus.FAILED, NodeRunningStatus.STATIC, NodeRunningStatus.RESTORED, NodeRunningStatus.CANCELED }: 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(): app.logger.debug(request.data) try: body = json.loads(request.data)['body'] graph = Graph.from_dict(body['graph']) graph.author = g.user._id actions = body['actions'] extra_response = {} for action in actions: if action == GraphPostAction.SAVE: if graph.graph_running_status != GraphRunningStatus.CREATED: return _make_fail_response('Cannot save graph with status `{}`'.format(graph.graph_running_status)) graph.save(force=True) elif action == GraphPostAction.AUTO_LAYOUT: graph.arrange_auto_layout() elif action == GraphPostAction.UPGRADE_NODES: upd = graph_collection_manager.upgrade_nodes(graph) extra_response['upgraded_nodes_count'] = upd elif action == GraphPostAction.APPROVE: if graph.graph_running_status != GraphRunningStatus.CREATED: return _make_fail_response('Graph status `{}` expected. Found `{}`'.format(GraphRunningStatus.CREATED, graph.graph_running_status)) validation_error = graph.get_validation_error() if validation_error: return JSONEncoder().encode({ 'status': GraphPostStatus.VALIDATION_FAILED, 'message': 'Graph validation failed', 'validation_error': validation_error.to_dict() }) graph.graph_running_status = GraphRunningStatus.READY graph.save(force=True) elif action == GraphPostAction.VALIDATE: validation_error = graph.get_validation_error() if validation_error: return JSONEncoder().encode({ 'status': GraphPostStatus.VALIDATION_FAILED, 'message': 'Graph validation failed', 'validation_error': validation_error.to_dict() }) elif action == GraphPostAction.CANCEL: if graph.graph_running_status not in [ GraphRunningStatus.RUNNING, GraphRunningStatus.FAILED_WAITING ]: return _make_fail_response('Graph status `{}` expected. Found `{}`'.format(GraphRunningStatus.RUNNING, graph.graph_running_status)) graph_cancellation_manager.cancel_graph(graph._id) else: return _make_fail_response('Unknown action `{}`'.format(action)) return JSONEncoder().encode(dict( { 'status': GraphPostStatus.SUCCESS, 'message': 'Graph(_id=`{}`) successfully updated'.format(str(graph._id)), 'graph': graph.to_dict(), 'url': '{}/graphs/{}'.format(WEB_CONFIG.endpoint.rstrip('/'), str(graph._id)) }, **extra_response)) except Exception as e: app.logger.error(traceback.format_exc()) return _make_fail_response('Internal error: "{}"'.format(repr(e)))