Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.Hive/3.3/HiveDao.cs @ 7403

Last change on this file since 7403 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 33.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.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);
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
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();
130        return waitingTasks;
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
615    #region Authorization Methods
616    public Permission GetPermissionForTask(Guid taskId, Guid userId) {
617      using (var db = CreateContext()) {
618        return GetPermissionForJob(GetJobForTask(taskId), userId);
619      }
620    }
621
622    public Permission GetPermissionForJob(Guid jobId, Guid userId) {
623      using (var db = CreateContext()) {
624        Job job = db.Jobs.SingleOrDefault(x => x.JobId == jobId);
625        if (job == null) return Permission.NotAllowed;
626        if (job.OwnerUserId == userId) return Permission.Full;
627        JobPermission permission = db.JobPermissions.SingleOrDefault(p => p.JobId == jobId && p.GrantedUserId == userId);
628        return permission != null ? permission.Permission : Permission.NotAllowed;
629      }
630    }
631
632    public Guid GetJobForTask(Guid taskId) {
633      using (var db = CreateContext()) {
634        return db.Tasks.Single(j => j.TaskId == taskId).JobId;
635      }
636    }
637    #endregion
638
639    #region Lifecycle Methods
640    public DateTime GetLastCleanup() {
641      using (var db = CreateContext()) {
642        var entity = db.Lifecycles.SingleOrDefault();
643        return entity != null ? entity.LastCleanup : DateTime.MinValue;
644      }
645    }
646
647    public void SetLastCleanup(DateTime datetime) {
648      using (var db = CreateContext()) {
649        var entity = db.Lifecycles.SingleOrDefault();
650        if (entity != null) {
651          entity.LastCleanup = datetime;
652        } else {
653          entity = new Lifecycle();
654          entity.LifecycleId = 0; // always only one entry with ID:0
655          entity.LastCleanup = datetime;
656          db.Lifecycles.InsertOnSubmit(entity);
657        }
658        db.SubmitChanges();
659      }
660    }
661    #endregion
662
663    #region Downtime Methods
664    public DT.Downtime GetDowntime(Guid id) {
665      using (var db = CreateContext()) {
666        return DT.Convert.ToDto(db.Downtimes.SingleOrDefault(x => x.DowntimeId == id));
667      }
668    }
669
670    public IEnumerable<DT.Downtime> GetDowntimes(Expression<Func<Downtime, bool>> predicate) {
671      using (var db = CreateContext()) {
672        return db.Downtimes.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
673      }
674    }
675
676    public Guid AddDowntime(DT.Downtime dto) {
677      using (var db = CreateContext()) {
678        var entity = DT.Convert.ToEntity(dto);
679        db.Downtimes.InsertOnSubmit(entity);
680        db.SubmitChanges();
681        return entity.DowntimeId;
682      }
683    }
684
685    public void UpdateDowntime(DT.Downtime dto) {
686      using (var db = CreateContext()) {
687        var entity = db.Downtimes.FirstOrDefault(x => x.DowntimeId == dto.Id);
688        if (entity == null) db.Downtimes.InsertOnSubmit(DT.Convert.ToEntity(dto));
689        else DT.Convert.ToEntity(dto, entity);
690        db.SubmitChanges();
691      }
692    }
693
694    public void DeleteDowntime(Guid id) {
695      using (var db = CreateContext()) {
696        var entity = db.Downtimes.FirstOrDefault(x => x.DowntimeId == id);
697        if (entity != null) db.Downtimes.DeleteOnSubmit(entity);
698        db.SubmitChanges();
699      }
700    }
701    #endregion
702
703    #region Statistics Methods
704    public DT.Statistics GetStatistic(Guid id) {
705      using (var db = CreateContext()) {
706        return DT.Convert.ToDto(db.Statistics.SingleOrDefault(x => x.StatisticsId == id));
707      }
708    }
709
710    public IEnumerable<DT.Statistics> GetStatistics(Expression<Func<Statistics, bool>> predicate) {
711      using (var db = CreateContext()) {
712        return db.Statistics.Where(predicate).Select(x => DT.Convert.ToDto(x)).ToArray();
713      }
714    }
715
716    public Guid AddStatistics(DT.Statistics dto) {
717      using (var db = CreateContext()) {
718        var entity = DT.Convert.ToEntity(dto);
719        db.Statistics.InsertOnSubmit(entity);
720        db.SubmitChanges();
721        foreach (var slaveStat in dto.SlaveStatistics) {
722          slaveStat.Id = entity.StatisticsId;
723          db.SlaveStatistics.InsertOnSubmit(DT.Convert.ToEntity(slaveStat));
724        }
725        if (dto.UserStatistics != null) {
726          foreach (var userStat in dto.UserStatistics) {
727            userStat.Id = entity.StatisticsId;
728            db.UserStatistics.InsertOnSubmit(DT.Convert.ToEntity(userStat));
729          }
730        }
731        db.SubmitChanges();
732        return entity.StatisticsId;
733      }
734    }
735
736    public void DeleteStatistics(Guid id) {
737      using (var db = CreateContext()) {
738        var entity = db.Statistics.FirstOrDefault(x => x.StatisticsId == id);
739        if (entity != null) db.Statistics.DeleteOnSubmit(entity);
740        db.SubmitChanges();
741      }
742    }
743
744    public List<DT.UserStatistics> GetUserStatistics() {
745      using (var db = CreateContext()) {
746        var userStats = new Dictionary<Guid, DT.UserStatistics>();
747
748        var usedCoresByUser = from job in db.Tasks
749                              where job.State == TaskState.Calculating
750                              group job by job.Job.OwnerUserId into g
751                              select new { UserId = g.Key, UsedCores = g.Count() };
752
753        foreach (var item in usedCoresByUser) {
754          if (!userStats.ContainsKey(item.UserId)) {
755            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
756          }
757          userStats[item.UserId].UsedCores += item.UsedCores;
758        }
759
760        var executionTimesByUser = from task in db.Tasks
761                                   group task by task.Job.OwnerUserId into g
762                                   select new { UserId = g.Key, ExecutionTime = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMs).Sum()) };
763        foreach (var item in executionTimesByUser) {
764          if (!userStats.ContainsKey(item.UserId)) {
765            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
766          }
767          userStats[item.UserId].ExecutionTime += item.ExecutionTime;
768        }
769
770        // execution times only of finished task - necessary to compute efficieny
771        var executionTimesFinishedJobs = from job in db.Tasks
772                                         where job.State == TaskState.Finished
773                                         group job by job.Job.OwnerUserId into g
774                                         select new { UserId = g.Key, ExecutionTimeFinishedJobs = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMs).Sum()) };
775
776        foreach (var item in executionTimesFinishedJobs) {
777          if (!userStats.ContainsKey(item.UserId)) {
778            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
779          }
780          userStats[item.UserId].ExecutionTimeFinishedJobs += item.ExecutionTimeFinishedJobs;
781        }
782
783        // start to end times only of finished task - necessary to compute efficiency
784        var startToEndTimesFinishedJobs = from job in db.Tasks
785                                          where job.State == TaskState.Finished
786                                          group job by job.Job.OwnerUserId into g
787                                          select new {
788                                            UserId = g.Key,
789                                            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))
790                                          };
791        foreach (var item in startToEndTimesFinishedJobs) {
792          if (!userStats.ContainsKey(item.UserId)) {
793            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
794          }
795          userStats[item.UserId].StartToEndTime += item.StartToEndTime;
796        }
797
798        // also consider executiontimes of DeletedJobStats
799        var deletedJobsExecutionTimesByUsers = from del in db.DeletedJobStatistics
800                                               group del by del.UserId into g
801                                               select new {
802                                                 UserId = g.Key,
803                                                 ExecutionTime = TimeSpan.FromSeconds(g.Select(x => x.ExecutionTimeS).Sum()),
804                                                 ExecutionTimeFinishedJobs = TimeSpan.FromSeconds(g.Select(x => x.ExecutionTimeSFinishedJobs).Sum()),
805                                                 StartToEndTime = TimeSpan.FromSeconds(g.Select(x => x.StartToEndTimeS).Sum())
806                                               };
807        foreach (var item in deletedJobsExecutionTimesByUsers) {
808          if (!userStats.ContainsKey(item.UserId)) {
809            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
810          }
811          userStats[item.UserId].ExecutionTime += item.ExecutionTime;
812          userStats[item.UserId].ExecutionTimeFinishedJobs += item.ExecutionTimeFinishedJobs;
813          userStats[item.UserId].StartToEndTime += item.StartToEndTime;
814        }
815
816        return userStats.Values.ToList();
817      }
818    }
819    #endregion
820
821    #region Helpers
822    private void CollectChildTasks(HiveDataContext db, Guid parentTaskId, List<Task> collection) {
823      var tasks = db.Tasks.Where(j => j.ParentTaskId == parentTaskId);
824      foreach (var task in tasks) {
825        collection.Add(task);
826        if (task.IsParentTask)
827          CollectChildTasks(db, task.TaskId, collection);
828      }
829    }
830    #endregion
831  }
832}
Note: See TracBrowser for help on using the repository browser.