1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Web.Mvc;
|
---|
26 | using System.Web.Script.Serialization;
|
---|
27 | using HeuristicLab.Services.Hive.DataAccess;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Services.Hive.Statistics.Controllers {
|
---|
30 | //[OutputCache(Duration = 60 * 5)]
|
---|
31 | public class ChartDataController : Controller {
|
---|
32 | private static readonly TimeSpan DefaultDuration = new TimeSpan(1, 0, 0, 0);
|
---|
33 |
|
---|
34 | public JsonResult AverageCpuUtilization(DateTime? start = null, DateTime? end = null) {
|
---|
35 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
|
---|
36 | //If no given start date, get date of most recent record
|
---|
37 | if (start == null) {
|
---|
38 | start = GetRecentDate(db);
|
---|
39 | }
|
---|
40 | var data =
|
---|
41 | from facts in GetClientFacts(db, start, end)
|
---|
42 | select new {
|
---|
43 | Time = facts.Key,
|
---|
44 | CpuUtilization = facts.Average(x => x.CpuUtilization)
|
---|
45 | };
|
---|
46 |
|
---|
47 | return Json(
|
---|
48 | CreateSeriesData(data.ToList(), x => x.Time, x => x.CpuUtilization),
|
---|
49 | JsonRequestBehavior.AllowGet);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public JsonResult UsedCores(DateTime? start = null, DateTime? end = null) {
|
---|
54 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
|
---|
55 | //If no given start date, get date of most recent record
|
---|
56 | if (start == null) {
|
---|
57 | start = GetRecentDate(db);
|
---|
58 | }
|
---|
59 | var data =
|
---|
60 | from facts in GetClientFacts(db, start, end)
|
---|
61 | select new {
|
---|
62 | Time = facts.Key,
|
---|
63 | UsedCores = facts.Sum(x => x.NumUsedCores),
|
---|
64 | TotalCores = facts.Sum(x => x.NumTotalCores)
|
---|
65 | };
|
---|
66 |
|
---|
67 | return Json(
|
---|
68 | CreateSeriesData(data.ToList(), x => x.Time, x => x.UsedCores, x => x.TotalCores),
|
---|
69 | JsonRequestBehavior.AllowGet);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public JsonResult UsedMemory(DateTime? start = null, DateTime? end = null) {
|
---|
74 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
|
---|
75 | //If no given start date, get date of most recent record
|
---|
76 | if (start == null) {
|
---|
77 | start = GetRecentDate(db);
|
---|
78 | }
|
---|
79 | var data =
|
---|
80 | from facts in GetClientFacts(db, start, end)
|
---|
81 | select new {
|
---|
82 | Time = facts.Key,
|
---|
83 | UsedMemory = facts.Sum(x => x.UsedMemory),
|
---|
84 | TotalMemory = facts.Sum(x => x.TotalMemory)
|
---|
85 | };
|
---|
86 |
|
---|
87 | // Return values in GB
|
---|
88 | return Json(
|
---|
89 | CreateSeriesData(data.ToList(), x => x.Time, x => x.UsedMemory / 1000, x => x.TotalMemory / 1000),
|
---|
90 | JsonRequestBehavior.AllowGet);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public JsonResult CurrentCpuUtilization(string state)
|
---|
95 | {
|
---|
96 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
97 | {
|
---|
98 | var currentCpuUtil = (from client in db.FactClientInfos
|
---|
99 | join clients in db.DimClients
|
---|
100 | on client.ClientId equals clients.Id
|
---|
101 | join resource in db.Resources.OfType<Slave>()
|
---|
102 | on clients.ResourceId equals resource.ResourceId
|
---|
103 | where clients.ExpirationTime == null &&
|
---|
104 | (state == "All") ||
|
---|
105 | (state == "Online") &&
|
---|
106 | (client.SlaveState == SlaveState.Idle || client.SlaveState == SlaveState.Calculating ) ||
|
---|
107 | (state == "Available") &&
|
---|
108 | (client.SlaveState == SlaveState.Idle)
|
---|
109 | select new { client.ClientId, client.CpuUtilization, client.Time })
|
---|
110 | .OrderBy(c => c.Time).GroupBy(c => c.ClientId).ToList();
|
---|
111 |
|
---|
112 | List<double> result = new List<double>();
|
---|
113 | result.Add(Math.Round(currentCpuUtil.Average(s => s.Last().CpuUtilization), 2));
|
---|
114 |
|
---|
115 | return Json(result, JsonRequestBehavior.AllowGet);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public JsonResult CurrentCores(string state)
|
---|
120 | {
|
---|
121 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
122 | {
|
---|
123 | var currentCores = (from client in db.FactClientInfos
|
---|
124 | join clients in db.DimClients
|
---|
125 | on client.ClientId equals clients.Id
|
---|
126 | join resource in db.Resources.OfType<Slave>()
|
---|
127 | on clients.ResourceId equals resource.ResourceId
|
---|
128 | where clients.ExpirationTime == null &&
|
---|
129 | (state == "All") ||
|
---|
130 | (state == "Online") &&
|
---|
131 | (client.SlaveState == SlaveState.Idle || client.SlaveState == SlaveState.Calculating) ||
|
---|
132 | (state == "Available") &&
|
---|
133 | (client.SlaveState == SlaveState.Idle)
|
---|
134 | select new { client.ClientId, client.NumTotalCores, client.NumUsedCores, client.Time })
|
---|
135 | .OrderBy(c => c.Time).GroupBy(c => c.ClientId).ToList();
|
---|
136 |
|
---|
137 | List<int> result = new List<int>();
|
---|
138 | result.Add(currentCores.Sum(c => c.Last().NumTotalCores));
|
---|
139 | result.Add(currentCores.Sum(s => s.Last().NumUsedCores));
|
---|
140 |
|
---|
141 | return Json(result, JsonRequestBehavior.AllowGet);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | public JsonResult CurrentMemory(string state)
|
---|
146 | {
|
---|
147 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
148 | {
|
---|
149 | var currentMemory = (from client in db.FactClientInfos
|
---|
150 | join clients in db.DimClients
|
---|
151 | on client.ClientId equals clients.Id
|
---|
152 | join resource in db.Resources.OfType<Slave>()
|
---|
153 | on clients.ResourceId equals resource.ResourceId
|
---|
154 | where clients.ExpirationTime == null &&
|
---|
155 | (state == "All") ||
|
---|
156 | (state == "Online") &&
|
---|
157 | (client.SlaveState == SlaveState.Idle || client.SlaveState == SlaveState.Calculating) ||
|
---|
158 | (state == "Available") &&
|
---|
159 | (client.SlaveState == SlaveState.Idle)
|
---|
160 | select new { client.ClientId, client.TotalMemory, client.UsedMemory, client.Time })
|
---|
161 | .OrderBy(c => c.Time).GroupBy(c => c.ClientId).ToList();
|
---|
162 |
|
---|
163 | List<int> result = new List<int>();
|
---|
164 | result.Add(currentMemory.Sum(c => c.Last().TotalMemory)/1000);
|
---|
165 | result.Add(currentMemory.Sum(s => s.Last().UsedMemory)/1000);
|
---|
166 |
|
---|
167 | return Json(result, JsonRequestBehavior.AllowGet);
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | public JsonResult GetTasks(string userName, DateTime? start = null, DateTime? end = null, string jobId = null, string taskState = null)
|
---|
172 | {
|
---|
173 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
174 | {
|
---|
175 | TaskState? state = GetTaskState(taskState);
|
---|
176 |
|
---|
177 | var data =
|
---|
178 | (from tasks in db.FactTasks
|
---|
179 | join jobs in db.DimJobs
|
---|
180 | on tasks.JobId equals jobs.JobId
|
---|
181 | where jobs.UserName == userName &&
|
---|
182 | (!start.HasValue || tasks.StartTime >= start) &&
|
---|
183 | (!end.HasValue || tasks.EndTime < end) &&
|
---|
184 | (string.IsNullOrEmpty(jobId) || tasks.JobId.ToString() == jobId) &&
|
---|
185 | (string.IsNullOrEmpty(taskState) || tasks.TaskState == state)
|
---|
186 | select new
|
---|
187 | {
|
---|
188 | JobName = jobs.JobName,
|
---|
189 | JobId = jobs.JobId,
|
---|
190 | TaskId = tasks.TaskId,
|
---|
191 | StartDate = tasks.StartTime
|
---|
192 | }).OrderByDescending(s => s.StartDate).ToList();
|
---|
193 |
|
---|
194 | //Was overflowing default MaxJsonLength
|
---|
195 | //return Json(data, JsonRequestBehavior.AllowGet);
|
---|
196 | return new JsonResult() { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet, MaxJsonLength = Int32.MaxValue };
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | public JsonResult TaskInfo(string taskId)
|
---|
201 | {
|
---|
202 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
203 | {
|
---|
204 | var data =
|
---|
205 | (from tasks in db.FactTasks
|
---|
206 | join jobs in db.DimJobs
|
---|
207 | on tasks.JobId equals jobs.JobId
|
---|
208 | where tasks.TaskId.ToString() == taskId
|
---|
209 | select new
|
---|
210 | {
|
---|
211 | TotalWaiting = tasks.TotalWaitingTime,
|
---|
212 | TotalTransfer = tasks.TotalTransferTime,
|
---|
213 | TotalRuntime = tasks.TotalRuntime,
|
---|
214 | StartDate = tasks.StartTime
|
---|
215 | }).OrderByDescending(s => s.StartDate).ToList();
|
---|
216 |
|
---|
217 | return Json(data, JsonRequestBehavior.AllowGet);
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | public JsonResult GetSlaves(DateTime? start = null, DateTime? end = null, string userName = null, string slaveId = null)
|
---|
222 | {
|
---|
223 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
224 | {
|
---|
225 | var data =
|
---|
226 | (from slaves in db.FactClientInfos
|
---|
227 | join users in db.DimUsers
|
---|
228 | on slaves.UserId equals users.UserId
|
---|
229 | join clients in db.DimClients
|
---|
230 | on slaves.ClientId equals clients.Id
|
---|
231 | where (!start.HasValue || slaves.Time >= start) &&
|
---|
232 | (!end.HasValue || slaves.Time < end) &&
|
---|
233 | (string.IsNullOrEmpty(userName) || users.Name == userName) &&
|
---|
234 | (string.IsNullOrEmpty(slaveId) || slaves.ClientId.ToString() == slaveId)
|
---|
235 | select new
|
---|
236 | {
|
---|
237 | ClientName = clients.Name,
|
---|
238 | SlaveID = slaves.ClientId,
|
---|
239 | }).GroupBy(s => s.SlaveID).ToList();
|
---|
240 | //Was overflowing default MaxJsonLength
|
---|
241 | //return Json(data, JsonRequestBehavior.AllowGet);
|
---|
242 | return new JsonResult() { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet, MaxJsonLength = Int32.MaxValue };
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | public JsonResult SlaveInfo(string slaveId, DateTime? start = null, DateTime? end = null, string userName = null) {
|
---|
247 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
248 | {
|
---|
249 | var data =
|
---|
250 | (from slaves in db.FactClientInfos
|
---|
251 | join users in db.DimUsers
|
---|
252 | on slaves.UserId equals users.UserId
|
---|
253 | join clients in db.DimClients
|
---|
254 | on slaves.ClientId equals clients.Id
|
---|
255 | where slaves.ClientId.ToString() == slaveId &&
|
---|
256 | (!start.HasValue || slaves.Time >= start) &&
|
---|
257 | (!end.HasValue || slaves.Time < end) &&
|
---|
258 | (string.IsNullOrEmpty(userName) || users.Name == userName)
|
---|
259 | select new
|
---|
260 | {
|
---|
261 | ClientName = clients.Name,
|
---|
262 | SlaveID = slaves.ClientId,
|
---|
263 | Time = slaves.Time,
|
---|
264 | UsedCores = slaves.NumUsedCores,
|
---|
265 | TotalCores = slaves.NumTotalCores,
|
---|
266 | UsedMemory = slaves.UsedMemory,
|
---|
267 | TotalMemory = slaves.TotalMemory,
|
---|
268 | CPUUtilization = slaves.CpuUtilization
|
---|
269 | }).OrderByDescending(s => s.Time).ToList();
|
---|
270 |
|
---|
271 | return Json(data, JsonRequestBehavior.AllowGet);
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | public JsonResult JobProgress(string jobId) {
|
---|
276 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
|
---|
277 | var data =
|
---|
278 | (from tasks in db.FactTasks
|
---|
279 | where tasks.JobId.ToString() == jobId
|
---|
280 | select new
|
---|
281 | {
|
---|
282 | State = tasks.TaskState
|
---|
283 | }).ToList();
|
---|
284 |
|
---|
285 | Dictionary<string, int> JobTasks = new Dictionary<string,int>();
|
---|
286 | JobTasks.Add("Wait",0);
|
---|
287 | JobTasks.Add("Transfer", 0);
|
---|
288 | JobTasks.Add("Calculate", 0);
|
---|
289 | JobTasks.Add("Finish", 0);
|
---|
290 | JobTasks.Add("Error", 0);
|
---|
291 |
|
---|
292 | foreach(var record in data) {
|
---|
293 | switch (record.State) {
|
---|
294 | case TaskState.Waiting:
|
---|
295 | JobTasks["Wait"] += 1;
|
---|
296 | break;
|
---|
297 | case TaskState.Transferring:
|
---|
298 | JobTasks["Transfer"] += 1;
|
---|
299 | break;
|
---|
300 | case TaskState.Calculating:
|
---|
301 | JobTasks["Calculate"] += 1;
|
---|
302 | break;
|
---|
303 | case TaskState.Finished:
|
---|
304 | JobTasks["Finish"] += 1;
|
---|
305 | break;
|
---|
306 | case TaskState.Aborted:
|
---|
307 | case TaskState.Failed:
|
---|
308 | JobTasks["Error"] += 1;
|
---|
309 | break;
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | return Json(JobTasks, JsonRequestBehavior.AllowGet);
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | public JsonResult UserTasks(string taskState) {
|
---|
318 | TaskState? state = GetTaskState(taskState);
|
---|
319 | List<KeyValuePair<string, int>> numberTasksByUser = new List<KeyValuePair<string,int>>();
|
---|
320 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
|
---|
321 | var userTasks = (from tasks in db.FactTasks
|
---|
322 | join jobs in db.DimJobs
|
---|
323 | on tasks.JobId equals jobs.JobId
|
---|
324 | where tasks.TaskState == state
|
---|
325 | select new
|
---|
326 | {
|
---|
327 | UserID = jobs.UserId,
|
---|
328 | UserName = jobs.UserName,
|
---|
329 | State = tasks.TaskState
|
---|
330 | }).GroupBy(j => j.UserID).ToList();
|
---|
331 |
|
---|
332 | foreach (var user in userTasks) {
|
---|
333 | numberTasksByUser.Add(new KeyValuePair<string, int>(user.First().UserName, user.Count()));
|
---|
334 | }
|
---|
335 | return Json(numberTasksByUser, JsonRequestBehavior.AllowGet);
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | private static TaskState? GetTaskState(string taskState) {
|
---|
340 | TaskState? state;
|
---|
341 | switch (taskState)
|
---|
342 | {
|
---|
343 | case "Aborted":
|
---|
344 | state = TaskState.Aborted;
|
---|
345 | break;
|
---|
346 | case "Calculating":
|
---|
347 | state = TaskState.Calculating;
|
---|
348 | break;
|
---|
349 | case "Failed":
|
---|
350 | state = TaskState.Failed;
|
---|
351 | break;
|
---|
352 | case "Finished":
|
---|
353 | state = TaskState.Finished;
|
---|
354 | break;
|
---|
355 | case "Offline":
|
---|
356 | state = TaskState.Offline;
|
---|
357 | break;
|
---|
358 | case "Paused":
|
---|
359 | state = TaskState.Paused;
|
---|
360 | break;
|
---|
361 | case "Transferring":
|
---|
362 | state = TaskState.Transferring;
|
---|
363 | break;
|
---|
364 | case "Waiting":
|
---|
365 | state = TaskState.Waiting;
|
---|
366 | break;
|
---|
367 | default :
|
---|
368 | state = null;
|
---|
369 | break;
|
---|
370 | }
|
---|
371 | return state;
|
---|
372 | }
|
---|
373 |
|
---|
374 | private static IOrderedQueryable<IGrouping<DateTime, FactClientInfo>> GetClientFacts(HiveDataContext db, DateTime? start = null, DateTime? end = null) {
|
---|
375 | start = start ?? DateTime.Now - DefaultDuration;
|
---|
376 | end = end ?? DateTime.Now;
|
---|
377 |
|
---|
378 | return from ci in db.FactClientInfos
|
---|
379 | where ci.Time >= start && ci.Time < end
|
---|
380 | group ci by ci.Time into timeGroup
|
---|
381 | orderby timeGroup.Key
|
---|
382 | select timeGroup;
|
---|
383 | }
|
---|
384 |
|
---|
385 | private static IEnumerable<IEnumerable<object[]>> CreateSeriesData<T>(IEnumerable<T> data, Func<T, DateTime> timeSelector, params Func<T, object>[] seriesSelectors) {
|
---|
386 | return seriesSelectors.Select(selector =>
|
---|
387 | data.Select(x => new[] {
|
---|
388 | timeSelector(x).ToUniversalTime().ToUnixTimestamp(),
|
---|
389 | selector(x)
|
---|
390 | })
|
---|
391 | );
|
---|
392 | }
|
---|
393 |
|
---|
394 | private static DateTime GetRecentDate(HiveDataContext db)
|
---|
395 | {
|
---|
396 | var mostRecent = from dates in db.FactClientInfos
|
---|
397 | group dates by dates.Time into startDate
|
---|
398 | select startDate.OrderByDescending(t=>t.Time).FirstOrDefault();
|
---|
399 | return mostRecent.Max(t=>t.Time);
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | public static class DateTimeExtensions {
|
---|
404 | public static long ToUnixTimestamp(this DateTime dateTime) {
|
---|
405 | var duration = dateTime - new DateTime(1970, 1, 1, 0, 0, 0);
|
---|
406 |
|
---|
407 | return (long)duration.TotalMilliseconds;
|
---|
408 | }
|
---|
409 | }
|
---|
410 | }
|
---|