Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectJobsView.cs @ 16060

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

#2839: applied several fixes:

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