Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/18/18 14:07:32 (6 years ago)
Author:
ddorfmei
Message:

#2931: Merged [16168-16232/trunk] into branch

Location:
branches/2931_OR-Tools_LP_MIP
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2931_OR-Tools_LP_MIP

  • branches/2931_OR-Tools_LP_MIP/HeuristicLab.Clients.Hive.Administrator

  • branches/2931_OR-Tools_LP_MIP/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectJobsView.cs

    r16139 r16235  
    2424using System.Drawing;
    2525using System.Linq;
     26using System.Threading.Tasks;
    2627using System.Windows.Forms;
    2728using HeuristicLab.MainForm;
     
    3031using HeuristicLab.Data;
    3132using HeuristicLab.Clients.Hive.Views;
     33using HeuristicLab.Core;
    3234
    3335namespace HeuristicLab.Clients.Hive.Administrator.Views {
     
    4143    private const string JOB_DATECREATED = "Date Created";
    4244    private const string JOB_STATE = "State";
     45    private const string JOB_EXECUTIONSTATE = "Execution State";
     46    private const string JOB_EXECUTIONTIME = "Execution Time";
    4347    private const string JOB_DESCRIPTION = "Description";
    4448    private const string JOB_TASKCOUNT = "Tasks";
     49    private const string JOB_WAITINGTASKCOUNT = "Waiting";
    4550    private const string JOB_CALCULATINGTASKCOUNT = "Calculating";
    4651    private const string JOB_FINISHEDTASKCOUNT = "Finished";
    47 
    4852
    4953    private readonly Color onlineStatusColor = Color.FromArgb(255, 189, 249, 143); // #bdf98f
     
    5357    private readonly Color deletionPendingStatusColor2 = Color.FromArgb(255, 249, 149, 143); // #f9958f
    5458
     59    private IProgress progress;
     60    public IProgress Progress {
     61      get { return progress; }
     62      set {
     63        this.progress = value;
     64        OnIsProgressingChanged();
     65      }
     66    }
     67
    5568    public new Project Content {
    5669      get { return (Project)base.Content; }
     
    6073    public ProjectJobsView() {
    6174      InitializeComponent();
    62 
    63       removeButton.Enabled = false;
     75      progress = new Progress();
     76
     77      removeButton.Enabled = false;     
     78    }
     79
     80    protected override void RegisterContentEvents() {
     81      base.RegisterContentEvents();
    6482      matrixView.DataGridView.SelectionChanged += DataGridView_SelectionChanged;
     83      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, progress);
     84    }
     85
     86    protected override void DeregisterContentEvents() {
     87      matrixView.DataGridView.SelectionChanged -= DataGridView_SelectionChanged;
     88      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this, false);
     89      base.DeregisterContentEvents();
    6590    }
    6691
    6792    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;
     93      SetEnabledStateOfControls();
    7694    }
    7795
     
    8199      removeButton.Enabled = false;
    82100      UpdateJobs();
    83     }
     101      SetEnabledStateOfControls();
     102    }
     103
    84104    protected override void SetEnabledStateOfControls() {
    85105      base.SetEnabledStateOfControls();
    86106      bool enabled = Content != null && !Locked && !ReadOnly;
    87107
    88       refreshButton.Enabled = enabled;
    89       removeButton.Enabled = false;
    90108      matrixView.Enabled = enabled;
    91     }
     109
     110      // Buttons (start/resume, pause, stop, remove)
     111      refreshButton.Enabled = startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = removeButton.Enabled = false;
     112
     113      if (enabled && progress.ProgressState != ProgressState.Started) {
     114        var jobs = GetSelectedJobs().ToList();
     115        if (jobs.Any()) {
     116                   
     117          startButton.Enabled = jobs.All(x =>
     118            !x.IsProgressing && HiveAdminClient.Instance.Tasks.ContainsKey(x.Id) && HiveAdminClient.Instance.Tasks[x.Id].Count > 0
     119            && x.Job.ProjectId != Guid.Empty //&& x.Job.ResourceIds != null && x.Job.ResourceIds.Any()
     120            && (x.ExecutionState == ExecutionState.Prepared || x.ExecutionState == ExecutionState.Paused));
     121          pauseButton.Enabled = jobs.All(x => !x.IsProgressing && x.ExecutionState == ExecutionState.Started);
     122          stopButton.Enabled = jobs.All(x => !x.IsProgressing && (x.ExecutionState == ExecutionState.Started || x.ExecutionState == ExecutionState.Paused));
     123          removeButton.Enabled = jobs.All(x => !x.IsProgressing && x.Job.State == JobState.Online);
     124        }
     125      }
     126
     127      // refresh Button
     128      if (Content != null && !Locked && progress.ProgressState != ProgressState.Started) {
     129        refreshButton.Enabled = true;
     130      }
     131    }
     132
    92133    #endregion Overrides
    93134
     
    98139
    99140    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()) {
     141      progress.Start("Refreshing jobs...");
     142      SetEnabledStateOfControls();
     143      var task = System.Threading.Tasks.Task.Factory.StartNew(RefreshJobsAsync);
     144
     145      task.ContinueWith((t) => {
     146        progress.Finish();       
     147        SetEnabledStateOfControls();
     148      });
     149    }
     150
     151    private void removeButton_Click(object sender, EventArgs e) {
     152      var jobs = GetSelectedJobs();
     153
     154      if (jobs.Any()) {
    116155        var result = MessageBox.Show("Do you really want to remove following job(s):\n\n"
    117           + String.Join("\n", jobNamesToDelete),
     156                                     + String.Join("\n", jobs.Select(x => x.Job.Name)),
    118157          "HeuristicLab Hive Administrator",
    119158          MessageBoxButtons.YesNo,
     
    121160
    122161        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 
     162          progress.Start("Removing job(s)...");
     163          SetEnabledStateOfControls();
     164          var task = System.Threading.Tasks.Task.Factory.StartNew(RemoveJobsAsync, jobs);
     165
     166          task.ContinueWith((t) => {
     167            RefreshJobs();
     168            progress.Finish();
     169            SetEnabledStateOfControls();
     170          }, TaskContinuationOptions.NotOnFaulted);
     171
     172          task.ContinueWith((t) => {
     173            RefreshJobs();
     174            progress.Finish();
     175            SetEnabledStateOfControls();
     176            MessageBox.Show("An error occured removing the job(s).", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
     177          }, TaskContinuationOptions.OnlyOnFaulted);
     178        }
     179      }
     180    }
     181
     182    private void startButton_Click(object sender, EventArgs e) {
     183      var jobs = GetSelectedJobs();
     184
     185      if (jobs.Any()) {
     186        var result = MessageBox.Show("Do you really want to resume following job(s):\n\n"
     187                                     + String.Join("\n", jobs.Select(x => x.Job.Name)),
     188          "HeuristicLab Hive Administrator",
     189          MessageBoxButtons.YesNo,
     190          MessageBoxIcon.Question);
     191
     192        if (result == DialogResult.Yes) {
     193          progress.Start("Resuming job(s)...");
     194          SetEnabledStateOfControls();
     195          var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeJobsAsync, jobs);
     196
     197          task.ContinueWith((t) => {
     198            RefreshJobs();           
     199            progress.Finish();
     200            SetEnabledStateOfControls();
     201          }, TaskContinuationOptions.NotOnFaulted);
     202
     203          task.ContinueWith((t) => {
     204            RefreshJobs();
     205            progress.Finish();
     206            SetEnabledStateOfControls();
     207            MessageBox.Show("An error occured resuming the job(s).", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
     208          }, TaskContinuationOptions.OnlyOnFaulted);
     209        }
     210      }
     211    }
     212
     213    private void pauseButton_Click(object sender, EventArgs e) {
     214      var jobs = GetSelectedJobs();
     215
     216      if (jobs.Any()) {
     217        var result = MessageBox.Show("Do you really want to pause following job(s):\n\n"
     218                                     + String.Join("\n", jobs.Select(x => x.Job.Name)),
     219          "HeuristicLab Hive Administrator",
     220          MessageBoxButtons.YesNo,
     221          MessageBoxIcon.Question);
     222
     223        if (result == DialogResult.Yes) {
     224          progress.Start("Pausing job(s)...");
     225          SetEnabledStateOfControls();
     226          var task = System.Threading.Tasks.Task.Factory.StartNew(PauseJobsAsync, jobs);
     227
     228          task.ContinueWith((t) => {
     229            RefreshJobs();           
     230            progress.Finish();
     231            SetEnabledStateOfControls();
     232          }, TaskContinuationOptions.NotOnFaulted);
     233
     234          task.ContinueWith((t) => {
     235            RefreshJobs();
     236            progress.Finish();
     237            SetEnabledStateOfControls();
     238            MessageBox.Show("An error occured pausing the job(s).", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
     239          }, TaskContinuationOptions.OnlyOnFaulted);
     240        }
     241      }
     242    }
     243
     244    private void stopButton_Click(object sender, EventArgs e) {
     245      var jobs = GetSelectedJobs();
     246
     247      if (jobs.Any()) {
     248        var result = MessageBox.Show("Do you really want to stop following job(s):\n\n"
     249                                     + String.Join("\n", jobs.Select(x => x.Job.Name)),
     250          "HeuristicLab Hive Administrator",
     251          MessageBoxButtons.YesNo,
     252          MessageBoxIcon.Question);
     253
     254        if (result == DialogResult.Yes) {
     255          progress.Start("Stopping job(s)...");
     256          SetEnabledStateOfControls();
     257          var task = System.Threading.Tasks.Task.Factory.StartNew(StopJobsAsync, jobs);
     258
     259          task.ContinueWith((t) => {
     260            RefreshJobs();
     261            progress.Finish();
     262            SetEnabledStateOfControls();
     263          }, TaskContinuationOptions.NotOnFaulted);
     264
     265          task.ContinueWith((t) => {
     266            RefreshJobs();
     267            progress.Finish();
     268            SetEnabledStateOfControls();
     269            MessageBox.Show("An error occured stopping the job(s).", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);           
     270          }, TaskContinuationOptions.OnlyOnFaulted);
     271        }
     272      }
     273    }
     274
     275    public event EventHandler IsProgressingChanged;
     276    private void OnIsProgressingChanged() {
     277      var handler = IsProgressingChanged;
     278      if (handler != null) handler(this, EventArgs.Empty);
     279    }
    136280    #endregion Event Handlers
    137281
    138282    #region Helpers
     283
     284    private IEnumerable<RefreshableJob> GetSelectedJobs() {
     285      if (Content == null || matrixView.DataGridView.SelectedRows == null || matrixView.DataGridView.SelectedRows.Count == 0)
     286        return Enumerable.Empty<RefreshableJob>() ;
     287
     288      var jobs = new List<RefreshableJob>();
     289      foreach (DataGridViewRow r in matrixView.DataGridView.SelectedRows) {
     290        if (((string)r.Cells[0].Value) == JobState.Online.ToString()) {
     291          jobs.Add(HiveAdminClient.Instance.Jobs[Content.Id].FirstOrDefault(x => x.Id == Guid.Parse((string) r.Cells[11].Value)));
     292        }
     293      }
     294
     295      return jobs;
     296    }
     297
    139298    private void RefreshJobs() {
    140299      HiveAdminClient.Instance.RefreshJobs();
    141300      UpdateJobs();
     301      SetEnabledStateOfControls();
    142302    }
    143303
     
    148308      var jobs = HiveAdminClient.Instance.Jobs[Content.Id];
    149309      var resources = HiveAdminClient.Instance.Resources;
    150       string[,] values = new string[jobs.Count, 10];
     310      string[,] values = new string[jobs.Count, 13];
    151311
    152312      for(int i = 0; i < jobs.Count; i++) {
    153         var job = jobs.ElementAt(i);
    154        
    155         values[i, 0] = job.State.ToString();
    156         values[i, 1] = job.DateCreated.ToString();
    157         values[i, 2] = job.OwnerUsername;
    158         values[i, 3] = job.Name;
    159         values[i, 4] = job.JobCount.ToString();
    160         values[i, 5] = job.CalculatingCount.ToString();
    161         values[i, 6] = job.FinishedCount.ToString();
    162         values[i, 7] = job.Description;       
    163         values[i, 8] = job.Id.ToString();
    164         values[i, 9] = job.OwnerUserId.ToString();
     313        var job = jobs.ElementAt(i);       
     314        values[i, 0] = job.Job.State.ToString();
     315        values[i, 1] = job.ExecutionState.ToString();
     316        values[i, 2] = job.ExecutionTime.ToString();
     317        values[i, 3] = job.Job.DateCreated.ToString();
     318        values[i, 4] = job.Job.OwnerUsername;
     319        values[i, 5] = job.Job.Name;
     320        values[i, 6] = job.Job.JobCount.ToString();
     321        values[i, 7] = (job.Job.JobCount - job.Job.CalculatingCount - job.Job.FinishedCount).ToString();
     322        values[i, 8] = job.Job.CalculatingCount.ToString();
     323        values[i, 9] = job.Job.FinishedCount.ToString();
     324        values[i, 10] = job.Job.Description;       
     325        values[i, 11] = job.Job.Id.ToString();
     326        values[i, 12] = job.Job.OwnerUserId.ToString();
    165327      }
    166328     
    167329      var matrix = new StringMatrix(values);
    168       matrix.ColumnNames = new string[] { JOB_STATE, JOB_DATECREATED, JOB_OWNER, JOB_NAME, JOB_TASKCOUNT, JOB_CALCULATINGTASKCOUNT, JOB_FINISHEDTASKCOUNT, JOB_DESCRIPTION, JOB_ID, JOB_OWNERID };
     330      matrix.ColumnNames = new string[] { JOB_STATE, JOB_EXECUTIONSTATE, JOB_EXECUTIONTIME, JOB_DATECREATED, JOB_OWNER, JOB_NAME, JOB_TASKCOUNT, JOB_WAITINGTASKCOUNT, JOB_CALCULATINGTASKCOUNT, JOB_FINISHEDTASKCOUNT, JOB_DESCRIPTION, JOB_ID, JOB_OWNERID };
    169331      matrix.SortableView = true;
    170332      return matrix;
     
    193355            matrixView.DataGridView.Columns[1].MinimumWidth = 108;
    194356          }
    195         } else {
    196           refreshButton.Enabled = false;
    197           removeButton.Enabled = false;
    198           matrixView.Content = null;
    199         }
    200       }
    201     }
    202 
    203     private void DeleteJobs(List<Guid> jobIds) {
    204       try {
    205         HiveAdminClient.DeleteJobs(jobIds);
    206       } catch (AnonymousUserException) {
    207         ShowHiveInformationDialog();
    208       }
     357        }
     358      }
     359    }
     360
     361    private void RefreshJobsAsync() {
     362      HiveAdminClient.Instance.RefreshJobs();
     363      UpdateJobs();
     364    }
     365
     366    private void ResumeJobsAsync(object jobs) {
     367      var jobList = (IEnumerable<RefreshableJob>)jobs;
     368      foreach (var job in jobList) {
     369        progress.Status = "Resuming job \"" + job.Job.Name + "\"...";
     370        HiveAdminClient.ResumeJob(job);
     371      }
     372    }
     373
     374    private void PauseJobsAsync(object jobs) {
     375      var jobList = (IEnumerable<RefreshableJob>)jobs;
     376      foreach (var job in jobList) {
     377        progress.Status = "Pausing job \"" + job.Job.Name + "\"...";
     378        HiveAdminClient.PauseJob(job);
     379      }
     380    }
     381
     382    private void StopJobsAsync(object jobs) {
     383      var jobList = (IEnumerable<RefreshableJob>) jobs;
     384      foreach (var job in jobList) {       
     385        progress.Status = "Stopping job \"" + job.Job.Name + "\"...";
     386        HiveAdminClient.StopJob(job);
     387      }
     388    }
     389
     390    private void RemoveJobsAsync(object jobs) {
     391      var jobList = (IEnumerable<RefreshableJob>)jobs;
     392      progress.Start();
     393      foreach (var job in jobList) {
     394        progress.Status = "Removing job \"" + job.Job.Name + "\"...";
     395        HiveAdminClient.RemoveJob(job);
     396      }
     397      progress.Finish();
    209398    }
    210399
Note: See TracChangeset for help on using the changeset viewer.