Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectJobsView.cs @ 16205

Last change on this file since 16205 was 16205, checked in by jzenisek, 6 years ago

#2839 adapting project job view

File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.Core.Views;
30using HeuristicLab.Data;
31using HeuristicLab.Clients.Hive.Views;
32
33namespace HeuristicLab.Clients.Hive.Administrator.Views {
34  [View("ProjectView")]
35  [Content(typeof(Project), IsDefaultView = false)]
36  public partial class ProjectJobsView : ItemView {
37    private const string JOB_ID = "Id";
38    private const string JOB_NAME = "Name";
39    private const string JOB_OWNER = "Owner";
40    private const string JOB_OWNERID = "Owner Id";
41    private const string JOB_DATECREATED = "Date Created";
42    private const string JOB_STATE = "State";
43    private const string JOB_DESCRIPTION = "Description";
44    private const string JOB_TASKCOUNT = "Tasks";
45    private const string JOB_CALCULATINGTASKCOUNT = "Calculating";
46    private const string JOB_FINISHEDTASKCOUNT = "Finished";
47
48
49    private readonly Color onlineStatusColor = Color.FromArgb(255, 189, 249, 143); // #bdf98f
50    private readonly Color onlineStatusColor2 = Color.FromArgb(255, 157, 249, 143); // #9df98f
51    private readonly Color statisticsPendingStatusColor = Color.FromArgb(255, 249, 210, 145); // #f9d291
52    private readonly Color deletionPendingStatusColor = Color.FromArgb(255, 249, 172, 144); // #f9ac90
53    private readonly Color deletionPendingStatusColor2 = Color.FromArgb(255, 249, 149, 143); // #f9958f
54
55    public new Project Content {
56      get { return (Project)base.Content; }
57      set { base.Content = value; }
58    }
59
60    public ProjectJobsView() {
61      InitializeComponent();
62
63      removeButton.Enabled = false;
64      matrixView.DataGridView.SelectionChanged += DataGridView_SelectionChanged;
65    }
66
67    private void DataGridView_SelectionChanged(object sender, EventArgs e) {
68      if (matrixView.DataGridView.SelectedRows == null || matrixView.DataGridView.SelectedRows.Count == 0) return;
69
70      bool anyDeletable = false;
71      foreach (DataGridViewRow r in matrixView.DataGridView.SelectedRows) {
72        if (((string)r.Cells[0].Value) == JobState.Online.ToString()) anyDeletable = true;
73      }
74
75      removeButton.Enabled = anyDeletable;
76    }
77
78    #region Overrides
79    protected override void OnContentChanged() {
80      base.OnContentChanged();
81      removeButton.Enabled = false;
82      UpdateJobs();
83    }
84    protected override void SetEnabledStateOfControls() {
85      base.SetEnabledStateOfControls();
86      bool enabled = Content != null && !Locked && !ReadOnly;
87
88      refreshButton.Enabled = enabled;
89      removeButton.Enabled = false;
90      matrixView.Enabled = enabled;
91    }
92    #endregion Overrides
93
94    #region Event Handlers
95    private void ProjectJobsView_Load(object sender, EventArgs e) {
96     
97    }
98
99    private void refreshButton_Click(object sender, EventArgs e) {
100      RefreshJobs();
101    }
102
103    private async void removeButton_Click(object sender, EventArgs e) {
104      if (matrixView.DataGridView.SelectedRows == null || matrixView.DataGridView.SelectedRows.Count == 0) return;
105
106      var jobNamesToDelete = new List<string>();
107      var jobIdsToDelete = new List<Guid>();
108      foreach (DataGridViewRow r in matrixView.DataGridView.SelectedRows) {
109        if(((string)r.Cells[0].Value) == JobState.Online.ToString()) {
110          jobNamesToDelete.Add(" - " + ((string)r.Cells[3].Value));
111          jobIdsToDelete.Add(Guid.Parse((string)r.Cells[8].Value));
112        }
113      }
114
115      if(jobIdsToDelete.Any()) {
116        var result = MessageBox.Show("Do you really want to remove following job(s):\n\n"
117          + String.Join("\n", jobNamesToDelete),
118          "HeuristicLab Hive Administrator",
119          MessageBoxButtons.YesNo,
120          MessageBoxIcon.Question);
121
122        if (result == DialogResult.Yes) {
123          await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
124            action: () => {
125              DeleteJobs(jobIdsToDelete);
126            },
127            finallyCallback: () => {
128              matrixView.DataGridView.ClearSelection();
129              removeButton.Enabled = false;
130              RefreshJobs();
131            });
132        }
133      }
134    }
135
136    private async void startButton_Click(object sender, EventArgs e) {
137      throw new NotImplementedException();
138    }
139
140    private async void pauseButton_Click(object sender, EventArgs e) {
141      throw new NotImplementedException();
142    }
143
144    private async void stopButton_Click(object sender, EventArgs e) {
145      throw new NotImplementedException();
146    }
147
148    #endregion Event Handlers
149
150    #region Helpers
151    private void RefreshJobs() {
152      HiveAdminClient.Instance.RefreshJobs();
153      UpdateJobs();
154    }
155
156    private StringMatrix CreateValueMatrix() {
157      if (Content == null || Content.Id == Guid.Empty)
158        return new StringMatrix();
159
160      var jobs = HiveAdminClient.Instance.Jobs[Content.Id];
161      var resources = HiveAdminClient.Instance.Resources;
162      string[,] values = new string[jobs.Count, 10];
163
164      for(int i = 0; i < jobs.Count; i++) {
165        var job = jobs.ElementAt(i);
166       
167        values[i, 0] = job.State.ToString();
168        values[i, 1] = job.DateCreated.ToString();
169        values[i, 2] = job.OwnerUsername;
170        values[i, 3] = job.Name;
171        values[i, 4] = job.JobCount.ToString();
172        values[i, 5] = job.CalculatingCount.ToString();
173        values[i, 6] = job.FinishedCount.ToString();
174        values[i, 7] = job.Description;       
175        values[i, 8] = job.Id.ToString();
176        values[i, 9] = job.OwnerUserId.ToString();
177      }
178     
179      var matrix = new StringMatrix(values);
180      matrix.ColumnNames = new string[] { JOB_STATE, JOB_DATECREATED, JOB_OWNER, JOB_NAME, JOB_TASKCOUNT, JOB_CALCULATINGTASKCOUNT, JOB_FINISHEDTASKCOUNT, JOB_DESCRIPTION, JOB_ID, JOB_OWNERID };
181      matrix.SortableView = true;
182      return matrix;
183    }
184   
185    private void UpdateJobs() {
186      if (InvokeRequired) Invoke((Action)UpdateJobs);
187      else {
188        if(Content != null && Content.Id != null && Content.Id != Guid.Empty) {
189          var matrix = CreateValueMatrix();
190          matrixView.Content = matrix;
191          if(matrix != null) {
192            foreach (DataGridViewRow row in matrixView.DataGridView.Rows) {
193              string val = ((string)row.Cells[0].Value);
194              if (val == JobState.Online.ToString()) {
195                row.DefaultCellStyle.BackColor = onlineStatusColor;
196              } else if (val == JobState.StatisticsPending.ToString()) {
197                row.DefaultCellStyle.BackColor = statisticsPendingStatusColor;
198              } else if (val == JobState.DeletionPending.ToString()) {
199                row.DefaultCellStyle.BackColor = deletionPendingStatusColor;
200              }
201            }
202
203            matrixView.DataGridView.AutoResizeColumns();
204            matrixView.DataGridView.Columns[0].MinimumWidth = 90;
205            matrixView.DataGridView.Columns[1].MinimumWidth = 108;
206          }
207        } else {
208          refreshButton.Enabled = false;
209          removeButton.Enabled = false;
210          matrixView.Content = null;
211        }
212      }
213    }
214
215    private void PauseJobs(List<Guid> jobIds) {
216      try {
217        // TODO
218      } catch (AnonymousUserException) {
219        ShowHiveInformationDialog();
220      }
221    }
222
223    private void StopJobs(List<Guid> jobIds) {
224      try {
225        // TODO
226      } catch (AnonymousUserException) {
227        ShowHiveInformationDialog();
228      }
229    }
230
231    private void DeleteJobs(List<Guid> jobIds) {
232      try {
233        HiveAdminClient.DeleteJobs(jobIds);
234      } catch (AnonymousUserException) {
235        ShowHiveInformationDialog();
236      }
237    }
238
239    private void ShowHiveInformationDialog() {
240      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
241      else {
242        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
243          dialog.ShowDialog(this);
244        }
245      }
246    }
247    #endregion Helpers
248  }
249}
Note: See TracBrowser for help on using the repository browser.