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 CurrentCores()
|
---|
95 | {
|
---|
96 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
97 | {
|
---|
98 | var currentCores = (from client in db.FactClientInfos
|
---|
99 | select new { client.NumTotalCores, client.NumUsedCores })
|
---|
100 | .ToList();
|
---|
101 |
|
---|
102 | List<int> result = new List<int>();
|
---|
103 | result.Add(currentCores.Sum(c => c.NumTotalCores));
|
---|
104 | result.Add(currentCores.Sum(s => s.NumUsedCores));
|
---|
105 |
|
---|
106 | return Json(result, JsonRequestBehavior.AllowGet);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public JsonResult CurrentCpuUtilization()
|
---|
111 | {
|
---|
112 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
113 | {
|
---|
114 | var currentCpuUtil = (from client in db.FactClientInfos
|
---|
115 | select new { client.CpuUtilization })
|
---|
116 | .ToList();
|
---|
117 |
|
---|
118 | List<double> result = new List<double>();
|
---|
119 | result.Add(Math.Round(currentCpuUtil.Average(s => s.CpuUtilization), 2));
|
---|
120 |
|
---|
121 | return Json(result, JsonRequestBehavior.AllowGet);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | public JsonResult CurrentMemory()
|
---|
126 | {
|
---|
127 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
128 | {
|
---|
129 | var currentMemory = (from client in db.FactClientInfos
|
---|
130 | select new { client.TotalMemory, client.UsedMemory })
|
---|
131 | .ToList();
|
---|
132 |
|
---|
133 | List<int> result = new List<int>();
|
---|
134 | result.Add(currentMemory.Sum(c => c.TotalMemory)/1000);
|
---|
135 | result.Add(currentMemory.Sum(s => s.UsedMemory)/1000);
|
---|
136 |
|
---|
137 | return Json(result, JsonRequestBehavior.AllowGet);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | public JsonResult GetTasks(string userName, DateTime? start = null, DateTime? end = null, string jobId = null, string taskState = null)
|
---|
142 | {
|
---|
143 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
144 | {
|
---|
145 | TaskState? state = GetTaskState(taskState);
|
---|
146 |
|
---|
147 | var data =
|
---|
148 | (from tasks in db.FactTasks
|
---|
149 | join jobs in db.DimJobs
|
---|
150 | on tasks.JobId equals jobs.JobId
|
---|
151 | where jobs.UserName == userName &&
|
---|
152 | (!start.HasValue || tasks.StartTime >= start) &&
|
---|
153 | (!end.HasValue || tasks.EndTime < end) &&
|
---|
154 | (string.IsNullOrEmpty(jobId) || tasks.JobId.ToString() == jobId) &&
|
---|
155 | (string.IsNullOrEmpty(taskState) || tasks.TaskState == state)
|
---|
156 | select new
|
---|
157 | {
|
---|
158 | JobName = jobs.JobName,
|
---|
159 | JobId = jobs.JobId,
|
---|
160 | TaskId = tasks.TaskId,
|
---|
161 | StartDate = tasks.StartTime
|
---|
162 | }).OrderByDescending(s => s.StartDate).ToList();
|
---|
163 |
|
---|
164 | //Was overflowing default MaxJsonLength
|
---|
165 | //return Json(data, JsonRequestBehavior.AllowGet);
|
---|
166 | return new JsonResult() { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet, MaxJsonLength = Int32.MaxValue };
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | public JsonResult TaskInfo(string taskId)
|
---|
171 | {
|
---|
172 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
173 | {
|
---|
174 | var data =
|
---|
175 | (from tasks in db.FactTasks
|
---|
176 | join jobs in db.DimJobs
|
---|
177 | on tasks.JobId equals jobs.JobId
|
---|
178 | where tasks.TaskId.ToString() == taskId
|
---|
179 | select new
|
---|
180 | {
|
---|
181 | TotalWaiting = tasks.TotalWaitingTime,
|
---|
182 | TotalTransfer = tasks.TotalTransferTime,
|
---|
183 | TotalRuntime = tasks.TotalRuntime,
|
---|
184 | StartDate = tasks.StartTime
|
---|
185 | }).OrderByDescending(s => s.StartDate).ToList();
|
---|
186 |
|
---|
187 | return Json(data, JsonRequestBehavior.AllowGet);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | public JsonResult GetSlaves(DateTime? start = null, DateTime? end = null, string userName = null, string slaveId = null)
|
---|
192 | {
|
---|
193 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
194 | {
|
---|
195 | var data =
|
---|
196 | (from slaves in db.FactClientInfos
|
---|
197 | join users in db.DimUsers
|
---|
198 | on slaves.UserId equals users.UserId
|
---|
199 | join clients in db.DimClients
|
---|
200 | on slaves.ClientId equals clients.Id
|
---|
201 | where (!start.HasValue || slaves.Time >= start) &&
|
---|
202 | (!end.HasValue || slaves.Time < end) &&
|
---|
203 | (string.IsNullOrEmpty(userName) || users.Name == userName) &&
|
---|
204 | (string.IsNullOrEmpty(slaveId) || slaves.ClientId.ToString() == slaveId)
|
---|
205 | select new
|
---|
206 | {
|
---|
207 | ClientName = clients.Name,
|
---|
208 | SlaveID = slaves.ClientId,
|
---|
209 | }).GroupBy(s => s.SlaveID).ToList();
|
---|
210 | //Was overflowing default MaxJsonLength
|
---|
211 | //return Json(data, JsonRequestBehavior.AllowGet);
|
---|
212 | return new JsonResult() { Data = data, JsonRequestBehavior = JsonRequestBehavior.AllowGet, MaxJsonLength = Int32.MaxValue };
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | public JsonResult SlaveInfo(string slaveId, DateTime? start = null, DateTime? end = null, string userName = null) {
|
---|
217 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
218 | {
|
---|
219 | var data =
|
---|
220 | (from slaves in db.FactClientInfos
|
---|
221 | join users in db.DimUsers
|
---|
222 | on slaves.UserId equals users.UserId
|
---|
223 | join clients in db.DimClients
|
---|
224 | on slaves.ClientId equals clients.Id
|
---|
225 | where slaves.ClientId.ToString() == slaveId &&
|
---|
226 | (!start.HasValue || slaves.Time >= start) &&
|
---|
227 | (!end.HasValue || slaves.Time < end) &&
|
---|
228 | (string.IsNullOrEmpty(userName) || users.Name == userName)
|
---|
229 | select new
|
---|
230 | {
|
---|
231 | ClientName = clients.Name,
|
---|
232 | SlaveID = slaves.ClientId,
|
---|
233 | Time = slaves.Time,
|
---|
234 | UsedCores = slaves.NumUsedCores,
|
---|
235 | TotalCores = slaves.NumTotalCores,
|
---|
236 | UsedMemory = slaves.UsedMemory,
|
---|
237 | TotalMemory = slaves.TotalMemory,
|
---|
238 | CPUUtilization = slaves.CpuUtilization
|
---|
239 | }).OrderByDescending(s => s.Time).ToList();
|
---|
240 |
|
---|
241 | return Json(data, JsonRequestBehavior.AllowGet);
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | public JsonResult JobProgress(string jobId) {
|
---|
246 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
|
---|
247 | var data =
|
---|
248 | (from tasks in db.FactTasks
|
---|
249 | where tasks.JobId.ToString() == jobId
|
---|
250 | select new
|
---|
251 | {
|
---|
252 | State = tasks.TaskState
|
---|
253 | }).ToList();
|
---|
254 |
|
---|
255 | Dictionary<string, int> JobTasks = new Dictionary<string,int>();
|
---|
256 | JobTasks.Add("Wait",0);
|
---|
257 | JobTasks.Add("Transfer", 0);
|
---|
258 | JobTasks.Add("Calculate", 0);
|
---|
259 | JobTasks.Add("Finish", 0);
|
---|
260 | JobTasks.Add("Error", 0);
|
---|
261 |
|
---|
262 | foreach(var record in data) {
|
---|
263 | switch (record.State) {
|
---|
264 | case TaskState.Waiting:
|
---|
265 | JobTasks["Wait"] += 1;
|
---|
266 | break;
|
---|
267 | case TaskState.Transferring:
|
---|
268 | JobTasks["Transfer"] += 1;
|
---|
269 | break;
|
---|
270 | case TaskState.Calculating:
|
---|
271 | JobTasks["Calculate"] += 1;
|
---|
272 | break;
|
---|
273 | case TaskState.Finished:
|
---|
274 | JobTasks["Finish"] += 1;
|
---|
275 | break;
|
---|
276 | case TaskState.Aborted:
|
---|
277 | case TaskState.Failed:
|
---|
278 | JobTasks["Error"] += 1;
|
---|
279 | break;
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | return Json(JobTasks, JsonRequestBehavior.AllowGet);
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | public JsonResult UserTasks(string taskState) {
|
---|
288 | TaskState? state = GetTaskState(taskState);
|
---|
289 | List<KeyValuePair<string, int>> numberTasksByUser = new List<KeyValuePair<string,int>>();
|
---|
290 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
|
---|
291 | var userTasks = (from tasks in db.FactTasks
|
---|
292 | join jobs in db.DimJobs
|
---|
293 | on tasks.JobId equals jobs.JobId
|
---|
294 | where tasks.TaskState == state
|
---|
295 | select new
|
---|
296 | {
|
---|
297 | UserID = jobs.UserId,
|
---|
298 | UserName = jobs.UserName,
|
---|
299 | State = tasks.TaskState
|
---|
300 | }).GroupBy(j => j.UserID).ToList();
|
---|
301 |
|
---|
302 | foreach (var user in userTasks) {
|
---|
303 | numberTasksByUser.Add(new KeyValuePair<string, int>(user.First().UserName, user.Count()));
|
---|
304 | }
|
---|
305 | return Json(numberTasksByUser, JsonRequestBehavior.AllowGet);
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | private static TaskState? GetTaskState(string taskState) {
|
---|
310 | TaskState? state;
|
---|
311 | switch (taskState)
|
---|
312 | {
|
---|
313 | case "Aborted":
|
---|
314 | state = TaskState.Aborted;
|
---|
315 | break;
|
---|
316 | case "Calculating":
|
---|
317 | state = TaskState.Calculating;
|
---|
318 | break;
|
---|
319 | case "Failed":
|
---|
320 | state = TaskState.Failed;
|
---|
321 | break;
|
---|
322 | case "Finished":
|
---|
323 | state = TaskState.Finished;
|
---|
324 | break;
|
---|
325 | case "Offline":
|
---|
326 | state = TaskState.Offline;
|
---|
327 | break;
|
---|
328 | case "Paused":
|
---|
329 | state = TaskState.Paused;
|
---|
330 | break;
|
---|
331 | case "Transferring":
|
---|
332 | state = TaskState.Transferring;
|
---|
333 | break;
|
---|
334 | case "Waiting":
|
---|
335 | state = TaskState.Waiting;
|
---|
336 | break;
|
---|
337 | default :
|
---|
338 | state = null;
|
---|
339 | break;
|
---|
340 | }
|
---|
341 | return state;
|
---|
342 | }
|
---|
343 |
|
---|
344 | private static IOrderedQueryable<IGrouping<DateTime, FactClientInfo>> GetClientFacts(HiveDataContext db, DateTime? start = null, DateTime? end = null) {
|
---|
345 | start = start ?? DateTime.Now - DefaultDuration;
|
---|
346 | end = end ?? DateTime.Now;
|
---|
347 |
|
---|
348 | return from ci in db.FactClientInfos
|
---|
349 | where ci.Time >= start && ci.Time < end
|
---|
350 | group ci by ci.Time into timeGroup
|
---|
351 | orderby timeGroup.Key
|
---|
352 | select timeGroup;
|
---|
353 | }
|
---|
354 |
|
---|
355 | private static IEnumerable<IEnumerable<object[]>> CreateSeriesData<T>(IEnumerable<T> data, Func<T, DateTime> timeSelector, params Func<T, object>[] seriesSelectors) {
|
---|
356 | return seriesSelectors.Select(selector =>
|
---|
357 | data.Select(x => new[] {
|
---|
358 | timeSelector(x).ToUniversalTime().ToUnixTimestamp(),
|
---|
359 | selector(x)
|
---|
360 | })
|
---|
361 | );
|
---|
362 | }
|
---|
363 |
|
---|
364 | private static DateTime GetRecentDate(HiveDataContext db)
|
---|
365 | {
|
---|
366 | var mostRecent = from dates in db.FactClientInfos
|
---|
367 | group dates by dates.Time into startDate
|
---|
368 | select startDate.OrderByDescending(t=>t.Time).FirstOrDefault();
|
---|
369 | return mostRecent.Max(t=>t.Time);
|
---|
370 | }
|
---|
371 | }
|
---|
372 |
|
---|
373 | public static class DateTimeExtensions {
|
---|
374 | public static long ToUnixTimestamp(this DateTime dateTime) {
|
---|
375 | var duration = dateTime - new DateTime(1970, 1, 1, 0, 0, 0);
|
---|
376 |
|
---|
377 | return (long)duration.TotalMilliseconds;
|
---|
378 | }
|
---|
379 | }
|
---|
380 | }
|
---|