Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 3.0 KB
RevLine 
[9533]1using System;
2using System.Linq;
3using System.Transactions;
4using HeuristicLab.Services.Hive.DataAccess;
5
6namespace HeuristicLab.Services.Hive {
[9526]7  public class HiveStatisticsGenerator : IStatisticsGenerator {
[9533]8    private static readonly TimeSpan SmallesTimeSpan = new TimeSpan(0, 5, 0);
9
[9526]10    public void GenerateStatistics() {
[9533]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      }
[9526]22    }
23
[9533]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
[9526]30
[9533]31      return newTime;
[9526]32    }
33
[9533]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();
[9526]40
[9533]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;
[9526]56    }
[9533]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) {
93
94    }
[9526]95  }
96}
Note: See TracBrowser for help on using the repository browser.