Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.WebApp.Statistics/3.3/WebApi/ClientController.cs @ 12525

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

#2388:

HeuristicLab.Services.WebApp.Statistics-3.3:

  • added groups page
  • improved jobs, clients and users pages

HeuristicLab.Services.WebApp-3.3:

  • merged from trunk
File size: 12.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 HeuristicLab.Services.WebApp.Status.WebApi;
30using DT = HeuristicLab.Services.WebApp.Statistics.WebApi.DataTransfer;
31
32namespace HeuristicLab.Services.WebApp.Statistics.WebApi {
33
34  [Authorize]
35  public class ClientController : ApiController {
36    private IPersistenceManager PersistenceManager {
37      get { return ServiceLocator.Instance.PersistenceManager; }
38    }
39
40    public DT.ClientDetails GetClientDetails(Guid id) {
41      using (var pm = PersistenceManager) {
42        var dimClientDao = pm.DimClientDao;
43        var factClientInfoDao = pm.FactClientInfoDao;
44        var factTaskDao = pm.FactTaskDao;
45        return pm.UseTransaction(() => {
46          var timeData = factClientInfoDao.GetByClientId(id)
47            .GroupBy(x => x.ClientId)
48            .Select(x => new {
49              TotalIdleTime = (long)x.Sum(y => y.TotalTimeIdle),
50              TotalOfflineTime = (long)x.Sum(y => y.TotalTimeOffline),
51              TotalUnavailableTime = (long)x.Sum(y => y.TotalTimeUnavailable)
52            }).FirstOrDefault();
53          var taskTimeData = factTaskDao.GetByClientId(id)
54            .GroupBy(x => x.LastClientId)
55            .Select(x => new {
56              TotalCalculatingTime = (long)x.Sum(y => y.CalculatingTime),
57              TotalTransferringTime = (long)x.Sum(y => y.TransferTime)
58            }).FirstOrDefault();
59          var startDate = factClientInfoDao.GetByClientId(id)
60            .Where(x => x.SlaveState == SlaveState.Offline)
61            .OrderByDescending(x => x.Time)
62            .Select(x => x.Time).FirstOrDefault();
63          long upTime = 0;
64          if (startDate == default(DateTime)) {
65            startDate = factClientInfoDao.GetByClientId(id)
66              .OrderByDescending(x => x.Time)
67              .Select(x => x.Time)
68              .FirstOrDefault();
69          }
70          if (startDate != default(DateTime)) {
71            upTime = (long)(DateTime.Now - startDate).TotalSeconds;
72          }
73          return (from client in dimClientDao.GetAll().Where(x => x.Id == id)
74                  join info in factClientInfoDao.GetAll()
75                    on client.Id equals info.ClientId into clientInfoJoin
76                  from clientInfo in clientInfoJoin.OrderByDescending(x => x.Time).Take(1)
77                  let offline = (client.ExpirationTime != null || clientInfo.SlaveState == SlaveState.Offline)
78                  select new DT.ClientDetails {
79                    Id = client.Id,
80                    Name = client.Name,
81                    TotalCores = clientInfo.NumTotalCores,
82                    UsedCores = offline ? 0 : clientInfo.NumUsedCores,
83                    TotalMemory = clientInfo.TotalMemory,
84                    UsedMemory = offline ? 0 : clientInfo.UsedMemory,
85                    CpuUtilization = offline ? 0 : clientInfo.CpuUtilization,
86                    State = clientInfo.SlaveState.ToString(),
87                    LastUpdate = clientInfo.Time,
88                    GroupId = client.ResourceGroupId,
89                    GroupName = client.GroupName,
90                    UpTime = offline ? 0 : upTime,
91                    TotalUnavailableTime = timeData != null ? timeData.TotalUnavailableTime : 0,
92                    TotalCalculatingTime = taskTimeData != null ? taskTimeData.TotalCalculatingTime : 0,
93                    TotalIdleTime = timeData != null ? timeData.TotalIdleTime : 0,
94                    TotalOfflineTime = timeData != null ? timeData.TotalOfflineTime : 0,
95                    TotalTransferringTime = taskTimeData != null ? taskTimeData.TotalTransferringTime : 0,
96                    TasksStates = factTaskDao.GetByClientId(id)
97                                    .GroupBy(x => x.TaskState)
98                                    .Select(x => new DT.TaskStateCount {
99                                      State = x.Key.ToString(),
100                                      Count = x.Count()
101                                    }).ToList()
102                  })
103                  .FirstOrDefault();
104        });
105      }
106    }
107
108    public DT.ClientPage GetClients(int page, int size, bool expired = false) {
109      using (var pm = PersistenceManager) {
110        var dimClientDao = pm.DimClientDao;
111        var factClientInfoDao = pm.FactClientInfoDao;
112        return pm.UseTransaction(() => {
113          var clients = expired ? dimClientDao.GetExpiredClients() : dimClientDao.GetActiveClients();
114          var query = (from client in clients
115                       join info in factClientInfoDao.GetAll()
116                         on client.Id equals info.ClientId into clientInfoJoin
117                       from clientInfo in clientInfoJoin.OrderByDescending(x => x.Time).Take(1)
118                       let offline = (expired || clientInfo.SlaveState == SlaveState.Offline)
119                       select new DT.Client {
120                         Id = client.Id,
121                         Name = client.Name,
122                         TotalCores = clientInfo.NumTotalCores,
123                         UsedCores = offline ? 0 : clientInfo.NumUsedCores,
124                         TotalMemory = clientInfo.TotalMemory,
125                         UsedMemory = offline ? 0 : clientInfo.UsedMemory,
126                         CpuUtilization = offline ? 0 : clientInfo.CpuUtilization,
127                         State = clientInfo.SlaveState.ToString(),
128                         GroupId = client.ResourceGroupId,
129                         GroupName = client.GroupName
130                       });
131          return new DT.ClientPage {
132            TotalClients = query.Count(),
133            Clients = query.Skip((page - 1) * size)
134              .Take(size)
135              .ToList()
136          };
137        });
138      }
139    }
140
141    public IEnumerable<DT.ClientStatus> GetClientHistory(Guid id, DateTime start, DateTime end) {
142      TimeSpan ts = end - start;
143      int increment = 1;
144      double totalMinutes = ts.TotalMinutes;
145      while (totalMinutes > 5761) {
146        totalMinutes -= 5761;
147        increment += 5;
148      }
149      using (var pm = PersistenceManager) {
150        var factClientInfo = pm.FactClientInfoDao;
151        var clientInfos = factClientInfo.GetByClientId(id)
152          .Where(x => x.Time >= start && x.Time <= end)
153          .OrderBy(x => x.Time)
154          .ToList();
155        var clientStatus = new DT.ClientStatus {
156          CpuUtilization = 0,
157          TotalCores = 0,
158          UsedCores = 0,
159          TotalMemory = 0,
160          UsedMemory = 0
161        };
162        int i = 1;
163        foreach (var clientInfo in clientInfos) {
164          clientStatus.CpuUtilization += clientInfo.CpuUtilization;
165          clientStatus.TotalCores += clientInfo.NumTotalCores;
166          clientStatus.UsedCores += clientInfo.NumUsedCores;
167          clientStatus.TotalMemory += clientInfo.TotalMemory;
168          clientStatus.UsedMemory += clientInfo.UsedMemory;
169          if (i >= increment) {
170            clientStatus.Timestamp = JavascriptUtils.ToTimestamp(clientInfo.Time);
171            clientStatus.CpuUtilization /= i;
172            clientStatus.TotalCores /= i;
173            clientStatus.UsedCores /= i;
174            clientStatus.TotalMemory /= i;
175            clientStatus.UsedMemory /= i;
176            yield return clientStatus;
177            clientStatus = new DT.ClientStatus {
178              CpuUtilization = 0,
179              TotalCores = 0,
180              UsedCores = 0,
181              TotalMemory = 0,
182              UsedMemory = 0
183            };
184            i = 1;
185          } else {
186            ++i;
187          }
188        }
189      }
190    }
191
192    public DT.ClientPage GetClientsByGroupId(Guid id, int page, int size, bool expired = false) {
193      using (var pm = PersistenceManager) {
194        var dimClientDao = pm.DimClientDao;
195        var factClientInfoDao = pm.FactClientInfoDao;
196        return pm.UseTransaction(() => {
197          var clients = expired ? dimClientDao.GetExpiredClients() : dimClientDao.GetActiveClients();
198          clients = clients.Where(x => x.ResourceGroupId == id);
199          var query = (from client in clients
200                       join info in factClientInfoDao.GetAll()
201                         on client.Id equals info.ClientId into clientInfoJoin
202                       from clientInfo in clientInfoJoin.OrderByDescending(x => x.Time).Take(1)
203                       let offline = (expired || clientInfo.SlaveState == SlaveState.Offline)
204                       select new DT.Client {
205                         Id = client.Id,
206                         Name = client.Name,
207                         TotalCores = clientInfo.NumTotalCores,
208                         UsedCores = offline ? 0 : clientInfo.NumUsedCores,
209                         TotalMemory = clientInfo.TotalMemory,
210                         UsedMemory = offline ? 0 : clientInfo.UsedMemory,
211                         CpuUtilization = offline ? 0 : clientInfo.CpuUtilization,
212                         State = clientInfo.SlaveState.ToString(),
213                         GroupId = client.ResourceGroupId,
214                         GroupName = client.GroupName
215                       });
216          return new DT.ClientPage {
217            TotalClients = query.Count(),
218            Clients = query.Skip((page - 1) * size)
219              .Take(size)
220              .ToList()
221          };
222        });
223      }
224    }
225
226    public IEnumerable<DT.ClientStatus> GetClientHistoryByGroupId(Guid id, DateTime start, DateTime end) {
227      TimeSpan ts = end - start;
228      int increment = 1;
229      double totalMinutes = ts.TotalMinutes;
230      while (totalMinutes > 5761) {
231        totalMinutes -= 5761;
232        increment += 5;
233      }
234      using (var pm = PersistenceManager) {
235        var factClientInfo = pm.FactClientInfoDao;
236        var dimClientDao = pm.DimClientDao;
237        var clientInfos = factClientInfo.GetAll()
238          .Where(x => x.Time >= start && x.Time <= end)
239          .OrderBy(x => x.Time)
240          .Join(dimClientDao.GetAll(),
241            fact => fact.ClientId,
242            client => client.Id,
243            (fact, client) => new {
244              client.ResourceGroupId,
245              fact.Time,
246              fact.CpuUtilization,
247              fact.NumTotalCores,
248              fact.NumUsedCores,
249              fact.TotalMemory,
250              fact.UsedMemory
251            })
252          .Where(x => x.ResourceGroupId == id)
253          .ToList();
254        var clientStatus = new DT.ClientStatus {
255          CpuUtilization = 0,
256          TotalCores = 0,
257          UsedCores = 0,
258          TotalMemory = 0,
259          UsedMemory = 0
260        };
261        int i = 1;
262        foreach (var clientInfo in clientInfos) {
263          clientStatus.CpuUtilization += clientInfo.CpuUtilization;
264          clientStatus.TotalCores += clientInfo.NumTotalCores;
265          clientStatus.UsedCores += clientInfo.NumUsedCores;
266          clientStatus.TotalMemory += clientInfo.TotalMemory;
267          clientStatus.UsedMemory += clientInfo.UsedMemory;
268          if (i >= increment) {
269            clientStatus.Timestamp = JavascriptUtils.ToTimestamp(clientInfo.Time);
270            clientStatus.CpuUtilization /= i;
271            clientStatus.TotalCores /= i;
272            clientStatus.UsedCores /= i;
273            clientStatus.TotalMemory /= i;
274            clientStatus.UsedMemory /= i;
275            yield return clientStatus;
276            clientStatus = new DT.ClientStatus {
277              CpuUtilization = 0,
278              TotalCores = 0,
279              UsedCores = 0,
280              TotalMemory = 0,
281              UsedMemory = 0
282            };
283            i = 1;
284          } else {
285            ++i;
286          }
287        }
288      }
289    }
290  }
291}
Note: See TracBrowser for help on using the repository browser.