Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2522_RefactorPluginInfrastructure/HeuristicLab.Services.WebApp.Statistics/3.3/WebApi/GroupController.cs @ 15651

Last change on this file since 15651 was 12858, checked in by ascheibe, 9 years ago

#2388

  • prevent disposing of hive data context
  • more cleanups
File size: 7.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.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.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    public DT.GroupPage GetGroups(int page, int size) {
113      var pm = PersistenceManager;
114      var dimClientDao = pm.DimClientDao;
115      var factClientInfoDao = pm.FactClientInfoDao;
116      var data = (from client in dimClientDao.GetActiveClients()
117                  join info in factClientInfoDao.GetAll()
118                    on client.Id equals info.ClientId into clientInfoJoin
119                  from clientInfo in clientInfoJoin.OrderByDescending(x => x.Time).Take(1)
120                  let offline = (clientInfo.SlaveState == DA.SlaveState.Offline)
121                  select new {
122                    ResourceGroupId = client.ResourceGroupId,
123                    TotalCores = clientInfo.NumTotalCores,
124                    UsedCores = offline ? 0 : clientInfo.NumUsedCores,
125                    TotalMemory = clientInfo.TotalMemory,
126                    UsedMemory = offline ? 0 : clientInfo.UsedMemory,
127                    CpuUtilization = offline ? 0 : clientInfo.CpuUtilization,
128                    SlaveState = clientInfo.SlaveState
129                  }).GroupBy(x => x.ResourceGroupId).Select(x => new {
130                    GroupId = x.Key,
131                    TotalClients = x.Count(),
132                    OnlineClients = x.Count(y => y.SlaveState != DA.SlaveState.Offline),
133                    TotalCores = x.Sum(y => y.TotalCores),
134                    UsedCores = x.Sum(y => y.UsedCores),
135                    TotalMemory = x.Sum(y => y.TotalMemory),
136                    UsedMemory = x.Sum(y => y.UsedMemory),
137                    CpuUtilization = x.Where(y => y.SlaveState != DA.SlaveState.Offline).Average(y => (double?)y.CpuUtilization) ?? 0.0
138                  });
139      var query = dimClientDao.GetAll()
140        .GroupBy(x => new { x.ResourceGroupId, x.GroupName })
141        .Select(x => new {
142          Id = x.Key.ResourceGroupId ?? default(Guid),
143          Name = x.Key.GroupName
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.