Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Hive_Management_Console_Refactoring_Ticket508/HeuristicLab.Hive.Server.Console/HiveServerManagementConsole.cs @ 1381

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

deleted user controls
job control will be showed on update
(#508)

File size: 20.8 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  public delegate void closeForm(bool cf, bool error);
37
38  public partial class HiveServerManagementConsole : Form {
39
40    public event closeForm closeFormEvent;
41
42    #region private variables
43    private ResponseList<ClientGroup> clients = null;
44    private ResponseList<ClientInfo> clientInfo = null;
45    private ResponseList<Job> jobs = null;
46
47    private Dictionary<long, ListViewGroup> clientObjects;
48    private Dictionary<long, ListViewItem> clientInfoObjects;
49    private Dictionary<long, ListViewItem> jobObjects;
50
51    private Job currentJob = null;
52    private ClientInfo currentClient = null;
53    private string nameCurrentJob = "";
54    private string nameCurrentClient = "";
55    private string nameCurrentUser = "";
56    private bool flagJob = false;
57    private bool flagClient = false;
58    private bool flagUser = false;
59
60    private List<Changes> changes = new List<Changes>();
61
62    private ToolTip tt = new ToolTip();
63    #endregion
64
65    public HiveServerManagementConsole() {
66      InitializeComponent();
67      AddClients();
68      AddJobs();
69      timerSyncronize.Start();
70    }
71
72    /// <summary>
73    /// event on Ticker
74    /// </summary>
75    /// <param name="obj"></param>
76    /// <param name="e"></param>
77    private void TickSync(object obj, EventArgs e) {
78      if (!updaterWoker.IsBusy) {
79        updaterWoker.RunWorkerAsync();
80      }
81    }
82
83    /// <summary>
84    /// Adds clients to ListView and TreeView
85    /// </summary>
86    private void AddClients() {
87      try {
88        clientObjects = new Dictionary<long, ListViewGroup>();
89        clientInfoObjects = new Dictionary<long, ListViewItem>();
90        IClientManager clientManager =
91          ServiceLocator.GetClientManager();
92
93        clients = clientManager.GetAllClientGroups();
94        lvClientControl.Items.Clear();
95        tvClientControl.Nodes.Clear();
96        int count = 0;
97        foreach (ClientGroup cg in clients.List) {
98          tvClientControl.Nodes.Add(cg.Name);
99          ListViewGroup lvg = new ListViewGroup(cg.Name, HorizontalAlignment.Left);
100          foreach (ClientInfo ci in clientManager.GetAllClients().List) {
101            tvClientControl.Nodes[tvClientControl.Nodes.Count - 1].Nodes.Add(ci.Name);
102            ListViewItem item = new ListViewItem(ci.Name, count, lvg);
103            lvClientControl.Items.Add(item);
104            clientInfoObjects.Add(ci.Id, item);
105            count = (count + 1) % 3;
106          }
107          lvClientControl.BeginUpdate();
108          lvClientControl.Groups.Add(lvg);
109          lvClientControl.EndUpdate();
110          clientObjects.Add(cg.Id, lvg);
111        } // Groups
112
113        clientInfo = clientManager.GetAllClients();
114        ListViewGroup lvunsorted = new ListViewGroup("unsorted", HorizontalAlignment.Left);
115        foreach (ClientInfo ci in clientInfo.List) {
116          tvClientControl.Nodes.Add(ci.Name);
117          lvClientControl.Items.Add(new ListViewItem(ci.Name, count, lvunsorted));
118          count = (count + 1) % 3;
119        }
120        lvClientControl.BeginUpdate();
121        lvClientControl.Groups.Add(lvunsorted);
122        lvClientControl.EndUpdate();
123        if (flagClient) {
124          ClientClicked();
125        }
126      }
127      catch (Exception ex) {
128        closeFormEvent(true, true);
129        this.Close();
130      }
131    }
132
133
134    List<ListViewGroup> jobGroup;
135    /// <summary>
136    /// Adds jobs to ListView and TreeView
137    /// </summary>
138    private void AddJobs() {
139      try {
140        jobObjects = new Dictionary<long, ListViewItem>();
141        IJobManager jobManager =
142          ServiceLocator.GetJobManager();
143        jobs = jobManager.GetAllJobs();
144
145        lvJobControl.Items.Clear();
146        tvJobControl.Nodes.Clear();
147
148        ListViewGroup lvJobCalculating = new ListViewGroup("calculating", HorizontalAlignment.Left);
149        ListViewGroup lvJobFinished = new ListViewGroup("finished", HorizontalAlignment.Left);
150        ListViewGroup lvJobPending = new ListViewGroup("pending", HorizontalAlignment.Left);
151
152        jobGroup = new List<ListViewGroup>();
153        jobGroup.Add(lvJobCalculating);
154        jobGroup.Add(lvJobFinished);
155        jobGroup.Add(lvJobPending);
156       
157        tvJobControl.Nodes.Add("calculating");
158        tvJobControl.Nodes.Add("finished");
159        tvJobControl.Nodes.Add("pending");
160        foreach (Job job in jobs.List) {
161          if (job.State == State.calculating) {
162            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobCalculating);
163            jobObjects.Add(job.Id, lvi);
164            tvJobControl.Nodes[0].Nodes.Add(job.Id.ToString());
165
166            //lvJobControl.Items.Add(lvi);
167           
168            lvi.ToolTipText = (job.Percentage * 100) + "% of job calculated";
169          } else if (job.State == State.finished) {
170            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobFinished);
171            jobObjects.Add(job.Id, lvi);
172            tvJobControl.Nodes[1].Nodes.Add(job.Id.ToString());
173            //lvJobControl.Items.Add(lvi);
174          } else if (job.State == State.offline) {
175            ListViewItem lvi = new ListViewItem(job.Id.ToString(), 0, lvJobPending);
176            jobObjects.Add(job.Id, lvi);
177            tvJobControl.Nodes[2].Nodes.Add(job.Id.ToString());
178            //lvJobControl.Items.Add(lvi);
179          }
180        } // Jobs
181        lvJobControl.BeginUpdate();
182        foreach (ListViewItem lvi in jobObjects.Values) {
183          lvJobControl.Items.Add(lvi);
184        }
185        lvJobControl.Groups.Add(lvJobCalculating);
186        lvJobControl.Groups.Add(lvJobFinished);
187        lvJobControl.Groups.Add(lvJobPending);
188        lvJobControl.EndUpdate();
189        if (flagJob) {
190          JobClicked();
191        }
192      }
193      catch (Exception ex) {
194        closeFormEvent(true, true);
195        this.Close();
196      }
197    }
198
199    /// <summary>
200    /// if one client is clicked, a panel is opened with the details
201    /// </summary>
202    private void ClientClicked() {
203      int i = 0;
204      while (clientInfo.List[i].Name != nameCurrentClient) {
205        i++;
206      }
207      currentClient = clientInfo.List[i];
208      scClientControl.Panel2.Controls.Clear();
209      scClientControl.Panel2.Controls.Add(plClientDetails);
210      pbClientControl.Image = ilClientControl.Images[0];
211      lblClientName.Text = currentClient.Name;
212      lblLogin.Text = currentClient.Login.ToString();
213      lblState.Text = currentClient.State.ToString();
214    }
215
216    /// <summary>
217    /// if one job is clicked, a panel is opened with the details
218    /// </summary>
219    private void JobClicked() {
220      int i = 0;
221      while (jobs.List[i].Id.ToString() != nameCurrentJob) {
222        i++;
223      }
224      lvSnapshots.Enabled = false;
225      int yPos = 0;
226      currentJob = jobs.List[i];
227      scJobControl.Panel2.Controls.Clear();
228      scJobControl.Panel2.Controls.Add(plJobDetails);
229      pbJobControl.Image = ilJobControl.Images[0];
230      lblJobName.Text = currentJob.Id.ToString();
231      progressJob.Value = (int)(currentJob.Percentage * 100);
232      yPos = progressJob.Location.Y;
233      yPos += 20;
234      lblProgress.Location = new Point(
235        lblProgress.Location.X, yPos);
236      lblProgress.Text = (int)(currentJob.Percentage * 100) + "% calculated";
237      yPos += 20;
238      lblUserCreatedJob.Location = new Point(
239        lblUserCreatedJob.Location.X, yPos);
240      lblUserCreatedJob.Text = /* currentJob.User.Name + */ " created Job";
241      yPos += 20;
242      lblJobCreated.Location = new Point(
243        lblJobCreated.Location.X, yPos);
244      lblJobCreated.Text = "Created at " + currentJob.DateCreated;
245      if (currentJob.ParentJob != null) {
246        yPos += 20;
247        lblParentJob.Location = new Point(
248          lblParentJob.Location.X, yPos);
249        lblParentJob.Text = currentJob.ParentJob.Id + " is parent job";
250      }
251      yPos += 20;
252      lblPriorityJob.Location = new Point(
253        lblPriorityJob.Location.X, yPos);
254      lblPriorityJob.Text = "Priority of job is " + currentJob.Priority;
255      if (currentJob.Client != null) {
256        yPos += 20;
257        lblClientCalculating.Location = new Point(
258          lblClientCalculating.Location.X, yPos);
259        lblClientCalculating.Text = currentJob.Client.Name + " calculated Job";
260        yPos += 20;
261        lblJobCalculationBegin.Location = new Point(
262          lblJobCalculationBegin.Location.X, yPos);
263        lblJobCalculationBegin.Text = "Startet calculation at " + currentJob.DateCalculated ;
264       
265        if (currentJob.State == State.finished) {
266          yPos += 20;
267          lblJobCalculationEnd.Location = new Point(
268            lblJobCalculationEnd.Location.X, yPos);
269
270          IJobManager jobManager =
271            ServiceLocator.GetJobManager();
272
273          ResponseObject<JobResult> jobRes = jobManager.GetLastJobResultOf(currentJob.Id);
274
275         
276          lblJobCalculationEnd.Text = "Calculation ended at " + jobRes.Obj.DateFinished;
277        }
278      }                       
279      if (currentJob.State != State.offline) {
280        yPos += 20;
281        lvSnapshots.Location = new Point(
282          lvSnapshots.Location.X, yPos);
283        lvSnapshots.Size = new Size(lvSnapshots.Size.Width,
284          plJobDetails.Size.Height - 10 - yPos);
285        lvSnapshots.Enabled = true;
286      }
287    }
288
289    #region Eventhandlers
290    /// <summary>
291    /// Send event to Login-GUI when closing
292    /// </summary>
293    /// <param name="sender"></param>
294    /// <param name="e"></param>
295    private void Close_Click(object sender, EventArgs e) {
296      if (closeFormEvent != null) {
297        closeFormEvent(true, false);
298      }
299      this.Close();
300    }
301
302    /// <summary>
303    /// Send evnt to Login-GUI when closing
304    /// </summary>
305    /// <param name="sender"></param>
306    /// <param name="e"></param>
307    private void HiveServerConsoleInformation_FormClosing(object sender, FormClosingEventArgs e) {
308      if (closeFormEvent != null) {
309        closeFormEvent(true, false);
310      }
311    }
312
313    private void Refresh() {
314      foreach (Changes change in changes) {
315        if (change.Types == Type.Job) {
316          if (change.ChangeType == Change.Update) {
317            //if (jobObjects[change.ID].ListView.Groups.ToString() == ) {}
318            //ListViewItem lvi = new ListViewItem(
319            //  jobs.List[change.Position].Id.ToString(), 0);
320          } else if (change.ChangeType == Change.Create) {
321            ListViewItem lvi = new ListViewItem(
322              jobs.List[change.Position].Id.ToString(), 0, jobGroup[2]);
323            jobObjects.Add(change.ID, lvi);
324            lvJobControl.Items.Add(lvi);
325
326          } else if (change.ChangeType == Change.Delete) {
327            jobObjects.Remove(change.Position);
328          }
329
330        }
331      }
332
333    }
334
335    private void AddJob_Click(object sender, EventArgs e) {
336      AddJobForm newForm = new AddJobForm();
337      newForm.Show();
338      //newForm.addJobEvent += new addDelegate(updaterWoker.RunWorkerAsync);
339    }
340
341    private void OnLVClientClicked(object sender, EventArgs e) {
342      nameCurrentClient = lvClientControl.SelectedItems[0].Text;
343      flagClient = true;
344      ClientClicked();
345    }
346
347    private void OnTVClientClicked(object sender, TreeViewEventArgs e) {
348      bool clientgroup = false;
349      foreach (ClientGroup cg in clients.List) {
350        if (tvClientControl.SelectedNode.Text == cg.Name) {
351          clientgroup = true;
352        }
353      }
354      if (clientgroup == false) {
355        nameCurrentClient = tvClientControl.SelectedNode.Text;
356        flagClient = true;
357        ClientClicked();
358      }
359
360    }
361
362    private void OnLVJobControlClicked(object sender, EventArgs e) {
363      nameCurrentJob = lvJobControl.SelectedItems[0].Text;
364      flagJob = true;
365      JobClicked();
366    }
367
368    private void OnTVJobControlClicked(object sender, TreeViewEventArgs e) {
369      try {
370        nameCurrentJob = Convert.ToInt32(tvJobControl.SelectedNode.Text).ToString();
371        flagJob = true;
372        JobClicked();
373      }
374      catch (Exception ex) { }
375
376    }
377
378    private void btnClientClose_Click(object sender, EventArgs e) {
379      scClientControl.Panel2.Controls.Clear();
380      scClientControl.Panel2.Controls.Add(lvClientControl);
381      flagClient = false;
382    }
383 
384    private void btnJobDetailClose_Click(object sender, EventArgs e) {
385      scJobControl.Panel2.Controls.Clear();
386      scJobControl.Panel2.Controls.Add(lvJobControl);
387      flagJob = false;
388    }
389
390    private void lvJobControl_MouseMove(object sender, MouseEventArgs e) {
391      if ((lvJobControl.GetItemAt(e.X, e.Y) != null) &&
392        (lvJobControl.GetItemAt(e.X, e.Y).ToolTipText != null)) {
393        tt.SetToolTip(lvJobControl, lvJobControl.GetItemAt(e.X, e.Y).ToolTipText);
394      }
395    }
396
397    private void updaterWoker_DoWork(object sender, DoWorkEventArgs e) {
398
399      IClientManager clientManager =
400          ServiceLocator.GetClientManager();
401     
402      #region ClientInfo
403      ResponseList<ClientInfo> clientInfoOld = clientInfo;
404      clientInfo = clientManager.GetAllClients();
405
406      IDictionary<int, ClientInfo> clientInfoOldHelp;
407
408      CloneList(clientInfoOld, out clientInfoOldHelp);
409
410      GetDelta(clientInfoOld.List, clientInfoOldHelp);
411      #endregion
412
413      #region Clients
414      ResponseList<ClientGroup> clientsOld = clients;
415     
416      clients = clientManager.GetAllClientGroups();
417
418      IDictionary<int, ClientGroup> clientsOldHelp;
419
420      CloneList(clientsOld, out clientsOldHelp);
421
422      GetDelta(clientsOld.List, clientsOldHelp);
423      #endregion
424
425      #region Job
426      ResponseList<Job> jobsOld = jobs;
427      IJobManager jobManager =
428          ServiceLocator.GetJobManager();
429
430      jobs = jobManager.GetAllJobs();
431
432      IDictionary<int, Job> jobsOldHelp;
433      CloneList(jobsOld, out jobsOldHelp);
434     
435      //System.Diagnostics.Debug.WriteLine(jobsOldHelp.List[0].Id.ToString());
436
437      GetDelta(jobsOld.List, jobsOldHelp);
438
439      #endregion
440
441      foreach (Changes change in changes) {
442        System.Diagnostics.Debug.WriteLine(change.ID + " " + change.ChangeType);
443      }
444
445    }
446    #endregion
447
448    #region Helper methods
449
450    private void CloneList(ResponseList<Job> oldList, out IDictionary<int, Job> newList) {
451      newList = new Dictionary<int, Job>();
452      for (int i = 0; i < oldList.List.Count; i++) {
453        newList.Add(i, oldList.List[i]);
454      }
455    }
456
457    private void CloneList(ResponseList<ClientInfo> oldList, out IDictionary<int, ClientInfo> newList) {
458      newList = new Dictionary<int, ClientInfo>();
459      for (int i = 0; i < oldList.List.Count; i ++) {
460        newList.Add(i, oldList.List[i]);
461      }
462    }
463
464    private void CloneList(ResponseList<ClientGroup> oldList, out IDictionary<int, ClientGroup> newList) {
465      newList = new Dictionary<int, ClientGroup>();
466      for (int i = 0; i < oldList.List.Count; i++) {
467        newList.Add(i, oldList.List[i]);
468      }
469    }
470
471    private bool IsEqual(ClientInfo ci1, ClientInfo ci2) {
472      if (ci1.ClientId.Equals(ci2.ClientId)) {
473        return true;
474      } else return false;
475    }
476
477    private void GetDelta(IList<ClientInfo> oldClient, IDictionary<int, ClientInfo> helpClients) {
478      bool found = false;
479
480      for (int i = 0; i < clientInfo.List.Count; i ++) {
481        ClientInfo ci = clientInfo.List[i];
482        for (int j = 0; j < oldClient.Count; j++) {
483          ClientInfo cio = oldClient[j];
484          if (ci.Id.Equals(cio.Id)) {
485            found = true;
486            if (ci.State != cio.State) {
487              changes.Add(new Changes { Types = Type.Client, ID = ci.Id, ChangeType = Change.Update, Position = i });
488            }
489            int removeAt = -1;
490            foreach (KeyValuePair<int, ClientInfo> kvp in helpClients) {
491              if (cio.Id.Equals(kvp.Value.Id)) {
492                removeAt = kvp.Key;
493                break;
494              }
495            }
496            if (removeAt >= 0) {
497              helpClients.Remove(removeAt);
498            }
499            break;
500          }
501        }
502        if (found == false) {
503          changes.Add(new Changes { Types = Type.Client, ID = ci.Id, ChangeType = Change.Create });
504        }
505        found = false;
506      }
507      foreach (KeyValuePair<int, ClientInfo> kvp in helpClients) {
508        changes.Add(new Changes { Types = Type.Client, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
509      }
510
511    }
512
513    private void GetDelta(IList<ClientGroup> oldClient, IDictionary<int, ClientGroup> helpClients) {
514
515      bool found = false;
516      for (int i = 0; i < clients.List.Count; i++) {
517        ClientGroup cg = clients.List[i];
518        for (int j = 0; j < oldClient.Count; i++) {
519          ClientGroup cgo = oldClient[j];
520          if (cg.Id.Equals(cgo.Id)) {
521            found = true;
522            foreach (Resource resource in cg.Resources) {
523              foreach (Resource resourceold in cgo.Resources) {
524                if (resource.Id.Equals(resourceold.Id)) {
525                  if (resourceold.Name != resource.Name) {
526                    changes.Add(new Changes { Types = Type.Client, ID = cg.Id, ChangeType = Change.Update, Position = i });
527                  }
528                }
529              }
530            }
531            for (int k = 0; k < helpClients.Count; k++) {
532              if (cgo.Id.Equals(helpClients[k].Id)) {
533                helpClients.Remove(k);
534                break;
535              }
536            }
537            break;
538          }
539        }
540        if (found == false) {
541          changes.Add(new Changes { Types = Type.ClientGroup, ID = cg.Id, ChangeType = Change.Create });
542        }
543        found = false;
544      }
545      foreach (KeyValuePair<int, ClientGroup> kvp in helpClients) {
546        changes.Add(new Changes { Types = Type.ClientGroup, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
547      }
548    }
549
550    private void GetDelta(IList<Job> oldJobs, IDictionary<int, Job> helpJobs) {
551      bool found = false;
552      for (int i = 0; i < jobs.List.Count; i ++ ) {
553        Job job = jobs.List[i];
554        for (int j = 0; j < oldJobs.Count; j++) {
555
556          Job jobold = oldJobs[j];
557
558          if (job.Id.Equals(jobold.Id)) {
559
560            found = true;
561            if (job.State != State.offline) {
562              if (!IsEqual(job.Client, jobold.Client)) {
563                changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Update });
564              }
565            } else if (job.DateCalculated != jobold.DateCalculated) {
566              changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Update });
567            }
568
569            int removeAt = -1;
570            foreach (KeyValuePair<int, Job> kvp in helpJobs) {
571              if (job.Id.Equals(kvp.Value.Id)) {
572                removeAt = kvp.Key;
573                break;
574              }
575            }
576            if (removeAt >= 0) {
577              helpJobs.Remove(removeAt);
578            }
579            break;
580          }
581
582        }
583        if (found == false) {
584          changes.Add(new Changes { Types = Type.Job, ID = job.Id, ChangeType = Change.Create });
585          System.Diagnostics.Debug.WriteLine("new Job: " + job.Id);
586        }
587        found = false;
588      }
589      foreach (KeyValuePair<int, Job> kvp in helpJobs) {
590        changes.Add(new Changes { Types = Type.Job, ID = kvp.Value.Id, ChangeType = Change.Delete, Position = kvp.Key });
591        System.Diagnostics.Debug.WriteLine("delete Job: " + kvp.Value.Id);
592      }
593    }
594
595
596    #endregion
597
598    private void updaterWoker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
599      Refresh();
600    }
601
602
603  }
604}
Note: See TracBrowser for help on using the repository browser.