Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/HiveDao.cs @ 12614

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