Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.WebApp.Statistics/3.3/WebApi/GroupController.cs @ 12562

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

#2388: Fixing spacing and added missing license information

File size: 7.9 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.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      using (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.ResourceGroupId,
45              x.IdleTime,
46              x.OfflineTime,
47              x.UnavailableTime
48            })
49            .Where(x => x.ResourceGroupId == id)
50            .GroupBy(x => x.ResourceGroupId)
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.GetActiveClients().Where(x => x.ResourceGroupId == 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                  select new {
76                    ResourceGroupId = client.ResourceGroupId,
77                    GroupName = client.GroupName,
78                    TotalCores = clientInfo.NumTotalCores,
79                    UsedCores = offline ? 0 : clientInfo.NumUsedCores,
80                    TotalMemory = clientInfo.TotalMemory,
81                    UsedMemory = offline ? 0 : clientInfo.UsedMemory,
82                    CpuUtilization = offline ? 0 : clientInfo.CpuUtilization,
83                    SlaveState = clientInfo.SlaveState
84                  })
85            .GroupBy(x => new { x.ResourceGroupId, x.GroupName })
86            .Select(x => new DT.GroupDetails {
87              Id = id,
88              Name = x.Key.GroupName,
89              TotalClients = x.Count(),
90              OnlineClients = x.Count(y => y.SlaveState != DA.SlaveState.Offline),
91              TotalCores = x.Sum(y => y.TotalCores),
92              UsedCores = x.Sum(y => y.UsedCores),
93              TotalMemory = x.Sum(y => y.TotalMemory),
94              UsedMemory = x.Sum(y => y.UsedMemory),
95              TotalCpuUtilization = x.Average(y => (double?)y.CpuUtilization) ?? 0.0,
96              ActiveCpuUtilization = x.Where(y => y.SlaveState != DA.SlaveState.Offline).Average(y => (double?)y.CpuUtilization) ?? 0.0,
97              TotalUnavailableTime = clientTimeData != null ? clientTimeData.TotalUnavailableTime : 0,
98              TotalCalculatingTime = taskTimeData != null ? taskTimeData.CalculatingTime : 0,
99              TotalIdleTime = clientTimeData != null ? clientTimeData.TotalIdleTime : 0,
100              TotalOfflineTime = clientTimeData != null ? clientTimeData.TotalOfflineTime : 0,
101              TotalTransferringTime = taskTimeData != null ? taskTimeData.TransferTime : 0,
102              TasksStates = factTaskDao.GetByGroupId(id)
103                                    .GroupBy(y => y.TaskState)
104                                    .Select(y => new DT.TaskStateCount {
105                                      State = y.Key.ToString(),
106                                      Count = y.Count()
107                                    }).ToList()
108            }).FirstOrDefault();
109        });
110      }
111    }
112
113    public DT.GroupPage GetGroups(int page, int size) {
114      using (var pm = PersistenceManager) {
115        var dimClientDao = pm.DimClientDao;
116        var factClientInfoDao = pm.FactClientInfoDao;
117        var data = (from client in dimClientDao.GetActiveClients()
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.ResourceGroupId,
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        var query = dimClientDao.GetAll()
141          .GroupBy(x => new { x.ResourceGroupId, x.GroupName })
142          .Select(x => new {
143            Id = x.Key.ResourceGroupId ?? default(Guid),
144            Name = x.Key.GroupName
145          });
146        return pm.UseTransaction(() => new DT.GroupPage {
147          TotalGroups = query.Count(),
148          Groups = query
149          .Skip((page - 1) * size)
150          .Take(size)
151          .Join(data, x => x.Id, y => y.GroupId,
152            (group, info) => new DT.Group {
153              Id = group.Id,
154              Name = group.Name,
155              TotalClients = info.TotalClients,
156              OnlineClients = info.OnlineClients,
157              TotalCores = info.TotalCores,
158              UsedCores = info.UsedCores,
159              TotalMemory = info.TotalMemory,
160              UsedMemory = info.UsedMemory,
161              CpuUtilization = info.CpuUtilization
162            }).ToList()
163        });
164      }
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.