Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.WebApp.Maintenance/3.3/WebApi/FactClientInfoController.cs @ 12777

Last change on this file since 12777 was 12777, checked in by dglaser, 9 years ago

#2429: Start and end dates are now considered when aggregating FactClientInfo

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Web.Http;
26using HeuristicLab.Services.Hive;
27using HeuristicLab.Services.Hive.DataAccess;
28using HeuristicLab.Services.Hive.DataAccess.Interfaces;
29using DT = HeuristicLab.Services.WebApp.Maintenance.WebApi.DataTransfer;
30
31namespace HeuristicLab.Services.WebApp.Maintenance.WebApi {
32  public class FactClientInfoController : ApiController {
33    private IPersistenceManager PersistenceManager {
34      get { return ServiceLocator.Instance.PersistenceManager; }
35    }
36
37    public DT.FactClientInfo GetFactClientInfo(DateTime start, DateTime end) {
38      using (var pm = PersistenceManager) {
39        var factClientInfo = pm.FactClientInfoDao;
40        var query = factClientInfo.GetAll().Where(x => x.Time >= start && x.Time <= end);
41        return new DT.FactClientInfo {
42          Rows = query.Count(),
43          Clients = query.Select(x => x.ClientId).Distinct().Count()
44        };
45      }
46    }
47
48    public void Aggregate(DateTime start, DateTime end, int entries) {
49      using (var pm = PersistenceManager) {
50        var factClientInfoDao = pm.FactClientInfoDao;
51        var dimTimeDao = pm.DimTimeDao;
52        var clientIds = pm.UseTransaction(() => factClientInfoDao.GetAll()
53          .Where(x => x.Time >= start && x.Time <= end)
54          .Select(x => x.ClientId)
55          .Distinct()
56        );
57        foreach (var id in clientIds) {
58          AggregateClient(pm, id, start, end, entries);
59        }
60        dimTimeDao.DeleteUnusedTimes();
61      }
62    }
63
64    private void AggregateClient(IPersistenceManager pm, Guid clientId, DateTime start, DateTime end, int entries) {
65      var factClientInfoDao = pm.FactClientInfoDao;
66      var clientInfos = pm.UseTransaction(() => factClientInfoDao.GetByClientId(clientId)
67        .Where(x => x.Time >= start && x.Time <= end)
68        .OrderBy(x => x.Time)
69        .ToList()
70      );
71      var e = clientInfos.GetEnumerator();
72      if (!e.MoveNext()) return;
73      do {
74        var infosToAggregate = new List<FactClientInfo> { e.Current };
75        var prev = e.Current;
76        while (e.MoveNext() && infosToAggregate.Count() != entries) {
77          var cur = e.Current;
78          if (prev.IsAllowedToCalculate != cur.IsAllowedToCalculate || prev.SlaveState != cur.SlaveState)
79            break;
80          infosToAggregate.Add(cur);
81          prev = cur;
82        }
83        if (infosToAggregate.Count() >= 2) {
84          AggregateClientInfos(pm, infosToAggregate);
85        }
86      } while (e.Current != null);
87    }
88
89    private void AggregateClientInfos(IPersistenceManager pm, List<FactClientInfo> clientInfos) {
90      var factClientInfoDao = pm.FactClientInfoDao;
91      var last = clientInfos.Last();
92      var infos = clientInfos.GroupBy(x => x.ClientId).Select(x => new {
93        NumUsedCores = (int)x.Average(y => y.NumUsedCores),
94        NumTotalCores = (int)x.Average(y => y.NumTotalCores),
95        UsedMemory = (int)x.Average(y => y.UsedMemory),
96        TotalMemory = (int)x.Average(y => y.TotalMemory),
97        CpuUtilization = Math.Round(x.Average(y => y.CpuUtilization), 2),
98        IdleTime = x.Sum(y => y.IdleTime),
99        OfflineTime = x.Sum(y => y.OfflineTime),
100        UnavailableTime = x.Sum(y => y.UnavailableTime)
101      }).SingleOrDefault();
102      if (infos != null) {
103        pm.UseTransaction(() => {
104          last.NumUsedCores = infos.NumUsedCores;
105          last.NumTotalCores = infos.NumTotalCores;
106          last.UsedMemory = infos.UsedMemory;
107          last.TotalMemory = infos.TotalMemory;
108          last.CpuUtilization = infos.CpuUtilization;
109          last.IdleTime = infos.IdleTime;
110          last.OfflineTime = infos.OfflineTime;
111          last.UnavailableTime = infos.UnavailableTime;
112          clientInfos.Remove(last);
113          factClientInfoDao.Delete(clientInfos);
114          pm.SubmitChanges();
115        });
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.