Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/NewHiveService.cs @ 12584

Last change on this file since 12584 was 12584, checked in by dglaser, 9 years ago

#2388: Changed all files to connect to localhost / sqlexpress

HeuristicLab.Services.Hive-3.3:

  • Added Converter.cs and NewHiveService.cs, both will be integrated into existing HiveService.cs and Convert.cs when all methods are successfully implemented

HeuristicLab.Services.Hive.Web.Hive-3.3:

  • Added publish profiles

HeuristicLab.Services.WebApp.Statistics-3.3:

  • Added functionality to download TaskData as .hl file
File size: 17.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Services.Access;
5using HeuristicLab.Services.Hive.DataAccess.Interfaces;
6using HeuristicLab.Services.Hive.DataTransfer;
7using HeuristicLab.Services.Hive.ServiceContracts;
8using DA = HeuristicLab.Services.Hive.DataAccess;
9using DT = HeuristicLab.Services.Hive.DataTransfer;
10
11namespace HeuristicLab.Services.Hive {
12  public class NewHiveService : IHiveService {
13    private static readonly DA.TaskState[] CompletedStates = { DA.TaskState.Finished, DA.TaskState.Aborted, DA.TaskState.Failed };
14
15    private IPersistenceManager PersistenceManager {
16      get { return ServiceLocator.Instance.PersistenceManager; }
17    }
18
19    private IUserManager UserManager {
20      get { return ServiceLocator.Instance.UserManager; }
21    }
22
23    private IRoleVerifier RoleVerifier {
24      get { return ServiceLocator.Instance.RoleVerifier; }
25    }
26
27    private IAuthorizationManager AuthorizationManager {
28      get { return ServiceLocator.Instance.AuthorizationManager; }
29    }
30
31    public Guid AddTask(DT.Task task, DT.TaskData taskData, IEnumerable<Guid> resourceIds) {
32      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
33      using (var pm = PersistenceManager) {
34        var taskDao = pm.TaskDao;
35        var stateLogDao = pm.StateLogDao;
36        var newTask = task.ToEntity();
37        newTask.JobData = taskData.ToEntity();
38        newTask.JobData.LastUpdate = DateTime.Now;
39        newTask.AssignedResources.AddRange(resourceIds.Select(
40          x => new DA.AssignedResource {
41            ResourceId = x
42          }));
43        newTask.State = DA.TaskState.Waiting;
44        return pm.UseTransaction(() => {
45          taskDao.Save(newTask);
46          pm.SubmitChanges();
47          stateLogDao.Save(new DA.StateLog {
48            State = DA.TaskState.Waiting,
49            DateTime = DateTime.Now,
50            TaskId = newTask.TaskId,
51            UserId = UserManager.CurrentUserId,
52            SlaveId = null,
53            Exception = null
54          });
55          pm.SubmitChanges();
56          return newTask.TaskId;
57        }, false, true);
58      }
59    }
60
61    public Guid AddChildTask(Guid parentTaskId, DT.Task task, DT.TaskData taskData) {
62      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
63      IEnumerable<Guid> resourceIds;
64      using (var pm = PersistenceManager) {
65        var assignedResourceDao = pm.AssignedResourceDao;
66        resourceIds = pm.UseTransaction(() => {
67          return assignedResourceDao.GetByTaskId(parentTaskId)
68            .Select(x => x.ResourceId)
69            .ToList();
70        });
71      }
72      task.ParentTaskId = parentTaskId;
73      return AddTask(task, taskData, resourceIds);
74    }
75
76    public DT.Task GetTask(Guid taskId) {
77      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
78      AuthorizationManager.AuthorizeForTask(taskId, Permission.Read);
79      using (var pm = PersistenceManager) {
80        var taskDao = pm.TaskDao;
81        return pm.UseTransaction(() => {
82          var task = taskDao.GetById(taskId);
83          return task.ToDto();
84        });
85      }
86    }
87
88    public IEnumerable<Task> GetTasks() {
89      // unused
90      throw new NotImplementedException();
91    }
92
93    public IEnumerable<LightweightTask> GetLightweightTasks(IEnumerable<Guid> taskIds) {
94      // unused
95      throw new NotImplementedException();
96    }
97
98    public IEnumerable<LightweightTask> GetLightweightChildTasks(Guid? parentTaskId, bool recursive, bool includeParent) {
99      // unused
100      throw new NotImplementedException();
101    }
102
103    public IEnumerable<DT.LightweightTask> GetLightweightJobTasks(Guid jobId) {
104      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
105      AuthorizationManager.AuthorizeForJob(jobId, Permission.Read);
106      using (var pm = PersistenceManager) {
107        var taskDao = pm.TaskDao;
108        return pm.UseTransaction(() => {
109          return taskDao.GetByJobId(jobId)
110            .Select(x => new DT.LightweightTask {
111              Id = x.TaskId,
112              ExecutionTime = TimeSpan.FromMilliseconds(x.ExecutionTimeMs),
113              ParentTaskId = x.ParentTaskId,
114              StateLog = x.StateLogs.OrderBy(y => y.DateTime)
115                                    .Select(z => z.ToDto())
116                                    .ToList(),
117              State = x.State.ToDto(),
118              Command = x.Command.ToDto(),
119              LastTaskDataUpdate = x.JobData.LastUpdate
120            });
121        }, false, true);
122      }
123    }
124
125    public IEnumerable<DT.LightweightTask> GetLightweightJobTasksWithoutStateLog(Guid jobId) {
126      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
127      AuthorizationManager.AuthorizeForJob(jobId, Permission.Read);
128      using (var pm = PersistenceManager) {
129        var taskDao = pm.TaskDao;
130        return pm.UseTransaction(() => {
131          return taskDao.GetByJobId(jobId)
132            .Select(x => new DT.LightweightTask {
133              Id = x.TaskId,
134              ExecutionTime = TimeSpan.FromMilliseconds(x.ExecutionTimeMs),
135              ParentTaskId = x.ParentTaskId,
136              StateLog = new List<DT.StateLog>(),
137              State = x.State.ToDto(),
138              Command = x.Command.ToDto(),
139              LastTaskDataUpdate = x.JobData.LastUpdate
140            });
141        }, false, true);
142      }
143    }
144
145    public DT.TaskData GetTaskData(Guid taskId) {
146      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
147      AuthorizationManager.AuthorizeForTask(taskId, Permission.Read);
148      using (var pm = PersistenceManager) {
149        var taskDataDao = pm.TaskDataDao;
150        return pm.UseTransaction(() => taskDataDao.GetById(taskId).ToDto());
151      }
152    }
153
154    public void UpdateTask(DT.Task taskDto) {
155      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
156      AuthorizationManager.AuthorizeForTask(taskDto.Id, Permission.Full);
157      using (var pm = PersistenceManager) {
158        var taskDao = pm.TaskDao;
159        pm.UseTransaction(() => {
160          var task = taskDao.GetById(taskDto.Id);
161          taskDto.CopyToEntity(task);
162          pm.SubmitChanges();
163        });
164      }
165    }
166
167    public void UpdateTaskData(DT.Task taskDto, DT.TaskData taskDataDto) {
168      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
169      AuthorizationManager.AuthorizeForTask(taskDto.Id, Permission.Full);
170      using (var pm = PersistenceManager) {
171        var taskDao = pm.TaskDao;
172        var taskDataDao = pm.TaskDataDao;
173        pm.UseTransaction(() => {
174          var task = taskDao.GetById(taskDto.Id);
175          var taskData = taskDataDao.GetById(taskDataDto.TaskId);
176          taskDto.CopyToEntity(task);
177          taskDataDto.CopyToEntity(taskData);
178          taskData.LastUpdate = DateTime.Now;
179          pm.SubmitChanges();
180        });
181      }
182    }
183
184    public void DeleteTask(Guid taskId) {
185      // unused
186      throw new NotImplementedException();
187    }
188
189    public void DeleteChildTasks(Guid parentTaskId) {
190      // unused
191      throw new NotImplementedException();
192    }
193
194    public DT.Task UpdateTaskState(Guid taskId, DT.TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
195      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
196      AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
197      using (var pm = PersistenceManager) {
198        var taskDao = pm.TaskDao;
199        var stateLogDao = pm.StateLogDao;
200        return pm.UseTransaction(() => {
201          var task = taskDao.GetById(taskId);
202          UpdateTaskState(pm, task, taskState, slaveId, userId, exception);
203          pm.SubmitChanges();
204          return task.ToDto();
205        });
206      }
207    }
208
209    private void UpdateTaskState(IPersistenceManager pm, DA.Task task, DT.TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
210      var stateLogDao = pm.StateLogDao;
211      var taskStateEntity = taskState.ToEntity();
212      if (task.Command == DA.Command.Pause && task.State == DA.TaskState.Paused
213          || task.Command == DA.Command.Abort && task.State == DA.TaskState.Aborted
214          || task.Command == DA.Command.Stop && task.State == DA.TaskState.Aborted) {
215        task.Command = null;
216      } else if (taskStateEntity == DA.TaskState.Paused) {
217        taskStateEntity = DA.TaskState.Waiting;
218      }
219      stateLogDao.Save(new DA.StateLog {
220        State = taskStateEntity,
221        DateTime = DateTime.Now,
222        TaskId = task.TaskId,
223        UserId = userId,
224        SlaveId = slaveId,
225        Exception = exception
226      });
227      task.State = taskStateEntity;
228    }
229
230    public void StopTask(Guid taskId) {
231      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
232      AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
233      using (var pm = PersistenceManager) {
234        var taskDao = pm.TaskDao;
235        pm.UseTransaction(() => {
236          var task = taskDao.GetById(taskId);
237          if (task.State == DA.TaskState.Calculating || task.State == DA.TaskState.Transferring) {
238            task.Command = DA.Command.Stop;
239          } else if (task.State != DA.TaskState.Aborted
240                     && task.State != DA.TaskState.Finished
241                     && task.State != DA.TaskState.Failed) {
242            UpdateTaskState(pm, task, DT.TaskState.Aborted, null, null, string.Empty);
243          }
244          pm.SubmitChanges();
245        });
246      }
247    }
248
249    public void PauseTask(Guid taskId) {
250      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
251      AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
252      using (var pm = PersistenceManager) {
253        var taskDao = pm.TaskDao;
254        pm.UseTransaction(() => {
255          var task = taskDao.GetById(taskId);
256          if (task.State == DA.TaskState.Calculating || task.State == DA.TaskState.Transferring) {
257            task.Command = DA.Command.Pause;
258          } else {
259            UpdateTaskState(pm, task, DT.TaskState.Paused, null, null, string.Empty);
260          }
261          pm.SubmitChanges();
262        });
263      }
264    }
265
266    public void RestartTask(Guid taskId) {
267      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client, HiveRoles.Slave);
268      AuthorizationManager.AuthorizeForTask(taskId, Permission.Full);
269      using (var pm = PersistenceManager) {
270        var taskDao = pm.TaskDao;
271        pm.UseTransaction(() => {
272          var task = taskDao.GetById(taskId);
273          task.Command = null;
274          UpdateTaskState(pm, task, DT.TaskState.Waiting, null, UserManager.CurrentUserId, string.Empty);
275          pm.SubmitChanges();
276        });
277      }
278    }
279
280
281    public Job GetJob(Guid id) {
282      RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator, HiveRoles.Client);
283      AuthorizationManager.AuthorizeForJob(id, Permission.Read);
284      using (var pm = PersistenceManager) {
285        var jobDao = pm.JobDao;
286        var jobPermissionDao = pm.JobPermissionDao;
287        var taskDao = pm.TaskDao;
288        return pm.UseTransaction(() => {
289          var job = jobDao.GetById(id).ToDto();
290          if (job != null) {
291            var statistics = taskDao.GetByJobId(job.Id)
292              .GroupBy(x => x.JobId)
293              .Select(x => new {
294                TotalCount = x.Count(),
295                CalculatingCount = x.Count(y => y.State == DA.TaskState.Calculating),
296                FinishedCount = x.Count(y => CompletedStates.Contains(y.State))
297              }).FirstOrDefault();
298            if (statistics != null) {
299              job.JobCount = statistics.TotalCount;
300              job.CalculatingCount = statistics.CalculatingCount;
301              job.FinishedCount = statistics.FinishedCount;
302            }
303            // TODO
304            //job.Permission = DT.Convert.ToDto(dao.GetPermissionForJob(job.Id, userManager.CurrentUserId));
305            job.OwnerUsername = UserManager.GetUserById(job.OwnerUserId).UserName;
306          }
307          return job;
308        });
309      }
310    }
311
312    public IEnumerable<Job> GetJobs() {
313      throw new NotImplementedException();
314    }
315
316    public IEnumerable<Job> GetAllJobs() {
317      // unused
318      throw new NotImplementedException();
319    }
320
321    public Guid AddJob(Job jobDto) {
322      throw new NotImplementedException();
323    }
324
325    public void UpdateJob(Job jobDto) {
326      throw new NotImplementedException();
327    }
328
329    public void DeleteJob(Guid JobId) {
330      throw new NotImplementedException();
331    }
332
333    public void GrantPermission(Guid jobId, Guid grantedUserId, Permission permission) {
334      throw new NotImplementedException();
335    }
336
337    public void RevokePermission(Guid hiveExperimentId, Guid grantedUserId) {
338      throw new NotImplementedException();
339    }
340
341    public IEnumerable<JobPermission> GetJobPermissions(Guid jobId) {
342      throw new NotImplementedException();
343    }
344
345    public bool IsAllowedPrivileged() {
346      throw new NotImplementedException();
347    }
348
349    public void Hello(Slave slave) {
350      throw new NotImplementedException();
351    }
352
353    public void GoodBye(Guid slaveId) {
354      throw new NotImplementedException();
355    }
356
357    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
358      throw new NotImplementedException();
359    }
360
361    public Plugin GetPlugin(Guid pluginId) {
362      throw new NotImplementedException();
363    }
364
365    public Plugin GetPluginByHash(byte[] hash) {
366      // unused
367      throw new NotImplementedException();
368    }
369
370    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginData) {
371      throw new NotImplementedException();
372    }
373
374    public IEnumerable<Plugin> GetPlugins() {
375      throw new NotImplementedException();
376    }
377
378    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
379      throw new NotImplementedException();
380    }
381
382    public void DeletePlugin(Guid pluginId) {
383      // unused
384      throw new NotImplementedException();
385    }
386
387    public void GrantResourcePermissions(Guid resourceId, Guid[] grantedUserIds) {
388      throw new NotImplementedException();
389    }
390
391    public void RevokeResourcePermissions(Guid resourceId, Guid[] grantedUserIds) {
392      throw new NotImplementedException();
393    }
394
395    public IEnumerable<ResourcePermission> GetResourcePermissions(Guid resourceId) {
396      throw new NotImplementedException();
397    }
398
399    public IEnumerable<Resource> GetChildResources(Guid resourceId) {
400      // unused
401      throw new NotImplementedException();
402    }
403
404    public Guid AddSlave(Slave slave) {
405      throw new NotImplementedException();
406    }
407
408    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
409      throw new NotImplementedException();
410    }
411
412    public Slave GetSlave(Guid slaveId) {
413      throw new NotImplementedException();
414    }
415
416    public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
417      // unused
418      throw new NotImplementedException();
419    }
420
421    public IEnumerable<Slave> GetSlaves() {
422      throw new NotImplementedException();
423    }
424
425    public IEnumerable<SlaveGroup> GetSlaveGroups() {
426      throw new NotImplementedException();
427    }
428
429    public void UpdateSlave(Slave slave) {
430      throw new NotImplementedException();
431    }
432
433    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
434      throw new NotImplementedException();
435    }
436
437    public void DeleteSlave(Guid slaveId) {
438      throw new NotImplementedException();
439    }
440
441    public void DeleteSlaveGroup(Guid slaveGroupId) {
442      throw new NotImplementedException();
443    }
444
445    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
446      throw new NotImplementedException();
447    }
448
449    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
450      throw new NotImplementedException();
451    }
452
453    public Guid GetResourceId(string resourceName) {
454      throw new NotImplementedException();
455    }
456
457    public IEnumerable<Task> GetTasksByResourceId(Guid resourceId) {
458      // unused
459      throw new NotImplementedException();
460    }
461
462    public void TriggerEventManager(bool force) {
463      // private method...
464      throw new NotImplementedException();
465    }
466
467    public int GetNewHeartbeatInterval(Guid slaveId) {
468      throw new NotImplementedException();
469    }
470
471    public Guid AddDowntime(Downtime downtime) {
472      throw new NotImplementedException();
473    }
474
475    public void DeleteDowntime(Guid downtimeId) {
476      throw new NotImplementedException();
477    }
478
479    public void UpdateDowntime(Downtime downtime) {
480      throw new NotImplementedException();
481    }
482
483    public IEnumerable<Downtime> GetDowntimesForResource(Guid resourceId) {
484      throw new NotImplementedException();
485    }
486
487    public string GetUsernameByUserId(Guid userId) {
488      throw new NotImplementedException();
489    }
490
491    public Guid GetUserIdByUsername(string username) {
492      throw new NotImplementedException();
493    }
494
495    public IEnumerable<UserPriority> GetUserPriorities() {
496      throw new NotImplementedException();
497    }
498
499    public IEnumerable<Statistics> GetStatistics() {
500      // unused
501      throw new NotImplementedException();
502    }
503
504    public IEnumerable<Statistics> GetStatisticsForTimePeriod(DateTime @from, DateTime to) {
505      // unused
506      throw new NotImplementedException();
507    }
508  }
509}
Note: See TracBrowser for help on using the repository browser.