1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Web.Http;
|
---|
26 | using HeuristicLab.Services.Hive;
|
---|
27 | using HeuristicLab.Services.Hive.DataAccess;
|
---|
28 | using HeuristicLab.Services.Hive.DataAccess.Interfaces;
|
---|
29 | using DT = HeuristicLab.Services.WebApp.Maintenance.WebApi.DataTransfer;
|
---|
30 |
|
---|
31 | namespace 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 | 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 | public void Aggregate(DateTime start, DateTime end, int entries) {
|
---|
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);
|
---|
58 | }
|
---|
59 | dimTimeDao.DeleteUnusedTimes();
|
---|
60 | }
|
---|
61 |
|
---|
62 | private void AggregateClient(IPersistenceManager pm, Guid clientId, DateTime start, DateTime end, int entries) {
|
---|
63 | var factClientInfoDao = pm.FactClientInfoDao;
|
---|
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 | );
|
---|
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;
|
---|
74 | while (e.MoveNext() && infosToAggregate.Count() != entries) {
|
---|
75 | var cur = e.Current;
|
---|
76 | if (prev.IsAllowedToCalculate != cur.IsAllowedToCalculate || prev.SlaveState != cur.SlaveState)
|
---|
77 | break;
|
---|
78 | infosToAggregate.Add(cur);
|
---|
79 | prev = cur;
|
---|
80 | }
|
---|
81 | if (infosToAggregate.Count() >= 2) {
|
---|
82 | AggregateClientInfos(pm, infosToAggregate);
|
---|
83 | }
|
---|
84 | } while (e.Current != null);
|
---|
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 | });
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|