Free cookie consent management tool by TermsFeed Policy Generator

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

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

add contextmenu for adding/deleting groups (#626)

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