Free cookie consent management tool by TermsFeed Policy Generator

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

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

making list for changes (#508)

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