Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2388:

HeuristicLab.Services.Hive.DataAccess-3.3:

  • updated daos
  • changed statistics database schema
  • updated HiveStatisticsGenerator

HeuristicLab.Services.WebApp.Statistics-3.3:

  • added jobs, client and user page
File size: 8.3 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                    UpTime = offline ? 0 : upTime,
89                    TotalUnavailableTime = timeData != null ? timeData.TotalUnavailableTime : 0,
90                    TotalCalculatingTime = taskTimeData != null ? taskTimeData.TotalCalculatingTime : 0,
91                    TotalIdleTime = timeData != null ? timeData.TotalIdleTime : 0,
92                    TotalOfflineTime = timeData != null ? timeData.TotalOfflineTime : 0,
93                    TotalTransferringTime = taskTimeData != null ? taskTimeData.TotalTransferringTime : 0,
94                    TasksStates = factTaskDao.GetByClientId(id)
95                                    .GroupBy(x => x.TaskState)
96                                    .Select(x => new DT.TaskStateCount {
97                                      State = x.Key.ToString(),
98                                      Count = x.Count()
99                                    }).ToList()
100                  })
101                  .FirstOrDefault();
102        });
103      }
104    }
105
106    public DT.ClientPage GetClients(int page, int size, bool expired = false) {
107      using (var pm = PersistenceManager) {
108        var dimClientDao = pm.DimClientDao;
109        var factClientInfoDao = pm.FactClientInfoDao;
110        return pm.UseTransaction(() => {
111          var clients = expired ? dimClientDao.GetExpiredClients() : dimClientDao.GetActiveClients();
112          var query = (from client in clients
113                       join info in factClientInfoDao.GetAll()
114                         on client.Id equals info.ClientId into clientInfoJoin
115                       from clientInfo in clientInfoJoin.OrderByDescending(x => x.Time).Take(1)
116                       let offline = (expired || clientInfo.SlaveState == SlaveState.Offline)
117                       select new DT.Client {
118                         Id = client.Id,
119                         Name = client.Name,
120                         TotalCores = clientInfo.NumTotalCores,
121                         UsedCores = offline ? 0 : clientInfo.NumUsedCores,
122                         TotalMemory = clientInfo.TotalMemory,
123                         UsedMemory = offline ? 0 : clientInfo.UsedMemory,
124                         CpuUtilization = offline ? 0 : clientInfo.CpuUtilization,
125                         State = clientInfo.SlaveState.ToString(),
126                       });
127          return new DT.ClientPage {
128            TotalClients = query.Count(),
129            Clients = query.Skip((page - 1) * size)
130              .Take(size)
131              .ToList()
132          };
133        });
134      }
135    }
136
137    public IEnumerable<DT.ClientStatus> GetClientHistory(Guid id, DateTime start, DateTime end) {
138      TimeSpan ts = end - start;
139      int increment = 1;
140      double totalMinutes = ts.TotalMinutes;
141      while (totalMinutes > 5761) {
142        totalMinutes -= 5761;
143        increment += 5;
144      }
145      using (var pm = PersistenceManager) {
146        var factClientInfo = pm.FactClientInfoDao;
147        var clientInfos = factClientInfo.GetByClientId(id)
148          .Where(x => x.Time >= start && x.Time <= end)
149          .OrderBy(x => x.Time)
150          .ToList();
151        var clientStatus = new DT.ClientStatus {
152          CpuUtilization = 0,
153          TotalCores = 0,
154          UsedCores = 0,
155          TotalMemory = 0,
156          UsedMemory = 0
157        };
158        int i = 1;
159        foreach (var clientInfo in clientInfos) {
160          clientStatus.CpuUtilization += clientInfo.CpuUtilization;
161          clientStatus.TotalCores += clientInfo.NumTotalCores;
162          clientStatus.UsedCores += clientInfo.NumUsedCores;
163          clientStatus.TotalMemory += clientInfo.TotalMemory;
164          clientStatus.UsedMemory += clientInfo.UsedMemory;
165          if (i >= increment) {
166            clientStatus.Timestamp = JavascriptUtils.ToTimestamp(clientInfo.Time);
167            clientStatus.CpuUtilization /= i;
168            clientStatus.TotalCores /= i;
169            clientStatus.UsedCores /= i;
170            clientStatus.TotalMemory /= i;
171            clientStatus.UsedMemory /= i;
172            yield return clientStatus;
173            clientStatus = new DT.ClientStatus {
174              CpuUtilization = 0,
175              TotalCores = 0,
176              UsedCores = 0,
177              TotalMemory = 0,
178              UsedMemory = 0
179            };
180            i = 1;
181          } else {
182            ++i;
183          }
184        }
185      }
186    }
187
188  }
189}
Note: See TracBrowser for help on using the repository browser.