Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.WebApp.Statistics/3.3/WebApi/ClientController.cs @ 12878

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

#2388

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