Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Console/3.2/HiveServerManagementConsole.cs @ 1723

Last change on this file since 1723 was 1723, checked in by aleitner, 15 years ago

update process with jobs and users are now with correct pictures and problems with update fixed (#600)

File size: 32.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Hive.Contracts.Interfaces;
31using HeuristicLab.Hive.Contracts.BusinessObjects;
32using HeuristicLab.Hive.Contracts;
33
34namespace HeuristicLab.Hive.Server.ServerConsole {
35
36  /// <summary>
37  /// if form is closed the loginform gets an information
38  /// </summary>
39  /// <param name="cf"></param>
40  /// <param name="error"></param>
41  public delegate void closeForm(bool cf, bool error);
42
43  public partial class HiveServerManagementConsole : Form {
44
45    public event closeForm closeFormEvent;
46
47    #region private variables
48    private ResponseList<ClientGroup> clientGroups = null;
49    private ResponseList<ClientInfo> clients = null;
50    private ResponseList<Job> jobs = null;
51
52    //TODO delete
53    private Dictionary<Guid, ListViewGroup> clientObjects;
54    private Dictionary<Guid, ListViewItem> clientInfoObjects;
55    private Dictionary<Guid, ListViewItem> jobObjects;
56
57    private Job currentJob = null;
58    private ClientInfo currentClient = null;
59    TreeNode currentNode = null;
60
61    //TODO delete
62    private string nameCurrentJob = "";
63    private string nameCurrentClient = "";
64    private bool flagJob = false;
65
66    private List<Changes> changes = new List<Changes>();
67
68    private ToolTip tt = new ToolTip();
69
70    private const string NOGROUP = "No group";
71    //private List<ListViewItem> clientList = new List<ListViewItem>();
72    private Dictionary<Guid, List<ListViewItem>> clientList = new Dictionary<Guid, List<ListViewItem>>();
73
74    private IClientManager ClientManager {
75      get {
76        return ServiceLocator.GetClientManager();
77      }
78
79    }
80    #endregion
81
82    public HiveServerManagementConsole() {
83      InitializeComponent();
84      Init();
85      AddClients();
86      AddJobs();
87      timerSyncronize.Start();
88    }
89
90
91    #region Backgroundworker
92    /// <summary>
93    /// event on Ticker
94    /// </summary>
95    /// <param name="obj"></param>
96    /// <param name="e"></param>
97    private void TickSync(object obj, EventArgs e) {
98      if (!updaterWoker.IsBusy) {
99        updaterWoker.RunWorkerAsync();
100      }
101    }
102
103    private void updaterWoker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
104      Refresh();
105    }
106
107    #endregion
108
109    private Guid ConvertStringToGuid(string stringGuid) {
110      return new Guid(stringGuid);
111    }
112
113
114    private void Init() {
115
116      //adding context menu items for jobs
117      menuItemAbortJob.Click += (s, e) => {
118        IJobManager jobManager = ServiceLocator.GetJobManager();
119        if (lvJobControl.SelectedItems.Count == 1) {
120          jobManager.AbortJob(((Job)(lvJobControl.SelectedItems[0].Tag)).Id);
121        }
122      };
123
124      //adding context menu items for jobs
125      menuItemGetSnapshot.Click += (s, e) => {
126        IJobManager jobManager = ServiceLocator.GetJobManager();
127        if (lvJobControl.SelectedItems.Count == 1) {
128          jobManager.RequestSnapshot(((Job)(lvJobControl.SelectedItems[0].Tag)).Id);
129        }
130      };
131
132    }
133
134    private void lvJobControl_MouseUp(object sender, MouseEventArgs e) {
135      // If the right mouse button was clicked and released,
136      // display the shortcut menu assigned to the ListView.
137      lvJobControl.ContextMenuStrip.Items.Clear();
138      ListViewHitTestInfo hitTestInfo = lvJobControl.HitTest(e.Location);
139      if (e.Button == MouseButtons.Right && hitTestInfo.Item != null && lvJobControl.SelectedItems.Count == 1) {
140        Job selectedJob = (Job)lvJobControl.SelectedItems[0].Tag;
141
142        if (selectedJob != null && selectedJob.State == State.calculating) {
143          lvJobControl.ContextMenuStrip.Items.Add(menuItemAbortJob);
144          lvJobControl.ContextMenuStrip.Items.Add(menuItemGetSnapshot);
145        }
146      }
147      lvJobControl.ContextMenuStrip.Show(new Point(e.X, e.Y));
148    }
149
150    ///// <summary>
151    ///// Adds clients to ListView and TreeView
152    ///// </summary>
153    //private void AddClients() {
154    //  try {
155    //    clientObjects = new Dictionary<Guid, ListViewGroup>();
156    //    clientInfoObjects = new Dictionary<Guid, ListViewItem>();
157
158    //    clients = ClientManager.GetAllClientGroups();
159
160    //    List<Guid> inGroup = new List<Guid>();
161    //    foreach (ClientGroup cg in clients.List) {
162    //      TreeNode tn = new TreeNode(cg.Name);
163    //      tn.Tag = cg;
164    //      tvClientControl.Nodes.Add(tn);
165
166    //      ListViewGroup lvg = new ListViewGroup(cg.Name, HorizontalAlignment.Left);
167
168    //      foreach (ClientInfo ci in cg.Resources) {
169    //        ListViewItem item = null;
170    //        if ((ci.State == State.offline) || (ci.State == State.nullState)) {
171    //          item = new ListViewItem(ci.Name, 3, lvg);
172    //        } else {
173    //          int percentageUsage = CapacityRam(ci.NrOfCores, ci.NrOfFreeCores);
174    //          int usage = 0;
175    //          if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
176    //            usage = 0;
177    //          } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
178    //            usage = 1;
179    //          } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
180    //            usage = 2;
181    //          }
182
183    //          item = new ListViewItem(ci.Name, usage, lvg);
184    //        }
185    //        item.Tag = ci.Id;
186    //        lvClientControl.Items.Add(item);
187    //        clientInfoObjects.Add(ci.Id, item);
188    //        inGroup.Add(ci.Id);
189
190    //      }
191    //      lvClientControl.BeginUpdate();
192    //      lvClientControl.Groups.Add(lvg);
193    //      lvClientControl.EndUpdate();
194    //      clientObjects.Add(cg.Id, lvg);
195    //    } // Groups
196
197    //    clientInfo = ClientManager.GetAllClients();
198    //    ListViewGroup lvunsorted = new ListViewGroup("no group", HorizontalAlignment.Left);
199    //    foreach (ClientInfo ci in clientInfo.List) {
200    //      bool help = false;
201    //      foreach (Guid client in inGroup) {
202    //        if (client == ci.Id) {
203    //          help = true;
204    //          break;
205    //        }
206    //      }
207    //      if (!help) {
208    //        ListViewItem item = null;
209    //        if ((ci.State == State.offline) || (ci.State == State.nullState)) {
210    //          item = new ListViewItem(ci.Name, 3, lvunsorted);
211    //        } else {
212    //          int percentageUsage = CapacityRam(ci.NrOfCores, ci.NrOfFreeCores);
213    //          int usage = 0;
214    //          if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
215    //            usage = 0;
216    //          } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
217    //            usage = 1;
218    //          } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
219    //            usage = 2;
220    //          }
221    //          item = new ListViewItem(ci.Name, usage, lvunsorted);
222    //        }
223    //        item.Tag = ci.Id;
224    //        lvClientControl.Items.Add(item);
225    //      }
226    //    }
227    //    lvClientControl.BeginUpdate();
228    //    lvClientControl.Groups.Add(lvunsorted);
229    //    lvClientControl.EndUpdate();
230    //    if (flagClient) {
231    //      ClientClicked();
232    //    }
233    //  }
234    //  catch (Exception ex) {
235    //    closeFormEvent(true, true);
236    //    this.Close();
237    //  }
238    //}
239
240    private void AddClients() {
241      clientList.Clear();
242      tvClientControl.Nodes.Clear();
243
244      clientGroups = ClientManager.GetAllClientGroups();
245
246      foreach (ClientGroup cg in clientGroups.List) {
247        AddClientOrGroup(cg, null);
248      }
249
250      if (currentNode != null) {
251        lvClientControl.Items.Clear();
252        lvClientControl.Groups.Clear();
253        AddGroupsToListView(currentNode);
254      }
255      tvClientControl.ExpandAll();
256    }
257
258    private void AddClientOrGroup(ClientGroup clientGroup, TreeNode currentNode) {
259      currentNode = CreateTreeNode(clientGroup, currentNode);
260      List<ListViewItem> clientGroupList = new List<ListViewItem>();
261      ListViewGroup lvg;
262      if (string.IsNullOrEmpty(clientGroup.Name)) {
263        lvg = new ListViewGroup(NOGROUP, HorizontalAlignment.Left);
264      } else {
265        lvg = new ListViewGroup(clientGroup.Name, HorizontalAlignment.Left);
266      }
267      lvClientControl.Groups.Add(lvg);
268      foreach (Resource resource in clientGroup.Resources) {
269        if (resource is ClientInfo) {
270          int percentageUsage = CapacityRam(((ClientInfo)resource).NrOfCores, ((ClientInfo)resource).NrOfFreeCores);
271          int usage = 3;
272          if ((((ClientInfo)resource).State != State.offline) &&
273            (((ClientInfo)resource).State != State.nullState)) {
274            if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
275              usage = 0;
276            } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
277              usage = 1;
278            } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
279              usage = 2;
280            }
281          }
282          ListViewItem lvi = new ListViewItem(resource.Name, usage, lvg);
283          lvi.Tag = resource as ClientInfo;
284          clientGroupList.Add(lvi);
285        } else if (resource is ClientGroup) {
286          AddClientOrGroup(resource as ClientGroup, currentNode);
287        }
288      }
289      clientList.Add(clientGroup.Id, clientGroupList);
290    }
291
292    private TreeNode CreateTreeNode(ClientGroup clientGroup, TreeNode currentNode) {
293      TreeNode tn;
294      if (string.IsNullOrEmpty(clientGroup.Name)) {
295        tn = new TreeNode(NOGROUP);
296      } else {
297        tn = new TreeNode(clientGroup.Name);
298      }
299      tn.Tag = clientGroup;
300      if (currentNode == null) {
301        tvClientControl.Nodes.Add(tn);
302      } else {
303        currentNode.Nodes.Add(tn);
304      }
305      return tn;
306    }
307
308
309    List<ListViewGroup> jobGroup;
310    /// <summary>
311    /// Adds jobs to ListView and TreeView
312    /// </summary>
313    private void AddJobs() {
314      try {
315        jobObjects = new Dictionary<Guid, ListViewItem>();
316        IJobManager jobManager =
317          ServiceLocator.GetJobManager();
318        jobs = jobManager.GetAllJobs();
319
320        lvJobControl.Items.Clear();
321
322        ListViewGroup lvJobCalculating = new ListViewGroup("calculating", HorizontalAlignment.Left);
323        ListViewGroup lvJobFinished = new ListViewGroup("finished", HorizontalAlignment.Left);
324        ListViewGroup lvJobPending = new ListViewGroup("pending", HorizontalAlignment.Left);
325
326        jobGroup = new List<ListViewGroup>();
327        jobGroup.Add(lvJobCalculating);
328        jobGroup.Add(lvJobFinished);
329        jobGroup.Add(lvJobPending);
330
331        foreach (Job job in jobs.List) {
332          if (job.State == State.calculating) {
333            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 1, lvJobCalculating);
334            lvi.Tag = job;
335            jobObjects.Add(job.Id, lvi);
336
337            //lvJobControl.Items.Add(lvi);
338
339            lvi.ToolTipText = (job.Percentage * 100) + "% of job calculated";
340          } else if (job.State == State.finished) {
341            ListViewItem lvi = new ListViewItem(job.Id.ToString(),0, lvJobFinished);
342            lvi.Tag = job;
343            jobObjects.Add(job.Id, lvi);
344            //lvJobControl.Items.Add(lvi);
345          } else if (job.State == State.offline) {
346            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 2, lvJobPending);
347            lvi.Tag = job;
348            jobObjects.Add(job.Id, lvi);
349            //lvJobControl.Items.Add(lvi);
350          }
351        } // Jobs
352        lvJobControl.BeginUpdate();
353        foreach (ListViewItem lvi in jobObjects.Values) {
354          lvJobControl.Items.Add(lvi);
355        }
356        lvJobControl.Groups.Add(lvJobCalculating);
357        lvJobControl.Groups.Add(lvJobFinished);
358        lvJobControl.Groups.Add(lvJobPending);
359        lvJobControl.EndUpdate();
360        if (flagJob) {
361          JobClicked();
362        }
363      }
364      catch (Exception ex) {
365        closeFormEvent(true, true);
366        this.Close();
367      }
368    }
369
370    /// <summary>
371    /// if one client is clicked, the details for the clicked client are shown
372    /// in the second panel
373    /// </summary>
374    private void ClientClicked() {
375      plClientDetails.Visible = true;
376      //int i = 0;
377      //while (clientInfo.List[i].Id.ToString() != nameCurrentClient) {
378      //  i++;
379      //}
380      if (lvClientControl.SelectedItems != null && lvClientControl.SelectedItems.Count > 0) {
381        currentClient = (ClientInfo)lvClientControl.SelectedItems[0].Tag;
382      }
383      if(currentClient != null) {
384      int percentageUsage = CapacityRam(currentClient.NrOfCores, currentClient.NrOfFreeCores);
385      int usage = 3;
386      if ((currentClient.State != State.offline) && (currentClient.State != State.nullState)) {
387        if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
388          usage = 0;
389        } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
390          usage = 1;
391        } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
392          usage = 2;
393        }
394      }
395      pbClientControl.Image = ilLargeImgClient.Images[usage];
396      lblClientName.Text = currentClient.Name;
397      lblLogin.Text = currentClient.Login.ToString();
398      lblState.Text = currentClient.State.ToString();
399      }
400    }
401
402    /// <summary>
403    /// if one job is clicked, the details for the clicked job are shown
404    /// in the second panel
405    /// </summary>
406    private void JobClicked() {
407      plJobDetails.Visible = true;
408      int i = 0;
409      while (jobs.List[i].Id.ToString() != nameCurrentJob) {
410        i++;
411      }
412      lvSnapshots.Enabled = true;
413      currentJob = jobs.List[i];
414      if (currentJob.State == State.offline) {
415        pbJobControl.Image = ilLargeImgJob.Images[2];
416      } else if (currentJob.State == State.calculating) {
417        pbJobControl.Image = ilLargeImgJob.Images[1];
418      } else if (currentJob.State == State.finished) {
419        pbJobControl.Image = ilLargeImgJob.Images[0];
420      }
421      lblJobName.Text = currentJob.Id.ToString();
422      progressJob.Value = (int)(currentJob.Percentage * 100);
423      lblProgress.Text = (int)(currentJob.Percentage * 100) + "% calculated";
424      lblUserCreatedJob.Text = currentJob.UserId.ToString() + /* currentJob.User.Name + */ " created Job";
425      lblJobCreated.Text = "Created at " + currentJob.DateCreated;
426      if (currentJob.ParentJob != null) {
427        lblParentJob.Text = currentJob.ParentJob.Id + " is parent job";
428      } else {
429        lblParentJob.Text = "";
430      }
431      lblPriorityJob.Text = "Priority of job is " + currentJob.Priority;
432      if (currentJob.Client != null) {
433        lblClientCalculating.Text = currentJob.Client.Name + " calculated Job";
434        lblJobCalculationBegin.Text = "Startet calculation at " + currentJob.DateCalculated;
435
436        if (currentJob.State == State.finished) {
437          IJobManager jobManager =
438            ServiceLocator.GetJobManager();
439          ResponseObject<JobResult> jobRes = jobManager.GetLastJobResultOf(currentJob.Id, false);
440          lblJobCalculationEnd.Text = "Calculation ended at " + jobRes.Obj.DateFinished;
441        }
442      } else {
443        lblClientCalculating.Text = "";
444        lblJobCalculationBegin.Text = "";
445        lblJobCalculationEnd.Text = "";
446      }
447      if (currentJob.State != State.offline) {
448        lvSnapshots.Items.Clear();
449        if (currentJob.State == State.finished)
450          GetSnapshotList();
451        lvSnapshots.Visible = true;
452      } else {
453        lvSnapshots.Visible = false;
454      }
455    }
456
457    private void Refresh() {
458      foreach (Changes change in changes) {
459        if (change.Types == Type.Job) {
460          RefreshJob(change);
461        } else if (change.Types == Type.Client) {
462          //RefreshClient(change);
463        } else if (change.Types == Type.ClientGroup) {
464          //RefreshClientGroup(change);
465        }
466      }
467    }
468
469    private void RefreshJob(Changes change) {
470      if (change.ChangeType == Change.Update) {
471        for (int i = 0; i < lvJobControl.Items.Count; i++) {
472          if (lvJobControl.Items[i].Text == change.ID.ToString()) {
473            if (nameCurrentJob == change.ID.ToString()) {
474              JobClicked();
475            }
476            foreach (Job job in jobs.List) {
477              if (job.Id == change.ID) {
478                lvJobControl.Items[i].Tag = job;
479                break;
480              }
481            }
482            State state = jobs.List[change.Position].State;
483            System.Diagnostics.Debug.WriteLine(lvJobControl.Items[i].Text.ToString());
484            if (state == State.finished) {
485              lvJobControl.Items[i].Group = jobGroup[1];
486              lvJobControl.Items[i].ImageIndex = 0;
487              System.Diagnostics.Debug.WriteLine("finished");
488            } else if (state == State.calculating) {
489              lvJobControl.Items[i].Group = jobGroup[0];
490              lvJobControl.Items[i].ImageIndex = 1;
491              System.Diagnostics.Debug.WriteLine("calculating");
492            } else if (state == State.offline) {
493              lvJobControl.Items[i].Group = jobGroup[2];
494              lvJobControl.Items[i].ImageIndex = 2;
495              System.Diagnostics.Debug.WriteLine("offline");
496
497            }
498            lvJobControl.Refresh();
499          }
500        }
501      } else if (change.ChangeType == Change.Create) {
502        ListViewItem lvi = new ListViewItem(
503          change.ID.ToString(), 2, jobGroup[2]);
504        jobObjects.Add(change.ID, lvi);
505        lvJobControl.Items.Add(lvi);
506
507      } else if (change.ChangeType == Change.Delete) {
508        jobObjects.Remove(change.ID);
509        for (int i = 0; i < lvJobControl.Items.Count; i++) {
510          if (change.ID.ToString() == lvJobControl.Items[i].Text.ToString()) {
511            lvJobControl.Items[i].Remove();
512            break;
513          }
514        }
515      }
516    }
517
518    private void RefreshClient(Changes change) {
519      if (change.ChangeType == Change.Update) {
520        for (int i = 0; i < lvClientControl.Items.Count; i++) {
521          if (lvClientControl.Items[i].Tag.ToString() == change.ID.ToString()) {
522            if (nameCurrentClient == change.ID.ToString()) {
523              ClientClicked();
524            }
525            State state = clients.List[change.Position].State;
526            System.Diagnostics.Debug.WriteLine(lvClientControl.Items[i].Text.ToString());
527
528            ClientInfo ci = null;
529
530            foreach (ClientInfo c in clients.List) {
531              if (c.Id == change.ID) {
532                ci = c;
533              }
534            }
535
536            int percentageUsage = CapacityRam(ci.NrOfCores, ci.NrOfFreeCores);
537            if ((state == State.offline) || (state == State.nullState)) {
538              lvClientControl.Items[i].ImageIndex = 3;
539            } else {
540              if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
541                lvClientControl.Items[i].ImageIndex = 0;
542              } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
543                lvClientControl.Items[i].ImageIndex = 1;
544              } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
545                lvClientControl.Items[i].ImageIndex = 2;
546              }
547
548            }
549            lvClientControl.Refresh();
550          }
551        }
552
553
554      } else if (change.ChangeType == Change.Create) {
555
556      } else if (change.ChangeType == Change.Delete) {
557        clientInfoObjects.Remove(change.ID);
558        for (int i = 0; i < lvClientControl.Items.Count; i++) {
559          if (change.ID.ToString() == lvClientControl.Items[i].Text.ToString()) {
560            lvClientControl.Items[i].Remove();
561            break;
562          }
563        }
564
565      }
566    }
567
568    private void RefreshClientGroup(Changes change) {
569
570    }
571
572    #region Eventhandlers
573    /// <summary>
574    /// Send event to Login-GUI when closing
575    /// </summary>
576    /// <param name="sender"></param>
577    /// <param name="e"></param>
578    private void Close_Click(object sender, EventArgs e) {
579      if (closeFormEvent != null) {
580        closeFormEvent(true, false);
581      }
582      this.Close();
583    }
584
585    /// <summary>
586    /// Send evnt to Login-GUI when closing
587    /// </summary>
588    /// <param name="sender"></param>
589    /// <param name="e"></param>
590    private void HiveServerConsoleInformation_FormClosing(object sender, FormClosingEventArgs e) {
591      if (closeFormEvent != null) {
592        closeFormEvent(true, false);
593      }
594    }
595
596    private void AddJob_Click(object sender, EventArgs e) {
597      AddJobForm newForm = new AddJobForm();
598      newForm.Show();
599      newForm.addJobEvent += new addDelegate(updaterWoker.RunWorkerAsync);
600    }
601
602    private void OnLVClientClicked(object sender, EventArgs e) {
603      nameCurrentClient = lvClientControl.SelectedItems[0].Tag.ToString();
604      ClientClicked();
605    }
606
607    private void OnLVJobControlClicked(object sender, EventArgs e) {
608      nameCurrentJob = lvJobControl.SelectedItems[0].Text;
609      flagJob = true;
610      JobClicked();
611    }
612
613    private void lvJobControl_MouseMove(object sender, MouseEventArgs e) {
614      if ((lvJobControl.GetItemAt(e.X, e.Y) != null) &&
615        (lvJobControl.GetItemAt(e.X, e.Y).ToolTipText != null)) {
616        tt.SetToolTip(lvJobControl, lvJobControl.GetItemAt(e.X, e.Y).ToolTipText);
617      }
618    }
619
620    private void updaterWoker_DoWork(object sender, DoWorkEventArgs e) {
621
622      changes.Clear();
623
624      //#region ClientInfo
625      //ResponseList<ClientInfo> clientInfoOld = clientInfo;
626      //clientInfo = ClientManager.GetAllClients();
627
628      //IDictionary<int, ClientInfo> clientInfoOldHelp;
629
630      //CloneList(clientInfoOld, out clientInfoOldHelp);
631
632      //GetDelta(clientInfoOld.List, clientInfoOldHelp);
633      //#endregion
634
635      #region Clients
636      //ResponseList<ClientGroup> clientsOld = clients;
637
638     // newClients = ClientManager.GetAllClientGroups();
639
640      //IDictionary<Guid, ClientGroup> clientsOldHelp;
641
642      //CloneList(clientsOld, out clientsOldHelp);
643
644      //GetDelta(clientsOld.List, clientsOldHelp);
645      //DetermineDelta();
646      #endregion
647
648      #region Job
649      ResponseList<Job> jobsOld = jobs;
650      IJobManager jobManager =
651          ServiceLocator.GetJobManager();
652
653      jobs = jobManager.GetAllJobs();
654
655      IDictionary<int, Job> jobsOldHelp;
656      CloneList(jobsOld, out jobsOldHelp);
657
658      GetDelta(jobsOld.List, jobsOldHelp);
659
660      #endregion
661
662      foreach (Changes change in changes) {
663        System.Diagnostics.Debug.WriteLine(change.ID + " " + change.ChangeType);
664      }
665
666    }
667    #endregion
668
669    #region Helper methods
670
671    private void CloneList(ResponseList<Job> oldList, out IDictionary<int, Job> newList) {
672      newList = new Dictionary<int, Job>();
673      for (int i = 0; i < oldList.List.Count; i++) {
674        newList.Add(i, oldList.List[i]);
675      }
676    }
677
678    //private void CloneList(ResponseList<ClientInfo> oldList, out IDictionary<int, ClientInfo> newList) {
679    //  newList = new Dictionary<int, ClientInfo>();
680    //  for (int i = 0; i < oldList.List.Count; i++) {
681    //    newList.Add(i, oldList.List[i]);
682    //  }
683    //}
684
685    private void CloneList(ResponseList<ClientGroup> oldList, out IDictionary<Guid, ClientGroup> newList) {
686      newList = new Dictionary<Guid, ClientGroup>();
687      foreach (ClientGroup clientGroup in oldList.List) {
688        newList.Add(clientGroup.Id, clientGroup);
689      }
690    }
691
692    private bool IsEqual(ClientInfo ci1, ClientInfo ci2) {
693      if (ci2 == null) {
694        return false;
695      }
696      if (ci1.Id.Equals(ci2.Id)) {
697        return true;
698      } else return false;
699    }
700
701    private int CapacityRam(int noCores, int freeCores) {
702      if (noCores > 0) {
703        int capacity = ((noCores - freeCores) / noCores) * 100;
704        System.Diagnostics.Debug.WriteLine(capacity);
705        return capacity;
706      }
707      return 100;
708    }
709
710    //private void GetDelta(IList<ClientInfo> oldClient, IDictionary<int, ClientInfo> helpClients) {
711    //  bool found = false;
712
713    //  for (int i = 0; i < clientInfo.List.Count; i++) {
714    //    ClientInfo ci = clientInfo.List[i];
715    //    for (int j = 0; j < oldClient.Count; j++) {
716    //      ClientInfo cio = oldClient[j];
717    //      if (ci.Id.Equals(cio.Id)) {
718    //        found = true;
719    //        if ((ci.State != cio.State) || (ci.NrOfFreeCores != cio.NrOfFreeCores)) {
720    //          changes.Add(new Changes { Types = Type.Client, ID = ci.Id, ChangeType = Change.Update, Position = i });
721    //        }
722    //        int removeAt = -1;
723    //        foreach (KeyValuePair<int, ClientInfo> kvp in helpClients) {
724    //          if (cio.Id.Equals(kvp.Value.Id)) {
725    //            removeAt = kvp.Key;
726    //            break;
727    //          }
728    //        }
729    //        if (removeAt >= 0) {
730    //          helpClients.Remove(removeAt);
731    //        }
732    //        break;
733    //      }
734    //    }
735    //    if (found == false) {
736    //      changes.Add(new Changes { Types = Type.Client, ID = ci.Id, ChangeType = Change.Create });
737    //    }
738    //    found = false;
739    //  }
740    //  foreach (KeyValuePair<int, ClientInfo> kvp in helpClients) {
741    //    changes.Add(new Changes { Types = Type.Client, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
742    //  }
743
744    //}
745
746    private void DetermineDelta() {
747     
748
749
750    }
751
752
753    //private void GetDelta(IList<ClientGroup> oldClient, IDictionary<Guid, ClientGroup> helpClients) {
754
755    //  bool found = false;
756    //  for (int i = 0; i < clients.List.Count; i++) {
757    //    ClientGroup cg = clientGroups.List[i];
758    //    for (int j = 0; j < oldClient.Count; i++) {
759    //      ClientGroup cgo = oldClient[j];
760    //      if (cg.Id.Equals(cgo.Id)) {
761    //        found = true;
762    //        foreach (Resource resource in cg.Resources) {
763    //          foreach (Resource resourceold in cgo.Resources) {
764    //            if (resource.Id.Equals(resourceold.Id)) {
765    //              if (resourceold.Name != resource.Name) {
766    //                changes.Add(new Changes { Types = Type.Client, ID = cg.Id, ChangeType = Change.Update, Position = i });
767    //              }
768    //            }
769    //          }
770    //        }
771    //        for (int k = 0; k < helpClients.Count; k++) {
772    //          if (cgo.Id.Equals(helpClients[k].Id)) {
773    //            helpClients.Remove(k);
774    //            break;
775    //          }
776    //        }
777    //        break;
778    //      }
779    //    }
780    //    if (found == false) {
781    //      changes.Add(new Changes { Types = Type.ClientGroup, ID = cg.Id, ChangeType = Change.Create });
782    //    }
783    //    found = false;
784    //  }
785    //  foreach (KeyValuePair<int, ClientGroup> kvp in helpClients) {
786    //    changes.Add(new Changes { Types = Type.ClientGroup, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
787    //  }
788    //}
789
790    private void GetDelta(IList<Job> oldJobs, IDictionary<int, Job> helpJobs) {
791      bool found = false;
792      for (int i = 0; i < jobs.List.Count; i++) {
793        Job job = jobs.List[i];
794        for (int j = 0; j < oldJobs.Count; j++) {
795
796          Job jobold = oldJobs[j];
797
798          if (job.Id.Equals(jobold.Id)) {
799
800            found = true;
801            bool change = false;
802            if (job.State != jobold.State) {
803              change = true;
804            }
805            if (job.State != State.offline) {
806              if ((!IsEqual(job.Client, jobold.Client)) || (job.State != jobold.State)
807                   || (job.Percentage != jobold.Percentage)) {
808                change = true;
809              }
810            } else if (job.DateCalculated != jobold.DateCalculated) {
811              change = true;
812            }
813            if (change) {
814              changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Update, Position = i });
815            }
816
817            int removeAt = -1;
818            foreach (KeyValuePair<int, Job> kvp in helpJobs) {
819              if (job.Id.Equals(kvp.Value.Id)) {
820                removeAt = kvp.Key;
821                break;
822              }
823            }
824            if (removeAt >= 0) {
825              helpJobs.Remove(removeAt);
826            }
827            break;
828          }
829
830        }
831        if (found == false) {
832          changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Create });
833          System.Diagnostics.Debug.WriteLine("new Job: " + job.Id);
834        }
835        found = false;
836      }
837      foreach (KeyValuePair<int, Job> kvp in helpJobs) {
838        changes.Add(new Changes { Types = Type.Job, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
839        System.Diagnostics.Debug.WriteLine("delete Job: " + kvp.Value.Id);
840      }
841    }
842
843    private void GetSnapshotList() {
844
845      lvSnapshots.Items.Clear();
846      IJobManager jobManager = ServiceLocator.GetJobManager();
847
848      ResponseObject<JobResult> jobRes = jobManager.GetLastJobResultOf(currentJob.Id, false);
849
850      // iterate threw all snapshots if method is implemented
851
852      ListViewItem curSnapshot = new ListViewItem(jobRes.Obj.Client.Name);
853      double percentage = jobRes.Obj.Percentage * 100;
854      curSnapshot.SubItems.Add(percentage.ToString() + " %");
855      curSnapshot.SubItems.Add(jobRes.Obj.Timestamp.ToString());
856      lvSnapshots.Items.Add(curSnapshot);
857    }
858
859    #endregion
860
861    private void largeIconsToolStripMenuItem_Click(object sender, EventArgs e) {
862      lvClientControl.View = View.LargeIcon;
863      lvJobControl.View = View.LargeIcon;
864      largeIconsToolStripMenuItem.CheckState = CheckState.Checked;
865      smallIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
866      listToolStripMenuItem.CheckState = CheckState.Unchecked;
867    }
868
869    private void smallIconsToolStripMenuItem_Click(object sender, EventArgs e) {
870      lvClientControl.View = View.SmallIcon;
871      lvJobControl.View = View.SmallIcon;
872      largeIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
873      smallIconsToolStripMenuItem.CheckState = CheckState.Checked;
874      listToolStripMenuItem.CheckState = CheckState.Unchecked;
875    }
876
877    private void listToolStripMenuItem_Click(object sender, EventArgs e) {
878      lvClientControl.View = View.List;
879      lvJobControl.View = View.List;
880      largeIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
881      smallIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
882      listToolStripMenuItem.CheckState = CheckState.Checked;
883    }
884
885    private void tvClientControl_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
886      lvClientControl.Items.Clear();
887      lvClientControl.Groups.Clear();
888      currentNode = e.Node;
889      AddGroupsToListView(e.Node);
890    }
891
892    private void AddGroupsToListView(TreeNode node) {
893      if (node != null) {
894        ListViewGroup lvg = new ListViewGroup(node.Text, HorizontalAlignment.Left);
895        lvClientControl.Groups.Add(lvg);
896        foreach (ListViewItem item in clientList[((ClientGroup)node.Tag).Id]) {
897          item.Group = lvg;
898          lvClientControl.Items.Add(item);
899        }
900
901        if (node.Nodes != null) {
902          foreach (TreeNode curNode in node.Nodes) {
903            AddGroupsToListView(curNode);
904          }
905        }
906      }
907    }
908
909    private void refreshToolStripMenuItem_Click(object sender, EventArgs e) {
910      AddClients();
911    }
912  }
913}
Note: See TracBrowser for help on using the repository browser.