- Timestamp:
- 05/27/13 17:03:43 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/HiveStatisticsGenerator.cs
r9538 r9541 6 6 namespace HeuristicLab.Services.Hive { 7 7 public class HiveStatisticsGenerator : IStatisticsGenerator { 8 private static readonly TimeSpan Smalles TimeSpan = new TimeSpan(0, 5, 0);8 private static readonly TimeSpan SmallestTimeSpan = new TimeSpan(0, 5, 0); 9 9 10 10 public void GenerateStatistics() { 11 11 using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) 12 12 using (var transaction = new TransactionScope()) { 13 14 13 var newTime = UpdateDimensionTables(db); 14 db.SubmitChanges(); 15 15 16 if (newTime != null) { 16 UpdateDataTables(newTime, db); 17 UpdateFactTables(newTime, db); 18 db.SubmitChanges(); 17 19 } 18 20 19 db.SubmitChanges();20 21 transaction.Complete(); 21 22 } … … 41 42 var now = DateTime.Now; 42 43 DimTime newTime = null; 43 if (lastUpdate == default(DateTime) || lastUpdate + Smalles TimeSpan < now) {44 if (lastUpdate == default(DateTime) || lastUpdate + SmallestTimeSpan < now) { 44 45 newTime = new DimTime { 45 Time = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute - now.Minute % Smalles TimeSpan.Minutes, 0),46 Time = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute - now.Minute % SmallestTimeSpan.Minutes, 0), 46 47 Hour = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0), 47 48 Day = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0), … … 131 132 } 132 133 133 private void UpdateDataTables(DimTime newTime, HiveDataContext db) { 134 134 private void UpdateFactTables(DimTime newTime, HiveDataContext db) { 135 UpdateClientInfoFacts(newTime, db); 136 //UpdateTaskFacts(newTime, db); 137 } 138 139 private void UpdateClientInfoFacts(DimTime newTime, HiveDataContext db) { 140 var lastFacts = 141 from cf in db.FactClientInfos 142 group cf by cf.ClientId 143 into grpFacts 144 select grpFacts.OrderByDescending(x => x.Time).First(); 145 146 var slaves = 147 from s in db.Resources.OfType<Slave>() 148 join c in db.DimClients on s.ResourceId equals c.ResourceId 149 join lcf in lastFacts on c.Id equals lcf.ClientId into joinCf 150 from cf in joinCf.DefaultIfEmpty() 151 select new { 152 Slave = s, 153 Client = c, 154 LastFact = cf 155 }; 156 157 var clientFacts = 158 from s in slaves.ToList() 159 select new FactClientInfo { 160 DimClient = s.Client, 161 DimTime = newTime, 162 UserId = s.Slave.OwnerUserId, 163 NumUsedCores = 164 s.Slave.Cores != null && s.Slave.FreeCores != null 165 ? s.Slave.Cores.Value - s.Slave.FreeCores.Value 166 : 0, 167 NumTotalCores = s.Slave.Cores ?? 0, 168 UsedMemory = 169 s.Slave.Memory != null && s.Slave.FreeMemory != null 170 ? s.Slave.Memory.Value - s.Slave.FreeMemory.Value 171 : 0, 172 TotalMemory = s.Slave.Memory ?? 0, 173 CpuUtilization = s.Slave.CpuUtilization, 174 TrafficIn = 0, 175 TrafficOut = 0, 176 TotalTimeIdle = CalcNewTotalTime(s.LastFact, newTime.Time, 177 x => x.TotalTimeIdle, 178 () => s.Slave.SlaveState == SlaveState.Idle && s.Slave.IsAllowedToCalculate), 179 TotalTimeCalculating = CalcNewTotalTime(s.LastFact, newTime.Time, 180 x => x.TotalTimeCalculating, 181 () => s.Slave.SlaveState == SlaveState.Calculating), 182 TotalTimeTransferring = 0.0, 183 TotalTimeUnavailable = CalcNewTotalTime(s.LastFact, newTime.Time, 184 x => x.TotalTimeUnavailable, 185 () => s.Slave.SlaveState == SlaveState.Idle && !s.Slave.IsAllowedToCalculate), 186 TotalTimeOffline = CalcNewTotalTime(s.LastFact, newTime.Time, 187 x => x.TotalTimeOffline, 188 () => s.Slave.SlaveState == SlaveState.Offline) 189 }; 190 191 db.FactClientInfos.InsertAllOnSubmit(clientFacts); 192 } 193 194 private double CalcNewTotalTime(FactClientInfo lastFact, DateTime newTime, Func<FactClientInfo, double> selector, Func<bool> condition) { 195 if (lastFact == null) { 196 return 0.0; 197 } 198 return condition() 199 ? selector(lastFact) + (newTime - lastFact.Time).TotalMinutes 200 : selector(lastFact); 135 201 } 136 202 }
Note: See TracChangeset
for help on using the changeset viewer.