1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Linq.Expressions;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Services.Hive.DataAccess {
|
---|
28 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
29 | using HeuristicLab.Services.Hive.DataAccess.Properties;
|
---|
30 | using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
31 |
|
---|
32 | public class HiveDao : IHiveDao {
|
---|
33 | public static HiveDataContext CreateContext() {
|
---|
34 | return new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
|
---|
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 | // todo: update required plugins
|
---|
71 | db.SubmitChanges();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void DeleteJob(Guid id) {
|
---|
76 | using (var db = CreateContext()) {
|
---|
77 | var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
|
---|
78 | if (entity != null) db.Jobs.DeleteOnSubmit(entity);
|
---|
79 | db.SubmitChanges(); // JobData and child jobs are deleted by db-trigger
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | /// <summary>
|
---|
84 | /// returns all parent jobs which are waiting for their child jobs to finish
|
---|
85 | /// </summary>
|
---|
86 | /// <param name="resourceIds">list of resourceids which for which the jobs should be valid</param>
|
---|
87 | /// <param name="count">maximum number of jobs to return</param>
|
---|
88 | /// <param name="finished">if true, all parent jobs which have FinishWhenChildJobsFinished=true are returned, otherwise only FinishWhenChildJobsFinished=false are returned</param>
|
---|
89 | /// <returns></returns>
|
---|
90 | public IEnumerable<DT.Job> GetParentJobs(IEnumerable<Guid> resourceIds, int count, bool finished) {
|
---|
91 | using (var db = CreateContext()) {
|
---|
92 | var query = from ar in db.AssignedResources
|
---|
93 | where resourceIds.Contains(ar.ResourceId)
|
---|
94 | && ar.Job.State == JobState.Waiting
|
---|
95 | && ar.Job.IsParentJob
|
---|
96 | && (finished ? ar.Job.FinishWhenChildJobsFinished : !ar.Job.FinishWhenChildJobsFinished)
|
---|
97 | && (from child in db.Jobs
|
---|
98 | where child.ParentJobId == ar.Job.JobId
|
---|
99 | select child.State == JobState.Finished
|
---|
100 | || child.State == JobState.Aborted
|
---|
101 | || child.State == JobState.Failed).All(x => x)
|
---|
102 | && (from child in db.Jobs // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
|
---|
103 | where child.ParentJobId == ar.Job.JobId
|
---|
104 | select child).Count() > 0
|
---|
105 | orderby ar.Job.Priority descending
|
---|
106 | select Convert.ToDto(ar.Job);
|
---|
107 | return count == 0 ? query.ToArray() : query.Take(count).ToArray();
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public IEnumerable<DT.Job> GetWaitingJobs(DT.Slave slave, int count) {
|
---|
112 | using (var db = CreateContext()) {
|
---|
113 | var resourceIds = GetParentResources(slave.Id).Select(r => r.Id);
|
---|
114 | var waitingParentJobs = GetParentJobs(resourceIds, count, false);
|
---|
115 | if (count > 0 && waitingParentJobs.Count() >= count) return waitingParentJobs.Take(count).ToArray();
|
---|
116 |
|
---|
117 | var query = from ar in db.AssignedResources
|
---|
118 | where resourceIds.Contains(ar.ResourceId)
|
---|
119 | && !(ar.Job.IsParentJob && ar.Job.FinishWhenChildJobsFinished)
|
---|
120 | && ar.Job.State == JobState.Waiting
|
---|
121 | && ar.Job.CoresNeeded <= slave.FreeCores
|
---|
122 | && ar.Job.MemoryNeeded <= slave.FreeMemory
|
---|
123 | orderby ar.Job.Priority descending
|
---|
124 | select Convert.ToDto(ar.Job);
|
---|
125 | var waitingJobs = (count == 0 ? query : query.Take(count)).ToArray();
|
---|
126 | return waitingJobs.Union(waitingParentJobs).OrderByDescending(x => x.Priority);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | public DT.Job UpdateJobState(Guid jobId, JobState jobState, Guid? slaveId, Guid? userId, string exception) {
|
---|
131 | using (var db = CreateContext()) {
|
---|
132 | var job = db.Jobs.SingleOrDefault(x => x.JobId == jobId);
|
---|
133 | job.State = jobState;
|
---|
134 | db.StateLogs.InsertOnSubmit(new StateLog {
|
---|
135 | JobId = jobId,
|
---|
136 | State = jobState,
|
---|
137 | SlaveId = slaveId,
|
---|
138 | UserId = userId,
|
---|
139 | Exception = exception,
|
---|
140 | DateTime = DateTime.Now
|
---|
141 | });
|
---|
142 | db.SubmitChanges();
|
---|
143 | job = db.Jobs.SingleOrDefault(x => x.JobId == jobId);
|
---|
144 | return Convert.ToDto(job);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | #region JobData Methods
|
---|
150 |
|
---|
151 | public DT.JobData GetJobData(Guid id) {
|
---|
152 | using (var db = CreateContext()) {
|
---|
153 | return Convert.ToDto(db.JobDatas.SingleOrDefault(x => x.JobId == id));
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | public IEnumerable<DT.JobData> GetJobDatas(Expression<Func<JobData, bool>> predicate) {
|
---|
158 | using (var db = CreateContext()) {
|
---|
159 | return db.JobDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | public Guid AddJobData(DT.JobData dto) {
|
---|
164 | using (var db = CreateContext()) {
|
---|
165 | var entity = Convert.ToEntity(dto);
|
---|
166 | db.JobDatas.InsertOnSubmit(entity);
|
---|
167 | db.SubmitChanges();
|
---|
168 | return entity.JobId;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | public void UpdateJobData(DT.JobData dto) {
|
---|
173 | using (var db = CreateContext()) {
|
---|
174 | var entity = db.JobDatas.FirstOrDefault(x => x.JobId == dto.JobId);
|
---|
175 | if (entity == null) db.JobDatas.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
176 | else Convert.ToEntity(dto, entity);
|
---|
177 | db.SubmitChanges();
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | public void DeleteJobData(Guid id) {
|
---|
182 | using (var db = CreateContext()) {
|
---|
183 | 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
|
---|
184 | if (entity != null) db.JobDatas.DeleteOnSubmit(entity);
|
---|
185 | db.SubmitChanges();
|
---|
186 | }
|
---|
187 | }
|
---|
188 | #endregion
|
---|
189 |
|
---|
190 | #region StateLog Methods
|
---|
191 |
|
---|
192 | public DT.StateLog GetStateLog(Guid id) {
|
---|
193 | using (var db = CreateContext()) {
|
---|
194 | return Convert.ToDto(db.StateLogs.SingleOrDefault(x => x.StateLogId == id));
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | public IEnumerable<DT.StateLog> GetStateLogs(Expression<Func<StateLog, bool>> predicate) {
|
---|
199 | using (var db = CreateContext()) {
|
---|
200 | return db.StateLogs.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | public Guid AddStateLog(DT.StateLog dto) {
|
---|
205 | using (var db = CreateContext()) {
|
---|
206 | var entity = Convert.ToEntity(dto);
|
---|
207 | db.StateLogs.InsertOnSubmit(entity);
|
---|
208 | db.SubmitChanges();
|
---|
209 | return entity.StateLogId;
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | public void UpdateStateLog(DT.StateLog dto) {
|
---|
214 | using (var db = CreateContext()) {
|
---|
215 | var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == dto.Id);
|
---|
216 | if (entity == null) db.StateLogs.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
217 | else Convert.ToEntity(dto, entity);
|
---|
218 | db.SubmitChanges();
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | public void DeleteStateLog(Guid id) {
|
---|
223 | using (var db = CreateContext()) {
|
---|
224 | var entity = db.StateLogs.FirstOrDefault(x => x.StateLogId == id);
|
---|
225 | if (entity != null) db.StateLogs.DeleteOnSubmit(entity);
|
---|
226 | db.SubmitChanges();
|
---|
227 | }
|
---|
228 | }
|
---|
229 | #endregion
|
---|
230 |
|
---|
231 | #region HiveExperiment Methods
|
---|
232 | public DT.HiveExperiment GetHiveExperiment(Guid id) {
|
---|
233 | using (var db = CreateContext()) {
|
---|
234 | return AddStatsToExperiment(db, Convert.ToDto(db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == id)));
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | private DT.HiveExperiment AddStatsToExperiment(HiveDataContext db, DT.HiveExperiment exp) {
|
---|
239 | var jobs = new List<Job>();
|
---|
240 | CollectChildJobs(db, exp.RootJobId, jobs);
|
---|
241 | exp.JobCount = jobs.Count;
|
---|
242 | exp.CalculatingCount = jobs.Count(j => j.State == JobState.Calculating);
|
---|
243 | exp.FinishedCount = jobs.Count(j => j.State == JobState.Finished);
|
---|
244 | return exp;
|
---|
245 | }
|
---|
246 |
|
---|
247 | public IEnumerable<DT.HiveExperiment> GetHiveExperiments(Expression<Func<HiveExperiment, bool>> predicate) {
|
---|
248 | using (var db = CreateContext()) {
|
---|
249 | return db.HiveExperiments.Where(predicate).Select(x => AddStatsToExperiment(db, Convert.ToDto(x))).ToArray();
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | public Guid AddHiveExperiment(DT.HiveExperiment dto) {
|
---|
254 | using (var db = CreateContext()) {
|
---|
255 | var entity = Convert.ToEntity(dto);
|
---|
256 | db.HiveExperiments.InsertOnSubmit(entity);
|
---|
257 | db.SubmitChanges();
|
---|
258 | return entity.HiveExperimentId;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | public void UpdateHiveExperiment(DT.HiveExperiment dto) {
|
---|
263 | using (var db = CreateContext()) {
|
---|
264 | var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == dto.Id);
|
---|
265 | if (entity == null) db.HiveExperiments.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
266 | else Convert.ToEntity(dto, entity);
|
---|
267 | db.SubmitChanges();
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | public void DeleteHiveExperiment(Guid id) {
|
---|
272 | using (var db = CreateContext()) {
|
---|
273 | var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == id);
|
---|
274 | if (entity != null) db.HiveExperiments.DeleteOnSubmit(entity);
|
---|
275 | db.SubmitChanges();
|
---|
276 | }
|
---|
277 | }
|
---|
278 | #endregion
|
---|
279 |
|
---|
280 | #region HiveExperimentPermission Methods
|
---|
281 |
|
---|
282 | public DT.HiveExperimentPermission GetHiveExperimentPermission(Guid hiveExperimentId, Guid grantedUserId) {
|
---|
283 | using (var db = CreateContext()) {
|
---|
284 | return Convert.ToDto(db.HiveExperimentPermissions.SingleOrDefault(x => x.HiveExperimentId == hiveExperimentId && x.GrantedUserId == grantedUserId));
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | public IEnumerable<DT.HiveExperimentPermission> GetHiveExperimentPermissions(Expression<Func<HiveExperimentPermission, bool>> predicate) {
|
---|
289 | using (var db = CreateContext()) {
|
---|
290 | return db.HiveExperimentPermissions.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | public void AddHiveExperimentPermission(DT.HiveExperimentPermission dto) {
|
---|
295 | using (var db = CreateContext()) {
|
---|
296 | var entity = Convert.ToEntity(dto);
|
---|
297 | db.HiveExperimentPermissions.InsertOnSubmit(entity);
|
---|
298 | db.SubmitChanges();
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | public void UpdateHiveExperimentPermission(DT.HiveExperimentPermission dto) {
|
---|
303 | using (var db = CreateContext()) {
|
---|
304 | var entity = db.HiveExperimentPermissions.FirstOrDefault(x => x.HiveExperimentId == dto.HiveExperimentId && x.GrantedUserId == dto.GrantedUserId);
|
---|
305 | if (entity == null) db.HiveExperimentPermissions.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
306 | else Convert.ToEntity(dto, entity);
|
---|
307 | db.SubmitChanges();
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | public void DeleteHiveExperimentPermission(Guid hiveExperimentId, Guid grantedUserId) {
|
---|
312 | using (var db = CreateContext()) {
|
---|
313 | var entity = db.HiveExperimentPermissions.FirstOrDefault(x => x.HiveExperimentId == hiveExperimentId && x.GrantedUserId == grantedUserId);
|
---|
314 | if (entity != null) db.HiveExperimentPermissions.DeleteOnSubmit(entity);
|
---|
315 | db.SubmitChanges();
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | #endregion
|
---|
320 |
|
---|
321 | #region Plugin Methods
|
---|
322 | public DT.Plugin GetPlugin(Guid id) {
|
---|
323 | using (var db = CreateContext()) {
|
---|
324 | return Convert.ToDto(db.Plugins.SingleOrDefault(x => x.PluginId == id));
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | public IEnumerable<DT.Plugin> GetPlugins(Expression<Func<Plugin, bool>> predicate) {
|
---|
329 | using (var db = CreateContext()) {
|
---|
330 | return db.Plugins.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | public Guid AddPlugin(DT.Plugin dto) {
|
---|
335 | using (var db = CreateContext()) {
|
---|
336 | var entity = Convert.ToEntity(dto);
|
---|
337 | db.Plugins.InsertOnSubmit(entity);
|
---|
338 | db.SubmitChanges();
|
---|
339 | return entity.PluginId;
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | public void UpdatePlugin(DT.Plugin dto) {
|
---|
344 | using (var db = CreateContext()) {
|
---|
345 | var entity = db.Plugins.FirstOrDefault(x => x.PluginId == dto.Id);
|
---|
346 | if (entity == null) db.Plugins.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
347 | else Convert.ToEntity(dto, entity);
|
---|
348 | db.SubmitChanges();
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | public void DeletePlugin(Guid id) {
|
---|
353 | using (var db = CreateContext()) {
|
---|
354 | var entity = db.Plugins.FirstOrDefault(x => x.PluginId == id);
|
---|
355 | if (entity != null) db.Plugins.DeleteOnSubmit(entity);
|
---|
356 | db.SubmitChanges();
|
---|
357 | }
|
---|
358 | }
|
---|
359 | #endregion
|
---|
360 |
|
---|
361 | #region PluginData Methods
|
---|
362 |
|
---|
363 | public DT.PluginData GetPluginData(Guid id) {
|
---|
364 | using (var db = CreateContext()) {
|
---|
365 | return Convert.ToDto(db.PluginDatas.SingleOrDefault(x => x.PluginDataId == id));
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | public IEnumerable<DT.PluginData> GetPluginDatas(Expression<Func<PluginData, bool>> predicate) {
|
---|
370 | using (var db = CreateContext()) {
|
---|
371 | return db.PluginDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | public Guid AddPluginData(DT.PluginData dto) {
|
---|
376 | using (var db = CreateContext()) {
|
---|
377 | var entity = Convert.ToEntity(dto);
|
---|
378 | db.PluginDatas.InsertOnSubmit(entity);
|
---|
379 | db.SubmitChanges();
|
---|
380 | return entity.PluginDataId;
|
---|
381 | }
|
---|
382 | }
|
---|
383 |
|
---|
384 | public void UpdatePluginData(DT.PluginData dto) {
|
---|
385 | using (var db = CreateContext()) {
|
---|
386 | var entity = db.PluginDatas.FirstOrDefault(x => x.PluginId == dto.PluginId);
|
---|
387 | if (entity == null) db.PluginDatas.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
388 | else Convert.ToEntity(dto, entity);
|
---|
389 | db.SubmitChanges();
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 | public void DeletePluginData(Guid id) {
|
---|
394 | using (var db = CreateContext()) {
|
---|
395 | var entity = db.PluginDatas.FirstOrDefault(x => x.PluginDataId == id); // todo: check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
|
---|
396 | if (entity != null) db.PluginDatas.DeleteOnSubmit(entity);
|
---|
397 | db.SubmitChanges();
|
---|
398 | }
|
---|
399 | }
|
---|
400 | #endregion
|
---|
401 |
|
---|
402 | #region Slave Methods
|
---|
403 | public DT.Slave GetSlave(Guid id) {
|
---|
404 | using (var db = CreateContext()) {
|
---|
405 | return Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
|
---|
410 | using (var db = CreateContext()) {
|
---|
411 | return db.Resources.OfType<Slave>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
412 | }
|
---|
413 | }
|
---|
414 |
|
---|
415 | public Guid AddSlave(DT.Slave dto) {
|
---|
416 | using (var db = CreateContext()) {
|
---|
417 | var entity = Convert.ToEntity(dto);
|
---|
418 | db.Resources.InsertOnSubmit(entity);
|
---|
419 | db.SubmitChanges();
|
---|
420 | return entity.ResourceId;
|
---|
421 | }
|
---|
422 | }
|
---|
423 |
|
---|
424 | public void UpdateSlave(DT.Slave dto) {
|
---|
425 | using (var db = CreateContext()) {
|
---|
426 | var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
427 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
428 | else Convert.ToEntity(dto, entity);
|
---|
429 | db.SubmitChanges();
|
---|
430 | }
|
---|
431 | }
|
---|
432 |
|
---|
433 | public void DeleteSlave(Guid id) {
|
---|
434 | using (var db = CreateContext()) {
|
---|
435 | var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
|
---|
436 | if (entity != null) db.Resources.DeleteOnSubmit(entity);
|
---|
437 | db.SubmitChanges();
|
---|
438 | }
|
---|
439 | }
|
---|
440 | #endregion
|
---|
441 |
|
---|
442 | #region SlaveGroup Methods
|
---|
443 | public DT.SlaveGroup GetSlaveGroup(Guid id) {
|
---|
444 | using (var db = CreateContext()) {
|
---|
445 | return Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
|
---|
450 | using (var db = CreateContext()) {
|
---|
451 | return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | public Guid AddSlaveGroup(DT.SlaveGroup dto) {
|
---|
456 | using (var db = CreateContext()) {
|
---|
457 | if (dto.Id == Guid.Empty)
|
---|
458 | dto.Id = Guid.NewGuid();
|
---|
459 | var entity = Convert.ToEntity(dto);
|
---|
460 | db.Resources.InsertOnSubmit(entity);
|
---|
461 | db.SubmitChanges();
|
---|
462 | return entity.ResourceId;
|
---|
463 | }
|
---|
464 | }
|
---|
465 |
|
---|
466 | public void UpdateSlaveGroup(DT.SlaveGroup dto) {
|
---|
467 | using (var db = CreateContext()) {
|
---|
468 | var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
469 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
470 | else Convert.ToEntity(dto, entity);
|
---|
471 | db.SubmitChanges();
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | public void DeleteSlaveGroup(Guid id) {
|
---|
476 | using (var db = CreateContext()) {
|
---|
477 | var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
|
---|
478 | if (entity != null) {
|
---|
479 | if (db.Resources.Where(r => r.ParentResourceId == id).Count() > 0) {
|
---|
480 | throw new DaoException("Cannot delete SlaveGroup as long as there are Slaves in the group");
|
---|
481 | }
|
---|
482 | db.Resources.DeleteOnSubmit(entity);
|
---|
483 | }
|
---|
484 | db.SubmitChanges();
|
---|
485 | }
|
---|
486 | }
|
---|
487 | #endregion
|
---|
488 |
|
---|
489 | #region Resource Methods
|
---|
490 | public DT.Resource GetResource(Guid id) {
|
---|
491 | using (var db = CreateContext()) {
|
---|
492 | return Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
|
---|
497 | using (var db = CreateContext()) {
|
---|
498 | return db.Resources.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | public Guid AddResource(DT.Resource dto) {
|
---|
503 | using (var db = CreateContext()) {
|
---|
504 | var entity = Convert.ToEntity(dto);
|
---|
505 | db.Resources.InsertOnSubmit(entity);
|
---|
506 | db.SubmitChanges();
|
---|
507 | return entity.ResourceId;
|
---|
508 | }
|
---|
509 | }
|
---|
510 |
|
---|
511 | public void UpdateResource(DT.Resource dto) {
|
---|
512 | using (var db = CreateContext()) {
|
---|
513 | var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
514 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
515 | else Convert.ToEntity(dto, entity);
|
---|
516 | db.SubmitChanges();
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | public void DeleteResource(Guid id) {
|
---|
521 | using (var db = CreateContext()) {
|
---|
522 | var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
|
---|
523 | if (entity != null) db.Resources.DeleteOnSubmit(entity);
|
---|
524 | db.SubmitChanges();
|
---|
525 | }
|
---|
526 | }
|
---|
527 |
|
---|
528 | public void AssignJobToResource(Guid jobId, Guid resourceId) {
|
---|
529 | using (var db = CreateContext()) {
|
---|
530 | var job = db.Jobs.Where(x => x.JobId == jobId).Single();
|
---|
531 | job.AssignedResources.Add(new AssignedResource() { JobId = jobId, ResourceId = resourceId });
|
---|
532 | db.SubmitChanges();
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 | public IEnumerable<DT.Resource> GetAssignedResources(Guid jobId) {
|
---|
537 | using (var db = CreateContext()) {
|
---|
538 | var job = db.Jobs.Where(x => x.JobId == jobId).Single();
|
---|
539 | return job.AssignedResources.Select(x => Convert.ToDto(x.Resource)).ToArray();
|
---|
540 | }
|
---|
541 | }
|
---|
542 |
|
---|
543 | /// <summary>
|
---|
544 | /// Returns all parent resources of a resource (the given resource is also added)
|
---|
545 | /// </summary>
|
---|
546 | private IEnumerable<DT.Resource> GetParentResources(Guid resourceId) {
|
---|
547 | using (var db = CreateContext()) {
|
---|
548 | var resources = new List<Resource>();
|
---|
549 | CollectParentResources(resources, db.Resources.Where(r => r.ResourceId == resourceId).Single());
|
---|
550 | return resources.Select(r => Convert.ToDto(r)).ToArray();
|
---|
551 | }
|
---|
552 | }
|
---|
553 |
|
---|
554 | private void CollectParentResources(List<Resource> resources, Resource resource) {
|
---|
555 | if (resource == null) return;
|
---|
556 | resources.Add(resource);
|
---|
557 | CollectParentResources(resources, resource.ParentResource);
|
---|
558 | }
|
---|
559 | #endregion
|
---|
560 |
|
---|
561 | #region Authorization Methods
|
---|
562 | public Permission GetPermissionForJob(Guid jobId, Guid userId) {
|
---|
563 | using (var db = CreateContext()) {
|
---|
564 | return GetPermissionForExperiment(GetExperimentForJob(jobId), userId);
|
---|
565 | }
|
---|
566 | }
|
---|
567 |
|
---|
568 | public Permission GetPermissionForExperiment(Guid experimentId, Guid userId) {
|
---|
569 | using (var db = CreateContext()) {
|
---|
570 | HiveExperimentPermission permission = db.HiveExperimentPermissions.SingleOrDefault(p => p.HiveExperimentId == experimentId && p.GrantedUserId == userId);
|
---|
571 | return permission != null ? permission.Permission : Permission.NotAllowed;
|
---|
572 | }
|
---|
573 | }
|
---|
574 |
|
---|
575 | public Guid GetExperimentForJob(Guid jobId) {
|
---|
576 | using (var db = CreateContext()) {
|
---|
577 | var job = db.Jobs.SingleOrDefault(j => j.JobId == jobId);
|
---|
578 | if (job.ParentJobId.HasValue) {
|
---|
579 | return GetExperimentForJob(job.ParentJobId.Value);
|
---|
580 | } else {
|
---|
581 | return db.HiveExperiments.SingleOrDefault(he => he.RootJobId == jobId).HiveExperimentId;
|
---|
582 | }
|
---|
583 | }
|
---|
584 | }
|
---|
585 |
|
---|
586 | #endregion
|
---|
587 |
|
---|
588 | #region Lifecycle Methods
|
---|
589 | public DateTime GetLastCleanup() {
|
---|
590 | using (var db = CreateContext()) {
|
---|
591 | var entity = db.Lifecycles.SingleOrDefault();
|
---|
592 | return entity != null ? entity.LastCleanup : DateTime.MinValue;
|
---|
593 | }
|
---|
594 | }
|
---|
595 |
|
---|
596 | public void SetLastCleanup(DateTime datetime) {
|
---|
597 | using (var db = CreateContext()) {
|
---|
598 | var entity = db.Lifecycles.SingleOrDefault();
|
---|
599 | if (entity != null) {
|
---|
600 | entity.LastCleanup = datetime;
|
---|
601 | } else {
|
---|
602 | entity = new Lifecycle();
|
---|
603 | entity.LifecycleId = 0; // always only one entry with ID:0
|
---|
604 | entity.LastCleanup = datetime;
|
---|
605 | db.Lifecycles.InsertOnSubmit(entity);
|
---|
606 | }
|
---|
607 | db.SubmitChanges();
|
---|
608 | }
|
---|
609 | }
|
---|
610 | #endregion
|
---|
611 |
|
---|
612 | #region AppointmentMethods
|
---|
613 | public Appointment GetAppointment(Guid id) {
|
---|
614 | using (var db = CreateContext()) {
|
---|
615 | return Convert.ToDto(db.UptimeCalendars.SingleOrDefault(x => x.UptimeCalendarId == id));
|
---|
616 | }
|
---|
617 | }
|
---|
618 |
|
---|
619 | public IEnumerable<Appointment> GetAppointments(Expression<Func<UptimeCalendar, bool>> predicate) {
|
---|
620 | using (var db = CreateContext()) {
|
---|
621 | return db.UptimeCalendars.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
622 | }
|
---|
623 | }
|
---|
624 |
|
---|
625 | public Guid AddAppointment(Appointment dto) {
|
---|
626 | using (var db = CreateContext()) {
|
---|
627 | var entity = Convert.ToEntity(dto);
|
---|
628 | db.UptimeCalendars.InsertOnSubmit(entity);
|
---|
629 | db.SubmitChanges();
|
---|
630 | return entity.UptimeCalendarId;
|
---|
631 | }
|
---|
632 | }
|
---|
633 |
|
---|
634 | public void UpdateAppointment(Appointment dto) {
|
---|
635 | using (var db = CreateContext()) {
|
---|
636 | var entity = db.UptimeCalendars.FirstOrDefault(x => x.UptimeCalendarId == dto.Id);
|
---|
637 | if (entity == null) db.UptimeCalendars.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
638 | else Convert.ToEntity(dto, entity);
|
---|
639 | db.SubmitChanges();
|
---|
640 | }
|
---|
641 | }
|
---|
642 |
|
---|
643 | public void DeleteAppointment(Guid id) {
|
---|
644 | using (var db = CreateContext()) {
|
---|
645 | var entity = db.UptimeCalendars.FirstOrDefault(x => x.UptimeCalendarId == id);
|
---|
646 | if (entity != null) db.UptimeCalendars.DeleteOnSubmit(entity);
|
---|
647 | db.SubmitChanges();
|
---|
648 | }
|
---|
649 | }
|
---|
650 | #endregion
|
---|
651 |
|
---|
652 | #region Helpers
|
---|
653 | private void CollectChildJobs(HiveDataContext db, Guid parentJobId, List<Job> collection) {
|
---|
654 | var jobs = db.Jobs.Where(j => j.ParentJobId == parentJobId);
|
---|
655 | foreach (var job in jobs) {
|
---|
656 | collection.Add(job);
|
---|
657 | if (job.IsParentJob)
|
---|
658 | CollectChildJobs(db, job.JobId, collection);
|
---|
659 | }
|
---|
660 | }
|
---|
661 | #endregion
|
---|
662 | }
|
---|
663 | }
|
---|