Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.CloudManager/3.3/Views/CloudResourcesView.cs @ 7668

Last change on this file since 7668 was 7668, checked in by spimming, 13 years ago

#1680:

  • restore subscription data on cloud manager startup
  • correct idxs for parsing subscription string
File size: 13.1 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Timers;
4using System.Windows.Forms;
5using HeuristicLab.Clients.Hive.CloudManager.Model;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Core.Views;
9using HeuristicLab.MainForm;
10
11namespace HeuristicLab.Clients.Hive.CloudManager.Views {
12  [View("Cloud Resources View")]
13  [Content(typeof(IItemList<Subscription>), IsDefaultView = true)]
14  public partial class CloudResourcesView : ItemView, IDisposable {
15    private System.Timers.Timer timer = new System.Timers.Timer();
16    private int updateInterval = 15000;
17    private DateTime dueTime;
18
19    private const int subscriptionImageIndex = 0;
20    private const int serviceImageIndex = 1;
21    private const int deploymentImageIndex = 2;
22
23    public new IItemList<Subscription> Content {
24      get { return (IItemList<Subscription>)base.Content; }
25      set { base.Content = value; }
26    }
27
28    private IItemList<HostedService> hostedServices;
29    public IItemList<HostedService> HostedServices {
30      get { return hostedServices; }
31      set { hostedServices = value; }
32    }
33
34    public CloudResourcesView() {
35      InitializeComponent();
36
37      treeCloudResources.ImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.NewFolder);
38      treeCloudResources.ImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.NetworkCenterLarge);
39      treeCloudResources.ImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.MonitorLarge);
40
41      HostedServices = CloudManagerClient.Instance.HostedServices;
42      Content = CloudManagerClient.Instance.Subscriptions;
43
44
45      CloudManagerClient.Instance.Refreshing += new EventHandler(Instance_Refreshing);
46      CloudManagerClient.Instance.Refreshed += new EventHandler(Instance_Refreshed);
47
48      timer.AutoReset = true;
49      timer.Interval = 1000;
50      timer.Elapsed += PerformUpdate;
51      timer.Start();
52      dueTime = DateTime.Now.AddMilliseconds(updateInterval);
53
54      CloudManagerClient.Instance.RestoreSubscriptionsFromUserConfig();
55    }
56
57    #region Register Content Events
58    protected override void DeregisterContentEvents() {
59      Content.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsAdded);
60      Content.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsRemoved);
61      base.DeregisterContentEvents();
62    }
63    protected override void RegisterContentEvents() {
64      base.RegisterContentEvents();
65      Content.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsAdded);
66      Content.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Subscription>>(Content_ItemsRemoved);
67    }
68    #endregion
69
70    /// <summary>
71    /// Clean up any resources being used.
72    /// </summary>
73    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
74    protected override void Dispose(bool disposing) {
75      if (disposing && (components != null)) {
76        components.Dispose();
77        CloudManagerClient.Instance.Refreshing -= new EventHandler(Instance_Refreshing);
78        CloudManagerClient.Instance.Refreshed -= new EventHandler(Instance_Refreshed);
79        timer.Stop();
80        timer.Dispose();
81        timer = null;
82      }
83      CloudManagerClient.Instance.PersistSubscriptionsToUserConfig();
84      base.Dispose(disposing);
85    }
86
87    protected override void SetEnabledStateOfControls() {
88      base.SetEnabledStateOfControls();
89      if (Content == null || Content.Count == 0) {
90        btnAddSlaveService.Enabled = false;
91        btnDelete.Enabled = false;
92        btnSave.Enabled = false;
93      } else {
94        btnAddSlaveService.Enabled = true;
95        btnDelete.Enabled = true;
96        btnSave.Enabled = true;
97      }
98    }
99
100    protected override void OnContentChanged() {
101      base.OnContentChanged();
102      SetEnabledStateOfControls();
103      if (Content == null) {
104        viewHost.Content = null;
105        treeCloudResources.Nodes.Clear();
106      } else if (treeCloudResources.Nodes.Count == 0) {
107        treeCloudResources.BeginUpdate();
108        //treeCloudResources.Nodes.Clear();
109        foreach (Subscription sub in Content) {
110          TreeNode nodeSub = GetTreeNode(sub, sub.SubscriptionName, subscriptionImageIndex);
111          foreach (HostedService s in HostedServices) {
112            if (s.Subscription.SubscriptionID == sub.SubscriptionID) {
113              TreeNode nodeServ = GetTreeNode(s, s.HostedServiceProperties.Label, serviceImageIndex);
114              foreach (Deployment d in s.Deployments) {
115                TreeNode nodeDepl = GetTreeNode(d, d.Label + " (" + d.DeploymentSlot + ")", deploymentImageIndex);
116                nodeServ.Nodes.Add(nodeDepl);
117              }
118              nodeSub.Nodes.Add(nodeServ);
119            }
120          }
121          treeCloudResources.Nodes.Add(nodeSub);
122        }
123        treeCloudResources.EndUpdate();
124      } else {
125        treeCloudResources.BeginUpdate();
126        TreeNodeCollection tncSubs = treeCloudResources.Nodes;
127
128        // Delete treenodes that have no corresponding data in Content and HosteServices
129        foreach (TreeNode tnSub in tncSubs) {
130          Subscription s = (Subscription)tnSub.Tag;
131          int idx = Content.IndexOf(s);
132          if (idx == -1) {
133            tncSubs.Remove(tnSub);
134          } else {
135            TreeNodeCollection tncHS = tnSub.Nodes;
136            foreach (TreeNode tnHS in tncHS) {
137              HostedService hs = (HostedService)tnHS.Tag;
138              int idxHS = HostedServices.IndexOf(hs);
139              if (idxHS == -1) {
140                tncHS.Remove(tnHS);
141              } else {
142                TreeNodeCollection tncDep = tnHS.Nodes;
143                foreach (TreeNode tnDep in tncDep) {
144                  Deployment dep = (Deployment)tnDep.Tag;
145                  HostedService hsDep = HostedServices[idxHS];
146                  int idxDep = hsDep.Deployments.IndexOf(dep);
147                  if (idxDep == -1) {
148                    tncDep.Remove(tnDep);
149                  }
150                }
151              }
152            }
153          }
154        }
155
156        // Add missing treenodes
157        foreach (Subscription sub in Content) {
158          bool foundSub = false;
159          TreeNode foundSubNode = null;
160          foreach (TreeNode tnSub in treeCloudResources.Nodes) {
161            if (((Subscription)tnSub.Tag).Equals(sub)) {
162              foundSub = true;
163              foundSubNode = tnSub;
164            }
165          }
166          TreeNode nodeSub;
167          if (!foundSub) {
168            nodeSub = GetTreeNode(sub, sub.SubscriptionName, subscriptionImageIndex);
169            treeCloudResources.Nodes.Add(nodeSub);
170          } else {
171            nodeSub = foundSubNode;
172          }
173          foreach (HostedService s in HostedServices) {
174            if (s.Subscription.SubscriptionID == sub.SubscriptionID) {
175              bool foundHS = false;
176              TreeNode foundHSNode = null;
177              foreach (TreeNode tnHS in nodeSub.Nodes) {
178                if (((HostedService)tnHS.Tag).Equals(s)) {
179                  foundHS = true;
180                  foundHSNode = tnHS;
181                }
182              }
183              TreeNode nodeServ;
184              if (!foundHS) {
185                nodeServ = GetTreeNode(s, s.HostedServiceProperties.Label, serviceImageIndex);
186                nodeSub.Nodes.Add(nodeServ);
187              } else {
188                nodeServ = foundHSNode;
189              }
190              foreach (Deployment d in s.Deployments) {
191                bool foundDep = false;
192                TreeNode foundDepNode;
193                foreach (TreeNode tnDep in nodeServ.Nodes) {
194                  if (((Deployment)tnDep.Tag).Equals(d)) {
195                    foundDep = true;
196                    foundDepNode = tnDep;
197                  }
198                }
199                TreeNode nodeDepl;
200                if (!foundDep) {
201                  nodeDepl = GetTreeNode(d, d.Label + " (" + d.DeploymentSlot + ")", deploymentImageIndex);
202                  nodeServ.Nodes.Add(nodeDepl);
203                } else {
204                  // nothing to do
205                }
206              }
207            }
208          }
209        }
210        // ----------------------------
211        treeCloudResources.EndUpdate();
212      }
213    }
214
215    private TreeNode GetTreeNode(Object tag, string text, int imageIndex) {
216      TreeNode node = new TreeNode();
217      node.Tag = tag;
218      node.Text = text;
219      node.ImageIndex = imageIndex;
220      node.SelectedImageIndex = node.ImageIndex;
221      return node;
222    }
223
224    private void Instance_Refreshing(object sender, EventArgs e) {
225      Debug.WriteLine("Instance_Refreshing");
226      //if (treeCloudResources.InvokeRequired) {
227      //  treeCloudResources.Invoke((MethodInvoker)delegate { OnContentChanged(); });
228      //} else {
229      //  OnContentChanged();
230      //}
231    }
232
233    private void Instance_Refreshed(object sender, EventArgs e) {
234      Debug.WriteLine("Instance_Refreshed");
235      if (treeCloudResources.InvokeRequired) {
236        treeCloudResources.Invoke((MethodInvoker)delegate { OnContentChanged(); });
237      } else {
238        OnContentChanged();
239      }
240    }
241
242    private void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Subscription>> e) {
243      OnContentChanged();
244    }
245
246    private void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Subscription>> e) {
247      OnContentChanged();
248    }
249
250    private void PerformUpdate(object sender, ElapsedEventArgs e) {
251      TimeSpan timeToRefresh = dueTime - DateTime.Now;
252
253      if (timeToRefresh.Seconds >= 0) {
254        this.Invoke((MethodInvoker)delegate { lblRefresh.Text = "Done. " + timeToRefresh.Seconds + "s to next refresh."; });
255      } else {
256        timer.Stop();
257        this.Invoke((MethodInvoker)delegate { lblRefresh.Text = "Refreshing data ..."; });
258
259        // statistics
260        Debug.WriteLine("perform update");
261        CloudManagerClient.Instance.Refresh();
262
263        dueTime = DateTime.Now.AddMilliseconds(updateInterval);
264        if (timer != null) {
265          timer.Start();
266        }
267      }
268    }
269
270    private void btnAddSubscription_Click(object sender, EventArgs e) {
271      using (var form = new AddSubscriptionDialog()) {
272        form.ShowDialog();
273        if (!form.ErrorOccured) {
274          if (form.Subscription != null) {
275            Subscription sub = form.Subscription;
276            CloudManagerClient.Instance.Add(sub);
277          }
278        }
279      }
280    }
281
282    private void btnAddSlaveService_Click(object sender, EventArgs e) {
283      // When refreshing subscriptions in the dialog,
284      // discoverservices and safetoconfig properties are always set to false
285      // -> work on deep copy
286      Cloner cloner = new Cloner();
287      IItemList<Subscription> subscriptions = cloner.Clone(Content);
288      using (var form = new AddAzureServiceDialog(subscriptions)) {
289        form.ShowDialog();
290
291      }
292    }
293
294    private void treeCloudResources_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
295      viewHost.Content = (IContent)e.Node.Tag;
296    }
297
298    private void btnSave_Click(object sender, EventArgs e) {
299      if (treeCloudResources.SelectedNode != null) {
300        TreeNode tn = treeCloudResources.SelectedNode;
301        TreeNode tnParent = tn.Parent;
302        Object obj = tn.Tag;
303        Object objParent = tnParent.Tag;
304        if (obj is Subscription) {
305          // CloudManagerClient.Instance.Save((Subscription)obj);
306        } else if (obj is HostedService) {
307          // nothing to do so far
308        } else if (obj is Deployment && objParent is HostedService) {
309          HostedService hs = (HostedService)objParent;
310          Deployment dep = (Deployment)obj;
311          //call async
312          CloudManagerClient.Instance.ChangeIntances(dep, hs);
313        }
314      }
315    }
316
317    private void btnDelete_Click(object sender, EventArgs e) {
318      if (treeCloudResources.SelectedNode != null) {
319        TreeNode tn = treeCloudResources.SelectedNode;
320        TreeNode tnParent = tn.Parent;
321        Object obj = tn.Tag;
322        Object objParent = null;
323        if (tnParent != null) {
324          objParent = tnParent.Tag;
325        }
326        viewHost.Content = null;
327        if (obj is Subscription) {
328          CloudManagerClient.Instance.Delete((Subscription)obj);
329        } else if (obj is HostedService) {
330          HostedService hs = (HostedService)obj;
331          using (var form = new DeleteHostedServiceView(hs)) {
332            form.ShowDialog();
333          }
334        } else if (obj is Deployment && objParent is HostedService) {
335          HostedService hs = (HostedService)objParent;
336          Deployment dep = (Deployment)obj;
337          using (var form = new DeleteDeploymentView(dep, hs)) {
338            form.ShowDialog();
339          }
340        }
341      }
342    }
343  }
344}
Note: See TracBrowser for help on using the repository browser.