Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/27/13 17:03:43 (11 years ago)
Author:
pfleck
Message:

#2063:
UserId in ClientInfo fact table is nullable. UserId is removed from primary key therefore.
StatisticsGenerator insert new ClientInfo into fact table.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/HiveStatisticsGenerator.cs

    r9538 r9541  
    66namespace HeuristicLab.Services.Hive {
    77  public class HiveStatisticsGenerator : IStatisticsGenerator {
    8     private static readonly TimeSpan SmallesTimeSpan = new TimeSpan(0, 5, 0);
     8    private static readonly TimeSpan SmallestTimeSpan = new TimeSpan(0, 5, 0);
    99
    1010    public void GenerateStatistics() {
    1111      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
    1212      using (var transaction = new TransactionScope()) {
    13 
    1413        var newTime = UpdateDimensionTables(db);
     14        db.SubmitChanges();
     15
    1516        if (newTime != null) {
    16           UpdateDataTables(newTime, db);
     17          UpdateFactTables(newTime, db);
     18          db.SubmitChanges();
    1719        }
    1820
    19         db.SubmitChanges();
    2021        transaction.Complete();
    2122      }
     
    4142      var now = DateTime.Now;
    4243      DimTime newTime = null;
    43       if (lastUpdate == default(DateTime) || lastUpdate + SmallesTimeSpan < now) {
     44      if (lastUpdate == default(DateTime) || lastUpdate + SmallestTimeSpan < now) {
    4445        newTime = new DimTime {
    45           Time = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute - now.Minute % SmallesTimeSpan.Minutes, 0),
     46          Time = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute - now.Minute % SmallestTimeSpan.Minutes, 0),
    4647          Hour = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0),
    4748          Day = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0),
     
    131132    }
    132133
    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);
    135201    }
    136202  }
Note: See TracChangeset for help on using the changeset viewer.