Free cookie consent management tool by TermsFeed Policy Generator

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

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

JobDetails and Images for job fixed (#600)

File size: 31.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
60    //TODO delete
61    private string nameCurrentJob = "";
62    private string nameCurrentClient = "";
63    private bool flagJob = false;
64    private bool flagClient = 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      tvClientControl.ExpandAll();
251    }
252
253    private void AddClientOrGroup(ClientGroup clientGroup, TreeNode currentNode) {
254      currentNode = CreateTreeNode(clientGroup, currentNode);
255      List<ListViewItem> clientGroupList = new List<ListViewItem>();
256      ListViewGroup lvg;
257      if (string.IsNullOrEmpty(clientGroup.Name)) {
258        lvg = new ListViewGroup(NOGROUP, HorizontalAlignment.Left);
259      } else {
260        lvg = new ListViewGroup(clientGroup.Name, HorizontalAlignment.Left);
261      }
262      lvClientControl.Groups.Add(lvg);
263      foreach (Resource resource in clientGroup.Resources) {
264        if (resource is ClientInfo) {
265          ListViewItem lvi = new ListViewItem(resource.Name, 0, lvg);
266          lvi.Tag = resource as ClientInfo;
267          clientGroupList.Add(lvi);
268        } else if (resource is ClientGroup) {
269          AddClientOrGroup(resource as ClientGroup, currentNode);
270        }
271      }
272      clientList.Add(clientGroup.Id, clientGroupList);
273    }
274
275    private TreeNode CreateTreeNode(ClientGroup clientGroup, TreeNode currentNode) {
276      TreeNode tn;
277      if (string.IsNullOrEmpty(clientGroup.Name)) {
278        tn = new TreeNode(NOGROUP);
279      } else {
280        tn = new TreeNode(clientGroup.Name);
281      }
282      tn.Tag = clientGroup;
283      if (currentNode == null) {
284        tvClientControl.Nodes.Add(tn);
285      } else {
286        currentNode.Nodes.Add(tn);
287      }
288      return tn;
289    }
290
291
292    List<ListViewGroup> jobGroup;
293    /// <summary>
294    /// Adds jobs to ListView and TreeView
295    /// </summary>
296    private void AddJobs() {
297      try {
298        jobObjects = new Dictionary<Guid, ListViewItem>();
299        IJobManager jobManager =
300          ServiceLocator.GetJobManager();
301        jobs = jobManager.GetAllJobs();
302
303        lvJobControl.Items.Clear();
304
305        ListViewGroup lvJobCalculating = new ListViewGroup("calculating", HorizontalAlignment.Left);
306        ListViewGroup lvJobFinished = new ListViewGroup("finished", HorizontalAlignment.Left);
307        ListViewGroup lvJobPending = new ListViewGroup("pending", HorizontalAlignment.Left);
308
309        jobGroup = new List<ListViewGroup>();
310        jobGroup.Add(lvJobCalculating);
311        jobGroup.Add(lvJobFinished);
312        jobGroup.Add(lvJobPending);
313
314        foreach (Job job in jobs.List) {
315          if (job.State == State.calculating) {
316            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 1, lvJobCalculating);
317            lvi.Tag = job;
318            jobObjects.Add(job.Id, lvi);
319
320            //lvJobControl.Items.Add(lvi);
321
322            lvi.ToolTipText = (job.Percentage * 100) + "% of job calculated";
323          } else if (job.State == State.finished) {
324            ListViewItem lvi = new ListViewItem(job.Id.ToString(),0, lvJobFinished);
325            lvi.Tag = job;
326            jobObjects.Add(job.Id, lvi);
327            //lvJobControl.Items.Add(lvi);
328          } else if (job.State == State.offline) {
329            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 2, lvJobPending);
330            lvi.Tag = job;
331            jobObjects.Add(job.Id, lvi);
332            //lvJobControl.Items.Add(lvi);
333          }
334        } // Jobs
335        lvJobControl.BeginUpdate();
336        foreach (ListViewItem lvi in jobObjects.Values) {
337          lvJobControl.Items.Add(lvi);
338        }
339        lvJobControl.Groups.Add(lvJobCalculating);
340        lvJobControl.Groups.Add(lvJobFinished);
341        lvJobControl.Groups.Add(lvJobPending);
342        lvJobControl.EndUpdate();
343        if (flagJob) {
344          JobClicked();
345        }
346      }
347      catch (Exception ex) {
348        closeFormEvent(true, true);
349        this.Close();
350      }
351    }
352
353    /// <summary>
354    /// if one client is clicked, the details for the clicked client are shown
355    /// in the second panel
356    /// </summary>
357    private void ClientClicked() {
358      plClientDetails.Visible = true;
359      //int i = 0;
360      //while (clientInfo.List[i].Id.ToString() != nameCurrentClient) {
361      //  i++;
362      //}
363      if (lvClientControl.SelectedItems != null && lvClientControl.SelectedItems.Count > 0) {
364        currentClient = (ClientInfo)lvClientControl.SelectedItems[0].Tag;
365      }
366      if(currentClient != null) {
367      int percentageUsage = CapacityRam(currentClient.NrOfCores, currentClient.NrOfFreeCores);
368      int usage = 3;
369      if ((currentClient.State != State.offline) && (currentClient.State != State.nullState)) {
370        if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
371          usage = 0;
372        } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
373          usage = 1;
374        } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
375          usage = 2;
376        }
377      }
378      pbClientControl.Image = ilLargeImgClient.Images[usage];
379      lblClientName.Text = currentClient.Name;
380      lblLogin.Text = currentClient.Login.ToString();
381      lblState.Text = currentClient.State.ToString();
382      }
383    }
384
385    /// <summary>
386    /// if one job is clicked, the details for the clicked job are shown
387    /// in the second panel
388    /// </summary>
389    private void JobClicked() {
390      plJobDetails.Visible = true;
391      int i = 0;
392      while (jobs.List[i].Id.ToString() != nameCurrentJob) {
393        i++;
394      }
395      lvSnapshots.Enabled = true;
396      currentJob = jobs.List[i];
397      if (currentJob.State == State.offline) {
398        pbJobControl.Image = ilLargeImgJob.Images[2];
399      } else if (currentJob.State == State.calculating) {
400        pbJobControl.Image = ilLargeImgJob.Images[1];
401      } else if (currentJob.State == State.finished) {
402        pbJobControl.Image = ilLargeImgJob.Images[0];
403      }
404      lblJobName.Text = currentJob.Id.ToString();
405      progressJob.Value = (int)(currentJob.Percentage * 100);
406      lblProgress.Text = (int)(currentJob.Percentage * 100) + "% calculated";
407      lblUserCreatedJob.Text = currentJob.UserId.ToString() + /* currentJob.User.Name + */ " created Job";
408      lblJobCreated.Text = "Created at " + currentJob.DateCreated;
409      if (currentJob.ParentJob != null) {
410        lblParentJob.Text = currentJob.ParentJob.Id + " is parent job";
411      } else {
412        lblParentJob.Text = "";
413      }
414      lblPriorityJob.Text = "Priority of job is " + currentJob.Priority;
415      if (currentJob.Client != null) {
416        lblClientCalculating.Text = currentJob.Client.Name + " calculated Job";
417        lblJobCalculationBegin.Text = "Startet calculation at " + currentJob.DateCalculated;
418
419        if (currentJob.State == State.finished) {
420          IJobManager jobManager =
421            ServiceLocator.GetJobManager();
422          ResponseObject<JobResult> jobRes = jobManager.GetLastJobResultOf(currentJob.Id, false);
423          lblJobCalculationEnd.Text = "Calculation ended at " + jobRes.Obj.DateFinished;
424        }
425      } else {
426        lblClientCalculating.Text = "";
427        lblJobCalculationBegin.Text = "";
428        lblJobCalculationEnd.Text = "";
429      }
430      if (currentJob.State != State.offline) {
431        lvSnapshots.Items.Clear();
432        if (currentJob.State == State.finished)
433          GetSnapshotList();
434        lvSnapshots.Visible = true;
435      } else {
436        lvSnapshots.Visible = false;
437      }
438    }
439
440    private void Refresh() {
441      foreach (Changes change in changes) {
442        if (change.Types == Type.Job) {
443          RefreshJob(change);
444        } else if (change.Types == Type.Client) {
445          //RefreshClient(change);
446        } else if (change.Types == Type.ClientGroup) {
447          //RefreshClientGroup(change);
448        }
449      }
450    }
451
452    private void RefreshJob(Changes change) {
453      if (change.ChangeType == Change.Update) {
454        for (int i = 0; i < lvJobControl.Items.Count; i++) {
455          if (lvJobControl.Items[i].Text == change.ID.ToString()) {
456            if (nameCurrentJob == change.ID.ToString()) {
457              JobClicked();
458            }
459            State state = jobs.List[change.Position].State;
460            System.Diagnostics.Debug.WriteLine(lvJobControl.Items[i].Text.ToString());
461            if (state == State.finished) {
462              lvJobControl.Items[i].Group = jobGroup[1];
463              lvJobControl.Items[i].ImageIndex = 0;
464              System.Diagnostics.Debug.WriteLine("finished");
465            } else if (state == State.calculating) {
466              lvJobControl.Items[i].Group = jobGroup[0];
467              lvJobControl.Items[i].ImageIndex = 1;
468              System.Diagnostics.Debug.WriteLine("calculating");
469            } else if (state == State.offline) {
470              lvJobControl.Items[i].Group = jobGroup[2];
471              lvJobControl.Items[i].ImageIndex = 2;
472              System.Diagnostics.Debug.WriteLine("offline");
473
474            }
475            lvJobControl.Refresh();
476          }
477        }
478      } else if (change.ChangeType == Change.Create) {
479        ListViewItem lvi = new ListViewItem(
480          change.ID.ToString(), 2, jobGroup[2]);
481        jobObjects.Add(change.ID, lvi);
482        lvJobControl.Items.Add(lvi);
483
484      } else if (change.ChangeType == Change.Delete) {
485        jobObjects.Remove(change.ID);
486        for (int i = 0; i < lvJobControl.Items.Count; i++) {
487          if (change.ID.ToString() == lvJobControl.Items[i].Text.ToString()) {
488            lvJobControl.Items[i].Remove();
489            break;
490          }
491        }
492      }
493    }
494
495    private void RefreshClient(Changes change) {
496      if (change.ChangeType == Change.Update) {
497        for (int i = 0; i < lvClientControl.Items.Count; i++) {
498          if (lvClientControl.Items[i].Tag.ToString() == change.ID.ToString()) {
499            if (nameCurrentClient == change.ID.ToString()) {
500              ClientClicked();
501            }
502            State state = clients.List[change.Position].State;
503            System.Diagnostics.Debug.WriteLine(lvClientControl.Items[i].Text.ToString());
504
505            ClientInfo ci = null;
506
507            foreach (ClientInfo c in clients.List) {
508              if (c.Id == change.ID) {
509                ci = c;
510              }
511            }
512
513            int percentageUsage = CapacityRam(ci.NrOfCores, ci.NrOfFreeCores);
514            if ((state == State.offline) || (state == State.nullState)) {
515              lvClientControl.Items[i].ImageIndex = 3;
516            } else {
517              if ((percentageUsage >= 0) && (percentageUsage <= 25)) {
518                lvClientControl.Items[i].ImageIndex = 0;
519              } else if ((percentageUsage > 25) && (percentageUsage <= 75)) {
520                lvClientControl.Items[i].ImageIndex = 1;
521              } else if ((percentageUsage > 75) && (percentageUsage <= 100)) {
522                lvClientControl.Items[i].ImageIndex = 2;
523              }
524
525            }
526            lvClientControl.Refresh();
527          }
528        }
529
530
531      } else if (change.ChangeType == Change.Create) {
532
533      } else if (change.ChangeType == Change.Delete) {
534        clientInfoObjects.Remove(change.ID);
535        for (int i = 0; i < lvClientControl.Items.Count; i++) {
536          if (change.ID.ToString() == lvClientControl.Items[i].Text.ToString()) {
537            lvClientControl.Items[i].Remove();
538            break;
539          }
540        }
541
542      }
543    }
544
545    private void RefreshClientGroup(Changes change) {
546
547    }
548
549    #region Eventhandlers
550    /// <summary>
551    /// Send event to Login-GUI when closing
552    /// </summary>
553    /// <param name="sender"></param>
554    /// <param name="e"></param>
555    private void Close_Click(object sender, EventArgs e) {
556      if (closeFormEvent != null) {
557        closeFormEvent(true, false);
558      }
559      this.Close();
560    }
561
562    /// <summary>
563    /// Send evnt to Login-GUI when closing
564    /// </summary>
565    /// <param name="sender"></param>
566    /// <param name="e"></param>
567    private void HiveServerConsoleInformation_FormClosing(object sender, FormClosingEventArgs e) {
568      if (closeFormEvent != null) {
569        closeFormEvent(true, false);
570      }
571    }
572
573    private void AddJob_Click(object sender, EventArgs e) {
574      AddJobForm newForm = new AddJobForm();
575      newForm.Show();
576      newForm.addJobEvent += new addDelegate(updaterWoker.RunWorkerAsync);
577    }
578
579    private void OnLVClientClicked(object sender, EventArgs e) {
580      nameCurrentClient = lvClientControl.SelectedItems[0].Tag.ToString();
581      flagClient = true;
582      ClientClicked();
583    }
584
585    private void OnLVJobControlClicked(object sender, EventArgs e) {
586      nameCurrentJob = lvJobControl.SelectedItems[0].Text;
587      flagJob = true;
588      JobClicked();
589    }
590
591    private void lvJobControl_MouseMove(object sender, MouseEventArgs e) {
592      if ((lvJobControl.GetItemAt(e.X, e.Y) != null) &&
593        (lvJobControl.GetItemAt(e.X, e.Y).ToolTipText != null)) {
594        tt.SetToolTip(lvJobControl, lvJobControl.GetItemAt(e.X, e.Y).ToolTipText);
595      }
596    }
597
598    private void updaterWoker_DoWork(object sender, DoWorkEventArgs e) {
599
600      changes.Clear();
601
602      //#region ClientInfo
603      //ResponseList<ClientInfo> clientInfoOld = clientInfo;
604      //clientInfo = ClientManager.GetAllClients();
605
606      //IDictionary<int, ClientInfo> clientInfoOldHelp;
607
608      //CloneList(clientInfoOld, out clientInfoOldHelp);
609
610      //GetDelta(clientInfoOld.List, clientInfoOldHelp);
611      //#endregion
612
613      #region Clients
614      //ResponseList<ClientGroup> clientsOld = clients;
615
616     // newClients = ClientManager.GetAllClientGroups();
617
618      //IDictionary<Guid, ClientGroup> clientsOldHelp;
619
620      //CloneList(clientsOld, out clientsOldHelp);
621
622      //GetDelta(clientsOld.List, clientsOldHelp);
623      //DetermineDelta();
624      #endregion
625
626      #region Job
627      ResponseList<Job> jobsOld = jobs;
628      IJobManager jobManager =
629          ServiceLocator.GetJobManager();
630
631      jobs = jobManager.GetAllJobs();
632
633      IDictionary<int, Job> jobsOldHelp;
634      CloneList(jobsOld, out jobsOldHelp);
635
636      GetDelta(jobsOld.List, jobsOldHelp);
637
638      #endregion
639
640      foreach (Changes change in changes) {
641        System.Diagnostics.Debug.WriteLine(change.ID + " " + change.ChangeType);
642      }
643
644    }
645    #endregion
646
647    #region Helper methods
648
649    private void CloneList(ResponseList<Job> oldList, out IDictionary<int, Job> newList) {
650      newList = new Dictionary<int, Job>();
651      for (int i = 0; i < oldList.List.Count; i++) {
652        newList.Add(i, oldList.List[i]);
653      }
654    }
655
656    //private void CloneList(ResponseList<ClientInfo> oldList, out IDictionary<int, ClientInfo> newList) {
657    //  newList = new Dictionary<int, ClientInfo>();
658    //  for (int i = 0; i < oldList.List.Count; i++) {
659    //    newList.Add(i, oldList.List[i]);
660    //  }
661    //}
662
663    private void CloneList(ResponseList<ClientGroup> oldList, out IDictionary<Guid, ClientGroup> newList) {
664      newList = new Dictionary<Guid, ClientGroup>();
665      foreach (ClientGroup clientGroup in oldList.List) {
666        newList.Add(clientGroup.Id, clientGroup);
667      }
668    }
669
670    private bool IsEqual(ClientInfo ci1, ClientInfo ci2) {
671      if (ci2 == null) {
672        return false;
673      }
674      if (ci1.Id.Equals(ci2.Id)) {
675        return true;
676      } else return false;
677    }
678
679    private int CapacityRam(int noCores, int freeCores) {
680      if (noCores > 0) {
681        int capacity = ((noCores - freeCores) / noCores) * 100;
682        System.Diagnostics.Debug.WriteLine(capacity);
683        return capacity;
684      }
685      return 100;
686    }
687
688    //private void GetDelta(IList<ClientInfo> oldClient, IDictionary<int, ClientInfo> helpClients) {
689    //  bool found = false;
690
691    //  for (int i = 0; i < clientInfo.List.Count; i++) {
692    //    ClientInfo ci = clientInfo.List[i];
693    //    for (int j = 0; j < oldClient.Count; j++) {
694    //      ClientInfo cio = oldClient[j];
695    //      if (ci.Id.Equals(cio.Id)) {
696    //        found = true;
697    //        if ((ci.State != cio.State) || (ci.NrOfFreeCores != cio.NrOfFreeCores)) {
698    //          changes.Add(new Changes { Types = Type.Client, ID = ci.Id, ChangeType = Change.Update, Position = i });
699    //        }
700    //        int removeAt = -1;
701    //        foreach (KeyValuePair<int, ClientInfo> kvp in helpClients) {
702    //          if (cio.Id.Equals(kvp.Value.Id)) {
703    //            removeAt = kvp.Key;
704    //            break;
705    //          }
706    //        }
707    //        if (removeAt >= 0) {
708    //          helpClients.Remove(removeAt);
709    //        }
710    //        break;
711    //      }
712    //    }
713    //    if (found == false) {
714    //      changes.Add(new Changes { Types = Type.Client, ID = ci.Id, ChangeType = Change.Create });
715    //    }
716    //    found = false;
717    //  }
718    //  foreach (KeyValuePair<int, ClientInfo> kvp in helpClients) {
719    //    changes.Add(new Changes { Types = Type.Client, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
720    //  }
721
722    //}
723
724    private void DetermineDelta() {
725     
726
727
728    }
729
730
731    //private void GetDelta(IList<ClientGroup> oldClient, IDictionary<Guid, ClientGroup> helpClients) {
732
733    //  bool found = false;
734    //  for (int i = 0; i < clients.List.Count; i++) {
735    //    ClientGroup cg = clientGroups.List[i];
736    //    for (int j = 0; j < oldClient.Count; i++) {
737    //      ClientGroup cgo = oldClient[j];
738    //      if (cg.Id.Equals(cgo.Id)) {
739    //        found = true;
740    //        foreach (Resource resource in cg.Resources) {
741    //          foreach (Resource resourceold in cgo.Resources) {
742    //            if (resource.Id.Equals(resourceold.Id)) {
743    //              if (resourceold.Name != resource.Name) {
744    //                changes.Add(new Changes { Types = Type.Client, ID = cg.Id, ChangeType = Change.Update, Position = i });
745    //              }
746    //            }
747    //          }
748    //        }
749    //        for (int k = 0; k < helpClients.Count; k++) {
750    //          if (cgo.Id.Equals(helpClients[k].Id)) {
751    //            helpClients.Remove(k);
752    //            break;
753    //          }
754    //        }
755    //        break;
756    //      }
757    //    }
758    //    if (found == false) {
759    //      changes.Add(new Changes { Types = Type.ClientGroup, ID = cg.Id, ChangeType = Change.Create });
760    //    }
761    //    found = false;
762    //  }
763    //  foreach (KeyValuePair<int, ClientGroup> kvp in helpClients) {
764    //    changes.Add(new Changes { Types = Type.ClientGroup, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
765    //  }
766    //}
767
768    private void GetDelta(IList<Job> oldJobs, IDictionary<int, Job> helpJobs) {
769      bool found = false;
770      for (int i = 0; i < jobs.List.Count; i++) {
771        Job job = jobs.List[i];
772        for (int j = 0; j < oldJobs.Count; j++) {
773
774          Job jobold = oldJobs[j];
775
776          if (job.Id.Equals(jobold.Id)) {
777
778            found = true;
779            bool change = false;
780            if (job.State != jobold.State) {
781              change = true;
782            }
783            if (job.State != State.offline) {
784              if ((!IsEqual(job.Client, jobold.Client)) || (job.State != jobold.State)
785                   || (job.Percentage != jobold.Percentage)) {
786                change = true;
787              }
788            } else if (job.DateCalculated != jobold.DateCalculated) {
789              change = true;
790            }
791            if (change) {
792              changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Update, Position = i });
793            }
794
795            int removeAt = -1;
796            foreach (KeyValuePair<int, Job> kvp in helpJobs) {
797              if (job.Id.Equals(kvp.Value.Id)) {
798                removeAt = kvp.Key;
799                break;
800              }
801            }
802            if (removeAt >= 0) {
803              helpJobs.Remove(removeAt);
804            }
805            break;
806          }
807
808        }
809        if (found == false) {
810          changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Create });
811          System.Diagnostics.Debug.WriteLine("new Job: " + job.Id);
812        }
813        found = false;
814      }
815      foreach (KeyValuePair<int, Job> kvp in helpJobs) {
816        changes.Add(new Changes { Types = Type.Job, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
817        System.Diagnostics.Debug.WriteLine("delete Job: " + kvp.Value.Id);
818      }
819    }
820
821    private void GetSnapshotList() {
822
823      lvSnapshots.Items.Clear();
824      IJobManager jobManager = ServiceLocator.GetJobManager();
825
826      ResponseObject<JobResult> jobRes = jobManager.GetLastJobResultOf(currentJob.Id, false);
827
828      // iterate threw all snapshots if method is implemented
829
830      ListViewItem curSnapshot = new ListViewItem(jobRes.Obj.Client.Name);
831      double percentage = jobRes.Obj.Percentage * 100;
832      curSnapshot.SubItems.Add(percentage.ToString() + " %");
833      curSnapshot.SubItems.Add(jobRes.Obj.Timestamp.ToString());
834      lvSnapshots.Items.Add(curSnapshot);
835    }
836
837    #endregion
838
839    private void largeIconsToolStripMenuItem_Click(object sender, EventArgs e) {
840      lvClientControl.View = View.LargeIcon;
841      lvJobControl.View = View.LargeIcon;
842      largeIconsToolStripMenuItem.CheckState = CheckState.Checked;
843      smallIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
844      listToolStripMenuItem.CheckState = CheckState.Unchecked;
845    }
846
847    private void smallIconsToolStripMenuItem_Click(object sender, EventArgs e) {
848      lvClientControl.View = View.SmallIcon;
849      lvJobControl.View = View.SmallIcon;
850      largeIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
851      smallIconsToolStripMenuItem.CheckState = CheckState.Checked;
852      listToolStripMenuItem.CheckState = CheckState.Unchecked;
853    }
854
855    private void listToolStripMenuItem_Click(object sender, EventArgs e) {
856      lvClientControl.View = View.List;
857      lvJobControl.View = View.List;
858      largeIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
859      smallIconsToolStripMenuItem.CheckState = CheckState.Unchecked;
860      listToolStripMenuItem.CheckState = CheckState.Checked;
861    }
862
863    private void tvClientControl_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
864      lvClientControl.Items.Clear();
865      lvClientControl.Groups.Clear();
866      AddGroupsToListView(e.Node);
867    }
868
869    private void AddGroupsToListView(TreeNode node) {
870      if (node != null) {
871        ListViewGroup lvg = new ListViewGroup(node.Text, HorizontalAlignment.Left);
872        lvClientControl.Groups.Add(lvg);
873        foreach (ListViewItem item in clientList[((ClientGroup)node.Tag).Id]) {
874          item.Group = lvg;
875          lvClientControl.Items.Add(item);
876        }
877
878        if (node.Nodes != null) {
879          foreach (TreeNode curNode in node.Nodes) {
880            AddGroupsToListView(curNode);
881          }
882        }
883      }
884    }
885
886    private void refreshToolStripMenuItem_Click(object sender, EventArgs e) {
887      AddClients();
888    }
889  }
890}
Note: See TracBrowser for help on using the repository browser.