Free cookie consent management tool by TermsFeed Policy Generator

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

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

deleted TreeView in Job- and ClientControl, splitted view for Overview and Details (#569)

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