Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Services.WebApp.Maintenance/3.3/WebApi/FactClientInfoController.cs @ 17174

Last change on this file since 17174 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 4.7 KB
RevLine 
[12761]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12761]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) {
[12858]38      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      };
[12761]45    }
46
47    public void Aggregate(DateTime start, DateTime end, int entries) {
[12858]48      var pm = PersistenceManager;
49      var factClientInfoDao = pm.FactClientInfoDao;
50      var dimTimeDao = pm.DimTimeDao;
51      var clientIds = pm.UseTransaction(() => factClientInfoDao.GetAll()
52        .Where(x => x.Time >= start && x.Time <= end)
53        .Select(x => x.ClientId)
54        .Distinct()
55      );
56      foreach (var id in clientIds) {
57        AggregateClient(pm, id, start, end, entries);
[12761]58      }
[12858]59      dimTimeDao.DeleteUnusedTimes();
[12761]60    }
61
[12777]62    private void AggregateClient(IPersistenceManager pm, Guid clientId, DateTime start, DateTime end, int entries) {
[12761]63      var factClientInfoDao = pm.FactClientInfoDao;
[12777]64      var clientInfos = pm.UseTransaction(() => factClientInfoDao.GetByClientId(clientId)
65        .Where(x => x.Time >= start && x.Time <= end)
66        .OrderBy(x => x.Time)
67        .ToList()
68      );
[12767]69      var e = clientInfos.GetEnumerator();
70      if (!e.MoveNext()) return;
71      do {
72        var infosToAggregate = new List<FactClientInfo> { e.Current };
73        var prev = e.Current;
[12769]74        while (e.MoveNext() && infosToAggregate.Count() != entries) {
[12767]75          var cur = e.Current;
76          if (prev.IsAllowedToCalculate != cur.IsAllowedToCalculate || prev.SlaveState != cur.SlaveState)
77            break;
78          infosToAggregate.Add(cur);
79          prev = cur;
[12761]80        }
[12769]81        if (infosToAggregate.Count() >= 2) {
[12767]82          AggregateClientInfos(pm, infosToAggregate);
83        }
[12769]84      } while (e.Current != null);
[12767]85    }
86
87    private void AggregateClientInfos(IPersistenceManager pm, List<FactClientInfo> clientInfos) {
88      var factClientInfoDao = pm.FactClientInfoDao;
89      var last = clientInfos.Last();
90      var infos = clientInfos.GroupBy(x => x.ClientId).Select(x => new {
91        NumUsedCores = (int)x.Average(y => y.NumUsedCores),
92        NumTotalCores = (int)x.Average(y => y.NumTotalCores),
93        UsedMemory = (int)x.Average(y => y.UsedMemory),
94        TotalMemory = (int)x.Average(y => y.TotalMemory),
95        CpuUtilization = Math.Round(x.Average(y => y.CpuUtilization), 2),
96        IdleTime = x.Sum(y => y.IdleTime),
97        OfflineTime = x.Sum(y => y.OfflineTime),
98        UnavailableTime = x.Sum(y => y.UnavailableTime)
99      }).SingleOrDefault();
100      if (infos != null) {
101        pm.UseTransaction(() => {
102          last.NumUsedCores = infos.NumUsedCores;
103          last.NumTotalCores = infos.NumTotalCores;
104          last.UsedMemory = infos.UsedMemory;
105          last.TotalMemory = infos.TotalMemory;
106          last.CpuUtilization = infos.CpuUtilization;
107          last.IdleTime = infos.IdleTime;
108          last.OfflineTime = infos.OfflineTime;
109          last.UnavailableTime = infos.UnavailableTime;
110          clientInfos.Remove(last);
111          factClientInfoDao.Delete(clientInfos);
112          pm.SubmitChanges();
113        });
[12761]114      }
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.