Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2429:

  • Unused plugins are now sorted by date and name
  • Fixed a small bug in the FactClientInfo aggregation
File size: 4.6 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, entries);
59        }
60        dimTimeDao.DeleteUnusedTimes();
61      }
62    }
63
64    private void AggregateClient(IPersistenceManager pm, Guid clientId, int entries) {
65      var factClientInfoDao = pm.FactClientInfoDao;
66      var clientInfos = pm.UseTransaction(() => factClientInfoDao.GetByClientId(clientId).OrderBy(x => x.Time).ToList());
67      var e = clientInfos.GetEnumerator();
68      if (!e.MoveNext()) return;
69      do {
70        var infosToAggregate = new List<FactClientInfo> { e.Current };
71        var prev = e.Current;
72        while (e.MoveNext() && infosToAggregate.Count() != entries) {
73          var cur = e.Current;
74          if (prev.IsAllowedToCalculate != cur.IsAllowedToCalculate || prev.SlaveState != cur.SlaveState)
75            break;
76          infosToAggregate.Add(cur);
77          prev = cur;
78        }
79        if (infosToAggregate.Count() >= 2) {
80          AggregateClientInfos(pm, infosToAggregate);
81        }
82      } while (e.Current != null);
83    }
84
85    private void AggregateClientInfos(IPersistenceManager pm, List<FactClientInfo> clientInfos) {
86      var factClientInfoDao = pm.FactClientInfoDao;
87      var last = clientInfos.Last();
88      var infos = clientInfos.GroupBy(x => x.ClientId).Select(x => new {
89        NumUsedCores = (int)x.Average(y => y.NumUsedCores),
90        NumTotalCores = (int)x.Average(y => y.NumTotalCores),
91        UsedMemory = (int)x.Average(y => y.UsedMemory),
92        TotalMemory = (int)x.Average(y => y.TotalMemory),
93        CpuUtilization = Math.Round(x.Average(y => y.CpuUtilization), 2),
94        IdleTime = x.Sum(y => y.IdleTime),
95        OfflineTime = x.Sum(y => y.OfflineTime),
96        UnavailableTime = x.Sum(y => y.UnavailableTime)
97      }).SingleOrDefault();
98      if (infos != null) {
99        pm.UseTransaction(() => {
100          last.NumUsedCores = infos.NumUsedCores;
101          last.NumTotalCores = infos.NumTotalCores;
102          last.UsedMemory = infos.UsedMemory;
103          last.TotalMemory = infos.TotalMemory;
104          last.CpuUtilization = infos.CpuUtilization;
105          last.IdleTime = infos.IdleTime;
106          last.OfflineTime = infos.OfflineTime;
107          last.UnavailableTime = infos.UnavailableTime;
108          clientInfos.Remove(last);
109          factClientInfoDao.Delete(clientInfos);
110          pm.SubmitChanges();
111        });
112      }
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.