Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11020 was 11020, checked in by mroscoe, 8 years ago

First check-in for Matt Roscoe. Major revision, multiple new files created and multiple files changed.

File size: 7.9 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 GetUserTask(string userName, DateTime? start = null, DateTime? end = null)
142    {
143      start = start ?? DateTime.Now - DefaultDuration;
144      end = end ?? DateTime.Now;
145      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
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          tasks.StartTime >= start &&
153          tasks.EndTime < end
154          select new
155          {
156            TotalWaiting = tasks.TotalWaitingTime,
157            TotalTransfer = tasks.TotalTransferTime,
158            TotalRuntime = tasks.TotalRuntime
159          }).ToList();
160
161        List<List<double>> overallResult = new List<List<double>>();
162        List<double> wait = new List<double>();
163        List<double> transfer = new List<double>();
164        List<double> run = new List<double>();
165        data.ForEach(w => wait.Add(w.TotalWaiting));
166        data.ForEach(t => transfer.Add(t.TotalTransfer));
167        data.ForEach(r => run.Add(r.TotalRuntime));
168        overallResult.Add(wait);
169        overallResult.Add(transfer);
170        overallResult.Add(run);
171
172        return Json(overallResult,JsonRequestBehavior.AllowGet);
173      }
174    }
175
176    private static IOrderedQueryable<IGrouping<DateTime, FactClientInfo>> GetClientFacts(HiveDataContext db, DateTime? start = null, DateTime? end = null) {
177      start = start ?? DateTime.Now - DefaultDuration;
178      end = end ?? DateTime.Now;
179
180      return from ci in db.FactClientInfos
181             where ci.Time >= start && ci.Time < end
182             group ci by ci.Time into timeGroup
183             orderby timeGroup.Key
184             select timeGroup;
185    }
186
187    private static IEnumerable<IEnumerable<object[]>> CreateSeriesData<T>(IEnumerable<T> data, Func<T, DateTime> timeSelector, params Func<T, object>[] seriesSelectors) {
188      return seriesSelectors.Select(selector =>
189        data.Select(x => new[] {
190          timeSelector(x).ToUniversalTime().ToUnixTimestamp(),
191          selector(x)
192        })
193      );
194    }
195
196    private static DateTime GetRecentDate(HiveDataContext db)
197    {
198      var mostRecent = from dates in db.FactClientInfos
199        group dates by dates.Time into startDate
200        select startDate.OrderByDescending(t=>t.Time).FirstOrDefault();
201      return mostRecent.Max(t=>t.Time);
202    }
203  }
204
205  public static class DateTimeExtensions {
206    public static long ToUnixTimestamp(this DateTime dateTime) {
207      var duration = dateTime - new DateTime(1970, 1, 1, 0, 0, 0);
208
209      return (long)duration.TotalMilliseconds;
210    }
211  }
212}
Note: See TracBrowser for help on using the repository browser.