Free cookie consent management tool by TermsFeed Policy Generator

source: branches/UnloadJobs/HeuristicLab.Services.Hive/3.3/HiveDao.cs @ 9202

Last change on this file since 9202 was 9202, checked in by ascheibe, 11 years ago

#2005

  • add repeating of failed tasks and task datas
  • added an additional ws method that loads lightweight tasks without the statelogs
File size: 41.0 KB
RevLine 
[6983]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6983]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.Linq;
25using System.Linq.Expressions;
26using DT = HeuristicLab.Services.Hive.DataTransfer;
27
28namespace HeuristicLab.Services.Hive.DataAccess {
29  public class HiveDao : IHiveDao {
30    public static HiveDataContext CreateContext(bool longRunning = false) {
31      var context = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
32      if (longRunning) context.CommandTimeout = (int)Settings.Default.LongRunningDatabaseCommandTimeout.TotalSeconds;
33      return context;
34    }
35
36    public HiveDao() { }
37
38    #region Task Methods
39    public DT.Task GetTask(Guid id) {
40      using (var db = CreateContext()) {
41        return DT.Convert.ToDto(db.Tasks.SingleOrDefault(x => x.TaskId == id));
42      }
43    }
44
45    public IEnumerable<DT.Task> GetTasks(Expression<Func<Task, bool>> predicate) {
46      using (var db = CreateContext()) {
47        return db.Tasks.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
48      }
49    }
50
[9022]51    public IEnumerable<DT.LightweightTask> GetLightweightTasks(Expression<Func<Task, bool>> predicate) {
52      List<DT.LightweightTask> tasks = new List<DT.LightweightTask>();
53
54      using (var db = CreateContext()) {
55        var tasksQuery = db.Tasks.Where(predicate).Select(task => new { task.TaskId, task.ExecutionTimeMs, task.ParentTaskId, task.StateLogs, task.State, task.Command });
56        var taskDatasQuery = db.Tasks.Where(predicate).Where(task => task.JobData != null).Select(task => new { task.TaskId, task.JobData.LastUpdate });
57
58        foreach (var task in tasksQuery) {
59          DT.LightweightTask t = new DT.LightweightTask();
60          t.Id = task.TaskId;
61          t.ExecutionTime = TimeSpan.FromMilliseconds(task.ExecutionTimeMs);
62          t.ParentTaskId = task.ParentTaskId;
63          t.StateLog = task.StateLogs == null ? new List<DT.StateLog>() : task.StateLogs.Select(x => DataTransfer.Convert.ToDto(x)).OrderBy(x => x.DateTime).ToList();
64          t.State = DataTransfer.Convert.ToDto(task.State);
65          t.Command = DataTransfer.Convert.ToDto(task.Command);
66          t.LastTaskDataUpdate = taskDatasQuery.Where(x => x.TaskId == task.TaskId).Count() > 0 ? taskDatasQuery.Select(x => x.LastUpdate).First() : DateTime.MinValue;
67          tasks.Add(t);
68        }
69      }
70      return tasks;
71    }
72
[9202]73    public IEnumerable<DT.LightweightTask> GetLightweightTasksWithoutStateLog(Expression<Func<Task, bool>> predicate) {
74      List<DT.LightweightTask> tasks = new List<DT.LightweightTask>();
75
76      using (var db = CreateContext()) {
77        var tasksQuery = db.Tasks.Where(predicate).Select(task => new { task.TaskId, task.ExecutionTimeMs, task.ParentTaskId, task.State, task.Command });
78        var taskDatasQuery = db.Tasks.Where(predicate).Where(task => task.JobData != null).Select(task => new { task.TaskId, task.JobData.LastUpdate });
79
80        foreach (var task in tasksQuery) {
81          DT.LightweightTask t = new DT.LightweightTask();
82          t.Id = task.TaskId;
83          t.ExecutionTime = TimeSpan.FromMilliseconds(task.ExecutionTimeMs);
84          t.ParentTaskId = task.ParentTaskId;
85          t.StateLog = new List<DT.StateLog>();
86          t.State = DataTransfer.Convert.ToDto(task.State);
87          t.Command = DataTransfer.Convert.ToDto(task.Command);
88          t.LastTaskDataUpdate = taskDatasQuery.Where(x => x.TaskId == task.TaskId).Count() > 0 ? taskDatasQuery.Select(x => x.LastUpdate).First() : DateTime.MinValue;
89          tasks.Add(t);
90        }
91      }
92      return tasks;
93    }
94
[6983]95    public Guid AddTask(DT.Task dto) {
96      using (var db = CreateContext()) {
97        var entity = DT.Convert.ToEntity(dto);
98        db.Tasks.InsertOnSubmit(entity);
99        db.SubmitChanges();
100        foreach (Guid pluginId in dto.PluginsNeededIds) {
101          db.RequiredPlugins.InsertOnSubmit(new RequiredPlugin() { TaskId = entity.TaskId, PluginId = pluginId });
102        }
103        db.SubmitChanges();
104        return entity.TaskId;
105      }
106    }
107
[9123]108    public void UpdateTaskAndPlugins(DT.Task dto) {
[6983]109      using (var db = CreateContext()) {
110        var entity = db.Tasks.FirstOrDefault(x => x.TaskId == dto.Id);
111        if (entity == null) db.Tasks.InsertOnSubmit(DT.Convert.ToEntity(dto));
112        else DT.Convert.ToEntity(dto, entity);
113        foreach (Guid pluginId in dto.PluginsNeededIds) {
114          if (db.RequiredPlugins.Count(p => p.PluginId == pluginId) == 0) {
115            db.RequiredPlugins.InsertOnSubmit(new RequiredPlugin() { TaskId = entity.TaskId, PluginId = pluginId });
116          }
117        }
118        db.SubmitChanges();
119      }
120    }
121
[9123]122    public void UpdateTask(DT.Task dto) {
123      using (var db = CreateContext()) {
124        var entity = db.Tasks.FirstOrDefault(x => x.TaskId == dto.Id);
125        if (entity == null) db.Tasks.InsertOnSubmit(DT.Convert.ToEntity(dto));
126        else DT.Convert.ToEntity(dto, entity);
127        db.SubmitChanges();
128      }
129    }
130
[6983]131    public void DeleteTask(Guid id) {
132      using (var db = CreateContext()) {
133        var entity = db.Tasks.FirstOrDefault(x => x.TaskId == id);
134        if (entity != null) db.Tasks.DeleteOnSubmit(entity);
135        db.SubmitChanges(); // taskData and child tasks are deleted by db-trigger
136      }
137    }
138
139    /// <summary>
140    /// returns all parent tasks which are waiting for their child tasks to finish
141    /// </summary>
142    /// <param name="resourceIds">list of resourceids which for which the task should be valid</param>
143    /// <param name="count">maximum number of task to return</param>
144    /// <param name="finished">if true, all parent task which have FinishWhenChildJobsFinished=true are returned, otherwise only FinishWhenChildJobsFinished=false are returned</param>
145    /// <returns></returns>
146    public IEnumerable<DT.Task> GetParentTasks(IEnumerable<Guid> resourceIds, int count, bool finished) {
147      using (var db = CreateContext()) {
148        var query = from ar in db.AssignedResources
149                    where resourceIds.Contains(ar.ResourceId)
150                       && ar.Task.State == TaskState.Waiting
151                       && ar.Task.IsParentTask
152                       && (finished ? ar.Task.FinishWhenChildJobsFinished : !ar.Task.FinishWhenChildJobsFinished)
153                       && (from child in db.Tasks
154                           where child.ParentTaskId == ar.Task.TaskId
155                           select child.State == TaskState.Finished
156                               || child.State == TaskState.Aborted
157                               || child.State == TaskState.Failed).All(x => x)
158                       && (from child in db.Tasks // avoid returning WaitForChildTasks task where no child-task exist (yet)
159                           where child.ParentTaskId == ar.Task.TaskId
160                           select child).Count() > 0
161                    orderby ar.Task.Priority descending, db.Random()
162                    select DT.Convert.ToDto(ar.Task);
163        return count == 0 ? query.ToArray() : query.Take(count).ToArray();
164      }
165    }
166
[9123]167    public IEnumerable<TaskInfoForScheduler> GetWaitingTasks(DT.Slave slave) {
[6983]168      using (var db = CreateContext()) {
169        var resourceIds = GetParentResources(slave.Id).Select(r => r.Id);
[7178]170        //Originally we checked here if there are parent tasks which should be calculated (with GetParentTasks(resourceIds, count, false);).
171        //Because there is at the moment no case where this makes sense (there don't exist parent tasks which need to be calculated),
172        //we skip this step because it's wasted runtime
[6983]173
174        var query = from ar in db.AssignedResources
175                    where resourceIds.Contains(ar.ResourceId)
176                       && !(ar.Task.IsParentTask && ar.Task.FinishWhenChildJobsFinished)
177                       && ar.Task.State == TaskState.Waiting
178                       && ar.Task.CoresNeeded <= slave.FreeCores
179                       && ar.Task.MemoryNeeded <= slave.FreeMemory
[9123]180                    select new TaskInfoForScheduler() { TaskId = ar.Task.TaskId, JobId = ar.Task.JobId, Priority = ar.Task.Priority };
181        var waitingTasks = query.ToArray();
[7178]182        return waitingTasks;
[6983]183      }
184    }
185
186    public DT.Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception) {
187      using (var db = CreateContext()) {
188        var job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId);
189        job.State = taskState;
190        db.StateLogs.InsertOnSubmit(new StateLog {
191          TaskId = taskId,
192          State = taskState,
193          SlaveId = slaveId,
194          UserId = userId,
195          Exception = exception,
196          DateTime = DateTime.Now
197        });
198        db.SubmitChanges();
199        job = db.Tasks.SingleOrDefault(x => x.TaskId == taskId);
200        return DT.Convert.ToDto(job);
201      }
202    }
203    #endregion
204
205    #region TaskData Methods
206    public DT.TaskData GetTaskData(Guid id) {
207      using (var db = CreateContext(true)) {
208        return DT.Convert.ToDto(db.TaskDatas.SingleOrDefault(x => x.TaskId == id));
209      }
210    }
211
212    public IEnumerable<DT.TaskData> GetTaskDatas(Expression<Func<TaskData, bool>> predicate) {
213      using (var db = CreateContext(true)) {
214        return db.TaskDatas.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
215      }
216    }
217
218    public Guid AddTaskData(DT.TaskData dto) {
219      using (var db = CreateContext(true)) {
220        var entity = DT.Convert.ToEntity(dto);
221        db.TaskDatas.InsertOnSubmit(entity);
222        db.SubmitChanges();
223        return entity.TaskId;
224      }
225    }
226
227    public void UpdateTaskData(DT.TaskData dto) {
228      using (var db = CreateContext(true)) {
229        var entity = db.TaskDatas.FirstOrDefault(x => x.TaskId == dto.TaskId);
230        if (entity == null) db.TaskDatas.InsertOnSubmit(DT.Convert.ToEntity(dto));
231        else DT.Convert.ToEntity(dto, entity);
232        db.SubmitChanges();
233      }
234    }
235
236    public void DeleteTaskData(Guid id) {
237      using (var db = CreateContext()) {
238        var entity = db.TaskDatas.FirstOrDefault(x => x.TaskId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
239        if (entity != null) db.TaskDatas.DeleteOnSubmit(entity);
240        db.SubmitChanges();
241      }
242    }
243    #endregion
244
245    #region StateLog Methods
246    public DT.StateLog GetStateLog(Guid id) {
247      using (var db = CreateContext()) {
248        return DT.Convert.ToDto(db.StateLogs.SingleOrDefault(x => x.StateLogId == id));
249      }
250    }
251
252    public IEnumerable<DT.StateLog> GetStateLogs(Expression<Func<StateLog, bool>> predicate) {
253      using (var db = CreateContext()) {
254        return db.StateLogs.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
255      }
256    }
257
258    public Guid AddStateLog(DT.StateLog dto) {
259      using (var db = CreateContext()) {
260        var entity = DT.Convert.ToEntity(dto);
261        db.StateLogs.InsertOnSubmit(entity);
262        db.SubmitChanges();
263        return entity.StateLogId;
264      }
265    }
266
267    public void UpdateStateLog(DT.StateLog dto) {
268      using (var db = CreateContext()) {
269        var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == dto.Id);
270        if (entity == null) db.StateLogs.InsertOnSubmit(DT.Convert.ToEntity(dto));
271        else DT.Convert.ToEntity(dto, entity);
272        db.SubmitChanges();
273      }
274    }
275
276    public void DeleteStateLog(Guid id) {
277      using (var db = CreateContext()) {
278        var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == id);
279        if (entity != null) db.StateLogs.DeleteOnSubmit(entity);
280        db.SubmitChanges();
281      }
282    }
283    #endregion
284
285    #region Job Methods
286    public DT.Job GetJob(Guid id) {
287      using (var db = CreateContext()) {
288        return AddStatsToJob(db, DT.Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id)));
289      }
290    }
291
292    private DT.Job AddStatsToJob(HiveDataContext db, DT.Job exp) {
293      if (exp == null)
294        return null;
295
296      var jobs = db.Tasks.Where(j => j.JobId == exp.Id);
297      exp.JobCount = jobs.Count();
298      exp.CalculatingCount = jobs.Count(j => j.State == TaskState.Calculating);
299      exp.FinishedCount = jobs.Count(j => j.State == TaskState.Finished);
300      return exp;
301    }
302
303    public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
304      using (var db = CreateContext()) {
305        return db.Jobs.Where(predicate).Select(x => AddStatsToJob(db, DT.Convert.ToDto(x))).ToArray();
306      }
307    }
308
[9123]309    public IEnumerable<JobInfoForScheduler> GetJobInfoForScheduler(Expression<Func<Job, bool>> predicate) {
310      using (var db = CreateContext()) {
311        return db.Jobs.Where(predicate).Select(x => new JobInfoForScheduler() { Id = x.JobId, DateCreated = x.DateCreated, OwnerUserId = x.OwnerUserId }).ToArray();
312      }
313    }
314
[6983]315    public Guid AddJob(DT.Job dto) {
316      using (var db = CreateContext()) {
317        var entity = DT.Convert.ToEntity(dto);
318        db.Jobs.InsertOnSubmit(entity);
[9123]319        if (!db.UserPriorities.Any(x => x.UserId == dto.OwnerUserId))
320          EnqueueUserPriority(new DT.UserPriority { Id = dto.OwnerUserId, DateEnqueued = dto.DateCreated });
[6983]321        db.SubmitChanges();
322        return entity.JobId;
323      }
324    }
325
326    public void UpdateJob(DT.Job dto) {
327      using (var db = CreateContext()) {
328        var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
329        if (entity == null) db.Jobs.InsertOnSubmit(DT.Convert.ToEntity(dto));
330        else DT.Convert.ToEntity(dto, entity);
331        db.SubmitChanges();
332      }
333    }
334
335    public void DeleteJob(Guid id) {
336      using (var db = CreateContext()) {
337        var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
338        if (entity != null) db.Jobs.DeleteOnSubmit(entity);
339        db.SubmitChanges();
340      }
341    }
342    #endregion
343
344    #region JobPermission Methods
345    public DT.JobPermission GetJobPermission(Guid jobId, Guid grantedUserId) {
346      using (var db = CreateContext()) {
347        return DT.Convert.ToDto(db.JobPermissions.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId));
348      }
349    }
350
351    public IEnumerable<DT.JobPermission> GetJobPermissions(Expression<Func<JobPermission, bool>> predicate) {
352      using (var db = CreateContext()) {
353        return db.JobPermissions.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
354      }
355    }
356
357    public void AddJobPermission(DT.JobPermission dto) {
358      using (var db = CreateContext()) {
359        var entity = DT.Convert.ToEntity(dto);
360        db.JobPermissions.InsertOnSubmit(entity);
361        db.SubmitChanges();
362      }
363    }
364
365    public void UpdateJobPermission(DT.JobPermission dto) {
366      using (var db = CreateContext()) {
367        var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == dto.JobId && x.GrantedUserId == dto.GrantedUserId);
368        if (entity == null) db.JobPermissions.InsertOnSubmit(DT.Convert.ToEntity(dto));
369        else DT.Convert.ToEntity(dto, entity);
370        db.SubmitChanges();
371      }
372    }
373
374    public void DeleteJobPermission(Guid jobId, Guid grantedUserId) {
375      using (var db = CreateContext()) {
376        var entity = db.JobPermissions.FirstOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId);
377        if (entity != null) db.JobPermissions.DeleteOnSubmit(entity);
378        db.SubmitChanges();
379      }
380    }
381
382    /// <summary>
383    /// Sets the permissions for a experiment. makes sure that only one permission per user exists.
384    /// </summary>
385    public void SetJobPermission(Guid jobId, Guid grantedByUserId, Guid grantedUserId, Permission permission) {
386      using (var db = CreateContext()) {
387        JobPermission jobPermission = db.JobPermissions.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == grantedUserId);
388        if (jobPermission != null) {
389          if (permission == Permission.NotAllowed) {
390            // not allowed, delete
391            db.JobPermissions.DeleteOnSubmit(jobPermission);
392          } else {
393            // update
394            jobPermission.Permission = permission;
395            jobPermission.GrantedByUserId = grantedByUserId; // update grantedByUserId, always the last "granter" is stored
396          }
397        } else {
398          // insert
399          if (permission != Permission.NotAllowed) {
400            jobPermission = new JobPermission() { JobId = jobId, GrantedByUserId = grantedByUserId, GrantedUserId = grantedUserId, Permission = permission };
401            db.JobPermissions.InsertOnSubmit(jobPermission);
402          }
403        }
404        db.SubmitChanges();
405      }
406    }
407    #endregion
408
409    #region Plugin Methods
410    public DT.Plugin GetPlugin(Guid id) {
411      using (var db = CreateContext()) {
412        return DT.Convert.ToDto(db.Plugins.SingleOrDefault(x => x.PluginId == id));
413      }
414    }
415
416    public IEnumerable<DT.Plugin> GetPlugins(Expression<Func<Plugin, bool>> predicate) {
417      using (var db = CreateContext()) {
418        return db.Plugins.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
419      }
420    }
421
422    public Guid AddPlugin(DT.Plugin dto) {
423      using (var db = CreateContext()) {
424        var entity = DT.Convert.ToEntity(dto);
425        db.Plugins.InsertOnSubmit(entity);
426        db.SubmitChanges();
427        return entity.PluginId;
428      }
429    }
430
431    public void UpdatePlugin(DT.Plugin dto) {
432      using (var db = CreateContext()) {
433        var entity = db.Plugins.FirstOrDefault(x => x.PluginId == dto.Id);
434        if (entity == null) db.Plugins.InsertOnSubmit(DT.Convert.ToEntity(dto));
435        else DT.Convert.ToEntity(dto, entity);
436        db.SubmitChanges();
437      }
438    }
439
440    public void DeletePlugin(Guid id) {
441      using (var db = CreateContext()) {
442        var entity = db.Plugins.FirstOrDefault(x => x.PluginId == id);
443        if (entity != null) db.Plugins.DeleteOnSubmit(entity);
444        db.SubmitChanges();
445      }
446    }
447    #endregion
448
449    #region PluginData Methods
450    public DT.PluginData GetPluginData(Guid id) {
451      using (var db = CreateContext()) {
452        return DT.Convert.ToDto(db.PluginDatas.SingleOrDefault(x => x.PluginDataId == id));
453      }
454    }
455
456    public IEnumerable<DT.PluginData> GetPluginDatas(Expression<Func<PluginData, bool>> predicate) {
457      using (var db = CreateContext()) {
458        return db.PluginDatas.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
459      }
460    }
461
462    public Guid AddPluginData(DT.PluginData dto) {
463      using (var db = CreateContext()) {
464        var entity = DT.Convert.ToEntity(dto);
465        db.PluginDatas.InsertOnSubmit(entity);
466        db.SubmitChanges();
467        return entity.PluginDataId;
468      }
469    }
470
471    public void UpdatePluginData(DT.PluginData dto) {
472      using (var db = CreateContext()) {
473        var entity = db.PluginDatas.FirstOrDefault(x => x.PluginId == dto.PluginId);
474        if (entity == null) db.PluginDatas.InsertOnSubmit(DT.Convert.ToEntity(dto));
475        else DT.Convert.ToEntity(dto, entity);
476        db.SubmitChanges();
477      }
478    }
479
480    public void DeletePluginData(Guid id) {
481      using (var db = CreateContext()) {
482        var entity = db.PluginDatas.FirstOrDefault(x => x.PluginDataId == id);
483        if (entity != null) db.PluginDatas.DeleteOnSubmit(entity);
484        db.SubmitChanges();
485      }
486    }
487    #endregion
488
489    #region Slave Methods
490    public DT.Slave GetSlave(Guid id) {
491      using (var db = CreateContext()) {
492        return DT.Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
493      }
494    }
495
496    public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
497      using (var db = CreateContext()) {
498        return db.Resources.OfType<Slave>().Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
499      }
500    }
501
502    public Guid AddSlave(DT.Slave dto) {
503      using (var db = CreateContext()) {
504        var entity = DT.Convert.ToEntity(dto);
505        db.Resources.InsertOnSubmit(entity);
506        db.SubmitChanges();
507        return entity.ResourceId;
508      }
509    }
510
511    public void UpdateSlave(DT.Slave dto) {
512      using (var db = CreateContext()) {
513        var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
514        if (entity == null) db.Resources.InsertOnSubmit(DT.Convert.ToEntity(dto));
515        else DT.Convert.ToEntity(dto, entity);
516        db.SubmitChanges();
517      }
518    }
519
520    public void DeleteSlave(Guid id) {
521      using (var db = CreateContext()) {
522        var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
523        if (entity != null) db.Resources.DeleteOnSubmit(entity);
524        db.SubmitChanges();
525      }
526    }
527    #endregion
528
529    #region SlaveGroup Methods
530    public DT.SlaveGroup GetSlaveGroup(Guid id) {
531      using (var db = CreateContext()) {
532        return DT.Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
533      }
534    }
535
536    public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
537      using (var db = CreateContext()) {
538        return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
539      }
540    }
541
542    public Guid AddSlaveGroup(DT.SlaveGroup dto) {
543      using (var db = CreateContext()) {
544        if (dto.Id == Guid.Empty)
545          dto.Id = Guid.NewGuid();
546        var entity = DT.Convert.ToEntity(dto);
547        db.Resources.InsertOnSubmit(entity);
548        db.SubmitChanges();
549        return entity.ResourceId;
550      }
551    }
552
553    public void UpdateSlaveGroup(DT.SlaveGroup dto) {
554      using (var db = CreateContext()) {
555        var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
556        if (entity == null) db.Resources.InsertOnSubmit(DT.Convert.ToEntity(dto));
557        else DT.Convert.ToEntity(dto, entity);
558        db.SubmitChanges();
559      }
560    }
561
562    public void DeleteSlaveGroup(Guid id) {
563      using (var db = CreateContext()) {
564        var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
565        if (entity != null) {
566          if (db.Resources.Where(r => r.ParentResourceId == id).Count() > 0) {
567            throw new InvalidOperationException("Cannot delete SlaveGroup as long as there are Slaves in the group");
568          }
569          db.Resources.DeleteOnSubmit(entity);
570        }
571        db.SubmitChanges();
572      }
573    }
574    #endregion
575
576    #region Resource Methods
577    public DT.Resource GetResource(Guid id) {
578      using (var db = CreateContext()) {
579        return DT.Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
580      }
581    }
582
583    public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
584      using (var db = CreateContext()) {
585        return db.Resources.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
586      }
587    }
588
589    public Guid AddResource(DT.Resource dto) {
590      using (var db = CreateContext()) {
591        var entity = DT.Convert.ToEntity(dto);
592        db.Resources.InsertOnSubmit(entity);
593        db.SubmitChanges();
594        return entity.ResourceId;
595      }
596    }
597
598    public void UpdateResource(DT.Resource dto) {
599      using (var db = CreateContext()) {
600        var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
601        if (entity == null) db.Resources.InsertOnSubmit(DT.Convert.ToEntity(dto));
602        else DT.Convert.ToEntity(dto, entity);
603        db.SubmitChanges();
604      }
605    }
606
607    public void DeleteResource(Guid id) {
608      using (var db = CreateContext()) {
609        var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
610        if (entity != null) db.Resources.DeleteOnSubmit(entity);
611        db.SubmitChanges();
612      }
613    }
614
615    public void AssignJobToResource(Guid jobId, Guid resourceId) {
616      using (var db = CreateContext()) {
617        var job = db.Tasks.Where(x => x.TaskId == jobId).Single();
618        job.AssignedResources.Add(new AssignedResource() { TaskId = jobId, ResourceId = resourceId });
619        db.SubmitChanges();
620      }
621    }
622
623    public IEnumerable<DT.Resource> GetAssignedResources(Guid jobId) {
624      using (var db = CreateContext()) {
625        var job = db.Tasks.Where(x => x.TaskId == jobId).Single();
626        return job.AssignedResources.Select(x => DT.Convert.ToDto(x.Resource)).ToArray();
627      }
628    }
629
630    /// <summary>
631    /// Returns all parent resources of a resource (the given resource is also added)
632    /// </summary>
633    public IEnumerable<DT.Resource> GetParentResources(Guid resourceId) {
634      using (var db = CreateContext()) {
635        var resources = new List<Resource>();
636        CollectParentResources(resources, db.Resources.Where(r => r.ResourceId == resourceId).Single());
637        return resources.Select(r => DT.Convert.ToDto(r)).ToArray();
638      }
639    }
640
641    private void CollectParentResources(List<Resource> resources, Resource resource) {
642      if (resource == null) return;
643      resources.Add(resource);
644      CollectParentResources(resources, resource.ParentResource);
645    }
646
647    /// <summary>
648    /// Returns all child resources of a resource (without the given resource)
649    /// </summary>
650    public IEnumerable<DT.Resource> GetChildResources(Guid resourceId) {
651      using (var db = CreateContext()) {
[9025]652        return CollectChildResources(resourceId, db);
[6983]653      }
654    }
655
[9025]656    public IEnumerable<DT.Resource> CollectChildResources(Guid resourceId, HiveDataContext db) {
657      var childs = new List<DT.Resource>();
658      foreach (var child in db.Resources.Where(x => x.ParentResourceId == resourceId)) {
659        childs.Add(DT.Convert.ToDto(child));
660        childs.AddRange(CollectChildResources(child.ResourceId, db));
661      }
662      return childs;
663    }
664
[6983]665    public IEnumerable<DT.Task> GetJobsByResourceId(Guid resourceId) {
666      using (var db = CreateContext()) {
667        var resources = GetChildResources(resourceId).Select(x => x.Id).ToList();
668        resources.Add(resourceId);
669
670        var jobs = db.Tasks.Where(j =>
671          j.State == TaskState.Calculating &&
672          j.StateLogs.OrderByDescending(x => x.DateTime).First().SlaveId.HasValue &&
673          resources.Contains(j.StateLogs.OrderByDescending(x => x.DateTime).First().SlaveId.Value));
674        return jobs.Select(j => DT.Convert.ToDto(j)).ToArray();
675      }
676    }
677    #endregion
678
[7916]679    #region ResourcePermission Methods
680    public DT.ResourcePermission GetResourcePermission(Guid resourceId, Guid grantedUserId) {
681      using (var db = CreateContext()) {
682        return DT.Convert.ToDto(db.ResourcePermissions.SingleOrDefault(x => x.ResourceId == resourceId && x.GrantedUserId == grantedUserId));
683      }
684    }
685
686    public IEnumerable<DT.ResourcePermission> GetResourcePermissions(Expression<Func<ResourcePermission, bool>> predicate) {
687      using (var db = CreateContext()) {
688        return db.ResourcePermissions.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
689      }
690    }
691
692    public void AddResourcePermission(DT.ResourcePermission dto) {
693      using (var db = CreateContext()) {
694        var entity = db.ResourcePermissions.SingleOrDefault(x => x.ResourceId == dto.ResourceId && x.GrantedUserId == dto.GrantedUserId);
695        if (entity == null) { db.ResourcePermissions.InsertOnSubmit(DT.Convert.ToEntity(dto)); db.SubmitChanges(); }
696      }
697    }
698
699    public void UpdateResourcePermission(DT.ResourcePermission dto) {
700      using (var db = CreateContext()) {
701        var entity = db.ResourcePermissions.FirstOrDefault(x => x.ResourceId == dto.ResourceId && x.GrantedUserId == dto.GrantedUserId);
702        if (entity == null) db.ResourcePermissions.InsertOnSubmit(DT.Convert.ToEntity(dto));
703        else DT.Convert.ToEntity(dto, entity);
704        db.SubmitChanges();
705      }
706    }
707
708    public void DeleteResourcePermission(Guid resourceId, Guid grantedUserId) {
709      using (var db = CreateContext()) {
710        var entity = db.ResourcePermissions.FirstOrDefault(x => x.ResourceId == resourceId && x.GrantedUserId == grantedUserId);
711        if (entity != null) db.ResourcePermissions.DeleteOnSubmit(entity);
712        db.SubmitChanges();
713      }
714    }
715    #endregion
716
[6983]717    #region Authorization Methods
718    public Permission GetPermissionForTask(Guid taskId, Guid userId) {
719      using (var db = CreateContext()) {
720        return GetPermissionForJob(GetJobForTask(taskId), userId);
721      }
722    }
723
724    public Permission GetPermissionForJob(Guid jobId, Guid userId) {
725      using (var db = CreateContext()) {
726        Job job = db.Jobs.SingleOrDefault(x => x.JobId == jobId);
727        if (job == null) return Permission.NotAllowed;
728        if (job.OwnerUserId == userId) return Permission.Full;
729        JobPermission permission = db.JobPermissions.SingleOrDefault(p => p.JobId == jobId && p.GrantedUserId == userId);
730        return permission != null ? permission.Permission : Permission.NotAllowed;
731      }
732    }
733
734    public Guid GetJobForTask(Guid taskId) {
735      using (var db = CreateContext()) {
736        return db.Tasks.Single(j => j.TaskId == taskId).JobId;
737      }
738    }
739    #endregion
740
741    #region Lifecycle Methods
742    public DateTime GetLastCleanup() {
743      using (var db = CreateContext()) {
744        var entity = db.Lifecycles.SingleOrDefault();
745        return entity != null ? entity.LastCleanup : DateTime.MinValue;
746      }
747    }
748
749    public void SetLastCleanup(DateTime datetime) {
750      using (var db = CreateContext()) {
751        var entity = db.Lifecycles.SingleOrDefault();
752        if (entity != null) {
753          entity.LastCleanup = datetime;
754        } else {
755          entity = new Lifecycle();
756          entity.LifecycleId = 0; // always only one entry with ID:0
757          entity.LastCleanup = datetime;
758          db.Lifecycles.InsertOnSubmit(entity);
759        }
760        db.SubmitChanges();
761      }
762    }
763    #endregion
764
765    #region Downtime Methods
766    public DT.Downtime GetDowntime(Guid id) {
767      using (var db = CreateContext()) {
768        return DT.Convert.ToDto(db.Downtimes.SingleOrDefault(x => x.DowntimeId == id));
769      }
770    }
771
772    public IEnumerable<DT.Downtime> GetDowntimes(Expression<Func<Downtime, bool>> predicate) {
773      using (var db = CreateContext()) {
774        return db.Downtimes.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
775      }
776    }
777
778    public Guid AddDowntime(DT.Downtime dto) {
779      using (var db = CreateContext()) {
780        var entity = DT.Convert.ToEntity(dto);
781        db.Downtimes.InsertOnSubmit(entity);
782        db.SubmitChanges();
783        return entity.DowntimeId;
784      }
785    }
786
787    public void UpdateDowntime(DT.Downtime dto) {
788      using (var db = CreateContext()) {
789        var entity = db.Downtimes.FirstOrDefault(x => x.DowntimeId == dto.Id);
790        if (entity == null) db.Downtimes.InsertOnSubmit(DT.Convert.ToEntity(dto));
791        else DT.Convert.ToEntity(dto, entity);
792        db.SubmitChanges();
793      }
794    }
795
796    public void DeleteDowntime(Guid id) {
797      using (var db = CreateContext()) {
798        var entity = db.Downtimes.FirstOrDefault(x => x.DowntimeId == id);
799        if (entity != null) db.Downtimes.DeleteOnSubmit(entity);
800        db.SubmitChanges();
801      }
802    }
803    #endregion
804
805    #region Statistics Methods
806    public DT.Statistics GetStatistic(Guid id) {
807      using (var db = CreateContext()) {
808        return DT.Convert.ToDto(db.Statistics.SingleOrDefault(x => x.StatisticsId == id));
809      }
810    }
811
812    public IEnumerable<DT.Statistics> GetStatistics(Expression<Func<Statistics, bool>> predicate) {
813      using (var db = CreateContext()) {
814        return db.Statistics.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
815      }
816    }
817
818    public Guid AddStatistics(DT.Statistics dto) {
819      using (var db = CreateContext()) {
820        var entity = DT.Convert.ToEntity(dto);
821        db.Statistics.InsertOnSubmit(entity);
822        db.SubmitChanges();
823        foreach (var slaveStat in dto.SlaveStatistics) {
824          slaveStat.Id = entity.StatisticsId;
825          db.SlaveStatistics.InsertOnSubmit(DT.Convert.ToEntity(slaveStat));
826        }
827        if (dto.UserStatistics != null) {
828          foreach (var userStat in dto.UserStatistics) {
829            userStat.Id = entity.StatisticsId;
830            db.UserStatistics.InsertOnSubmit(DT.Convert.ToEntity(userStat));
831          }
832        }
833        db.SubmitChanges();
834        return entity.StatisticsId;
835      }
836    }
837
838    public void DeleteStatistics(Guid id) {
839      using (var db = CreateContext()) {
840        var entity = db.Statistics.FirstOrDefault(x => x.StatisticsId == id);
841        if (entity != null) db.Statistics.DeleteOnSubmit(entity);
842        db.SubmitChanges();
843      }
844    }
845
[9022]846    public Dictionary<Guid, int> GetWaitingTasksByUser() {
847      using (var db = CreateContext()) {
848        var waitingTasksByUser = from task in db.Tasks
849                                 where task.State == TaskState.Waiting
850                                 group task by task.Job.OwnerUserId into g
851                                 select new { UserId = g.Key, UsedCores = g.Count() };
852        return waitingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores);
853      }
854    }
855
[9033]856    public Dictionary<Guid, int> GetWaitingTasksByUserForResources(List<Guid> resourceIds) {
857      using (var db = CreateContext()) {
858        var waitingTasksByUser = from task in db.Tasks
859                                 where task.State == TaskState.Waiting && task.AssignedResources.Any(x => resourceIds.Contains(x.ResourceId))
860                                 group task by task.Job.OwnerUserId into g
861                                 select new { UserId = g.Key, UsedCores = g.Count() };
862        return waitingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores);
863      }
864    }
865
[9022]866    public Dictionary<Guid, int> GetCalculatingTasksByUser() {
867      using (var db = CreateContext()) {
868        var calculatingTasksByUser = from task in db.Tasks
[9025]869                                     where task.State == TaskState.Calculating
870                                     group task by task.Job.OwnerUserId into g
871                                     select new { UserId = g.Key, UsedCores = g.Count() };
[9022]872        return calculatingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores);
873      }
874    }
875
[9033]876    public Dictionary<Guid, int> GetCalculatingTasksByUserForResources(List<Guid> resourceIds) {
877      using (var db = CreateContext()) {
878        var calculatingTasksByUser = from task in db.Tasks
879                                     where task.State == TaskState.Calculating && task.AssignedResources.Any(x => resourceIds.Contains(x.ResourceId))
880                                     group task by task.Job.OwnerUserId into g
881                                     select new { UserId = g.Key, UsedCores = g.Count() };
882        return calculatingTasksByUser.ToDictionary(x => x.UserId, x => x.UsedCores);
883      }
884    }
885
[6983]886    public List<DT.UserStatistics> GetUserStatistics() {
887      using (var db = CreateContext()) {
888        var userStats = new Dictionary<Guid, DT.UserStatistics>();
889
890        var usedCoresByUser = from job in db.Tasks
891                              where job.State == TaskState.Calculating
892                              group job by job.Job.OwnerUserId into g
893                              select new { UserId = g.Key, UsedCores = g.Count() };
894
895        foreach (var item in usedCoresByUser) {
896          if (!userStats.ContainsKey(item.UserId)) {
897            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
898          }
899          userStats[item.UserId].UsedCores += item.UsedCores;
900        }
901
902        var executionTimesByUser = from task in db.Tasks
903                                   group task by task.Job.OwnerUserId into g
904                                   select new { UserId = g.Key, ExecutionTime = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMs).Sum()) };
905        foreach (var item in executionTimesByUser) {
906          if (!userStats.ContainsKey(item.UserId)) {
907            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
908          }
909          userStats[item.UserId].ExecutionTime += item.ExecutionTime;
910        }
911
912        // execution times only of finished task - necessary to compute efficieny
913        var executionTimesFinishedJobs = from job in db.Tasks
914                                         where job.State == TaskState.Finished
915                                         group job by job.Job.OwnerUserId into g
916                                         select new { UserId = g.Key, ExecutionTimeFinishedJobs = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMs).Sum()) };
917
918        foreach (var item in executionTimesFinishedJobs) {
919          if (!userStats.ContainsKey(item.UserId)) {
920            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
921          }
922          userStats[item.UserId].ExecutionTimeFinishedJobs += item.ExecutionTimeFinishedJobs;
923        }
924
925        // start to end times only of finished task - necessary to compute efficiency
926        var startToEndTimesFinishedJobs = from job in db.Tasks
927                                          where job.State == TaskState.Finished
928                                          group job by job.Job.OwnerUserId into g
929                                          select new {
930                                            UserId = g.Key,
931                                            StartToEndTime = new TimeSpan(g.Select(x => x.StateLogs.OrderByDescending(sl => sl.DateTime).First().DateTime - x.StateLogs.OrderBy(sl => sl.DateTime).First().DateTime).Sum(ts => ts.Ticks))
932                                          };
933        foreach (var item in startToEndTimesFinishedJobs) {
934          if (!userStats.ContainsKey(item.UserId)) {
935            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
936          }
937          userStats[item.UserId].StartToEndTime += item.StartToEndTime;
938        }
939
940        // also consider executiontimes of DeletedJobStats
941        var deletedJobsExecutionTimesByUsers = from del in db.DeletedJobStatistics
942                                               group del by del.UserId into g
943                                               select new {
944                                                 UserId = g.Key,
945                                                 ExecutionTime = TimeSpan.FromSeconds(g.Select(x => x.ExecutionTimeS).Sum()),
946                                                 ExecutionTimeFinishedJobs = TimeSpan.FromSeconds(g.Select(x => x.ExecutionTimeSFinishedJobs).Sum()),
947                                                 StartToEndTime = TimeSpan.FromSeconds(g.Select(x => x.StartToEndTimeS).Sum())
948                                               };
949        foreach (var item in deletedJobsExecutionTimesByUsers) {
950          if (!userStats.ContainsKey(item.UserId)) {
951            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
952          }
953          userStats[item.UserId].ExecutionTime += item.ExecutionTime;
954          userStats[item.UserId].ExecutionTimeFinishedJobs += item.ExecutionTimeFinishedJobs;
955          userStats[item.UserId].StartToEndTime += item.StartToEndTime;
956        }
957
958        return userStats.Values.ToList();
959      }
960    }
961    #endregion
962
[9123]963    #region UserPriority Methods
964    public IEnumerable<DT.UserPriority> GetUserPriorities(Expression<Func<UserPriority, bool>> predicate) {
965      using (var db = CreateContext()) {
966        return db.UserPriorities.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
967      }
968    }
969
970    public void EnqueueUserPriority(DT.UserPriority dto) {
971      using (var db = CreateContext()) {
972        var entity = db.UserPriorities.FirstOrDefault(x => x.UserId == dto.Id);
973        if (entity == null) db.UserPriorities.InsertOnSubmit(DT.Convert.ToEntity(dto));
974        else DT.Convert.ToEntity(dto, entity);
975        db.SubmitChanges();
976      }
977    }
978    #endregion
979
[6983]980    #region Helpers
981    private void CollectChildTasks(HiveDataContext db, Guid parentTaskId, List<Task> collection) {
982      var tasks = db.Tasks.Where(j => j.ParentTaskId == parentTaskId);
983      foreach (var task in tasks) {
984        collection.Add(task);
985        if (task.IsParentTask)
986          CollectChildTasks(db, task.TaskId, collection);
987      }
988    }
989    #endregion
990  }
991}
Note: See TracBrowser for help on using the repository browser.