Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.Statistics/3.3/Controllers/ChartDataController.cs @ 11036

Last change on this file since 11036 was 11036, checked in by mroscoe, 10 years ago
File size: 11.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Web.Mvc;
26using System.Web.Script.Serialization;
27using HeuristicLab.Services.Hive.DataAccess;
28
29namespace 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 NumberPages(string userName, string limit, 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        var numberRecords =
147          (from tasks in db.FactTasks
148           join jobs in db.DimJobs
149             on tasks.JobId equals jobs.JobId
150           where jobs.UserName == userName &&
151           (!start.HasValue || tasks.StartTime >= start) &&
152           (!end.HasValue || tasks.EndTime < end) &&
153           (string.IsNullOrEmpty(jobId) || tasks.JobId.ToString() == jobId) &&
154           (string.IsNullOrEmpty(taskState) || tasks.TaskState == state)
155           select new
156           {
157             TaskID = tasks.TaskId,
158             StartDate = tasks.StartTime
159           }).OrderByDescending(s => s.StartDate).ToList().Count;
160
161        var numberPages = numberRecords / Convert.ToDecimal(limit);
162        numberPages = Decimal.Truncate(numberPages);
163        numberPages += 1;
164
165        return Json(numberPages, JsonRequestBehavior.AllowGet);
166      }
167    }
168
169    public JsonResult UserTask(string userName, string limit, DateTime? start = null, DateTime? end = null, string jobId = null, string taskState=null)
170    {
171      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
172      {
173        TaskState state = GetTaskState(taskState);
174        var data =
175          (from tasks in db.FactTasks
176           join jobs in db.DimJobs
177             on tasks.JobId equals jobs.JobId
178           where jobs.UserName == userName &&
179           (!start.HasValue || tasks.StartTime >= start) &&
180           (!end.HasValue || tasks.EndTime < end) &&
181           (string.IsNullOrEmpty(jobId) || tasks.JobId.ToString() == jobId) &&
182           (string.IsNullOrEmpty(taskState) || tasks.TaskState == state)
183           select new
184           {
185             TaskID = tasks.TaskId,
186             TotalWaiting = tasks.TotalWaitingTime,
187             TotalTransfer = tasks.TotalTransferTime,
188             TotalRuntime = tasks.TotalRuntime,
189             StartDate = tasks.StartTime
190           }).OrderByDescending(s => s.StartDate).ToList();
191
192        List<KeyValuePair<string, List<double>>> results = new List<KeyValuePair<string, List<double>>>();
193
194        List<string> id = new List<string>();
195        List<double> wait = new List<double>();
196        List<double> transfer = new List<double>();
197        List<double> run = new List<double>();
198        List<double> times = new List<double>();
199        data.ForEach(i => id.Add(i.TaskID.ToString()));
200        data.ForEach(w => wait.Add(w.TotalWaiting));
201        data.ForEach(t => transfer.Add(t.TotalTransfer));
202        data.ForEach(r => run.Add(r.TotalRuntime));
203
204        for (int i = 0; i < id.Count; i++) {
205          results.Add(new KeyValuePair<string, List<double>>(id[i],new List<double>{wait[i],transfer[i],run[i]}));
206        }
207
208        return Json(results, JsonRequestBehavior.AllowGet);
209      }
210    }
211
212    public JsonResult SlaveInfo(string limit, DateTime? start = null, DateTime? end = null, string userName = null)
213    {
214      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
215      {
216        var data =
217          (from slaves in db.FactClientInfos
218           join users in db.DimUsers
219            on slaves.UserId equals users.UserId
220           where (!start.HasValue || slaves.Time >= start) &&
221           (!end.HasValue || slaves.Time < end) &&
222           (string.IsNullOrEmpty(userName) || users.Name == userName)
223           select new
224           {
225             SlaveID = slaves.ClientId,
226             Time = slaves.Time,
227             UsedCores = slaves.NumUsedCores,
228             TotalCores = slaves.NumTotalCores,
229             UsedMemory = slaves.UsedMemory,
230             TotalMemory = slaves.TotalMemory,
231             CPUUtilization = slaves.CpuUtilization
232           }).OrderByDescending(s => s.Time).GroupBy(s => s.SlaveID).ToList();
233
234        return Json(data, JsonRequestBehavior.AllowGet);
235      }
236    }
237
238    private static TaskState GetTaskState(string taskState) {
239      TaskState state = TaskState.Finished;
240      switch (taskState)
241      {
242        case "Aborted":
243          state = TaskState.Aborted;
244          break;
245        case "Calculating":
246          state = TaskState.Calculating;
247          break;
248        case "Failed":
249          state = TaskState.Failed;
250          break;
251        case "Finished":
252          state = TaskState.Finished;
253          break;
254        case "Offline":
255          state = TaskState.Offline;
256          break;
257        case "Paused":
258          state = TaskState.Paused;
259          break;
260        case "Transferring":
261          state = TaskState.Transferring;
262          break;
263        case "Waiting":
264          state = TaskState.Waiting;
265          break;
266      }
267      return state;
268    }
269
270    private static IOrderedQueryable<IGrouping<DateTime, FactClientInfo>> GetClientFacts(HiveDataContext db, DateTime? start = null, DateTime? end = null) {
271      start = start ?? DateTime.Now - DefaultDuration;
272      end = end ?? DateTime.Now;
273
274      return from ci in db.FactClientInfos
275             where ci.Time >= start && ci.Time < end
276             group ci by ci.Time into timeGroup
277             orderby timeGroup.Key
278             select timeGroup;
279    }
280
281    private static IEnumerable<IEnumerable<object[]>> CreateSeriesData<T>(IEnumerable<T> data, Func<T, DateTime> timeSelector, params Func<T, object>[] seriesSelectors) {
282      return seriesSelectors.Select(selector =>
283        data.Select(x => new[] {
284          timeSelector(x).ToUniversalTime().ToUnixTimestamp(),
285          selector(x)
286        })
287      );
288    }
289
290    private static DateTime GetRecentDate(HiveDataContext db)
291    {
292      var mostRecent = from dates in db.FactClientInfos
293        group dates by dates.Time into startDate
294        select startDate.OrderByDescending(t=>t.Time).FirstOrDefault();
295      return mostRecent.Max(t=>t.Time);
296    }
297  }
298
299  public static class DateTimeExtensions {
300    public static long ToUnixTimestamp(this DateTime dateTime) {
301      var duration = dateTime - new DateTime(1970, 1, 1, 0, 0, 0);
302
303      return (long)duration.TotalMilliseconds;
304    }
305  }
306}
Note: See TracBrowser for help on using the repository browser.