[5156] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[6367] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5156] | 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;
|
---|
[4598] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Linq.Expressions;
|
---|
[6419] | 26 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
| 27 | using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
[4598] | 28 |
|
---|
| 29 | namespace HeuristicLab.Services.Hive.DataAccess {
|
---|
| 30 | public class HiveDao : IHiveDao {
|
---|
[6419] | 31 | public static HiveDataContext CreateContext(bool longRunning = false) {
|
---|
| 32 | var context = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
|
---|
[6452] | 33 | if (longRunning) context.CommandTimeout = (int)Settings.Default.LongRunningDatabaseCommandTimeout.TotalSeconds;
|
---|
[6419] | 34 | return context;
|
---|
[4649] | 35 | }
|
---|
[4598] | 36 |
|
---|
[5786] | 37 | public HiveDao() { }
|
---|
[4598] | 38 |
|
---|
| 39 | #region Job Methods
|
---|
| 40 | public DT.Job GetJob(Guid id) {
|
---|
[4649] | 41 | using (var db = CreateContext()) {
|
---|
| 42 | return Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id));
|
---|
| 43 | }
|
---|
[4598] | 44 | }
|
---|
| 45 |
|
---|
| 46 | public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
|
---|
[4649] | 47 | using (var db = CreateContext()) {
|
---|
| 48 | return db.Jobs.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 49 | }
|
---|
[4598] | 50 | }
|
---|
| 51 |
|
---|
| 52 | public Guid AddJob(DT.Job dto) {
|
---|
[4649] | 53 | using (var db = CreateContext()) {
|
---|
| 54 | var entity = Convert.ToEntity(dto);
|
---|
| 55 | db.Jobs.InsertOnSubmit(entity);
|
---|
| 56 | db.SubmitChanges();
|
---|
[5402] | 57 | foreach (Guid pluginId in dto.PluginsNeededIds) {
|
---|
| 58 | db.RequiredPlugins.InsertOnSubmit(new RequiredPlugin() { JobId = entity.JobId, PluginId = pluginId });
|
---|
| 59 | }
|
---|
| 60 | db.SubmitChanges();
|
---|
[4649] | 61 | return entity.JobId;
|
---|
| 62 | }
|
---|
[4598] | 63 | }
|
---|
| 64 |
|
---|
| 65 | public void UpdateJob(DT.Job dto) {
|
---|
[4649] | 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);
|
---|
[6367] | 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 });
|
---|
[6419] | 73 | }
|
---|
[6367] | 74 | }
|
---|
[4649] | 75 | db.SubmitChanges();
|
---|
| 76 | }
|
---|
[4598] | 77 | }
|
---|
| 78 |
|
---|
| 79 | public void DeleteJob(Guid id) {
|
---|
[4649] | 80 | using (var db = CreateContext()) {
|
---|
| 81 | var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
|
---|
| 82 | if (entity != null) db.Jobs.DeleteOnSubmit(entity);
|
---|
[5106] | 83 | db.SubmitChanges(); // JobData and child jobs are deleted by db-trigger
|
---|
[4649] | 84 | }
|
---|
[4598] | 85 | }
|
---|
[4615] | 86 |
|
---|
[5526] | 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) {
|
---|
[4649] | 95 | using (var db = CreateContext()) {
|
---|
| 96 | var query = from ar in db.AssignedResources
|
---|
[5404] | 97 | where resourceIds.Contains(ar.ResourceId)
|
---|
[5526] | 98 | && ar.Job.State == JobState.Waiting
|
---|
[5633] | 99 | && ar.Job.IsParentJob
|
---|
[5526] | 100 | && (finished ? ar.Job.FinishWhenChildJobsFinished : !ar.Job.FinishWhenChildJobsFinished)
|
---|
[5404] | 101 | && (from child in db.Jobs
|
---|
| 102 | where child.ParentJobId == ar.Job.JobId
|
---|
[6229] | 103 | select child.State == JobState.Finished
|
---|
| 104 | || child.State == JobState.Aborted
|
---|
[5779] | 105 | || child.State == JobState.Failed).All(x => x)
|
---|
[5404] | 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
|
---|
[6452] | 109 | orderby ar.Job.Priority descending, db.Random()
|
---|
[4649] | 110 | select Convert.ToDto(ar.Job);
|
---|
[5404] | 111 | return count == 0 ? query.ToArray() : query.Take(count).ToArray();
|
---|
[4649] | 112 | }
|
---|
[4615] | 113 | }
|
---|
[4629] | 114 |
|
---|
[5404] | 115 | public IEnumerable<DT.Job> GetWaitingJobs(DT.Slave slave, int count) {
|
---|
[4649] | 116 | using (var db = CreateContext()) {
|
---|
[5404] | 117 | var resourceIds = GetParentResources(slave.Id).Select(r => r.Id);
|
---|
[5526] | 118 | var waitingParentJobs = GetParentJobs(resourceIds, count, false);
|
---|
[5404] | 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)
|
---|
[5526] | 123 | && !(ar.Job.IsParentJob && ar.Job.FinishWhenChildJobsFinished)
|
---|
[5511] | 124 | && ar.Job.State == JobState.Waiting
|
---|
[5404] | 125 | && ar.Job.CoresNeeded <= slave.FreeCores
|
---|
| 126 | && ar.Job.MemoryNeeded <= slave.FreeMemory
|
---|
[6452] | 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)
|
---|
[5404] | 128 | select Convert.ToDto(ar.Job);
|
---|
| 129 | var waitingJobs = (count == 0 ? query : query.Take(count)).ToArray();
|
---|
[5375] | 130 | return waitingJobs.Union(waitingParentJobs).OrderByDescending(x => x.Priority);
|
---|
[4649] | 131 | }
|
---|
[4629] | 132 | }
|
---|
[5636] | 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 | }
|
---|
[4598] | 151 | #endregion
|
---|
| 152 |
|
---|
| 153 | #region JobData Methods
|
---|
| 154 | public DT.JobData GetJobData(Guid id) {
|
---|
[6419] | 155 | using (var db = CreateContext(true)) {
|
---|
[4649] | 156 | return Convert.ToDto(db.JobDatas.SingleOrDefault(x => x.JobId == id));
|
---|
| 157 | }
|
---|
[4598] | 158 | }
|
---|
| 159 |
|
---|
| 160 | public IEnumerable<DT.JobData> GetJobDatas(Expression<Func<JobData, bool>> predicate) {
|
---|
[6419] | 161 | using (var db = CreateContext(true)) {
|
---|
[4649] | 162 | return db.JobDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 163 | }
|
---|
[4598] | 164 | }
|
---|
| 165 |
|
---|
| 166 | public Guid AddJobData(DT.JobData dto) {
|
---|
[6419] | 167 | using (var db = CreateContext(true)) {
|
---|
[4649] | 168 | var entity = Convert.ToEntity(dto);
|
---|
| 169 | db.JobDatas.InsertOnSubmit(entity);
|
---|
| 170 | db.SubmitChanges();
|
---|
| 171 | return entity.JobId;
|
---|
| 172 | }
|
---|
[4598] | 173 | }
|
---|
| 174 |
|
---|
| 175 | public void UpdateJobData(DT.JobData dto) {
|
---|
[6419] | 176 | using (var db = CreateContext(true)) {
|
---|
[4649] | 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 | }
|
---|
[4598] | 182 | }
|
---|
| 183 |
|
---|
| 184 | public void DeleteJobData(Guid id) {
|
---|
[4649] | 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 | }
|
---|
[4598] | 190 | }
|
---|
| 191 | #endregion
|
---|
| 192 |
|
---|
[5526] | 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 |
|
---|
[4598] | 233 | #region HiveExperiment Methods
|
---|
| 234 | public DT.HiveExperiment GetHiveExperiment(Guid id) {
|
---|
[4649] | 235 | using (var db = CreateContext()) {
|
---|
[5955] | 236 | return AddStatsToExperiment(db, Convert.ToDto(db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == id)));
|
---|
[4649] | 237 | }
|
---|
[4598] | 238 | }
|
---|
| 239 |
|
---|
[5955] | 240 | private DT.HiveExperiment AddStatsToExperiment(HiveDataContext db, DT.HiveExperiment exp) {
|
---|
[6006] | 241 | if (exp == null)
|
---|
| 242 | return null;
|
---|
| 243 |
|
---|
| 244 | var jobs = db.Jobs.Where(j => j.HiveExperimentId == exp.Id);
|
---|
| 245 | exp.JobCount = jobs.Count();
|
---|
[5955] | 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 |
|
---|
[4598] | 251 | public IEnumerable<DT.HiveExperiment> GetHiveExperiments(Expression<Func<HiveExperiment, bool>> predicate) {
|
---|
[4649] | 252 | using (var db = CreateContext()) {
|
---|
[5955] | 253 | return db.HiveExperiments.Where(predicate).Select(x => AddStatsToExperiment(db, Convert.ToDto(x))).ToArray();
|
---|
[4649] | 254 | }
|
---|
[4598] | 255 | }
|
---|
| 256 |
|
---|
| 257 | public Guid AddHiveExperiment(DT.HiveExperiment dto) {
|
---|
[4649] | 258 | using (var db = CreateContext()) {
|
---|
| 259 | var entity = Convert.ToEntity(dto);
|
---|
| 260 | db.HiveExperiments.InsertOnSubmit(entity);
|
---|
| 261 | db.SubmitChanges();
|
---|
| 262 | return entity.HiveExperimentId;
|
---|
| 263 | }
|
---|
[4598] | 264 | }
|
---|
| 265 |
|
---|
| 266 | public void UpdateHiveExperiment(DT.HiveExperiment dto) {
|
---|
[4649] | 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 | }
|
---|
[4598] | 273 | }
|
---|
| 274 |
|
---|
| 275 | public void DeleteHiveExperiment(Guid id) {
|
---|
[4649] | 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 | }
|
---|
[4598] | 281 | }
|
---|
| 282 | #endregion
|
---|
| 283 |
|
---|
[5511] | 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 | #endregion
|
---|
| 322 |
|
---|
[4905] | 323 | #region Plugin Methods
|
---|
| 324 | public DT.Plugin GetPlugin(Guid id) {
|
---|
| 325 | using (var db = CreateContext()) {
|
---|
| 326 | return Convert.ToDto(db.Plugins.SingleOrDefault(x => x.PluginId == id));
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | public IEnumerable<DT.Plugin> GetPlugins(Expression<Func<Plugin, bool>> predicate) {
|
---|
| 331 | using (var db = CreateContext()) {
|
---|
| 332 | return db.Plugins.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 333 | }
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | public Guid AddPlugin(DT.Plugin dto) {
|
---|
| 337 | using (var db = CreateContext()) {
|
---|
| 338 | var entity = Convert.ToEntity(dto);
|
---|
| 339 | db.Plugins.InsertOnSubmit(entity);
|
---|
| 340 | db.SubmitChanges();
|
---|
| 341 | return entity.PluginId;
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | public void UpdatePlugin(DT.Plugin dto) {
|
---|
| 346 | using (var db = CreateContext()) {
|
---|
| 347 | var entity = db.Plugins.FirstOrDefault(x => x.PluginId == dto.Id);
|
---|
| 348 | if (entity == null) db.Plugins.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 349 | else Convert.ToEntity(dto, entity);
|
---|
| 350 | db.SubmitChanges();
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | public void DeletePlugin(Guid id) {
|
---|
| 355 | using (var db = CreateContext()) {
|
---|
| 356 | var entity = db.Plugins.FirstOrDefault(x => x.PluginId == id);
|
---|
| 357 | if (entity != null) db.Plugins.DeleteOnSubmit(entity);
|
---|
| 358 | db.SubmitChanges();
|
---|
| 359 | }
|
---|
| 360 | }
|
---|
| 361 | #endregion
|
---|
| 362 |
|
---|
| 363 | #region PluginData Methods
|
---|
| 364 | public DT.PluginData GetPluginData(Guid id) {
|
---|
| 365 | using (var db = CreateContext()) {
|
---|
[5402] | 366 | return Convert.ToDto(db.PluginDatas.SingleOrDefault(x => x.PluginDataId == id));
|
---|
[4905] | 367 | }
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | public IEnumerable<DT.PluginData> GetPluginDatas(Expression<Func<PluginData, bool>> predicate) {
|
---|
| 371 | using (var db = CreateContext()) {
|
---|
| 372 | return db.PluginDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 373 | }
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | public Guid AddPluginData(DT.PluginData dto) {
|
---|
| 377 | using (var db = CreateContext()) {
|
---|
| 378 | var entity = Convert.ToEntity(dto);
|
---|
| 379 | db.PluginDatas.InsertOnSubmit(entity);
|
---|
| 380 | db.SubmitChanges();
|
---|
[5402] | 381 | return entity.PluginDataId;
|
---|
[4905] | 382 | }
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | public void UpdatePluginData(DT.PluginData dto) {
|
---|
| 386 | using (var db = CreateContext()) {
|
---|
| 387 | var entity = db.PluginDatas.FirstOrDefault(x => x.PluginId == dto.PluginId);
|
---|
| 388 | if (entity == null) db.PluginDatas.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 389 | else Convert.ToEntity(dto, entity);
|
---|
| 390 | db.SubmitChanges();
|
---|
| 391 | }
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | public void DeletePluginData(Guid id) {
|
---|
| 395 | using (var db = CreateContext()) {
|
---|
[6369] | 396 | var entity = db.PluginDatas.FirstOrDefault(x => x.PluginDataId == id);
|
---|
[4905] | 397 | if (entity != null) db.PluginDatas.DeleteOnSubmit(entity);
|
---|
| 398 | db.SubmitChanges();
|
---|
| 399 | }
|
---|
| 400 | }
|
---|
| 401 | #endregion
|
---|
| 402 |
|
---|
[4598] | 403 | #region Slave Methods
|
---|
| 404 | public DT.Slave GetSlave(Guid id) {
|
---|
[4649] | 405 | using (var db = CreateContext()) {
|
---|
| 406 | return Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
|
---|
| 407 | }
|
---|
[4598] | 408 | }
|
---|
| 409 |
|
---|
| 410 | public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
|
---|
[4649] | 411 | using (var db = CreateContext()) {
|
---|
| 412 | return db.Resources.OfType<Slave>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 413 | }
|
---|
[4598] | 414 | }
|
---|
| 415 |
|
---|
| 416 | public Guid AddSlave(DT.Slave dto) {
|
---|
[4649] | 417 | using (var db = CreateContext()) {
|
---|
| 418 | var entity = Convert.ToEntity(dto);
|
---|
| 419 | db.Resources.InsertOnSubmit(entity);
|
---|
| 420 | db.SubmitChanges();
|
---|
| 421 | return entity.ResourceId;
|
---|
| 422 | }
|
---|
[4598] | 423 | }
|
---|
| 424 |
|
---|
| 425 | public void UpdateSlave(DT.Slave dto) {
|
---|
[4649] | 426 | using (var db = CreateContext()) {
|
---|
| 427 | var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
| 428 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 429 | else Convert.ToEntity(dto, entity);
|
---|
| 430 | db.SubmitChanges();
|
---|
| 431 | }
|
---|
[4598] | 432 | }
|
---|
| 433 |
|
---|
| 434 | public void DeleteSlave(Guid id) {
|
---|
[4649] | 435 | using (var db = CreateContext()) {
|
---|
| 436 | var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
|
---|
| 437 | if (entity != null) db.Resources.DeleteOnSubmit(entity);
|
---|
| 438 | db.SubmitChanges();
|
---|
| 439 | }
|
---|
[4598] | 440 | }
|
---|
| 441 | #endregion
|
---|
| 442 |
|
---|
| 443 | #region SlaveGroup Methods
|
---|
| 444 | public DT.SlaveGroup GetSlaveGroup(Guid id) {
|
---|
[4649] | 445 | using (var db = CreateContext()) {
|
---|
| 446 | return Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
|
---|
| 447 | }
|
---|
[4598] | 448 | }
|
---|
| 449 |
|
---|
| 450 | public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
|
---|
[4649] | 451 | using (var db = CreateContext()) {
|
---|
| 452 | return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 453 | }
|
---|
[4598] | 454 | }
|
---|
| 455 |
|
---|
| 456 | public Guid AddSlaveGroup(DT.SlaveGroup dto) {
|
---|
[4649] | 457 | using (var db = CreateContext()) {
|
---|
[5375] | 458 | if (dto.Id == Guid.Empty)
|
---|
| 459 | dto.Id = Guid.NewGuid();
|
---|
[4649] | 460 | var entity = Convert.ToEntity(dto);
|
---|
| 461 | db.Resources.InsertOnSubmit(entity);
|
---|
| 462 | db.SubmitChanges();
|
---|
| 463 | return entity.ResourceId;
|
---|
| 464 | }
|
---|
[4598] | 465 | }
|
---|
| 466 |
|
---|
| 467 | public void UpdateSlaveGroup(DT.SlaveGroup dto) {
|
---|
[4649] | 468 | using (var db = CreateContext()) {
|
---|
| 469 | var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
| 470 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 471 | else Convert.ToEntity(dto, entity);
|
---|
| 472 | db.SubmitChanges();
|
---|
| 473 | }
|
---|
[4598] | 474 | }
|
---|
| 475 |
|
---|
| 476 | public void DeleteSlaveGroup(Guid id) {
|
---|
[4649] | 477 | using (var db = CreateContext()) {
|
---|
| 478 | var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
|
---|
[5404] | 479 | if (entity != null) {
|
---|
| 480 | if (db.Resources.Where(r => r.ParentResourceId == id).Count() > 0) {
|
---|
[6431] | 481 | throw new InvalidOperationException("Cannot delete SlaveGroup as long as there are Slaves in the group");
|
---|
[5404] | 482 | }
|
---|
| 483 | db.Resources.DeleteOnSubmit(entity);
|
---|
| 484 | }
|
---|
[4649] | 485 | db.SubmitChanges();
|
---|
| 486 | }
|
---|
[4598] | 487 | }
|
---|
| 488 | #endregion
|
---|
| 489 |
|
---|
| 490 | #region Resource Methods
|
---|
| 491 | public DT.Resource GetResource(Guid id) {
|
---|
[4649] | 492 | using (var db = CreateContext()) {
|
---|
| 493 | return Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
|
---|
| 494 | }
|
---|
[4598] | 495 | }
|
---|
| 496 |
|
---|
| 497 | public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
|
---|
[4649] | 498 | using (var db = CreateContext()) {
|
---|
| 499 | return db.Resources.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 500 | }
|
---|
[4598] | 501 | }
|
---|
| 502 |
|
---|
| 503 | public Guid AddResource(DT.Resource dto) {
|
---|
[4649] | 504 | using (var db = CreateContext()) {
|
---|
| 505 | var entity = Convert.ToEntity(dto);
|
---|
| 506 | db.Resources.InsertOnSubmit(entity);
|
---|
| 507 | db.SubmitChanges();
|
---|
| 508 | return entity.ResourceId;
|
---|
| 509 | }
|
---|
[4598] | 510 | }
|
---|
| 511 |
|
---|
| 512 | public void UpdateResource(DT.Resource dto) {
|
---|
[4649] | 513 | using (var db = CreateContext()) {
|
---|
| 514 | var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
| 515 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
| 516 | else Convert.ToEntity(dto, entity);
|
---|
| 517 | db.SubmitChanges();
|
---|
| 518 | }
|
---|
[4598] | 519 | }
|
---|
| 520 |
|
---|
| 521 | public void DeleteResource(Guid id) {
|
---|
[4649] | 522 | using (var db = CreateContext()) {
|
---|
| 523 | var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
|
---|
| 524 | if (entity != null) db.Resources.DeleteOnSubmit(entity);
|
---|
| 525 | db.SubmitChanges();
|
---|
| 526 | }
|
---|
[4598] | 527 | }
|
---|
[5155] | 528 |
|
---|
| 529 | public void AssignJobToResource(Guid jobId, Guid resourceId) {
|
---|
| 530 | using (var db = CreateContext()) {
|
---|
| 531 | var job = db.Jobs.Where(x => x.JobId == jobId).Single();
|
---|
| 532 | job.AssignedResources.Add(new AssignedResource() { JobId = jobId, ResourceId = resourceId });
|
---|
| 533 | db.SubmitChanges();
|
---|
| 534 | }
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | public IEnumerable<DT.Resource> GetAssignedResources(Guid jobId) {
|
---|
| 538 | using (var db = CreateContext()) {
|
---|
| 539 | var job = db.Jobs.Where(x => x.JobId == jobId).Single();
|
---|
| 540 | return job.AssignedResources.Select(x => Convert.ToDto(x.Resource)).ToArray();
|
---|
| 541 | }
|
---|
| 542 | }
|
---|
[5404] | 543 |
|
---|
| 544 | /// <summary>
|
---|
| 545 | /// Returns all parent resources of a resource (the given resource is also added)
|
---|
| 546 | /// </summary>
|
---|
[6110] | 547 | public IEnumerable<DT.Resource> GetParentResources(Guid resourceId) {
|
---|
[5404] | 548 | using (var db = CreateContext()) {
|
---|
| 549 | var resources = new List<Resource>();
|
---|
| 550 | CollectParentResources(resources, db.Resources.Where(r => r.ResourceId == resourceId).Single());
|
---|
| 551 | return resources.Select(r => Convert.ToDto(r)).ToArray();
|
---|
| 552 | }
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | private void CollectParentResources(List<Resource> resources, Resource resource) {
|
---|
| 556 | if (resource == null) return;
|
---|
| 557 | resources.Add(resource);
|
---|
| 558 | CollectParentResources(resources, resource.ParentResource);
|
---|
| 559 | }
|
---|
[6267] | 560 |
|
---|
| 561 | /// <summary>
|
---|
| 562 | /// Returns all child resources of a resource (without the given resource)
|
---|
| 563 | /// </summary>
|
---|
| 564 | public IEnumerable<DT.Resource> GetChildResources(Guid resourceId) {
|
---|
| 565 | using (var db = CreateContext()) {
|
---|
| 566 | var childs = new List<DT.Resource>();
|
---|
| 567 | foreach (var child in db.Resources.Where(x => x.ParentResourceId == resourceId)) {
|
---|
| 568 | childs.Add(Convert.ToDto(child));
|
---|
| 569 | childs.AddRange(GetChildResources(child.ResourceId));
|
---|
| 570 | }
|
---|
| 571 | return childs;
|
---|
| 572 | }
|
---|
| 573 | }
|
---|
| 574 |
|
---|
| 575 | public IEnumerable<DT.Job> GetJobsByResourceId(Guid resourceId) {
|
---|
| 576 | using (var db = CreateContext()) {
|
---|
| 577 | var resources = GetChildResources(resourceId).Select(x => x.Id).ToList();
|
---|
| 578 | resources.Add(resourceId);
|
---|
| 579 |
|
---|
| 580 | var jobs = db.Jobs.Where(j =>
|
---|
| 581 | j.State == JobState.Calculating &&
|
---|
| 582 | j.StateLogs.OrderByDescending(x => x.DateTime).First().SlaveId.HasValue &&
|
---|
| 583 | resources.Contains(j.StateLogs.OrderByDescending(x => x.DateTime).First().SlaveId.Value));
|
---|
| 584 | return jobs.Select(j => Convert.ToDto(j)).ToArray();
|
---|
| 585 | }
|
---|
| 586 | }
|
---|
[4598] | 587 | #endregion
|
---|
| 588 |
|
---|
| 589 | #region Authorization Methods
|
---|
[5511] | 590 | public Permission GetPermissionForJob(Guid jobId, Guid userId) {
|
---|
[4649] | 591 | using (var db = CreateContext()) {
|
---|
[5511] | 592 | return GetPermissionForExperiment(GetExperimentForJob(jobId), userId);
|
---|
[4649] | 593 | }
|
---|
[4598] | 594 | }
|
---|
[5511] | 595 |
|
---|
| 596 | public Permission GetPermissionForExperiment(Guid experimentId, Guid userId) {
|
---|
| 597 | using (var db = CreateContext()) {
|
---|
| 598 | HiveExperimentPermission permission = db.HiveExperimentPermissions.SingleOrDefault(p => p.HiveExperimentId == experimentId && p.GrantedUserId == userId);
|
---|
| 599 | return permission != null ? permission.Permission : Permission.NotAllowed;
|
---|
| 600 | }
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 | public Guid GetExperimentForJob(Guid jobId) {
|
---|
| 604 | using (var db = CreateContext()) {
|
---|
[6006] | 605 | return db.Jobs.Single(j => j.JobId == jobId).HiveExperimentId;
|
---|
[5511] | 606 | }
|
---|
| 607 | }
|
---|
[4598] | 608 | #endregion
|
---|
[5526] | 609 |
|
---|
[5593] | 610 | #region Lifecycle Methods
|
---|
| 611 | public DateTime GetLastCleanup() {
|
---|
| 612 | using (var db = CreateContext()) {
|
---|
| 613 | var entity = db.Lifecycles.SingleOrDefault();
|
---|
| 614 | return entity != null ? entity.LastCleanup : DateTime.MinValue;
|
---|
| 615 | }
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | public void SetLastCleanup(DateTime datetime) {
|
---|
| 619 | using (var db = CreateContext()) {
|
---|
| 620 | var entity = db.Lifecycles.SingleOrDefault();
|
---|
| 621 | if (entity != null) {
|
---|
| 622 | entity.LastCleanup = datetime;
|
---|
| 623 | } else {
|
---|
| 624 | entity = new Lifecycle();
|
---|
| 625 | entity.LifecycleId = 0; // always only one entry with ID:0
|
---|
| 626 | entity.LastCleanup = datetime;
|
---|
| 627 | db.Lifecycles.InsertOnSubmit(entity);
|
---|
| 628 | }
|
---|
| 629 | db.SubmitChanges();
|
---|
| 630 | }
|
---|
| 631 | }
|
---|
| 632 | #endregion
|
---|
[5633] | 633 |
|
---|
[6452] | 634 | #region Downtime Methods
|
---|
| 635 | public DT.Downtime GetDowntime(Guid id) {
|
---|
[5633] | 636 | using (var db = CreateContext()) {
|
---|
[6452] | 637 | return Convert.ToDto(db.Downtimes.SingleOrDefault(x => x.DowntimeId == id));
|
---|
[5633] | 638 | }
|
---|
| 639 | }
|
---|
| 640 |
|
---|
[6452] | 641 | public IEnumerable<DT.Downtime> GetDowntimes(Expression<Func<Downtime, bool>> predicate) {
|
---|
[5633] | 642 | using (var db = CreateContext()) {
|
---|
[6452] | 643 | return db.Downtimes.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
[5633] | 644 | }
|
---|
| 645 | }
|
---|
| 646 |
|
---|
[6452] | 647 | public Guid AddDowntime(DT.Downtime dto) {
|
---|
[5633] | 648 | using (var db = CreateContext()) {
|
---|
| 649 | var entity = Convert.ToEntity(dto);
|
---|
[6452] | 650 | db.Downtimes.InsertOnSubmit(entity);
|
---|
[5633] | 651 | db.SubmitChanges();
|
---|
[6452] | 652 | return entity.DowntimeId;
|
---|
[5633] | 653 | }
|
---|
| 654 | }
|
---|
| 655 |
|
---|
[6452] | 656 | public void UpdateDowntime(DT.Downtime dto) {
|
---|
[5633] | 657 | using (var db = CreateContext()) {
|
---|
[6452] | 658 | var entity = db.Downtimes.FirstOrDefault(x => x.DowntimeId == dto.Id);
|
---|
| 659 | if (entity == null) db.Downtimes.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
[5633] | 660 | else Convert.ToEntity(dto, entity);
|
---|
| 661 | db.SubmitChanges();
|
---|
| 662 | }
|
---|
| 663 | }
|
---|
| 664 |
|
---|
[6452] | 665 | public void DeleteDowntime(Guid id) {
|
---|
[5633] | 666 | using (var db = CreateContext()) {
|
---|
[6452] | 667 | var entity = db.Downtimes.FirstOrDefault(x => x.DowntimeId == id);
|
---|
| 668 | if (entity != null) db.Downtimes.DeleteOnSubmit(entity);
|
---|
[5633] | 669 | db.SubmitChanges();
|
---|
| 670 | }
|
---|
| 671 | }
|
---|
| 672 | #endregion
|
---|
[5955] | 673 |
|
---|
[6229] | 674 | #region Statistics Methods
|
---|
| 675 | public DT.Statistics GetStatistic(Guid id) {
|
---|
| 676 | using (var db = CreateContext()) {
|
---|
| 677 | return Convert.ToDto(db.Statistics.SingleOrDefault(x => x.StatisticsId == id));
|
---|
| 678 | }
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | public IEnumerable<DT.Statistics> GetStatistics(Expression<Func<Statistics, bool>> predicate) {
|
---|
| 682 | using (var db = CreateContext()) {
|
---|
| 683 | return db.Statistics.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
| 684 | }
|
---|
| 685 | }
|
---|
| 686 |
|
---|
| 687 | public Guid AddStatistics(DT.Statistics dto) {
|
---|
| 688 | using (var db = CreateContext()) {
|
---|
| 689 | var entity = Convert.ToEntity(dto);
|
---|
| 690 | db.Statistics.InsertOnSubmit(entity);
|
---|
| 691 | db.SubmitChanges();
|
---|
| 692 | foreach (var slaveStat in dto.SlaveStatistics) {
|
---|
| 693 | slaveStat.Id = entity.StatisticsId;
|
---|
| 694 | db.SlaveStatistics.InsertOnSubmit(Convert.ToEntity(slaveStat));
|
---|
| 695 | }
|
---|
| 696 | foreach (var userStat in dto.UserStatistics) {
|
---|
| 697 | userStat.Id = entity.StatisticsId;
|
---|
| 698 | db.UserStatistics.InsertOnSubmit(Convert.ToEntity(userStat));
|
---|
| 699 | }
|
---|
| 700 | db.SubmitChanges();
|
---|
| 701 | return entity.StatisticsId;
|
---|
| 702 | }
|
---|
| 703 | }
|
---|
| 704 |
|
---|
| 705 | public void DeleteStatistics(Guid id) {
|
---|
| 706 | using (var db = CreateContext()) {
|
---|
| 707 | var entity = db.Statistics.FirstOrDefault(x => x.StatisticsId == id);
|
---|
| 708 | if (entity != null) db.Statistics.DeleteOnSubmit(entity);
|
---|
| 709 | db.SubmitChanges();
|
---|
| 710 | }
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | public List<DT.UserStatistics> GetUserStatistics() {
|
---|
| 714 | using (var db = CreateContext()) {
|
---|
[6267] | 715 | var userStats = new Dictionary<Guid, DT.UserStatistics>();
|
---|
| 716 |
|
---|
| 717 | var usedCoresByUser = from job in db.Jobs
|
---|
| 718 | where job.State == JobState.Calculating
|
---|
| 719 | group job by job.HiveExperiment.OwnerUserId into g
|
---|
| 720 | select new { UserId = g.Key, UsedCores = g.Count() };
|
---|
| 721 |
|
---|
| 722 | foreach (var item in usedCoresByUser) {
|
---|
| 723 | if (!userStats.ContainsKey(item.UserId)) {
|
---|
| 724 | userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
|
---|
| 725 | }
|
---|
| 726 | userStats[item.UserId].UsedCores += item.UsedCores;
|
---|
| 727 | }
|
---|
| 728 |
|
---|
[6229] | 729 | var executionTimesByUser = from job in db.Jobs
|
---|
| 730 | group job by job.HiveExperiment.OwnerUserId into g
|
---|
[6267] | 731 | select new { UserId = g.Key, ExecutionTime = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMs).Sum()) };
|
---|
[6229] | 732 | foreach (var item in executionTimesByUser) {
|
---|
[6267] | 733 | if (!userStats.ContainsKey(item.UserId)) {
|
---|
| 734 | userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
|
---|
| 735 | }
|
---|
| 736 | userStats[item.UserId].ExecutionTime += item.ExecutionTime;
|
---|
[6229] | 737 | }
|
---|
| 738 |
|
---|
[6267] | 739 | // execution times only of finished jobs - necessary to compute efficieny
|
---|
| 740 | var executionTimesFinishedJobs = from job in db.Jobs
|
---|
| 741 | where job.State == JobState.Finished
|
---|
| 742 | group job by job.HiveExperiment.OwnerUserId into g
|
---|
| 743 | select new { UserId = g.Key, ExecutionTimeFinishedJobs = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMs).Sum()) };
|
---|
[6229] | 744 |
|
---|
[6267] | 745 | foreach (var item in executionTimesFinishedJobs) {
|
---|
| 746 | if (!userStats.ContainsKey(item.UserId)) {
|
---|
| 747 | userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
|
---|
| 748 | }
|
---|
| 749 | userStats[item.UserId].ExecutionTimeFinishedJobs += item.ExecutionTimeFinishedJobs;
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | // start to end times only of finished jobs - necessary to compute efficiency
|
---|
| 753 | var startToEndTimesFinishedJobs = from job in db.Jobs
|
---|
| 754 | where job.State == JobState.Finished
|
---|
| 755 | group job by job.HiveExperiment.OwnerUserId into g
|
---|
| 756 | select new {
|
---|
| 757 | UserId = g.Key,
|
---|
[6431] | 758 | 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))
|
---|
[6267] | 759 | };
|
---|
| 760 | foreach (var item in startToEndTimesFinishedJobs) {
|
---|
| 761 | if (!userStats.ContainsKey(item.UserId)) {
|
---|
| 762 | userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
|
---|
| 763 | }
|
---|
| 764 | userStats[item.UserId].StartToEndTime += item.StartToEndTime;
|
---|
| 765 | }
|
---|
| 766 |
|
---|
| 767 | // also consider executiontimes of DeletedJobStats
|
---|
| 768 | var deletedJobsExecutionTimesByUsers = from del in db.DeletedJobStatistics
|
---|
| 769 | group del by del.UserId into g
|
---|
| 770 | select new {
|
---|
| 771 | UserId = g.Key,
|
---|
| 772 | ExecutionTime = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMs).Sum()),
|
---|
| 773 | ExecutionTimeFinishedJobs = TimeSpan.FromMilliseconds(g.Select(x => x.ExecutionTimeMsFinishedJobs).Sum()),
|
---|
| 774 | StartToEndTime = TimeSpan.FromMilliseconds(g.Select(x => x.StartToEndTimeMs).Sum())
|
---|
| 775 | };
|
---|
| 776 | foreach (var item in deletedJobsExecutionTimesByUsers) {
|
---|
| 777 | if (!userStats.ContainsKey(item.UserId)) {
|
---|
| 778 | userStats.Add(item.UserId, new DT.UserStatistics() { UserId = item.UserId });
|
---|
| 779 | }
|
---|
| 780 | userStats[item.UserId].ExecutionTime += item.ExecutionTime;
|
---|
| 781 | userStats[item.UserId].ExecutionTimeFinishedJobs += item.ExecutionTimeFinishedJobs;
|
---|
| 782 | userStats[item.UserId].StartToEndTime += item.StartToEndTime;
|
---|
| 783 | }
|
---|
| 784 |
|
---|
| 785 | return userStats.Values.ToList();
|
---|
[6229] | 786 | }
|
---|
| 787 | }
|
---|
| 788 | #endregion
|
---|
| 789 |
|
---|
[5955] | 790 | #region Helpers
|
---|
| 791 | private void CollectChildJobs(HiveDataContext db, Guid parentJobId, List<Job> collection) {
|
---|
| 792 | var jobs = db.Jobs.Where(j => j.ParentJobId == parentJobId);
|
---|
| 793 | foreach (var job in jobs) {
|
---|
| 794 | collection.Add(job);
|
---|
| 795 | if (job.IsParentJob)
|
---|
| 796 | CollectChildJobs(db, job.JobId, collection);
|
---|
| 797 | }
|
---|
| 798 | }
|
---|
| 799 | #endregion
|
---|
[6267] | 800 | }
|
---|
[4598] | 801 | }
|
---|