1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Transactions;
|
---|
4 | using HeuristicLab.Services.Hive.DataAccess;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Services.Hive {
|
---|
7 | public class HiveStatisticsGenerator : IStatisticsGenerator {
|
---|
8 | private static readonly TimeSpan SmallesTimeSpan = new TimeSpan(0, 5, 0);
|
---|
9 |
|
---|
10 | public void GenerateStatistics() {
|
---|
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 | }
|
---|
22 | }
|
---|
23 |
|
---|
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(db);
|
---|
30 |
|
---|
31 | return newTime;
|
---|
32 | }
|
---|
33 |
|
---|
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 | Month = new DateTime(now.Year, now.Month, 1, 0, 0, 0),
|
---|
49 | Year = new DateTime(now.Year, 1, 1, 0, 0, 0)
|
---|
50 | };
|
---|
51 | db.DimTimes.InsertOnSubmit(newTime);
|
---|
52 | }
|
---|
53 |
|
---|
54 | return newTime;
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void UpdateJobs(HiveDataContext db) {
|
---|
58 | var newJobs =
|
---|
59 | from j in db.Jobs
|
---|
60 | where !db.DimJobs.Select(x => x.JobId).Contains(j.JobId)
|
---|
61 | select j;
|
---|
62 |
|
---|
63 | var newDimJobs =
|
---|
64 | from j in newJobs.ToList()
|
---|
65 | select new DimJob {
|
---|
66 | JobId = j.JobId,
|
---|
67 | JobName = j.Name,
|
---|
68 | UserId = j.OwnerUserId,
|
---|
69 | UserName = ""
|
---|
70 | };
|
---|
71 |
|
---|
72 | db.DimJobs.InsertAllOnSubmit(newDimJobs);
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void UpdateUsers(HiveDataContext db) {
|
---|
76 | var newUsers =
|
---|
77 | from u in db.Resources.Where(x => x.OwnerUserId != null).Select(x => x.OwnerUserId.Value).Union(db.Jobs.Select(x => x.OwnerUserId))
|
---|
78 | where !db.DimUsers.Select(x => x.UserId).Contains(u)
|
---|
79 | select u;
|
---|
80 |
|
---|
81 | var newDimUsers =
|
---|
82 | from u in newUsers.ToList()
|
---|
83 | select new DimUser {
|
---|
84 | UserId = u,
|
---|
85 | Name = ""
|
---|
86 | };
|
---|
87 |
|
---|
88 | db.DimUsers.InsertAllOnSubmit(newDimUsers);
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void UpdateClients(HiveDataContext db) {
|
---|
92 | var removedClients =
|
---|
93 | from c in db.DimClients
|
---|
94 | where c.ExpirationTime == null &&
|
---|
95 | !db.Resources.OfType<Slave>().Select(x => x.ResourceId).Contains(c.ResourceId)
|
---|
96 | select c;
|
---|
97 |
|
---|
98 | var modifiedClients =
|
---|
99 | from s in db.Resources.OfType<Slave>()
|
---|
100 | join c in db.DimClients on s.ResourceId equals c.ResourceId
|
---|
101 | where c.ExpirationTime == null
|
---|
102 | && (s.Name != c.Name || s.ParentResourceId != c.ResourceGroupId ||
|
---|
103 | s.ParentResource.ParentResourceId != c.ResourceGroup2Id)
|
---|
104 | select new { Slave = s, Client = c };
|
---|
105 |
|
---|
106 | foreach (var client in removedClients.Union(modifiedClients.Select(x => x.Client))) {
|
---|
107 | client.ExpirationTime = DateTime.Now;
|
---|
108 | }
|
---|
109 |
|
---|
110 | var newClients =
|
---|
111 | from s in db.Resources.OfType<Slave>()
|
---|
112 | where !db.DimClients.Select(x => x.ResourceId).Contains(s.ResourceId)
|
---|
113 | || modifiedClients.Select(x => x.Slave.ResourceId).Contains(s.ResourceId)
|
---|
114 | select new {
|
---|
115 | Slave = s,
|
---|
116 | Group = s.ParentResourceId,
|
---|
117 | Group2 = s.ParentResource.ParentResourceId
|
---|
118 | };
|
---|
119 |
|
---|
120 | var newDimClients =
|
---|
121 | from s in newClients.ToList()
|
---|
122 | select new DimClient {
|
---|
123 | ResourceId = s.Slave.ResourceId,
|
---|
124 | Name = s.Slave.Name,
|
---|
125 | ExpirationTime = null,
|
---|
126 | ResourceGroupId = s.Group,
|
---|
127 | ResourceGroup2Id = s.Group2
|
---|
128 | };
|
---|
129 |
|
---|
130 | db.DimClients.InsertAllOnSubmit(newDimClients);
|
---|
131 | }
|
---|
132 |
|
---|
133 | private void UpdateDataTables(DimTime newTime, HiveDataContext db) {
|
---|
134 |
|
---|
135 | }
|
---|
136 | }
|
---|
137 | } |
---|