public async Task<IHttpActionResult> GetDependencyTree(string objName) { try { var callTree = new CallTreeData(); // Validation - require query if (string.IsNullOrEmpty(objName)) return Ok(callTree); // Try get graph from cache DbGraph graph = cachedGraph; // Load graph if not cached if (graph == null) { graph = await graphService.GenerateGraph(); cachedGraph = graph; // Persist to cache } // Get call tree from object if found var obj = await graphService.GetCallTree(objName); if (obj != null) { var dic = new Dictionary<string, string[]>(); buildDependencyDic(obj, dic); foreach (var entry in dic) { // Add object entry callTree.objects.Add(entry.Key, entry.Value); // Add meta data entry for object var item = graph.Objects.FirstOrDefault(o => o.Name == entry.Key); if (item != null) callTree.metadata.Add(entry.Key, new ObjectData { type = item.ObjectTypeKey }); } } return Ok(callTree); } catch (Exception ex) { return InternalServerError(ex); } }
public async Task<DbGraph> GenerateGraph() { //get schema var schema = await getSchema(); List<DbObjectInfo> objectInfos = schema.Objects; List<DbObjectReferenceRow> refTable = schema.References; List<DbObject> graph = new List<DbObject>(); //collect all db objects foreach (var obj in objectInfos) { //ensure isnt already inserted var source = graph.FirstOrDefault(r => r.Name == obj.fullName); if (source == null) { source = Mapper.Map(obj); graph.Add(source); } } //merge into mapped lists foreach (var reference in refTable) { //try get both source and target from graph var source = graph.FirstOrDefault(r => r.Name == reference.fromFullName); var target = graph.FirstOrDefault(r => r.Name == reference.toFullName); //ensure both from and to objects exist in object list //and that the source isnt referencing the target (causes infinite loop) if (source == null || target == null || source.Name == target.Name) continue; //create reference from source to target and vice versa if (!source.DependsOn.Any(r => r.Name == target.Name)) { source.DependsOn.Add(target); target.DependedOnBy.Add(source); } } var graphObj = new DbGraph { Objects = graph, CapturedUtc = DateTime.UtcNow, }; return graphObj; }