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