Changeset 12858 for branches/HiveStatistics/sources/HeuristicLab.Services.WebApp.Statistics/3.3/WebApi/ClientController.cs
- Timestamp:
- 08/13/15 15:22:51 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HiveStatistics/sources/HeuristicLab.Services.WebApp.Statistics/3.3/WebApi/ClientController.cs
r12776 r12858 38 38 39 39 public DT.ClientDetails GetClientDetails(Guid id) { 40 using (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) 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) 60 65 .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 } 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 }); 113 112 } 114 113 115 114 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 = offline ? SlaveState.Offline.ToString() : 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 } 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 }); 147 145 } 148 146 … … 155 153 increment += 5; 156 154 } 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 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() 169 227 }; 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 = offline ? SlaveState.Offline.ToString() : 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 } 228 }); 233 229 } 234 230 … … 241 237 increment += 5; 242 238 } 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 } 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; 296 291 } 297 292 }
Note: See TracChangeset
for help on using the changeset viewer.