Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Services.WebApp.Statistics/3.3/WebApi/GroupController.cs

Last change on this file was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 7.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Linq;
24using System.Web.Http;
25using HeuristicLab.Services.Hive;
26using HeuristicLab.Services.Hive.DataAccess.Interfaces;
27using DA = HeuristicLab.Services.Hive.DataAccess;
28using DT = HeuristicLab.Services.WebApp.Statistics.WebApi.DataTransfer;
29
30namespace HeuristicLab.Services.WebApp.Statistics.WebApi {
31  public class GroupController : ApiController {
32    private IPersistenceManager PersistenceManager {
33      get { return ServiceLocator.Instance.PersistenceManager; }
34    }
35
36    public DT.GroupDetails GetGroupDetails(Guid id) {
37      var pm = PersistenceManager;
38      var dimClientDao = pm.DimClientDao;
39      var factClientInfoDao = pm.FactClientInfoDao;
40      var factTaskDao = pm.FactTaskDao;
41      return pm.UseTransaction(() => {
42        var clientTimeData = factClientInfoDao.GetAll()
43          .Join(dimClientDao.GetAll(), x => x.ClientId, y => y.Id, (x, y) => new {
44            y.ParentResourceId,
45            x.IdleTime,
46            x.OfflineTime,
47            x.UnavailableTime
48          })
49          .Where(x => x.ParentResourceId == id)
50          .GroupBy(x => x.ParentResourceId)
51          .Select(x => new {
52            TotalIdleTime = x.Sum(y => y.IdleTime),
53            TotalOfflineTime = x.Sum(y => y.OfflineTime),
54            TotalUnavailableTime = x.Sum(y => y.UnavailableTime)
55          })
56          .FirstOrDefault();
57
58        var taskTimeData = factTaskDao.GetByGroupId(id)
59          .Select(x => new {
60            id,
61            x.CalculatingTime,
62            x.TransferTime
63          })
64          .GroupBy(x => x.id)
65          .Select(x => new {
66            CalculatingTime = x.Sum(y => y.CalculatingTime),
67            TransferTime = x.Sum(y => y.TransferTime)
68          })
69          .FirstOrDefault();
70        return (from client in dimClientDao.GetAllOnlineSlaves().Where(x => x.ParentResourceId == id)
71                join info in factClientInfoDao.GetAll()
72                  on client.Id equals info.ClientId into clientInfoJoin
73                from clientInfo in clientInfoJoin.OrderByDescending(x => x.Time).Take(1)
74                let offline = (clientInfo.SlaveState == DA.SlaveState.Offline)
75                let parent = client.ParentResourceId.HasValue ? dimClientDao.GetById(client.ParentResourceId.Value) : null
76                select new {
77                  ResourceGroupId = client.ParentResourceId,
78                  GroupName = parent != null ? parent.Name : null,
79                  TotalCores = clientInfo.NumTotalCores,
80                  UsedCores = offline ? 0 : clientInfo.NumUsedCores,
81                  TotalMemory = clientInfo.TotalMemory,
82                  UsedMemory = offline ? 0 : clientInfo.UsedMemory,
83                  CpuUtilization = offline ? 0 : clientInfo.CpuUtilization,
84                  SlaveState = clientInfo.SlaveState
85                })
86          .GroupBy(x => new { x.ResourceGroupId, x.GroupName })
87          .Select(x => new DT.GroupDetails {
88            Id = id,
89            Name = x.Key.GroupName,
90            TotalClients = x.Count(),
91            OnlineClients = x.Count(y => y.SlaveState != DA.SlaveState.Offline),
92            TotalCores = x.Sum(y => y.TotalCores),
93            UsedCores = x.Sum(y => y.UsedCores),
94            TotalMemory = x.Sum(y => y.TotalMemory),
95            UsedMemory = x.Sum(y => y.UsedMemory),
96            TotalCpuUtilization = x.Average(y => (double?)y.CpuUtilization) ?? 0.0,
97            ActiveCpuUtilization = x.Where(y => y.SlaveState != DA.SlaveState.Offline).Average(y => (double?)y.CpuUtilization) ?? 0.0,
98            TotalUnavailableTime = clientTimeData != null ? clientTimeData.TotalUnavailableTime : 0,
99            TotalCalculatingTime = taskTimeData != null ? taskTimeData.CalculatingTime : 0,
100            TotalIdleTime = clientTimeData != null ? clientTimeData.TotalIdleTime : 0,
101            TotalOfflineTime = clientTimeData != null ? clientTimeData.TotalOfflineTime : 0,
102            TotalTransferringTime = taskTimeData != null ? taskTimeData.TransferTime : 0,
103            TasksStates = factTaskDao.GetByGroupId(id)
104                                  .GroupBy(y => y.TaskState)
105                                  .Select(y => new DT.TaskStateCount {
106                                    State = y.Key.ToString(),
107                                    Count = y.Count()
108                                  }).ToList()
109          }).FirstOrDefault();
110      });
111    }
112
113    public DT.GroupPage GetGroups(int page, int size) {
114      var pm = PersistenceManager;
115      var dimClientDao = pm.DimClientDao;
116      var factClientInfoDao = pm.FactClientInfoDao;
117      var data = (from client in dimClientDao.GetAllOnlineSlaves()
118                  join info in factClientInfoDao.GetAll()
119                    on client.Id equals info.ClientId into clientInfoJoin
120                  from clientInfo in clientInfoJoin.OrderByDescending(x => x.Time).Take(1)
121                  let offline = (clientInfo.SlaveState == DA.SlaveState.Offline)
122                  select new {
123                    ResourceGroupId = client.ParentResourceId,
124                    TotalCores = clientInfo.NumTotalCores,
125                    UsedCores = offline ? 0 : clientInfo.NumUsedCores,
126                    TotalMemory = clientInfo.TotalMemory,
127                    UsedMemory = offline ? 0 : clientInfo.UsedMemory,
128                    CpuUtilization = offline ? 0 : clientInfo.CpuUtilization,
129                    SlaveState = clientInfo.SlaveState
130                  }).GroupBy(x => x.ResourceGroupId).Select(x => new {
131                    GroupId = x.Key,
132                    TotalClients = x.Count(),
133                    OnlineClients = x.Count(y => y.SlaveState != DA.SlaveState.Offline),
134                    TotalCores = x.Sum(y => y.TotalCores),
135                    UsedCores = x.Sum(y => y.UsedCores),
136                    TotalMemory = x.Sum(y => y.TotalMemory),
137                    UsedMemory = x.Sum(y => y.UsedMemory),
138                    CpuUtilization = x.Where(y => y.SlaveState != DA.SlaveState.Offline).Average(y => (double?)y.CpuUtilization) ?? 0.0
139                  });
140
141      var query = dimClientDao.GetAllOnlineSlaveGroups().Select(x => new {
142        Id = x.ResourceId,
143        Name = x.Name
144      });
145      return pm.UseTransaction(() => new DT.GroupPage {
146        TotalGroups = query.Count(),
147        Groups = query
148        .Skip((page - 1) * size)
149        .Take(size)
150        .Join(data, x => x.Id, y => y.GroupId,
151          (group, info) => new DT.Group {
152            Id = group.Id,
153            Name = group.Name,
154            TotalClients = info.TotalClients,
155            OnlineClients = info.OnlineClients,
156            TotalCores = info.TotalCores,
157            UsedCores = info.UsedCores,
158            TotalMemory = info.TotalMemory,
159            UsedMemory = info.UsedMemory,
160            CpuUtilization = info.CpuUtilization
161          }).ToList()
162      });
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.