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

  • branches/2931_OR-Tools_LP_MIP/HeuristicLab.Clients.Hive/3.3/HiveAdminClient.cs

    r16139 r16235  
    2626using System.Collections.Generic;
    2727using System.Linq;
     28using HeuristicLab.Clients.Access;
    2829
    2930namespace HeuristicLab.Clients.Hive {
     
    7071    }
    7172
    72     private Dictionary<Guid, HiveItemCollection<Job>> jobs;
    73     public Dictionary<Guid, HiveItemCollection<Job>> Jobs {
     73    private Dictionary<Guid, HiveItemCollection<RefreshableJob>> jobs;
     74    public Dictionary<Guid, HiveItemCollection<RefreshableJob>> Jobs {
    7475      get { return jobs; }
    7576      set {
     
    7778          jobs = value;
    7879        }
     80    }
     81
     82    private Dictionary<Guid, List<LightweightTask>> tasks;
     83    public Dictionary<Guid, List<LightweightTask>> Tasks {
     84      get { return tasks; }
    7985    }
    8086
     
    143149        projects = new ItemList<Project>();
    144150        projectResourceAssignments = new ItemList<AssignedProjectResource>();
    145         jobs = new Dictionary<Guid, HiveItemCollection<Job>>();
     151        jobs = new Dictionary<Guid, HiveItemCollection<RefreshableJob>>();
     152        tasks = new Dictionary<Guid, List<LightweightTask>>();
    146153        projectNames = new Dictionary<Guid, string>();
    147154        resourceNames = new Dictionary<Guid, string>();
     
    160167            service.GetAssignedResourcesForProjectsAdministration(projectIds)
    161168              .ForEach(a => projectResourceAssignments.Add(a));
    162             projectIds.ForEach(p => jobs.Add(p, new HiveItemCollection<Job>()));
     169            projectIds.ForEach(p => jobs.Add(p, new HiveItemCollection<RefreshableJob>()));
    163170            var unsortedJobs = service.GetJobsByProjectIds(projectIds)
    164171              .OrderBy(x => x.DateCreated).ToList();
    165172
    166             unsortedJobs.Where(j => j.State == JobState.DeletionPending).ToList().ForEach(j => jobs[j.ProjectId].Add(j));
    167             unsortedJobs.Where(j => j.State == JobState.StatisticsPending).ToList().ForEach(j => jobs[j.ProjectId].Add(j));
    168             unsortedJobs.Where(j => j.State == JobState.Online).ToList().ForEach(j => jobs[j.ProjectId].Add(j));
     173            unsortedJobs.Where(j => j.State == JobState.DeletionPending).ToList().ForEach(j => jobs[j.ProjectId].Add(new RefreshableJob(j)));
     174            unsortedJobs.Where(j => j.State == JobState.StatisticsPending).ToList().ForEach(j => jobs[j.ProjectId].Add(new RefreshableJob(j)));
     175            unsortedJobs.Where(j => j.State == JobState.Online).ToList().ForEach(j => jobs[j.ProjectId].Add(new RefreshableJob(j)));
     176
     177            foreach (var job in jobs.SelectMany(x => x.Value))
     178              LoadLightweightJob(job);
    169179
    170180            projectNames = service.GetProjectNames();
     
    306316    public void RefreshJobs() {
    307317      var projectIds = new List<Guid>();
    308       jobs = new Dictionary<Guid, HiveItemCollection<Job>>();
     318      jobs = new Dictionary<Guid, HiveItemCollection<RefreshableJob>>();
     319      tasks = new Dictionary<Guid, List<LightweightTask>>();
    309320
    310321      HiveServiceLocator.Instance.CallHiveService(service => {
    311322        service.GetProjectsForAdministration().ForEach(p => projectIds.Add(p.Id));
    312323        if(projectIds.Any()) {
    313           projectIds.ForEach(p => jobs.Add(p, new HiveItemCollection<Job>()));
     324          projectIds.ForEach(p => jobs.Add(p, new HiveItemCollection<RefreshableJob>()));
    314325          var unsortedJobs = service.GetJobsByProjectIds(projectIds)
    315326            .OrderBy(x => x.DateCreated).ToList();
    316           unsortedJobs.Where(j => j.State == JobState.DeletionPending).ToList().ForEach(j => jobs[j.ProjectId].Add(j));
    317           unsortedJobs.Where(j => j.State == JobState.StatisticsPending).ToList().ForEach(j => jobs[j.ProjectId].Add(j));
    318           unsortedJobs.Where(j => j.State == JobState.Online).ToList().ForEach(j => jobs[j.ProjectId].Add(j));
     327         
     328          unsortedJobs.Where(j => j.State == JobState.DeletionPending).ToList().ForEach(j => jobs[j.ProjectId].Add(new RefreshableJob(j)));
     329          unsortedJobs.Where(j => j.State == JobState.StatisticsPending).ToList().ForEach(j => jobs[j.ProjectId].Add(new RefreshableJob(j)));
     330          unsortedJobs.Where(j => j.State == JobState.Online).ToList().ForEach(j => jobs[j.ProjectId].Add(new RefreshableJob(j)));
     331
     332          foreach(var job in jobs.SelectMany(x => x.Value))
     333            LoadLightweightJob(job);
    319334        }
    320335      });
     336    }
     337
     338    public void LoadLightweightJob(RefreshableJob refreshableJob) {
     339      var job = refreshableJob.Job;
     340      var lightweightTasks = HiveServiceLocator.Instance.CallHiveService(s => s.GetLightweightJobTasksWithoutStateLog(job.Id));
     341
     342      if (tasks.ContainsKey(job.Id)) {
     343        tasks[job.Id].Clear();
     344        tasks[job.Id].AddRange(lightweightTasks);
     345      } else {
     346        tasks.Add(job.Id, new List<LightweightTask>(lightweightTasks));       
     347      }
     348
     349      if (lightweightTasks != null && lightweightTasks.Count > 0 && lightweightTasks.All(x => x.Id != Guid.Empty)) {
     350        if (lightweightTasks.All(x =>
     351          x.State == TaskState.Finished
     352          || x.State == TaskState.Aborted
     353          || x.State == TaskState.Failed)) {
     354          refreshableJob.ExecutionState = ExecutionState.Stopped;
     355          refreshableJob.RefreshAutomatically = false;
     356        } else if (
     357          lightweightTasks
     358            .Where(x => x.ParentTaskId != null)
     359            .All(x =>
     360              x.State != TaskState.Waiting
     361              || x.State != TaskState.Transferring
     362              || x.State != TaskState.Calculating)
     363          && lightweightTasks
     364             .Where(x => x.ParentTaskId != null)
     365             .Any(x => x.State == TaskState.Paused)) {
     366          refreshableJob.ExecutionState = ExecutionState.Paused;
     367          refreshableJob.RefreshAutomatically = false;
     368        } else if (lightweightTasks.Any(x => x.State == TaskState.Calculating
     369                                  || x.State == TaskState.Transferring
     370                                  || x.State == TaskState.Waiting)) {
     371          refreshableJob.ExecutionState = ExecutionState.Started;
     372        }
     373
     374        refreshableJob.ExecutionTime = TimeSpan.FromMilliseconds(lightweightTasks.Sum(x => x.ExecutionTime.TotalMilliseconds));
     375      }
    321376    }
    322377
     
    326381        var unsortedJobs = jobs.Values.ElementAt(i);
    327382
    328         var sortedJobs = new HiveItemCollection<Job>();
    329         sortedJobs.AddRange(unsortedJobs.Where(j => j.State == JobState.DeletionPending));
    330         sortedJobs.AddRange(unsortedJobs.Where(j => j.State == JobState.StatisticsPending));
    331         sortedJobs.AddRange(unsortedJobs.Where(j => j.State == JobState.Online));
     383        var sortedJobs = new HiveItemCollection<RefreshableJob>();
     384        sortedJobs.AddRange(unsortedJobs.Where(j => j.Job.State == JobState.DeletionPending));
     385        sortedJobs.AddRange(unsortedJobs.Where(j => j.Job.State == JobState.StatisticsPending));
     386        sortedJobs.AddRange(unsortedJobs.Where(j => j.Job.State == JobState.Online));
    332387
    333388        jobs[projectId] = sortedJobs;
     
    404459    }
    405460
    406     public static void DeleteJobs(List<Guid> jobIds) {
    407 
     461    public static void RemoveJobs(List<Guid> jobIds) {
    408462      HiveServiceLocator.Instance.CallHiveService((s) => s.UpdateJobStates(jobIds, JobState.StatisticsPending));
     463    }
     464    #endregion
     465
     466    #region Job Handling
     467
     468    public static void ResumeJob(RefreshableJob refreshableJob) {
     469      HiveServiceLocator.Instance.CallHiveService(service => {
     470        var tasks = service.GetLightweightJobTasksWithoutStateLog(refreshableJob.Id);
     471        foreach (var task in tasks) {
     472          if (task.State == TaskState.Paused) {
     473            service.RestartTask(task.Id);
     474          }
     475        }
     476      });
     477      refreshableJob.ExecutionState = ExecutionState.Started;
     478    }
     479
     480    public static void PauseJob(RefreshableJob refreshableJob) {
     481      HiveServiceLocator.Instance.CallHiveService(service => {
     482        var tasks = service.GetLightweightJobTasksWithoutStateLog(refreshableJob.Id);
     483        foreach (var task in tasks) {
     484          if (task.State != TaskState.Finished && task.State != TaskState.Aborted && task.State != TaskState.Failed)
     485            service.PauseTask(task.Id);
     486        }
     487      });
     488      refreshableJob.ExecutionState = ExecutionState.Paused;
     489    }
     490
     491    public static void StopJob(RefreshableJob refreshableJob) {
     492      HiveServiceLocator.Instance.CallHiveService(service => {
     493        var tasks = service.GetLightweightJobTasksWithoutStateLog(refreshableJob.Id);
     494        foreach (var task in tasks) {
     495          if (task.State != TaskState.Finished && task.State != TaskState.Aborted && task.State != TaskState.Failed)
     496            service.StopTask(task.Id);
     497        }
     498      });
     499      refreshableJob.ExecutionState = ExecutionState.Stopped;
     500    }
     501
     502    public static void RemoveJob(RefreshableJob refreshableJob) {
     503      HiveServiceLocator.Instance.CallHiveService((service) => {
     504        service.UpdateJobState(refreshableJob.Id, JobState.StatisticsPending);
     505      });
    409506    }
    410507    #endregion
     
    436533      if (resourceDescendants.ContainsKey(id)) return resources.Where(x => resourceDescendants[id].Contains(x.Id));
    437534      else return Enumerable.Empty<Resource>();
     535    }
     536
     537    public IEnumerable<Resource> GetDisabledResourceAncestors(IEnumerable<Resource> availableResources) {
     538      var missingParentIds = availableResources
     539        .Where(x => x.ParentResourceId.HasValue)
     540        .SelectMany(x => resourceAncestors[x.Id]).Distinct()
     541        .Where(x => !availableResources.Select(y => y.Id).Contains(x));
     542
     543      return resources.OfType<SlaveGroup>().Union(disabledParentResources).Where(x => missingParentIds.Contains(x.Id));
    438544    }
    439545
     
    478584
    479585      if(projectAncestors.ContainsKey(pro.Id)) {
    480         return GetAvailableProjectAncestors(pro.Id).Where(x => x.OwnerUserId == userId).Any();
     586        return GetAvailableProjectAncestors(pro.Id).Any(x => x.OwnerUserId == userId);
     587      }
     588
     589      if (pro.ParentProjectId != null && pro.ParentProjectId != Guid.Empty) {
     590        var parent = projects.FirstOrDefault(x => x.Id == pro.ParentProjectId.Value);
     591        if (parent != null)
     592          return parent.OwnerUserId == userId || GetAvailableProjectAncestors(parent.Id).Any(x => x.OwnerUserId == userId);
    481593      }
    482594
     
    491603      // ... or the new parent is not stored yet
    492604      // ... or there is not parental change
    493       if (child == null
    494         || (parent != null && parent.Id == Guid.Empty)
    495         || (parent != null && parent.Id == child.ParentProjectId)) {
     605      if (child == null
     606          || (parent != null && parent.Id == Guid.Empty)
     607          || (parent != null && parent.Id == child.ParentProjectId)) {
     608        changePossible = false;
     609      } else if (parent == null && !IsAdmin()) {
     610        // ... if parent is null, but user is no admin (only admins are allowed to create root projects)
     611        changePossible = false;
     612      } else if (parent != null && (!IsAdmin() && parent.OwnerUserId != UserInformation.Instance.User.Id && !CheckOwnershipOfParentProject(parent, UserInformation.Instance.User.Id))) {
     613        // ... if the user is no admin nor owner of the new parent or grand..grandparents
    496614        changePossible = false;
    497615      } else if(parent != null && projectDescendants.ContainsKey(child.Id)) {
     
    525643      return changePossible;
    526644    }
     645
     646    public IEnumerable<Resource> GetAssignedResourcesForJob(Guid jobId) {
     647      var assignedJobResource =  HiveServiceLocator.Instance.CallHiveService(service => service.GetAssignedResourcesForJob(jobId));
     648      return Resources.Where(x => assignedJobResource.Select(y => y.ResourceId).Contains(x.Id));
     649    }
     650
     651    private bool IsAdmin() {
     652      return HiveRoles.CheckAdminUserPermissions();
     653    }
    527654    #endregion
    528655  }
Note: See TracChangeset for help on using the changeset viewer.