Changeset 9533


Ignore:
Timestamp:
05/24/13 17:17:36 (6 years ago)
Author:
pfleck
Message:

#2063:
StatisticsGenerator now updates Time, Job and User dimension-tables.

File:
1 edited

Legend:

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

    r9526 r9533  
    1 namespace HeuristicLab.Services.Hive {
     1using System;
     2using System.Linq;
     3using System.Transactions;
     4using HeuristicLab.Services.Hive.DataAccess;
     5
     6namespace HeuristicLab.Services.Hive {
    27  public class HiveStatisticsGenerator : IStatisticsGenerator {
     8    private static readonly TimeSpan SmallesTimeSpan = new TimeSpan(0, 5, 0);
     9
    310    public void GenerateStatistics() {
    4       //throw new System.NotImplementedException();
     11      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
     12      using (var transaction = new TransactionScope()) {
     13
     14        var newTime = UpdateDimensionTables(db);
     15        if (newTime != null) {
     16          UpdateDataTables(newTime, db);
     17        }
     18
     19        db.SubmitChanges();
     20        transaction.Complete();
     21      }
    522    }
    623
    7     private void UpdateDimensionTables() {
     24    private DimTime UpdateDimensionTables(HiveDataContext db) {
     25      var newTime = UpdateTime(db);
     26      // Update other tables out of sync with time dimension?
     27      UpdateJobs(db);
     28      UpdateUsers(db);
     29      // UpdateClients
    830
     31      return newTime;
    932    }
    1033
    11     private void UpdateDataTables() {
     34    private DimTime UpdateTime(HiveDataContext db) {
     35      var lastUpdate =
     36        (from t in db.DimTimes
     37         orderby t.Time descending
     38         select t.Time)
     39        .FirstOrDefault();
     40
     41      var now = DateTime.Now;
     42      DimTime newTime = null;
     43      if (lastUpdate == default(DateTime) || lastUpdate + SmallesTimeSpan < now) {
     44        newTime = new DimTime {
     45          Time = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute - now.Minute % SmallesTimeSpan.Minutes, 0),
     46          Hour = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0),
     47          Day = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0),
     48          Week = new DateTime(now.Year, now.Month, now.Day - (int)now.DayOfWeek, 0, 0, 0), // Week begins with Sunday
     49          Month = new DateTime(now.Year, now.Month, 1, 0, 0, 0),
     50          Year = new DateTime(now.Year, 1, 1, 0, 0, 0)
     51        };
     52        db.DimTimes.InsertOnSubmit(newTime);
     53      }
     54
     55      return newTime;
     56    }
     57
     58    private void UpdateJobs(HiveDataContext db) {
     59      var newJobs =
     60        from j in db.Jobs
     61        where !db.DimJobs.Select(x => x.JobId).Contains(j.JobId)
     62        select j;
     63
     64      var newDimJobs =
     65        from j in newJobs.ToList()
     66        select new DimJob {
     67          JobId = j.JobId,
     68          JobName = j.Name,
     69          UserId = j.OwnerUserId,
     70          UserName = ""
     71        };
     72
     73      db.DimJobs.InsertAllOnSubmit(newDimJobs);
     74    }
     75
     76    private void UpdateUsers(HiveDataContext db) {
     77      var newUsers =
     78        from u in db.Resources.Where(x => x.OwnerUserId != null).Select(x => x.OwnerUserId.Value).Union(db.Jobs.Select(x => x.OwnerUserId))
     79        where !db.DimUsers.Select(x => x.UserId).Contains(u)
     80        select u;
     81
     82      var newDimUsers =
     83        from u in newUsers.ToList()
     84        select new DimUser {
     85          UserId = u,
     86          Name = ""
     87        };
     88
     89      db.DimUsers.InsertAllOnSubmit(newDimUsers);
     90    }
     91
     92    private void UpdateDataTables(DimTime newTime, HiveDataContext db) {
    1293
    1394    }
Note: See TracChangeset for help on using the changeset viewer.