Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Services.Hive/3.3/HiveStatisticsGenerator.cs @ 18198

Last change on this file since 18198 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 25.7 KB
RevLine 
[12468]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12468]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[12441]23using System.Collections.Generic;
24using System.Data.Linq;
25using System.Linq;
[12765]26using HeuristicLab.Services.Access.DataAccess;
[12441]27using HeuristicLab.Services.Hive.DataAccess;
[12468]28using HeuristicLab.Services.Hive.DataAccess.Manager;
[12441]29
30namespace HeuristicLab.Services.Hive {
31  public class HiveStatisticsGenerator : IStatisticsGenerator {
32
[12468]33    private const string UnknownUserName = "Unknown";
[12441]34    private static readonly TimeSpan SmallestTimeSpan = new TimeSpan(0, 5, 0);
[12516]35    private static readonly TaskState[] CompletedStates = { TaskState.Finished, TaskState.Aborted, TaskState.Failed };
[12441]36
37    public void GenerateStatistics() {
[12468]38      using (var pm = new PersistenceManager()) {
[17059]39
[12468]40        pm.UseTransaction(() => {
[17059]41          UpdateDimProjectTable(pm);
42          pm.SubmitChanges();
43        });
44
45        pm.UseTransaction(() => {
[12468]46          UpdateDimUserTable(pm);
[17059]47         
[12468]48          UpdateDimJobTable(pm);
[12484]49          UpdateDimClientsTable(pm);
[12468]50          pm.SubmitChanges();
51        });
[12441]52
[12516]53        DimTime time = null;
[12468]54        pm.UseTransaction(() => {
[12516]55          time = UpdateDimTimeTable(pm);
56          pm.SubmitChanges();
57        });
58
59        if (time != null) {
60          pm.UseTransaction(() => {
61            UpdateFactClientInfoTable(time, pm);
[12468]62            pm.SubmitChanges();
[17059]63            UpdateFactProjectInfoTable(time, pm);
64            pm.SubmitChanges();
[12516]65          });
66
67          pm.UseTransaction(() => {
[12468]68            try {
[17067]69              UpdateFactTaskTable(pm);
[12516]70              UpdateExistingDimJobs(pm);
[17067]71              FlagJobsForDeletion(pm);
[12516]72              pm.SubmitChanges();
[12468]73            }
74            catch (DuplicateKeyException e) {
75              var logger = LogFactory.GetLogger(typeof(HiveStatisticsGenerator).Namespace);
76              logger.Log(string.Format(
77                @"Propable change from summertime to wintertime, resulting in overlapping times.
78                          On wintertime to summertime change, slave timeouts and a fact gap will occur.
79                          Exception Details: {0}", e));
80            }
[12516]81          });
82        }
[12441]83      }
84    }
85
[12468]86    private DimTime UpdateDimTimeTable(PersistenceManager pm) {
87      var dimTimeDao = pm.DimTimeDao;
[12441]88      var now = DateTime.Now;
[12468]89      var timeEntry = new DimTime {
[12484]90        Time = now,
91        Minute = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0),
[12468]92        Hour = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0),
93        Day = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0),
94        Month = new DateTime(now.Year, now.Month, 1, 0, 0, 0),
95        Year = new DateTime(now.Year, 1, 1, 0, 0, 0)
96      };
[12484]97      return dimTimeDao.Save(timeEntry);
[12441]98    }
99
[12468]100    private void UpdateDimUserTable(PersistenceManager pm) {
101      var dimUserDao = pm.DimUserDao;
102      var resourceDao = pm.ResourceDao;
103      var jobDao = pm.JobDao;
104      var existingUserIds = dimUserDao.GetAll().Select(x => x.UserId);
105      var vaildResourceOwnerIds = resourceDao.GetResourcesWithValidOwner().Select(x => x.OwnerUserId.Value);
106      var jobOwnerIds = jobDao.GetAll().Select(x => x.OwnerUserId);
107      var newUserIds = vaildResourceOwnerIds
108        .Union(jobOwnerIds)
109        .Where(id => !existingUserIds.Contains(id))
110        .ToList();
111      dimUserDao.Save(newUserIds.Select(x => new DimUser {
112        UserId = x,
113        Name = GetUserName(x)
114      }));
[12441]115    }
116
[17059]117    // add new projects
118    // delete expired projects
119    // update information of existing projects
120    private void UpdateDimProjectTable(PersistenceManager pm) {
121      var projectDao = pm.ProjectDao;
122      var dimProjectDao = pm.DimProjectDao;
123
124      var projects = projectDao.GetAll().ToList();
125      var dimProjects = dimProjectDao.GetAllOnlineProjects().ToList();
126
127      var onlineProjects = dimProjects.Where(x => projects.Select(y => y.ProjectId).Contains(x.ProjectId));
128      var addedProjects = projects.Where(x => !dimProjects.Select(y => y.ProjectId).Contains(x.ProjectId));
129      var removedProjects = dimProjects.Where(x => !projects.Select(y => y.ProjectId).Contains(x.ProjectId));
130
131      // set expiration time of removed projects
132      foreach (var p in removedProjects) {
133        p.DateExpired = DateTime.Now;
134      }
135
136      // add new projects
137      dimProjectDao.Save(addedProjects.Select(x => new DimProject {
138        ProjectId = x.ProjectId,
139        ParentProjectId = x.ParentProjectId,
140        Name = x.Name,
141        Description = x.Description,
142        OwnerUserId = x.OwnerUserId,
143        StartDate = x.StartDate,
144        EndDate = x.EndDate,
145        DateCreated = x.DateCreated,
146        DateExpired = null
147      }));
148
149      // expire project if its parent has changed and create a new entry
150      // otherwise perform "normal" update
151      foreach (var dimP in onlineProjects) {
152        var p = projects.Where(x => x.ProjectId == dimP.ProjectId).SingleOrDefault();
153        if (p != null) {
154          if (dimP.ParentProjectId == null ? p.ParentProjectId != null : dimP.ParentProjectId != p.ParentProjectId) { // or: (!object.Equals(dimP.ParentProjectId, p.ParentProjectId))
155            dimP.DateExpired = DateTime.Now;
156            dimProjectDao.Save(new DimProject {
157              ProjectId = p.ProjectId,
158              ParentProjectId = p.ParentProjectId,
159              Name = p.Name,
160              Description = p.Description,
161              OwnerUserId = p.OwnerUserId,
162              StartDate = p.StartDate,
163              EndDate = p.EndDate,
164              DateCreated = p.DateCreated,
165              DateExpired = null
166            });
167          } else {
168            dimP.Name = p.Name;
169            dimP.Description = p.Description;
170            dimP.OwnerUserId = p.OwnerUserId;
171            dimP.StartDate = p.StartDate;
172            dimP.EndDate = p.EndDate;
173          }
174        }
175      }
176    }
177
[12468]178    private void UpdateDimJobTable(PersistenceManager pm) {
[17059]179      var dimProjectDao = pm.DimProjectDao;
[12468]180      var dimJobDao = pm.DimJobDao;
181      var jobDao = pm.JobDao;
[12516]182      var taskDao = pm.TaskDao;
[12468]183      var dimJobIds = dimJobDao.GetAll().Select(x => x.JobId);
184      var newJobs = jobDao.GetAll()
185        .Where(x => !dimJobIds.Contains(x.JobId))
186        .Select(x => new {
187          JobId = x.JobId,
188          UserId = x.OwnerUserId,
189          JobName = x.Name ?? string.Empty,
[12516]190          DateCreated = x.DateCreated,
[17059]191          ProjectId = dimProjectDao.GetLastValidIdByProjectId(x.ProjectId),
[12516]192          TotalTasks = taskDao.GetAll().Count(y => y.JobId == x.JobId)
[12468]193        })
194        .ToList();
195      dimJobDao.Save(newJobs.Select(x => new DimJob {
196        JobId = x.JobId,
197        JobName = x.JobName,
198        UserId = x.UserId,
[12477]199        UserName = GetUserName(x.UserId),
[12516]200        DateCreated = x.DateCreated,
[17059]201        ProjectId = x.ProjectId,
[12516]202        TotalTasks = x.TotalTasks,
203        CompletedTasks = 0,
204        DateCompleted = null
[12468]205      }));
[12441]206    }
207
[12516]208    private void UpdateExistingDimJobs(PersistenceManager pm) {
[17059]209      var dimProjectDao = pm.DimProjectDao;
[12776]210      var jobDao = pm.JobDao;
[12516]211      var dimJobDao = pm.DimJobDao;
212      var factTaskDao = pm.FactTaskDao;
213      foreach (var dimJob in dimJobDao.GetNotCompletedJobs()) {
214        var taskStates = factTaskDao.GetByJobId(dimJob.JobId)
215            .GroupBy(x => x.TaskState)
216            .Select(x => new {
217              State = x.Key,
218              Count = x.Count()
219            }).ToList();
220        int totalTasks = 0, completedTasks = 0;
221        foreach (var state in taskStates) {
222          totalTasks += state.Count;
223          if (CompletedStates.Contains(state.State)) {
224            completedTasks += state.Count;
225          }
226        }
[17059]227        var job = jobDao.GetById(dimJob.JobId);
[12516]228        if (totalTasks == completedTasks) {
[12776]229          var completeDate = factTaskDao.GetLastCompletedTaskFromJob(dimJob.JobId);
230          if (completeDate == null) {
[17059]231            if (job == null) {
[12776]232              completeDate = DateTime.Now;
233            }
234          }
235          dimJob.DateCompleted = completeDate;
[12516]236        }
[17059]237        if(job != null) {
238          dimJob.JobName = job.Name;
239          dimJob.ProjectId = dimProjectDao.GetLastValidIdByProjectId(job.ProjectId);
240        }
241
[12516]242        dimJob.TotalTasks = totalTasks;
243        dimJob.CompletedTasks = completedTasks;
244      }
245    }
246
[17059]247    private void FlagJobsForDeletion(PersistenceManager pm) {
248      var jobDao = pm.JobDao;
249      var jobs = jobDao.GetJobsReadyForDeletion();
250      foreach(var job in jobs) {
251        job.State = JobState.DeletionPending;
252      }
253    }
254
[12484]255    private void UpdateDimClientsTable(PersistenceManager pm) {
[12468]256      var dimClientDao = pm.DimClientDao;
[17059]257      var resourceDao = pm.ResourceDao;
[12441]258
[17059]259      var resources = resourceDao.GetAll().ToList();
260      var dimClients = dimClientDao.GetAllOnlineClients().ToList();
[12441]261
[17059]262      var onlineClients = dimClients.Where(x => resources.Select(y => y.ResourceId).Contains(x.ResourceId));
263      var addedResources = resources.Where(x => !dimClients.Select(y => y.ResourceId).Contains(x.ResourceId));
264      var removedResources = dimClients.Where(x => !resources.Select(y => y.ResourceId).Contains(x.ResourceId));
265
266      // set expiration time of removed resources
267      foreach(var r in removedResources) {
268        r.DateExpired = DateTime.Now;
269      }
270
271      // add new resources
272      dimClientDao.Save(addedResources.Select(x => new DimClient {
[12468]273        ResourceId = x.ResourceId,
[17059]274        ParentResourceId = x.ParentResourceId,
[12468]275        Name = x.Name,
[17059]276        ResourceType = x.ResourceType,
277        DateCreated = DateTime.Now,
278        DateExpired = null
[12468]279      }));
[17059]280
281      // expire client if its parent has changed and create a new entry
282      // otherwise perform "normal" update
283      foreach(var dimc in onlineClients) {
284        var r = resources.Where(x => x.ResourceId == dimc.ResourceId).SingleOrDefault();
285        if(r != null) {
286          if(dimc.ParentResourceId == null ? r.ParentResourceId != null : dimc.ParentResourceId != r.ParentResourceId) {
287            var now = DateTime.Now;
288            dimc.DateExpired = now;
289            dimClientDao.Save(new DimClient {
290              ResourceId = r.ResourceId,
291              ParentResourceId = r.ParentResourceId,
292              Name = r.Name,
293              ResourceType = r.ResourceType,
294              DateCreated = now,
295              DateExpired = null
296            });
297          } else {
298            dimc.Name = r.Name;
299          }
300        }
301      }
[12441]302    }
303
[17059]304    //// (1) for new slaves (not yet reported in Table DimClients) ...
305    //// and modified slaves (name or parent resource changed) a new DimClient-entry is created
306    //// (2) for already reported removed and modifid clients the expiration date is set
307    //private void UpdateDimClientsTableOld(PersistenceManager pm) {
308    //  var dimClientDao = pm.DimClientDao;
309    //  var slaveDao = pm.SlaveDao;
310    //  var slaves = slaveDao.GetAll();
311    //  var recentlyAddedClients = dimClientDao.GetAllOnlineClients();
312    //  var slaveIds = slaves.Select(x => x.ResourceId);
313
314    //  var removedClientIds = recentlyAddedClients
315    //    .Where(x => !slaveIds.Contains(x.ResourceId))
316    //    .Select(x => x.Id);
317    //  var modifiedClients =
318    //    from slave in slaves
319    //    join client in recentlyAddedClients on slave.ResourceId equals client.ResourceId
320    //    where (slave.Name != client.Name
321    //           || slave.ParentResourceId == null && client.ResourceGroupId != null // because both can be null and null comparison
322    //           || slave.ParentResourceId != null && client.ResourceGroupId == null // does return no entry on the sql server
323    //           || slave.ParentResourceId != client.ResourceGroupId
324    //           || ((slave.ParentResource != null) && slave.ParentResource.ParentResourceId != client.ResourceGroup2Id))
325    //    select new {
326    //      SlaveId = slave.ResourceId,
327    //      ClientId = client.Id
328    //    };
329    //  var clientIds = dimClientDao.GetAllOnlineClients().Select(x => x.ResourceId);
330    //  var modifiedClientIds = modifiedClients.Select(x => x.SlaveId);
331    //  var newClients = slaves
332    //    .Where(x => !clientIds.Contains(x.ResourceId)
333    //                || modifiedClientIds.Contains(x.ResourceId))
334    //    .Select(x => new {
335    //      x.ResourceId,
336    //      x.Name,
337    //      ResourceGroupId = x.ParentResourceId,
338    //      GroupName = x.ParentResource != null ? x.ParentResource.Name : null,
339    //      ResourceGroup2Id = x.ParentResource != null ? x.ParentResource.ParentResourceId : null,
340    //      GroupName2 = x.ParentResource != null ? x.ParentResource.ParentResource != null ? x.ParentResource.ParentResource.Name : null : null
341    //    })
342    //    .ToList();
343
344    //  var clientsToUpdate = removedClientIds.Union(modifiedClients.Select(x => x.ClientId));
345    //  dimClientDao.UpdateExpirationTime(clientsToUpdate, DateTime.Now);
346    //  dimClientDao.Save(newClients.Select(x => new DimClient {
347    //    ResourceId = x.ResourceId,
348    //    Name = x.Name,
349    //    ExpirationTime = null,
350    //    ResourceGroupId = x.ResourceGroupId,
351    //    GroupName = x.GroupName,
352    //    ResourceGroup2Id = x.ResourceGroup2Id,
353    //    GroupName2 = x.GroupName2
354    //  }));
355    //}
356
357
[12468]358    private void UpdateFactClientInfoTable(DimTime newTime, PersistenceManager pm) {
359      var factClientInfoDao = pm.FactClientInfoDao;
360      var slaveDao = pm.SlaveDao;
361      var dimClientDao = pm.DimClientDao;
[12441]362
[12468]363      var newRawFactInfos =
364        from s in slaveDao.GetAll()
[17059]365        join c in dimClientDao.GetAllOnlineSlaves() on s.ResourceId equals c.ResourceId
[12468]366        join lcf in factClientInfoDao.GetLastUpdateTimestamps() on c.ResourceId equals lcf.ResourceId into joinCf
[12441]367        from cf in joinCf.DefaultIfEmpty()
368        select new {
[12468]369          ClientId = c.Id,
370          UserId = s.OwnerUserId ?? Guid.Empty,
371          TotalCores = s.Cores ?? 0,
372          FreeCores = s.FreeCores ?? 0,
373          TotalMemory = s.Memory ?? 0,
374          FreeMemory = s.FreeMemory ?? 0,
375          CpuUtilization = s.CpuUtilization,
376          SlaveState = s.SlaveState,
377          IsAllowedToCalculate = s.IsAllowedToCalculate,
378          LastFactTimestamp = cf.Timestamp
[12441]379        };
380
[12468]381      factClientInfoDao.Save(
382        from x in newRawFactInfos.ToList()
383        let duration = x.LastFactTimestamp != null
[12516]384                       ? (int)(newTime.Time - (DateTime)x.LastFactTimestamp).TotalSeconds
385                       : (int)SmallestTimeSpan.TotalSeconds
[12441]386        select new FactClientInfo {
[12468]387          ClientId = x.ClientId,
[12441]388          DimTime = newTime,
[12468]389          UserId = x.UserId,
390          NumUsedCores = x.TotalCores - x.FreeCores,
[12516]391          NumTotalCores = x.TotalCores,
[12468]392          UsedMemory = x.TotalMemory - x.FreeMemory,
393          TotalMemory = x.TotalMemory,
[12551]394          CpuUtilization = Math.Round(x.CpuUtilization, 2),
[12468]395          SlaveState = x.SlaveState,
[12551]396          IdleTime = x.SlaveState == SlaveState.Idle && x.IsAllowedToCalculate ? duration : 0,
397          UnavailableTime = x.SlaveState == SlaveState.Idle && !x.IsAllowedToCalculate ? duration : 0,
398          OfflineTime = x.SlaveState == SlaveState.Offline ? duration : 0,
399          IsAllowedToCalculate = x.IsAllowedToCalculate
[12468]400        }
401      );
[12441]402    }
403
[17059]404    private void UpdateFactProjectInfoTable(DimTime newTime, PersistenceManager pm) {
405      var factProjectInfoDao = pm.FactProjectInfoDao;
406      var dimProjectDao = pm.DimProjectDao;
407      var projectDao = pm.ProjectDao;
408
409      var projectAvailabilityStats = projectDao.GetAvailabilityStatsPerProject();
410      var projectUsageStats = projectDao.GetUsageStatsPerProject();
411      var dimProjects = dimProjectDao.GetAllOnlineProjects().ToList();
412
413      factProjectInfoDao.Save(
414        from dimp in dimProjects
415        let aStats = projectAvailabilityStats.Where(x => x.ProjectId == dimp.ProjectId).SingleOrDefault()
416        let uStats = projectUsageStats.Where(x => x.ProjectId == dimp.ProjectId).SingleOrDefault()
417        select new FactProjectInfo {
418            ProjectId = dimp.Id,
419            DimTime = newTime,
420            NumTotalCores = aStats != null ? aStats.Cores : 0,
421            TotalMemory = aStats != null ? aStats.Memory : 0,
422            NumUsedCores = uStats != null ? uStats.Cores : 0,
423            UsedMemory = uStats != null ? uStats.Memory : 0
424          }
425        );
426    }
427
428    private void UpdateFactTaskTable(PersistenceManager pm) {
[12468]429      var factTaskDao = pm.FactTaskDao;
430      var taskDao = pm.TaskDao;
431      var dimClientDao = pm.DimClientDao;
[12441]432
[12468]433      var factTaskIds = factTaskDao.GetAll().Select(x => x.TaskId);
[17059]434      var notFinishedFactTasks = factTaskDao.GetNotFinishedTasks();
435      //var notFinishedFactTasks = factTaskDao.GetNotFinishedTasks().Select(x => new {
436      //  x.TaskId,
437      //  x.LastClientId
438      //});
[12468]439
[17059]440      // query several properties for all new and not finished tasks
441      // in order to use them later either...
442      // (1) to update the fact task entry of not finished tasks
443      // (2) to insert a new fact task entry for new tasks
444      var newAndNotFinishedTasks =
[12962]445        (from task in taskDao.GetAllChildTasks()
446         let stateLogs = task.StateLogs.OrderByDescending(x => x.DateTime)
447         let lastSlaveId = stateLogs.First(x => x.SlaveId != null).SlaveId
448         where (!factTaskIds.Contains(task.TaskId)
449                || notFinishedFactTasks.Select(x => x.TaskId).Contains(task.TaskId))
450         join lastFactTask in notFinishedFactTasks on task.TaskId equals lastFactTask.TaskId into lastFactPerTask
451         from lastFact in lastFactPerTask.DefaultIfEmpty()
[17059]452         join client in dimClientDao.GetAllOnlineClients() on lastSlaveId equals client.ResourceId into clientsPerSlaveId
[12962]453         from client in clientsPerSlaveId.DefaultIfEmpty()
454         select new {
455           TaskId = task.TaskId,
456           JobId = task.JobId,
457           Priority = task.Priority,
458           CoresRequired = task.CoresNeeded,
459           MemoryRequired = task.MemoryNeeded,
460           State = task.State,
461           StateLogs = stateLogs.OrderBy(x => x.DateTime),
462           LastClientId = client != null
463                          ? client.Id : lastFact != null
464                          ? lastFact.LastClientId : (Guid?)null,
465           NotFinishedTask = notFinishedFactTasks.Any(y => y.TaskId == task.TaskId)
466         }).ToList();
467
[17059]468      // (1) update data of already existing facts
469      // i.e. for all in newAndNotFinishedTasks where NotFinishedTask = true
470      foreach (var notFinishedFactTask in notFinishedFactTasks) {
471        var nfftUpdate = newAndNotFinishedTasks.Where(x => x.TaskId == notFinishedFactTask.TaskId).SingleOrDefault();
472        if(nfftUpdate != null) {
473          var taskData = CalculateFactTaskData(nfftUpdate.StateLogs);
474
475          notFinishedFactTask.StartTime = taskData.StartTime;
476          notFinishedFactTask.EndTime = taskData.EndTime;
477          notFinishedFactTask.LastClientId = nfftUpdate.LastClientId;
478          notFinishedFactTask.Priority = nfftUpdate.Priority;
479          notFinishedFactTask.CoresRequired = nfftUpdate.CoresRequired;
480          notFinishedFactTask.MemoryRequired = nfftUpdate.MemoryRequired;
481          notFinishedFactTask.NumCalculationRuns = taskData.CalculationRuns;
482          notFinishedFactTask.NumRetries = taskData.Retries;
483          notFinishedFactTask.WaitingTime = taskData.WaitingTime;
484          notFinishedFactTask.CalculatingTime = taskData.CalculatingTime;
485          notFinishedFactTask.TransferTime = taskData.TransferTime;
486          notFinishedFactTask.TaskState = nfftUpdate.State;
487          notFinishedFactTask.Exception = taskData.Exception;
488          notFinishedFactTask.InitialWaitingTime = taskData.InitialWaitingTime;
489        }
490      }
491
492      // (2) insert facts for new tasks
493      // i.e. for all in newAndNotFinishedTasks where NotFinishedTask = false
[12468]494      factTaskDao.Save(
[17059]495        from x in newAndNotFinishedTasks
[12962]496        where !x.NotFinishedTask
[12484]497        let taskData = CalculateFactTaskData(x.StateLogs)
[12441]498        select new FactTask {
[12468]499          TaskId = x.TaskId,
500          JobId = x.JobId,
[12484]501          StartTime = taskData.StartTime,
502          EndTime = taskData.EndTime,
[12468]503          LastClientId = x.LastClientId,
504          Priority = x.Priority,
505          CoresRequired = x.CoresRequired,
506          MemoryRequired = x.MemoryRequired,
[12484]507          NumCalculationRuns = taskData.CalculationRuns,
508          NumRetries = taskData.Retries,
509          WaitingTime = taskData.WaitingTime,
510          CalculatingTime = taskData.CalculatingTime,
511          TransferTime = taskData.TransferTime,
[12468]512          TaskState = x.State,
[12484]513          Exception = taskData.Exception,
514          InitialWaitingTime = taskData.InitialWaitingTime
[12468]515        });
[12962]516
517
[17059]518      ////update data of already existing facts
519      //foreach (var notFinishedTask in factTaskDao.GetNotFinishedTasks()) {
520      //  var ntc = newTasks.Where(x => x.TaskId == notFinishedTask.TaskId);
521      //  if (ntc.Any()) {
522      //    var x = ntc.Single();
523      //    var taskData = CalculateFactTaskData(x.StateLogs);
524
525      //    notFinishedTask.StartTime = taskData.StartTime;
526      //    notFinishedTask.EndTime = taskData.EndTime;
527      //    notFinishedTask.LastClientId = x.LastClientId;
528      //    notFinishedTask.Priority = x.Priority;
529      //    notFinishedTask.CoresRequired = x.CoresRequired;
530      //    notFinishedTask.MemoryRequired = x.MemoryRequired;
531      //    notFinishedTask.NumCalculationRuns = taskData.CalculationRuns;
532      //    notFinishedTask.NumRetries = taskData.Retries;
533      //    notFinishedTask.WaitingTime = taskData.WaitingTime;
534      //    notFinishedTask.CalculatingTime = taskData.CalculatingTime;
535      //    notFinishedTask.TransferTime = taskData.TransferTime;
536      //    notFinishedTask.TaskState = x.State;
537      //    notFinishedTask.Exception = taskData.Exception;
538      //    notFinishedTask.InitialWaitingTime = taskData.InitialWaitingTime;
539      //  }
540      //}
[12468]541    }
[12441]542
[12468]543    private string GetUserName(Guid userId) {
544      try {
[12765]545        // we cannot use the ServiceLocator.Instance.UserManager since the janitor service
546        // is not hosted in the iis the MemberShip.GetUser method causes exceptions
547        // needs to be further investigated current workaround: use the authenticationcontext
548        // we could also connect to the access service to get the user name
549        using (ASPNETAuthenticationDataContext dc = new ASPNETAuthenticationDataContext()) {
550          var user = dc.aspnet_Users.SingleOrDefault(x => x.UserId == userId);
551          return user != null ? user.UserName : UnknownUserName;
552        }
[12468]553      }
554      catch (Exception) {
555        return UnknownUserName;
556      }
[12441]557    }
558
[12484]559    private class FactTaskData {
560      public int CalculationRuns { get; set; }
561      public int Retries { get; set; }
[12551]562      public long CalculatingTime { get; set; }
563      public long WaitingTime { get; set; }
564      public long TransferTime { get; set; }
565      public long InitialWaitingTime { get; set; }
[12484]566      public string Exception { get; set; }
567      public DateTime? StartTime { get; set; }
568      public DateTime? EndTime { get; set; }
[12441]569    }
570
[12484]571    private FactTaskData CalculateFactTaskData(IEnumerable<StateLog> stateLogs) {
572      var factTaskData = new FactTaskData();
573      var enumerator = stateLogs.GetEnumerator();
574      if (enumerator.MoveNext()) {
575        StateLog current = enumerator.Current, first = current, prev = null;
576        while (current != null) {
577          var next = enumerator.MoveNext() ? enumerator.Current : null;
578          int timeSpanInSeconds;
579          if (next != null) {
580            timeSpanInSeconds = (int)(next.DateTime - current.DateTime).TotalSeconds;
581          } else {
582            timeSpanInSeconds = (int)(DateTime.Now - current.DateTime).TotalSeconds;
583            factTaskData.Exception = current.Exception;
584          }
585          switch (current.State) {
586            case TaskState.Calculating:
587              factTaskData.CalculatingTime += timeSpanInSeconds;
588              factTaskData.CalculationRuns++;
589              if (factTaskData.CalculationRuns == 1) {
590                factTaskData.StartTime = current.DateTime;
591                factTaskData.InitialWaitingTime = (int)(current.DateTime - first.DateTime).TotalSeconds;
592              }
593              if (prev != null && prev.State != TaskState.Transferring) {
594                factTaskData.Retries++;
595              }
596              break;
[12441]597
[12484]598            case TaskState.Waiting:
599              factTaskData.WaitingTime += timeSpanInSeconds;
600              break;
[12441]601
[12484]602            case TaskState.Transferring:
603              factTaskData.TransferTime += timeSpanInSeconds;
604              break;
605
606            case TaskState.Finished:
607            case TaskState.Failed:
608            case TaskState.Aborted:
609              factTaskData.EndTime = current.DateTime;
610              break;
611          }
612          prev = current;
613          current = next;
614        }
[12441]615      }
[12484]616      return factTaskData;
[12441]617    }
618  }
619}
Note: See TracBrowser for help on using the repository browser.