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