Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Services.Hive/3.3/HiveDao.cs @ 8191

Last change on this file since 8191 was 8085, checked in by gkronber, 12 years ago

#1847: merged trunk changes r7800:HEAD into gp move operators branch

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