Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2845_EnhancedProgress/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveResourceSelector.cs @ 16308

Last change on this file since 16308 was 16308, checked in by pfleck, 6 years ago

#2845 reverted the last merge (r16307) because some revisions were missing

File size: 12.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Text;
27using System.Windows.Forms;
28using HeuristicLab.Clients.Hive.JobManager.ExtensionMethods;
29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33
34namespace HeuristicLab.Clients.Hive.JobManager.Views {
35  [View("Hive Resource Selector View")]
36  [Content(typeof(IItemList<Resource>), true)]
37  public partial class HiveResourceSelector : ItemView, IDisposable {
38    private const int slaveImageIndex = 0;
39    private const int slaveGroupImageIndex = 1;
40    private string currentSearchString;
41    private ISet<TreeNode> mainTreeNodes;
42    private ISet<TreeNode> filteredTreeNodes;
43    private ISet<TreeNode> nodeStore;
44   
45    private ISet<Resource> selectedResources;
46    public ISet<Resource> SelectedResources {
47      get { return selectedResources; }
48      set { selectedResources = value; }
49    }
50
51    public new IItemList<Resource> Content {
52      get { return (IItemList<Resource>)base.Content; }
53      set { base.Content = value; }
54    }
55
56    public HiveResourceSelector() {
57      InitializeComponent();
58      mainTreeNodes = new HashSet<TreeNode>();
59      filteredTreeNodes = new HashSet<TreeNode>();
60      nodeStore = new HashSet<TreeNode>();
61      selectedResources = new HashSet<Resource>();
62      imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.MonitorLarge);
63      imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.NetworkCenterLarge);
64    }
65 
66    public void StartProgressView() {
67      if (InvokeRequired) {
68        Invoke(new Action(StartProgressView));
69      } else {
70        Progress.ShowMarquee(this, "Downloading resources. Please be patient.");
71      }
72    }
73
74    public void FinishProgressView() {
75      if (InvokeRequired) {
76        Invoke(new Action(FinishProgressView));
77      } else {
78        Progress.Hide(this);
79      }
80    }
81
82    protected override void OnContentChanged() {
83      base.OnContentChanged();
84
85      if (Content != null) {
86        selectedResources = new HashSet<Resource>(Content.Where(x => selectedResources.Any(y => x.Id == y.Id)));
87        UpdateMainTree();
88        ExtractStatistics();
89      } else {
90        mainTreeNodes.Clear();
91        UpdateFilteredTree();
92      }
93    }
94
95    #region MainTree Methods
96    private void UpdateMainTree() {
97      mainTreeNodes.Clear();
98
99      foreach (Resource g in Content) {
100        if (g.GetType() == typeof(SlaveGroup)) {
101          //root node
102          if (g.ParentResourceId == null) {
103            TreeNode tn = new TreeNode();
104            tn.ImageIndex = slaveGroupImageIndex;
105            tn.SelectedImageIndex = tn.ImageIndex;
106
107            tn.Tag = g;
108            tn.Text = g.Name;
109            tn.Checked = selectedResources.Any(x => x.Id == g.Id);
110
111            BuildMainTree(tn);
112            mainTreeNodes.Add(tn);
113          }
114        }
115      }
116      UpdateFilteredTree();
117    }
118
119    private void BuildMainTree(TreeNode tn) {
120      foreach (Resource r in Content.Where(s => s.ParentResourceId != null && s.ParentResourceId == ((Resource)tn.Tag).Id)) {
121        TreeNode stn = new TreeNode(r.Name);
122        if (r is Slave) stn.ImageIndex = slaveImageIndex;
123        else if (r is SlaveGroup) stn.ImageIndex = slaveGroupImageIndex;
124        stn.SelectedImageIndex = stn.ImageIndex;
125        stn.Tag = r;
126        stn.Checked = selectedResources.Any(x => x.Id == r.Id);
127        tn.Nodes.Add(stn);
128        mainTreeNodes.Add(stn);
129
130        BuildMainTree(stn);
131      }
132    }
133    #endregion
134
135    #region FilteredTree Methods
136    private void UpdateFilteredTree() {
137      filteredTreeNodes.Clear();
138      foreach (TreeNode n in mainTreeNodes) {
139        n.BackColor = SystemColors.Window;
140        if (currentSearchString == null || ((Resource)n.Tag).Name.ToLower().Contains(currentSearchString)) {
141          n.BackColor = string.IsNullOrEmpty(currentSearchString) ? SystemColors.Window : Color.LightBlue;
142          filteredTreeNodes.Add(n);
143          TraverseParentNodes(n);
144        }
145      }
146      UpdateResourceTree();
147    }
148
149    private void TraverseParentNodes(TreeNode node) {
150      if (node != null) {
151        for (TreeNode parent = node.Parent; parent != null; parent = parent.Parent)
152          filteredTreeNodes.Add(parent);
153      }
154    }
155    #endregion
156
157    #region ResourceTree Methods
158    private void UpdateResourceTree() {
159      resourcesTreeView.Nodes.Clear();
160      nodeStore.Clear();
161
162      foreach (TreeNode node in filteredTreeNodes) {
163        var clone = nodeStore.SingleOrDefault(x => ((Resource)x.Tag).Id == ((Resource)node.Tag).Id);
164        if (clone == null) {
165          clone = (TreeNode)node.Clone();
166          nodeStore.Add(clone);
167          clone.Nodes.Clear();
168        }
169        foreach (TreeNode child in node.Nodes)
170          if (filteredTreeNodes.Any(x => ((Resource)x.Tag).Id == ((Resource)child.Tag).Id)) {
171            var childClone = nodeStore.SingleOrDefault(x => ((Resource)x.Tag).Id == ((Resource)child.Tag).Id);
172            if (childClone == null) {
173              childClone = (TreeNode)child.Clone();
174              nodeStore.Add(childClone);
175              childClone.Nodes.Clear();
176            }
177            clone.Nodes.Add(childClone);
178          }
179      }
180      resourcesTreeView.Nodes.AddRange(nodeStore.Where(x => ((Resource)x.Tag).ParentResourceId == null).ToArray());
181      if (string.IsNullOrEmpty(currentSearchString)) ExpandSlaveGroupNodes();
182      else resourcesTreeView.ExpandAll();
183    }
184    #endregion
185
186    #region Events
187    private void resourcesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
188      if (e.Action != TreeViewAction.Unknown) {
189        if (e.Node.Checked) {
190          IncludeChildNodes(mainTreeNodes.SingleOrDefault(x => ((Resource)x.Tag).Id == ((Resource)e.Node.Tag).Id));
191          IncludeParentNodes(mainTreeNodes.SingleOrDefault(x => ((Resource)x.Tag).Id == ((Resource)e.Node.Tag).Id));
192        } else {
193          ExcludeChildNodes(mainTreeNodes.SingleOrDefault(x => ((Resource)x.Tag).Id == ((Resource)e.Node.Tag).Id));
194          ExcludeParentNodes(mainTreeNodes.SingleOrDefault(x => ((Resource)x.Tag).Id == ((Resource)e.Node.Tag).Id));
195        }
196        ExtractStatistics();
197      }
198    }
199
200    private void resourcesTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
201      if (e.Action != TreeViewAction.Unknown) {
202        ExtractStatistics(e.Node);
203      }
204    }
205
206    private void searchTextBox_TextChanged(object sender, System.EventArgs e) {
207      currentSearchString = searchTextBox.Text.ToLower();
208      UpdateFilteredTree();
209    }
210    #endregion
211
212    #region Helpers
213    private void IncludeChildNodes(TreeNode node) {
214      if (node != null) {
215        node.Checked = true;
216        selectedResources.Add((Resource)node.Tag);
217        AdjustNodeCheckedState(node);
218        foreach (TreeNode n in node.Nodes) IncludeChildNodes(n);
219      }
220    }
221
222    private void IncludeParentNodes(TreeNode node) {
223      if (node != null && node.Parent != null) {
224        TreeNode parent = node.Parent;
225        if (parent.Nodes.OfType<TreeNode>().All(x => x.Checked)) {
226          parent.Checked = true;
227          selectedResources.Add((Resource)parent.Tag);
228          AdjustNodeCheckedState(parent);
229          IncludeParentNodes(parent);
230        }
231      }
232    }
233
234    private void ExcludeChildNodes(TreeNode node) {
235      if (node != null) {
236        node.Checked = false;
237        selectedResources.Remove((Resource)node.Tag);
238        AdjustNodeCheckedState(node);
239        foreach (TreeNode n in node.Nodes) ExcludeChildNodes(n);
240      }
241    }
242
243    private void ExcludeParentNodes(TreeNode node) {
244      if (node != null) {
245        node.Checked = false;
246        selectedResources.Remove((Resource)node.Tag);
247        AdjustNodeCheckedState(node);
248        ExcludeParentNodes(node.Parent);
249      }
250    }
251
252    private void AdjustNodeCheckedState(TreeNode node) {
253      var filterdNode = filteredTreeNodes.SingleOrDefault(x => ((Resource)x.Tag).Id == ((Resource)node.Tag).Id);
254      var storedNode = nodeStore.SingleOrDefault(x => ((Resource)x.Tag).Id == ((Resource)node.Tag).Id);
255      if (filterdNode != null) filterdNode.Checked = node.Checked;
256      if (storedNode != null) storedNode.Checked = node.Checked;
257    }
258
259    private void ExpandSlaveGroupNodes() {
260      foreach (TreeNode n in nodeStore.Where(x => x.Tag is SlaveGroup)) {
261        TreeNode[] children = new TreeNode[n.Nodes.Count];
262        n.Nodes.CopyTo(children, 0);
263        if (children.Any(x => x.Tag is SlaveGroup)) n.Expand();
264      }
265    }
266
267    private void ExtractStatistics(TreeNode treeNode = null) {
268      StringBuilder sb = new StringBuilder();
269      Resource resource = treeNode == null ? null : treeNode.Tag as Resource;
270      ISet<Resource> resources = treeNode == null ? selectedResources : new HashSet<Resource>(treeNode.DescendantNodes().Select(x => x.Tag as Resource)); ;
271      IEnumerable<SlaveGroup> slaveGroups = resources.OfType<SlaveGroup>();
272      IEnumerable<Slave> slaves = resources.OfType<Slave>();
273      int cpuSpeed = 0, cores = 0, freeCores = 0, memory = 0, freeMemory = 0;
274      string contextString = treeNode == null ? "Selected" : "Included";
275
276      if (resources.Any() || resource != null) {
277        foreach (Slave s in slaves) {
278          cpuSpeed += s.CpuSpeed.GetValueOrDefault();
279          cores += s.Cores.GetValueOrDefault();
280          freeCores += s.FreeCores.GetValueOrDefault();
281          memory += s.Memory.GetValueOrDefault();
282          freeMemory += s.FreeMemory.GetValueOrDefault();
283        }
284        if (resource != null) {
285          if (resource is SlaveGroup) sb.Append("Slave group: ");
286          else if (resource is Slave) {
287            sb.Append("Slave: ");
288            if (!resources.Any()) {
289              Slave s = resource as Slave;
290              cpuSpeed = s.CpuSpeed.GetValueOrDefault();
291              cores = s.Cores.GetValueOrDefault();
292              freeCores = s.FreeCores.GetValueOrDefault();
293              memory = s.Memory.GetValueOrDefault();
294              freeMemory = s.FreeMemory.GetValueOrDefault();
295            }
296          }
297          sb.AppendLine(string.Format("{0}", resource.Name));
298        }
299        if (resource == null || resource is SlaveGroup) {
300          if (resources.Any()) {
301            sb.AppendFormat("{0} slave groups ({1}): ", contextString, slaveGroups.Count());
302            foreach (SlaveGroup sg in slaveGroups) sb.AppendFormat("{0}; ", sg.Name);
303            sb.AppendLine();
304            sb.AppendFormat("{0} slaves ({1}): ", contextString, slaves.Count());
305            foreach (Slave s in slaves) sb.AppendFormat("{0}; ", s.Name);
306            sb.AppendLine();
307          } else {
308            sb.Append("The selection does not inlcude any further resources.");
309          }
310        }
311        sb.AppendLine();
312        sb.AppendLine(string.Format("CPU speed: {0} MHz", cpuSpeed));
313        if (resources.Any()) sb.AppendLine(string.Format("Avg. CPU speed: {0:0.00} MHz", (double)cpuSpeed / resources.Count()));
314        sb.AppendLine(string.Format("Cores: {0}", cores));
315        sb.AppendLine(string.Format("Free cores: {0}", freeCores));
316        if (resources.Any()) sb.AppendLine(string.Format("Avg. free cores: {0:0.00}", (double)freeCores / resources.Count()));
317        sb.AppendLine(string.Format("Memory: {0} MB", memory));
318        sb.AppendFormat("Free memory: {0} MB", freeMemory);
319        if (resources.Any()) sb.Append(string.Format("{0}Avg. free memory: {1:0.00} MB", Environment.NewLine, (double)freeMemory / resources.Count()));
320      } else {
321        sb.Append("No resources selected.");
322      }
323
324      descriptionTextBox.Text = sb.ToString();
325    }
326    #endregion
327  }
328}
Note: See TracBrowser for help on using the repository browser.