Free cookie consent management tool by TermsFeed Policy Generator

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

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

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