Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDao.cs @ 12681

Last change on this file since 12681 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 33.6 KB
RevLine 
[6983]1#region License Information
2/* HeuristicLab
[12009]3 * Copyright (C) 2002-2015 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 HeuristicLab.Services.Hive.Common.DataTransfer;
27using DT = HeuristicLab.Services.Hive.Common.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    public HiveDao() { }
38
39    #region Job Methods
40    public DT.Job GetJob(Guid id) {
41      using (var db = CreateContext()) {
42        return Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id));
43      }
44    }
45
46    public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
47      using (var db = CreateContext()) {
48        return db.Jobs.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
49      }
50    }
51
52    public Guid AddJob(DT.Job dto) {
53      using (var db = CreateContext()) {
54        var entity = Convert.ToEntity(dto);
55        db.Jobs.InsertOnSubmit(entity);
56        db.SubmitChanges();
57        foreach (Guid pluginId in dto.PluginsNeededIds) {
58          db.RequiredPlugins.InsertOnSubmit(new RequiredPlugin() { JobId = entity.JobId, PluginId = pluginId });
59        }
60        db.SubmitChanges();
61        return entity.JobId;
62      }
63    }
64
65    public void UpdateJob(DT.Job dto) {
66      using (var db = CreateContext()) {
67        var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
68        if (entity == null) db.Jobs.InsertOnSubmit(Convert.ToEntity(dto));
69        else Convert.ToEntity(dto, entity);
70        foreach (Guid pluginId in dto.PluginsNeededIds) {
71          if (db.RequiredPlugins.Count(p => p.PluginId == pluginId) == 0) {
72            db.RequiredPlugins.InsertOnSubmit(new RequiredPlugin() { JobId = entity.JobId, PluginId = pluginId });
73          }
74        }
75        db.SubmitChanges();
76      }
77    }
78
79    public void DeleteJob(Guid id) {
80      using (var db = CreateContext()) {
81        var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
82        if (entity != null) db.Jobs.DeleteOnSubmit(entity);
83        db.SubmitChanges(); // JobData and child jobs are deleted by db-trigger
84      }
85    }
86
87    /// <summary>
88    /// returns all parent jobs which are waiting for their child jobs to finish
89    /// </summary>
90    /// <param name="resourceIds">list of resourceids which for which the jobs should be valid</param>
91    /// <param name="count">maximum number of jobs to return</param>
92    /// <param name="finished">if true, all parent jobs which have FinishWhenChildJobsFinished=true are returned, otherwise only FinishWhenChildJobsFinished=false are returned</param>
93    /// <returns></returns>
94    public IEnumerable<DT.Job> GetParentJobs(IEnumerable<Guid> resourceIds, int count, bool finished) {
95      using (var db = CreateContext()) {
96        var query = from ar in db.AssignedResources
97                    where resourceIds.Contains(ar.ResourceId)
98                       && ar.Job.State == JobState.Waiting
99                       && ar.Job.IsParentJob
100                       && (finished ? ar.Job.FinishWhenChildJobsFinished : !ar.Job.FinishWhenChildJobsFinished)
101                       && (from child in db.Jobs
102                           where child.ParentJobId == ar.Job.JobId
103                           select child.State == JobState.Finished
104                               || child.State == JobState.Aborted
105                               || child.State == JobState.Failed).All(x => x)
106                       && (from child in db.Jobs // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
107                           where child.ParentJobId == ar.Job.JobId
108                           select child).Count() > 0
109                    orderby ar.Job.Priority descending, db.Random()
110                    select Convert.ToDto(ar.Job);
111        return count == 0 ? query.ToArray() : query.Take(count).ToArray();
112      }
113    }
114
115    public IEnumerable<DT.Job> GetWaitingJobs(DT.Slave slave, int count) {
116      using (var db = CreateContext()) {
117        var resourceIds = GetParentResources(slave.Id).Select(r => r.Id);
118        var waitingParentJobs = GetParentJobs(resourceIds, count, false);
119        if (count > 0 && waitingParentJobs.Count() >= count) return waitingParentJobs.Take(count).ToArray();
120
121        var query = from ar in db.AssignedResources
122                    where resourceIds.Contains(ar.ResourceId)
123                       && !(ar.Job.IsParentJob && ar.Job.FinishWhenChildJobsFinished)
124                       && ar.Job.State == JobState.Waiting
125                       && ar.Job.CoresNeeded <= slave.FreeCores
126                       && ar.Job.MemoryNeeded <= slave.FreeMemory
127                    orderby ar.Job.Priority descending, db.Random() // take random job to avoid the race condition that occurs when this method is called concurrently (the same job would be returned)
128                    select Convert.ToDto(ar.Job);
129        var waitingJobs = (count == 0 ? query : query.Take(count)).ToArray();
130        return waitingJobs.Union(waitingParentJobs).OrderByDescending(x => x.Priority);
131      }
132    }
133
134    public DT.Job UpdateJobState(Guid jobId, JobState jobState, Guid? slaveId, Guid? userId, string exception) {
135      using (var db = CreateContext()) {
136        var job = db.Jobs.SingleOrDefault(x => x.JobId == jobId);
137        job.State = jobState;
138        db.StateLogs.InsertOnSubmit(new StateLog {
139          JobId = jobId,
140          State = jobState,
141          SlaveId = slaveId,
142          UserId = userId,
143          Exception = exception,
144          DateTime = DateTime.Now
145        });
146        db.SubmitChanges();
147        job = db.Jobs.SingleOrDefault(x => x.JobId == jobId);
148        return Convert.ToDto(job);
149      }
150    }
151    #endregion
152
153    #region JobData Methods
154    public DT.JobData GetJobData(Guid id) {
155      using (var db = CreateContext(true)) {
156        return Convert.ToDto(db.JobDatas.SingleOrDefault(x => x.JobId == id));
157      }
158    }
159
160    public IEnumerable<DT.JobData> GetJobDatas(Expression<Func<JobData, bool>> predicate) {
161      using (var db = CreateContext(true)) {
162        return db.JobDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
163      }
164    }
165
166    public Guid AddJobData(DT.JobData dto) {
167      using (var db = CreateContext(true)) {
168        var entity = Convert.ToEntity(dto);
169        db.JobDatas.InsertOnSubmit(entity);
170        db.SubmitChanges();
171        return entity.JobId;
172      }
173    }
174
175    public void UpdateJobData(DT.JobData dto) {
176      using (var db = CreateContext(true)) {
177        var entity = db.JobDatas.FirstOrDefault(x => x.JobId == dto.JobId);
178        if (entity == null) db.JobDatas.InsertOnSubmit(Convert.ToEntity(dto));
179        else Convert.ToEntity(dto, entity);
180        db.SubmitChanges();
181      }
182    }
183
184    public void DeleteJobData(Guid id) {
185      using (var db = CreateContext()) {
186        var entity = db.JobDatas.FirstOrDefault(x => x.JobId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
187        if (entity != null) db.JobDatas.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 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 => Convert.ToDto(x)).ToArray();
203      }
204    }
205
206    public Guid AddStateLog(DT.StateLog dto) {
207      using (var db = CreateContext()) {
208        var entity = 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(Convert.ToEntity(dto));
219        else 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 HiveExperiment Methods
234    public DT.HiveExperiment GetHiveExperiment(Guid id) {
235      using (var db = CreateContext()) {
236        return AddStatsToExperiment(db, Convert.ToDto(db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == id)));
237      }
238    }
239
240    private DT.HiveExperiment AddStatsToExperiment(HiveDataContext db, DT.HiveExperiment exp) {
241      if (exp == null)
242        return null;
243
244      var jobs = db.Jobs.Where(j => j.HiveExperimentId == exp.Id);
245      exp.JobCount = jobs.Count();
246      exp.CalculatingCount = jobs.Count(j => j.State == JobState.Calculating);
247      exp.FinishedCount = jobs.Count(j => j.State == JobState.Finished);
248      return exp;
249    }
250
251    public IEnumerable<DT.HiveExperiment> GetHiveExperiments(Expression<Func<HiveExperiment, bool>> predicate) {
252      using (var db = CreateContext()) {
253        return db.HiveExperiments.Where(predicate).Select(x => AddStatsToExperiment(db, Convert.ToDto(x))).ToArray();
254      }
255    }
256
257    public Guid AddHiveExperiment(DT.HiveExperiment dto) {
258      using (var db = CreateContext()) {
259        var entity = Convert.ToEntity(dto);
260        db.HiveExperiments.InsertOnSubmit(entity);
261        db.SubmitChanges();
262        return entity.HiveExperimentId;
263      }
264    }
265
266    public void UpdateHiveExperiment(DT.HiveExperiment dto) {
267      using (var db = CreateContext()) {
268        var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == dto.Id);
269        if (entity == null) db.HiveExperiments.InsertOnSubmit(Convert.ToEntity(dto));
270        else Convert.ToEntity(dto, entity);
271        db.SubmitChanges();
272      }
273    }
274
275    public void DeleteHiveExperiment(Guid id) {
276      using (var db = CreateContext()) {
277        var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == id);
278        if (entity != null) db.HiveExperiments.DeleteOnSubmit(entity);
279        db.SubmitChanges();
280      }
281    }
282    #endregion
283
284    #region HiveExperimentPermission Methods
285    public DT.HiveExperimentPermission GetHiveExperimentPermission(Guid hiveExperimentId, Guid grantedUserId) {
286      using (var db = CreateContext()) {
287        return Convert.ToDto(db.HiveExperimentPermissions.SingleOrDefault(x => x.HiveExperimentId == hiveExperimentId && x.GrantedUserId == grantedUserId));
288      }
289    }
290
291    public IEnumerable<DT.HiveExperimentPermission> GetHiveExperimentPermissions(Expression<Func<HiveExperimentPermission, bool>> predicate) {
292      using (var db = CreateContext()) {
293        return db.HiveExperimentPermissions.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
294      }
295    }
296
297    public void AddHiveExperimentPermission(DT.HiveExperimentPermission dto) {
298      using (var db = CreateContext()) {
299        var entity = Convert.ToEntity(dto);
300        db.HiveExperimentPermissions.InsertOnSubmit(entity);
301        db.SubmitChanges();
302      }
303    }
304
305    public void UpdateHiveExperimentPermission(DT.HiveExperimentPermission dto) {
306      using (var db = CreateContext()) {
307        var entity = db.HiveExperimentPermissions.FirstOrDefault(x => x.HiveExperimentId == dto.HiveExperimentId && x.GrantedUserId == dto.GrantedUserId);
308        if (entity == null) db.HiveExperimentPermissions.InsertOnSubmit(Convert.ToEntity(dto));
309        else Convert.ToEntity(dto, entity);
310        db.SubmitChanges();
311      }
312    }
313
314    public void DeleteHiveExperimentPermission(Guid hiveExperimentId, Guid grantedUserId) {
315      using (var db = CreateContext()) {
316        var entity = db.HiveExperimentPermissions.FirstOrDefault(x => x.HiveExperimentId == hiveExperimentId && x.GrantedUserId == grantedUserId);
317        if (entity != null) db.HiveExperimentPermissions.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 SetHiveExperimentPermission(Guid hiveExperimentId, Guid grantedByUserId, Guid grantedUserId, Permission permission) {
326      using (var db = CreateContext()) {
327        HiveExperimentPermission hiveExperimentPermission = db.HiveExperimentPermissions.SingleOrDefault(x => x.HiveExperimentId == hiveExperimentId && x.GrantedUserId == grantedUserId);
328        if (hiveExperimentPermission != null) {
329          if (permission == Permission.NotAllowed) {
330            // not allowed, delete
331            db.HiveExperimentPermissions.DeleteOnSubmit(hiveExperimentPermission);
332          } else {
333            // update
334            hiveExperimentPermission.Permission = permission;
335            hiveExperimentPermission.GrantedByUserId = grantedByUserId; // update grantedByUserId, always the last "granter" is stored
336          }
337        } else {
338          // insert
339          if (permission != Permission.NotAllowed) {
340            hiveExperimentPermission = new HiveExperimentPermission() { HiveExperimentId = hiveExperimentId, GrantedByUserId = grantedByUserId, GrantedUserId = grantedUserId, Permission = permission };
341            db.HiveExperimentPermissions.InsertOnSubmit(hiveExperimentPermission);
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 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 => Convert.ToDto(x)).ToArray();
359      }
360    }
361
362    public Guid AddPlugin(DT.Plugin dto) {
363      using (var db = CreateContext()) {
364        var entity = 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(Convert.ToEntity(dto));
375        else 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 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 => Convert.ToDto(x)).ToArray();
399      }
400    }
401
402    public Guid AddPluginData(DT.PluginData dto) {
403      using (var db = CreateContext()) {
404        var entity = 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(Convert.ToEntity(dto));
415        else 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 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 => Convert.ToDto(x)).ToArray();
439      }
440    }
441
442    public Guid AddSlave(DT.Slave dto) {
443      using (var db = CreateContext()) {
444        var entity = 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(Convert.ToEntity(dto));
455        else 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 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 => 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 = 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(Convert.ToEntity(dto));
497        else 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 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 => Convert.ToDto(x)).ToArray();
526      }
527    }
528
529    public Guid AddResource(DT.Resource dto) {
530      using (var db = CreateContext()) {
531        var entity = 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(Convert.ToEntity(dto));
542        else 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.Jobs.Where(x => x.JobId == jobId).Single();
558        job.AssignedResources.Add(new AssignedResource() { JobId = 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.Jobs.Where(x => x.JobId == jobId).Single();
566        return job.AssignedResources.Select(x => 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 => 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(Convert.ToDto(child));
595          childs.AddRange(GetChildResources(child.ResourceId));
596        }
597        return childs;
598      }
599    }
600
601    public IEnumerable<DT.Job> 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.Jobs.Where(j =>
607          j.State == JobState.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 => Convert.ToDto(j)).ToArray();
611      }
612    }
613    #endregion
614
615    #region Authorization Methods
616    public Permission GetPermissionForJob(Guid jobId, Guid userId) {
617      using (var db = CreateContext()) {
618        return GetPermissionForExperiment(GetExperimentForJob(jobId), userId);
619      }
620    }
621
622    public Permission GetPermissionForExperiment(Guid experimentId, Guid userId) {
623      using (var db = CreateContext()) {
624        HiveExperiment hiveExperiment = db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == experimentId);
625        if (hiveExperiment == null) return Permission.NotAllowed;
626        if (hiveExperiment.OwnerUserId == userId) return Permission.Full;
627        HiveExperimentPermission permission = db.HiveExperimentPermissions.SingleOrDefault(p => p.HiveExperimentId == experimentId && p.GrantedUserId == userId);
628        return permission != null ? permission.Permission : Permission.NotAllowed;
629      }
630    }
631
632    public Guid GetExperimentForJob(Guid jobId) {
633      using (var db = CreateContext()) {
634        return db.Jobs.Single(j => j.JobId == jobId).HiveExperimentId;
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 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 => Convert.ToDto(x)).ToArray();
673      }
674    }
675
676    public Guid AddDowntime(DT.Downtime dto) {
677      using (var db = CreateContext()) {
678        var entity = 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(Convert.ToEntity(dto));
689        else 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 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 => Convert.ToDto(x)).ToArray();
713      }
714    }
715
716    public Guid AddStatistics(DT.Statistics dto) {
717      using (var db = CreateContext()) {
718        var entity = 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(Convert.ToEntity(slaveStat));
724        }
725        foreach (var userStat in dto.UserStatistics) {
726          userStat.Id = entity.StatisticsId;
727          db.UserStatistics.InsertOnSubmit(Convert.ToEntity(userStat));
728        }
729        db.SubmitChanges();
730        return entity.StatisticsId;
731      }
732    }
733
734    public void DeleteStatistics(Guid id) {
735      using (var db = CreateContext()) {
736        var entity = db.Statistics.FirstOrDefault(x => x.StatisticsId == id);
737        if (entity != null) db.Statistics.DeleteOnSubmit(entity);
738        db.SubmitChanges();
739      }
740    }
741
742    public List<DT.UserStatistics> GetUserStatistics() {
743      using (var db = CreateContext()) {
744        var userStats = new Dictionary<Guid, DT.UserStatistics>();
745
746        var usedCoresByUser = from job in db.Jobs
747                              where job.State == JobState.Calculating
748                              group job by job.HiveExperiment.OwnerUserId into g
749                              select new { UserId = g.Key, UsedCores = g.Count() };
750
751        foreach (var item in usedCoresByUser) {
752          if (!userStats.ContainsKey(item.UserId)) {
753            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
754          }
755          userStats[item.UserId].UsedCores += item.UsedCores;
756        }
757
758        var executionTimesByUser = from job in db.Jobs
759                                   group job by job.HiveExperiment.OwnerUserId into g
760                                   select new { UserId = g.Key, ExecutionTime = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMs).Sum()) };
761        foreach (var item in executionTimesByUser) {
762          if (!userStats.ContainsKey(item.UserId)) {
763            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
764          }
765          userStats[item.UserId].ExecutionTime += item.ExecutionTime;
766        }
767
768        // execution times only of finished jobs - necessary to compute efficieny
769        var executionTimesFinishedJobs = from job in db.Jobs
770                                         where job.State == JobState.Finished
771                                         group job by job.HiveExperiment.OwnerUserId into g
772                                         select new { UserId = g.Key, ExecutionTimeFinishedJobs = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMs).Sum()) };
773
774        foreach (var item in executionTimesFinishedJobs) {
775          if (!userStats.ContainsKey(item.UserId)) {
776            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
777          }
778          userStats[item.UserId].ExecutionTimeFinishedJobs += item.ExecutionTimeFinishedJobs;
779        }
780
781        // start to end times only of finished jobs - necessary to compute efficiency
782        var startToEndTimesFinishedJobs = from job in db.Jobs
783                                          where job.State == JobState.Finished
784                                          group job by job.HiveExperiment.OwnerUserId into g
785                                          select new {
786                                            UserId = g.Key,
787                                            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))
788                                          };
789        foreach (var item in startToEndTimesFinishedJobs) {
790          if (!userStats.ContainsKey(item.UserId)) {
791            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
792          }
793          userStats[item.UserId].StartToEndTime += item.StartToEndTime;
794        }
795
796        // also consider executiontimes of DeletedJobStats
797        var deletedJobsExecutionTimesByUsers = from del in db.DeletedJobStatistics
798                                               group del by del.UserId into g
799                                               select new {
800                                                 UserId = g.Key,
801                                                 ExecutionTime = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMs).Sum()),
802                                                 ExecutionTimeFinishedJobs = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMsFinishedJobs).Sum()),
803                                                 StartToEndTime = TimeSpan.FromMilliseconds(g.Select(x => x.StartToEndTimeMs).Sum())
804                                               };
805        foreach (var item in deletedJobsExecutionTimesByUsers) {
806          if (!userStats.ContainsKey(item.UserId)) {
807            userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
808          }
809          userStats[item.UserId].ExecutionTime += item.ExecutionTime;
810          userStats[item.UserId].ExecutionTimeFinishedJobs += item.ExecutionTimeFinishedJobs;
811          userStats[item.UserId].StartToEndTime += item.StartToEndTime;
812        }
813
814        return userStats.Values.ToList();
815      }
816    }
817    #endregion
818
819    #region Helpers
820    private void CollectChildJobs(HiveDataContext db, Guid parentJobId, List<Job> collection) {
821      var jobs = db.Jobs.Where(j => j.ParentJobId == parentJobId);
822      foreach (var job in jobs) {
823        collection.Add(job);
824        if (job.IsParentJob)
825          CollectChildJobs(db, job.JobId, collection);
826      }
827    }
828    #endregion
829  }
830}
Note: See TracBrowser for help on using the repository browser.