Ignore:
Timestamp:
04/20/18 11:52:33 (4 years ago)
Author:
jzenisek
Message:

#2839: updated genealogy computation for hive job administrator

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2839_HiveProjectManagement/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveResourceSelector.cs

    r15658 r15913  
    4343    public const string additionalSlavesGroupDescription = "Contains additional slaves which are either ungrouped or the parenting slave group is not assigned to the selected project.";
    4444
     45    private const string CURRENT_SELECTION_TAG = " [current selection]";
     46    private const string NEW_SELECTION_TAG = " [new selection]";
     47    private const string CHANGED_SELECTION_TAG = " [changed selection]";
    4548
    4649    private readonly HashSet<TreeNode> mainTreeNodes = new HashSet<TreeNode>();
     
    5457    private readonly HashSet<Resource> newIncludedResources = new HashSet<Resource>();
    5558
    56     private readonly Dictionary<Guid, HashSet<Project>> projectAncestors = new Dictionary<Guid, HashSet<Project>>();
    57     private readonly Dictionary<Guid, HashSet<Project>> projectDescendants = new Dictionary<Guid, HashSet<Project>>();
    58     private readonly Dictionary<Guid, HashSet<Resource>> resourceAncestors = new Dictionary<Guid, HashSet<Resource>>();
    59     private readonly Dictionary<Guid, HashSet<Resource>> resourceDescendants = new Dictionary<Guid, HashSet<Resource>>();
    60 
    6159    private IEnumerable<Resource> addedResources;
    6260    private IEnumerable<Resource> removedResources;
     
    6866    private readonly Color addedIncludeColor = Color.FromArgb(25, 169, 221, 221); // #a9dddd
    6967    private readonly Color removedIncludeColor = Color.FromArgb(25, 249, 210, 145); // #f9d291
    70     private readonly Color selectedColor = Color.FromArgb(255, 240, 194, 59); // #f0c23b
     68    private readonly Color selectedBackColor = Color.DodgerBlue;
     69    private readonly Color selectedForeColor = Color.White;
    7170
    7271    private string currentSearchString;
     
    173172      base.OnContentChanged();
    174173
    175       if (Content != null) {       
    176         UpdateProjectGenealogy();
    177         UpdateResourceGenealogy();
    178        
     174      if (Content != null) {               
    179175        if (SelectedProjectId.HasValue && SelectedProjectId.Value != Guid.Empty) {
    180176          SelectedProject = GetSelectedProjectById(SelectedProjectId.Value);
     
    218214        projectsTreeView.SelectedNode = null;
    219215      } else {
    220         ReColorTreeNodes(projectsTreeView.Nodes, selectedColor, Color.Transparent, true);
    221         e.Node.BackColor = selectedColor;
     216        ResetTreeNodes(projectsTreeView.Nodes);
     217        e.Node.BackColor = selectedBackColor;
     218        e.Node.ForeColor = selectedForeColor;
    222219       
    223220        if(node.Id == projectId) {
    224           e.Node.Text += " [current selection]";
     221          e.Node.Text += CURRENT_SELECTION_TAG;
    225222        } else if(projectId == null || projectId == Guid.Empty) {
    226           e.Node.Text += " [new selection]";
     223          e.Node.Text += NEW_SELECTION_TAG;
    227224        } else {
    228           e.Node.Text += " [changed selection]";
     225          e.Node.Text += CHANGED_SELECTION_TAG;
    229226        }
    230227
     
    270267
    271268    #region Helpers
    272 
    273 
    274     #region old
    275     private void UpdateMainTree() {
    276       mainTreeNodes.Clear();
    277 
    278       foreach (Project g in Content.OrderBy(x => x.Name)) {
    279         if (g.ParentProjectId == null) {
    280           TreeNode tn = new TreeNode();
    281           tn.ImageIndex = greenFlagImageIndex;
    282           tn.SelectedImageIndex = tn.ImageIndex;
    283 
    284           tn.Tag = g;
    285           tn.Text = g.Name;
    286           tn.Checked = assignedResources.Any(x => x.Id == g.Id);
    287 
    288           BuildMainTree(tn);
    289           mainTreeNodes.Add(tn);
    290         }
    291       }
    292       UpdateFilteredTree();
    293     }
    294 
    295     private void BuildMainTree(TreeNode tn) {
    296       foreach (Project r in Content.Where(s => s.ParentProjectId != null && s.ParentProjectId == ((Project)tn.Tag).Id).OrderBy(x => x.Name)) {
    297         TreeNode stn = new TreeNode(r.Name);
    298         stn.ImageIndex = redFlagImageIndex;
    299         stn.SelectedImageIndex = stn.ImageIndex;
    300         stn.Tag = r;
    301         stn.Checked = assignedResources.Any(x => x.Id == r.Id);
    302         tn.Nodes.Add(stn);
    303         mainTreeNodes.Add(stn);
    304 
    305         BuildMainTree(stn);
    306       }
    307     }
    308 
    309     private void UpdateFilteredTree() {
    310       filteredTreeNodes.Clear();
    311       foreach (TreeNode n in mainTreeNodes) {
    312         n.BackColor = SystemColors.Window;
    313         if (currentSearchString == null || ((Project)n.Tag).Name.ToLower().Contains(currentSearchString)) {
    314           n.BackColor = string.IsNullOrEmpty(currentSearchString) ? SystemColors.Window : Color.LightBlue;
    315           filteredTreeNodes.Add(n);
    316           TraverseParentNodes(n);
    317         }
    318       }
    319       UpdateProjectsTree();
    320     }
    321 
    322     private void UpdateProjectsTree() {
    323       projectsTreeView.Nodes.Clear();
    324       nodeStore.Clear();
    325 
    326       foreach (TreeNode node in filteredTreeNodes) {
    327         var clone = nodeStore.SingleOrDefault(x => ((Project)x.Tag).Id == ((Project)node.Tag).Id);
    328         if (clone == null) {
    329           clone = (TreeNode)node.Clone();
    330           nodeStore.Add(clone);
    331           clone.Nodes.Clear();
    332         }
    333         foreach (TreeNode child in node.Nodes)
    334           if (filteredTreeNodes.Any(x => ((Project)x.Tag).Id == ((Project)child.Tag).Id)) {
    335             var childClone = nodeStore.SingleOrDefault(x => ((Project)x.Tag).Id == ((Project)child.Tag).Id);
    336             if (childClone == null) {
    337               childClone = (TreeNode)child.Clone();
    338               nodeStore.Add(childClone);
    339               childClone.Nodes.Clear();
    340             }
    341             clone.Nodes.Add(childClone);
    342           }
    343       }
    344       projectsTreeView.Nodes.AddRange(nodeStore.Where(x => ((Project)x.Tag).ParentProjectId == null).ToArray());
    345       if (string.IsNullOrEmpty(currentSearchString)) ExpandSlaveGroupNodes();
    346       else projectsTreeView.ExpandAll();
    347     }
    348 
    349     private void TraverseParentNodes(TreeNode node) {
    350       if (node != null) {
    351         for (TreeNode parent = node.Parent; parent != null; parent = parent.Parent)
    352           filteredTreeNodes.Add(parent);
    353       }
    354     }
    355     #endregion
    356269
    357270    private Project GetSelectedProjectById(Guid projectId) {
     
    368281          if(project.Name.ToLower().Contains(currentSearchString.ToLower())) {
    369282            filteredProjects.Add(project);
    370             filteredProjects.UnionWith(projectAncestors[project.Id]);
     283            filteredProjects.UnionWith(Content.Where(p => HiveClient.Instance.ProjectAncestors[project.Id].Contains(p.Id)));
    371284          }
    372285        }
     
    379292      if (!projects.Any()) return;
    380293
    381       // select all top level projects (withouth parent, or without parent within current project collection)
     294      // select all top level projects (withouth parent, or without any ancestor within current project collection)
    382295      var mainProjects = new HashSet<Project>(projects.Where(x => x.ParentProjectId == null));
    383296      var parentedMainProjects = new HashSet<Project>(projects
    384297        .Where(x => x.ParentProjectId.HasValue
    385         && !projects.Select(y => y.Id).Contains(x.ParentProjectId.Value)));
     298        && !projects.Select(y => y.Id).Contains(x.ParentProjectId.Value)
     299        && !projects.SelectMany(y => HiveClient.Instance.ProjectAncestors[y.Id]).Contains(x.ParentProjectId.Value)));
    386300      mainProjects.UnionWith(parentedMainProjects);
    387301      var subProbjects = new HashSet<Project>(projects.Except(mainProjects));
     302      foreach(var p in subProbjects) {
     303        p.ParentProjectId = HiveClient.Instance.ProjectAncestors[p.Id].Where(x => projects.Select(y => y.Id).Contains(x)).FirstOrDefault();
     304      }
    388305
    389306      var stack = new Stack<Project>(mainProjects.OrderByDescending(x => x.Name));
     
    412329
    413330        if (SelectedProject != null && SelectedProject.Id.Equals(newProject.Id)) {
    414           newNode.BackColor = selectedColor;
     331          newNode.BackColor = selectedBackColor;
     332          newNode.ForeColor = selectedForeColor;
    415333          if(SelectedProject.Id == projectId) {
    416             newNode.Text += " [current selection]";
     334            newNode.Text += CURRENT_SELECTION_TAG;
    417335          } else if (projectId == null || projectId == Guid.Empty) {
    418             newNode.Text += " [new selection]";
     336            newNode.Text += NEW_SELECTION_TAG;
    419337          } else {
    420             newNode.Text += " [changed selection]";
     338            newNode.Text += CHANGED_SELECTION_TAG;
    421339          }
    422340        }
     
    439357      projectsTreeView.ExpandAll();
    440358    }
    441 
    442     private void UpdateProjectGenealogy() {
    443       projectAncestors.Clear();
    444       projectDescendants.Clear();
    445       var projects = Content;
    446 
    447       foreach (var p in projects) {
    448         projectAncestors.Add(p.Id, new HashSet<Project>());
    449         projectDescendants.Add(p.Id, new HashSet<Project>());
    450       }
    451 
    452       foreach (var p in projects) {
    453         var parentProjectId = p.ParentProjectId;
    454         while (parentProjectId != null) {
    455           var parent = projects.SingleOrDefault(x => x.Id == parentProjectId);
    456           if (parent != null) {
    457             projectAncestors[p.Id].Add(parent);
    458             projectDescendants[parent.Id].Add(p);
    459             parentProjectId = parent.ParentProjectId;
    460           } else {
    461             parentProjectId = null;
    462           }
    463         }
    464       }
    465     }
    466 
    467359
    468360    private static IEnumerable<Resource> GetAssignedResourcesForProject(Guid projectId) {
     
    495387        foreach (var resource in assignedProjectResources) {
    496388          availableResources.Add(resource);
    497           foreach(var descendant in resourceDescendants[resource.Id]) {
     389          foreach(var descendant in HiveClient.Instance.Resources.Where(x => HiveClient.Instance.ResourceDescendants[resource.Id].Contains(x.Id))) {
    498390            availableResources.Add(descendant);
    499391          }
     
    544436    private void UpdateNewAssignedResources() {
    545437      for(int i = newAssignedResources.Count-1; i>=0; i--) {
    546         if(newAssignedResources.Intersect(resourceAncestors[newAssignedResources.ElementAt(i).Id]).Any()) {
     438        if(newAssignedResources.Intersect(HiveClient.Instance.GetAvailableParentResources(newAssignedResources.ElementAt(i).Id)).Any()) {
    547439          newAssignedResources.Remove(newAssignedResources.ElementAt(i));
    548440        }
     
    556448      if (JobId != Guid.Empty) {
    557449        foreach (var item in assignedResources) {
    558           foreach (var descendant in resourceDescendants[item.Id]) {
     450          foreach (var descendant in HiveClient.Instance.GetAvailableChildResources(item.Id)) {
    559451            includedResources.Add(descendant);
    560452          }
     
    563455
    564456      foreach (var item in newAssignedResources) {
    565         foreach (var descendant in resourceDescendants[item.Id]) {
     457        foreach (var descendant in HiveClient.Instance.GetAvailableChildResources(item.Id)) {
    566458          newIncludedResources.Add(descendant);
    567459        }
     
    572464      newIncludedResources.Clear();
    573465      foreach (var item in newAssignedResources) {
    574         foreach (var descendant in resourceDescendants[item.Id]) {
     466        foreach (var descendant in HiveClient.Instance.GetAvailableChildResources(item.Id)) {
    575467          newIncludedResources.Add(descendant);
    576         }
    577       }
    578     }
    579 
    580     private void UpdateResourceGenealogy() {
    581       resourceAncestors.Clear();
    582       resourceDescendants.Clear();
    583       var resources = HiveClient.Instance.Resources;
    584 
    585       foreach (var r in resources) {
    586         resourceAncestors.Add(r.Id, new HashSet<Resource>());
    587         resourceDescendants.Add(r.Id, new HashSet<Resource>());
    588       }
    589 
    590       foreach (var r in resources) {
    591         var parentResourceId = r.ParentResourceId;
    592         while (parentResourceId != null) {
    593           var parent = resources.SingleOrDefault(x => x.Id == parentResourceId);
    594           if (parent != null) {
    595             resourceAncestors[r.Id].Add(parent);
    596             resourceDescendants[parent.Id].Add(r);
    597             parentResourceId = parent.ParentResourceId;
    598           } else {
    599             parentResourceId = null;
    600           }
    601468        }
    602469      }
     
    743610      HashSet<Slave> newAssignedSlaves = new HashSet<Slave>(newAssignedResources.OfType<Slave>());
    744611      foreach (var slaveGroup in newAssignedResources.OfType<SlaveGroup>()) {
    745         foreach(var slave in resourceDescendants[slaveGroup.Id].OfType<Slave>()) {
     612        foreach(var slave in HiveClient.Instance.ResourceDescendants[slaveGroup.Id].OfType<Slave>()) {
    746613          newAssignedSlaves.Add(slave);
    747614        }
     
    753620        var slaveGroup = resource as SlaveGroup;
    754621        if (slaveGroup != null) {
    755           selectedSlaves = new HashSet<Slave>(resourceDescendants[slaveGroup.Id].OfType<Slave>());
     622          selectedSlaves = new HashSet<Slave>(HiveClient.Instance.ResourceDescendants[slaveGroup.Id].OfType<Slave>());
    756623          //selectedSlaves.IntersectWith(newAssignedSlaves);
    757624        } else {
     
    771638    }
    772639
    773     private void ReColorTreeNodes(TreeNodeCollection nodes, Color c1, Color c2, bool resetText) {
     640    private void StyleTreeNode(TreeNode n, string name) {
     641      n.Text = name;
     642      n.BackColor = Color.Transparent;
     643      n.ForeColor = Color.Black;
     644    }
     645
     646    private void ResetTreeNodes(TreeNodeCollection nodes) {
    774647      foreach (TreeNode n in nodes) {
    775         if (n.BackColor.Equals(c1)) {
    776           n.BackColor = c2;
    777           if(resetText) n.Text = ((Project)n.Tag).Name;
    778         }
     648        string name = "";
     649        if (n.Tag is Project) name = ((Project)n.Tag).Name;
     650        else if (n.Tag is Resource) name = ((Resource)n.Tag).Name;
     651        StyleTreeNode(n, name);
    779652        if (n.Nodes.Count > 0) {
    780           ReColorTreeNodes(n.Nodes, c1, c2, resetText);
     653          ResetTreeNodes(n.Nodes);
    781654        }
    782655      }
Note: See TracChangeset for help on using the changeset viewer.