Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2388:

HeuristicLab.Services.Hive.DataAccess-3.3:

  • updated database schema
  • updated sql scripts
  • updated HiveStatisticsGenerator

HeuristicLab.Services.WebApp-3.3:

  • merged from trunk

HeuristicLab.Services.WebApp.Status-3.3:

  • updated data api controller

HeuristicLab.Services.WebApp.Statistics-3.3:

  • added exception page
  • improved jobs, clients, users and groups page
File size: 13.1 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 = x.Sum(y => y.IdleTime),
50              TotalOfflineTime = x.Sum(y => y.OfflineTime),
51              TotalUnavailableTime = x.Sum(y => y.UnavailableTime)
52            }).FirstOrDefault();
53          var taskTimeData = factTaskDao.GetByClientId(id)
54            .GroupBy(x => x.LastClientId)
55            .Select(x => new {
56              TotalCalculatingTime = x.Sum(y => y.CalculatingTime),
57              TotalTransferringTime = 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                    Users = factTaskDao.GetAll()
103                              .Where(x => x.LastClientId == id)
104                              .Select(x => new DT.User {
105                                Id = x.DimJob.UserId,
106                                Name = x.DimJob.UserName
107                              })
108                              .Distinct().ToList()
109                  })
110                  .FirstOrDefault();
111        });
112      }
113    }
114
115    public DT.ClientPage GetClients(int page, int size, bool expired = false) {
116      using (var pm = PersistenceManager) {
117        var dimClientDao = pm.DimClientDao;
118        var factClientInfoDao = pm.FactClientInfoDao;
119        return pm.UseTransaction(() => {
120          var clients = expired ? dimClientDao.GetExpiredClients() : dimClientDao.GetActiveClients();
121          var query = (from client in clients
122                       join info in factClientInfoDao.GetAll()
123                         on client.Id equals info.ClientId into clientInfoJoin
124                       from clientInfo in clientInfoJoin.OrderByDescending(x => x.Time).Take(1)
125                       let offline = (expired || clientInfo.SlaveState == SlaveState.Offline)
126                       select new DT.Client {
127                         Id = client.Id,
128                         Name = client.Name,
129                         TotalCores = clientInfo.NumTotalCores,
130                         UsedCores = offline ? 0 : clientInfo.NumUsedCores,
131                         TotalMemory = clientInfo.TotalMemory,
132                         UsedMemory = offline ? 0 : clientInfo.UsedMemory,
133                         CpuUtilization = offline ? 0 : clientInfo.CpuUtilization,
134                         State = clientInfo.SlaveState.ToString(),
135                         GroupId = client.ResourceGroupId,
136                         GroupName = client.GroupName,
137                         IsAllowedToCalculate = clientInfo.IsAllowedToCalculate
138                       });
139          return new DT.ClientPage {
140            TotalClients = query.Count(),
141            Clients = query.Skip((page - 1) * size)
142              .Take(size)
143              .ToList()
144          };
145        });
146      }
147    }
148
149    public IEnumerable<DT.ClientStatus> GetClientHistory(Guid id, DateTime start, DateTime end) {
150      TimeSpan ts = end - start;
151      int increment = 1;
152      double totalMinutes = ts.TotalMinutes;
153      while (totalMinutes > 5761) {
154        totalMinutes -= 5761;
155        increment += 5;
156      }
157      using (var pm = PersistenceManager) {
158        var factClientInfo = pm.FactClientInfoDao;
159        var clientInfos = factClientInfo.GetByClientId(id)
160          .Where(x => x.Time >= start && x.Time <= end)
161          .OrderBy(x => x.Time)
162          .ToList();
163        var clientStatus = new DT.ClientStatus {
164          CpuUtilization = 0,
165          TotalCores = 0,
166          UsedCores = 0,
167          TotalMemory = 0,
168          UsedMemory = 0
169        };
170        int i = 1;
171        foreach (var clientInfo in clientInfos) {
172          clientStatus.CpuUtilization += clientInfo.CpuUtilization;
173          clientStatus.TotalCores += clientInfo.NumTotalCores;
174          clientStatus.UsedCores += clientInfo.NumUsedCores;
175          clientStatus.TotalMemory += clientInfo.TotalMemory;
176          clientStatus.UsedMemory += clientInfo.UsedMemory;
177          if (i >= increment) {
178            clientStatus.Timestamp = JavascriptUtils.ToTimestamp(clientInfo.Time);
179            clientStatus.CpuUtilization /= i;
180            clientStatus.TotalCores /= i;
181            clientStatus.UsedCores /= i;
182            clientStatus.TotalMemory /= i;
183            clientStatus.UsedMemory /= i;
184            yield return clientStatus;
185            clientStatus = new DT.ClientStatus {
186              CpuUtilization = 0,
187              TotalCores = 0,
188              UsedCores = 0,
189              TotalMemory = 0,
190              UsedMemory = 0
191            };
192            i = 1;
193          } else {
194            ++i;
195          }
196        }
197      }
198    }
199
200    public DT.ClientPage GetClientsByGroupId(Guid id, int page, int size, bool expired = false) {
201      using (var pm = PersistenceManager) {
202        var dimClientDao = pm.DimClientDao;
203        var factClientInfoDao = pm.FactClientInfoDao;
204        return pm.UseTransaction(() => {
205          var clients = expired ? dimClientDao.GetExpiredClients() : dimClientDao.GetActiveClients();
206          clients = clients.Where(x => x.ResourceGroupId == id);
207          var query = (from client in clients
208                       join info in factClientInfoDao.GetAll()
209                         on client.Id equals info.ClientId into clientInfoJoin
210                       from clientInfo in clientInfoJoin.OrderByDescending(x => x.Time).Take(1)
211                       let offline = (expired || clientInfo.SlaveState == SlaveState.Offline)
212                       select new DT.Client {
213                         Id = client.Id,
214                         Name = client.Name,
215                         TotalCores = clientInfo.NumTotalCores,
216                         UsedCores = offline ? 0 : clientInfo.NumUsedCores,
217                         TotalMemory = clientInfo.TotalMemory,
218                         UsedMemory = offline ? 0 : clientInfo.UsedMemory,
219                         CpuUtilization = offline ? 0 : clientInfo.CpuUtilization,
220                         State = clientInfo.SlaveState.ToString(),
221                         GroupId = client.ResourceGroupId,
222                         GroupName = client.GroupName,
223                         IsAllowedToCalculate = clientInfo.IsAllowedToCalculate
224                       });
225          return new DT.ClientPage {
226            TotalClients = query.Count(),
227            Clients = query.Skip((page - 1) * size)
228              .Take(size)
229              .ToList()
230          };
231        });
232      }
233    }
234
235    public IEnumerable<DT.ClientStatus> GetClientHistoryByGroupId(Guid id, DateTime start, DateTime end) {
236      TimeSpan ts = end - start;
237      int increment = 1;
238      double totalMinutes = ts.TotalMinutes;
239      while (totalMinutes > 5761) {
240        totalMinutes -= 5761;
241        increment += 5;
242      }
243      using (var pm = PersistenceManager) {
244        var factClientInfo = pm.FactClientInfoDao;
245        var dimClientDao = pm.DimClientDao;
246        var clientInfos = factClientInfo.GetAll()
247          .Where(x => x.Time >= start && x.Time <= end)
248          .OrderBy(x => x.Time)
249          .Join(dimClientDao.GetAll(),
250            fact => fact.ClientId,
251            client => client.Id,
252            (fact, client) => new {
253              client.ResourceGroupId,
254              fact.Time,
255              fact.CpuUtilization,
256              fact.NumTotalCores,
257              fact.NumUsedCores,
258              fact.TotalMemory,
259              fact.UsedMemory
260            })
261          .Where(x => x.ResourceGroupId == id)
262          .ToList();
263        var clientStatus = new DT.ClientStatus {
264          CpuUtilization = 0,
265          TotalCores = 0,
266          UsedCores = 0,
267          TotalMemory = 0,
268          UsedMemory = 0
269        };
270        int i = 1;
271        foreach (var clientInfo in clientInfos) {
272          clientStatus.CpuUtilization += clientInfo.CpuUtilization;
273          clientStatus.TotalCores += clientInfo.NumTotalCores;
274          clientStatus.UsedCores += clientInfo.NumUsedCores;
275          clientStatus.TotalMemory += clientInfo.TotalMemory;
276          clientStatus.UsedMemory += clientInfo.UsedMemory;
277          if (i >= increment) {
278            clientStatus.Timestamp = JavascriptUtils.ToTimestamp(clientInfo.Time);
279            clientStatus.CpuUtilization /= i;
280            clientStatus.TotalCores /= i;
281            clientStatus.UsedCores /= i;
282            clientStatus.TotalMemory /= i;
283            clientStatus.UsedMemory /= i;
284            yield return clientStatus;
285            clientStatus = new DT.ClientStatus {
286              CpuUtilization = 0,
287              TotalCores = 0,
288              UsedCores = 0,
289              TotalMemory = 0,
290              UsedMemory = 0
291            };
292            i = 1;
293          } else {
294            ++i;
295          }
296        }
297      }
298    }
299  }
300}
Note: See TracBrowser for help on using the repository browser.