Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/HiveStatisticsGenerator.cs @ 9545

Last change on this file since 9545 was 9545, checked in by pfleck, 11 years ago

#2063:
ClientInfo facts are now generated correctly when resources are changed and new client dimensions are created.
Query username for statistics tables from user id.

File size: 7.7 KB
Line 
1using System;
2using System.Linq;
3using System.Transactions;
4using HeuristicLab.Services.Access;
5using HeuristicLab.Services.Hive.DataAccess;
6
7namespace HeuristicLab.Services.Hive {
8  public class HiveStatisticsGenerator : IStatisticsGenerator {
9
10    private IUserManager userManager { get { return ServiceLocator.Instance.UserManager; } }
11
12    private static readonly TimeSpan SmallestTimeSpan = new TimeSpan(0, 5, 0);
13
14    public void GenerateStatistics() {
15      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
16      using (var transaction = new TransactionScope()) {
17        var newTime = UpdateDimensionTables(db);
18        db.SubmitChanges();
19
20        if (newTime != null) {
21          UpdateFactTables(newTime, db);
22          db.SubmitChanges();
23        }
24
25        transaction.Complete();
26      }
27    }
28
29    private DimTime UpdateDimensionTables(HiveDataContext db) {
30      var newTime = UpdateTime(db);
31      // Update other tables out of sync with time dimension?
32      UpdateJobs(db);
33      UpdateUsers(db);
34      UpdateClients(db);
35
36      return newTime;
37    }
38
39    private DimTime UpdateTime(HiveDataContext db) {
40      var lastUpdate =
41        (from t in db.DimTimes
42         orderby t.Time descending
43         select t.Time)
44        .FirstOrDefault();
45
46      var now = DateTime.Now;
47      DimTime newTime = null;
48      if (lastUpdate == default(DateTime) || lastUpdate + SmallestTimeSpan < now) {
49        newTime = new DimTime {
50          Time = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute - now.Minute % SmallestTimeSpan.Minutes, 0),
51          Hour = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0),
52          Day = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0),
53          Month = new DateTime(now.Year, now.Month, 1, 0, 0, 0),
54          Year = new DateTime(now.Year, 1, 1, 0, 0, 0)
55        };
56        db.DimTimes.InsertOnSubmit(newTime);
57      }
58
59      return newTime;
60    }
61
62    private void UpdateJobs(HiveDataContext db) {
63      var newJobs =
64        from j in db.Jobs
65        where !db.DimJobs.Select(x => x.JobId).Contains(j.JobId)
66        select j;
67
68      var newDimJobs =
69        from j in newJobs.ToList()
70        select new DimJob {
71          JobId = j.JobId,
72          JobName = j.Name,
73          UserId = j.OwnerUserId,
74          UserName = userManager.GetUserById(j.OwnerUserId).UserName
75        };
76
77      db.DimJobs.InsertAllOnSubmit(newDimJobs);
78    }
79
80    private void UpdateUsers(HiveDataContext db) {
81      var newUsers =
82        from u in db.Resources.Where(x => x.OwnerUserId != null).Select(x => x.OwnerUserId.Value).Union(db.Jobs.Select(x => x.OwnerUserId))
83        where !db.DimUsers.Select(x => x.UserId).Contains(u)
84        select u;
85
86      var newDimUsers =
87        from u in newUsers.ToList()
88        select new DimUser {
89          UserId = u,
90          Name = userManager.GetUserById(u).UserName
91        };
92
93      db.DimUsers.InsertAllOnSubmit(newDimUsers);
94    }
95
96    private void UpdateClients(HiveDataContext db) {
97      var removedClients =
98        from c in db.DimClients
99        where c.ExpirationTime == null &&
100              !db.Resources.OfType<Slave>().Select(x => x.ResourceId).Contains(c.ResourceId)
101        select c;
102
103      var modifiedClients =
104        from s in db.Resources.OfType<Slave>()
105        join c in db.DimClients on s.ResourceId equals c.ResourceId
106        where c.ExpirationTime == null
107              && (s.Name != c.Name || s.ParentResourceId != c.ResourceGroupId ||
108                  s.ParentResource.ParentResourceId != c.ResourceGroup2Id)
109        select new { Slave = s, Client = c };
110
111      foreach (var client in removedClients.Union(modifiedClients.Select(x => x.Client))) {
112        client.ExpirationTime = DateTime.Now;
113      }
114
115      var newClients =
116        from s in db.Resources.OfType<Slave>()
117        where !db.DimClients.Select(x => x.ResourceId).Contains(s.ResourceId)
118          || modifiedClients.Select(x => x.Slave.ResourceId).Contains(s.ResourceId)
119        select new {
120          Slave = s,
121          Group = s.ParentResourceId,
122          Group2 = s.ParentResource.ParentResourceId
123        };
124
125      var newDimClients =
126        from s in newClients.ToList()
127        select new DimClient {
128          ResourceId = s.Slave.ResourceId,
129          Name = s.Slave.Name,
130          ExpirationTime = null,
131          ResourceGroupId = s.Group,
132          ResourceGroup2Id = s.Group2
133        };
134
135      db.DimClients.InsertAllOnSubmit(newDimClients);
136    }
137
138    private void UpdateFactTables(DimTime newTime, HiveDataContext db) {
139      UpdateClientInfoFacts(newTime, db);
140      //UpdateTaskFacts(newTime, db);
141    }
142
143    private void UpdateClientInfoFacts(DimTime newTime, HiveDataContext db) {
144      var lastFacts =
145        from cf in db.FactClientInfos
146        join r in db.DimClients on cf.ClientId equals r.Id
147        group cf by r.ResourceId into grpFacts
148        select new {
149          ResourceId = grpFacts.Key,
150          Fact = grpFacts.OrderByDescending(x => x.Time).First(),
151        };
152
153      var slaves =
154        from s in db.Resources.OfType<Slave>()
155        join c in db.DimClients on s.ResourceId equals c.ResourceId
156        join lcf in lastFacts on c.ResourceId equals lcf.ResourceId into joinCf
157        from cf in joinCf.DefaultIfEmpty()
158        where c.ExpirationTime == null
159        select new {
160          Slave = s,
161          Client = c,
162          LastFact = cf != null ? cf.Fact : null
163        };
164
165      var clientFacts =
166        from s in slaves.ToList()
167        select new FactClientInfo {
168          DimClient = s.Client,
169          DimTime = newTime,
170          UserId = s.Slave.OwnerUserId,
171          NumUsedCores =
172            s.Slave.Cores != null && s.Slave.FreeCores != null
173              ? s.Slave.Cores.Value - s.Slave.FreeCores.Value
174              : 0,
175          NumTotalCores = s.Slave.Cores ?? 0,
176          UsedMemory =
177            s.Slave.Memory != null && s.Slave.FreeMemory != null
178              ? s.Slave.Memory.Value - s.Slave.FreeMemory.Value
179              : 0,
180          TotalMemory = s.Slave.Memory ?? 0,
181          CpuUtilization = s.Slave.CpuUtilization,
182          TrafficIn = 0,
183          TrafficOut = 0,
184          TotalTimeIdle = CalcNewTotalTime(s.LastFact, newTime.Time,
185                                           x => x.TotalTimeIdle,
186                                           () => s.Slave.SlaveState == SlaveState.Idle && s.Slave.IsAllowedToCalculate),
187          TotalTimeCalculating = CalcNewTotalTime(s.LastFact, newTime.Time,
188                                                  x => x.TotalTimeCalculating,
189                                                  () => s.Slave.SlaveState == SlaveState.Calculating),
190          TotalTimeTransferring = 0.0,
191          TotalTimeUnavailable = CalcNewTotalTime(s.LastFact, newTime.Time,
192                                                  x => x.TotalTimeUnavailable,
193                                                  () => s.Slave.SlaveState == SlaveState.Idle && !s.Slave.IsAllowedToCalculate),
194          TotalTimeOffline = CalcNewTotalTime(s.LastFact, newTime.Time,
195                                              x => x.TotalTimeOffline,
196                                              () => s.Slave.SlaveState == SlaveState.Offline)
197        };
198
199      db.FactClientInfos.InsertAllOnSubmit(clientFacts);
200    }
201
202    private double CalcNewTotalTime(FactClientInfo lastFact, DateTime newTime, Func<FactClientInfo, double> selector, Func<bool> condition) {
203      if (lastFact == null) {
204        return 0.0;
205      }
206      return condition()
207               ? selector(lastFact) + (newTime - lastFact.Time).TotalMinutes
208               : selector(lastFact);
209    }
210  }
211}
Note: See TracBrowser for help on using the repository browser.