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 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 | for (int i = 0; i < data.Count; i++) {
|
---|
195 | results.Add(
|
---|
196 | new KeyValuePair<string, List<double>>(
|
---|
197 | data[i].TaskID.ToString(),new List<double>{data[i].TotalWaiting,data[i].TotalTransfer,data[i].TotalRuntime}
|
---|
198 | )
|
---|
199 | );
|
---|
200 | }
|
---|
201 |
|
---|
202 | return Json(results, JsonRequestBehavior.AllowGet);
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | public JsonResult SlaveInfo(string limit, DateTime? start = null, DateTime? end = null, string userName = null, string slaveId=null)
|
---|
207 | {
|
---|
208 | using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
|
---|
209 | {
|
---|
210 | var data =
|
---|
211 | (from slaves in db.FactClientInfos
|
---|
212 | join users in db.DimUsers
|
---|
213 | on slaves.UserId equals users.UserId
|
---|
214 | where (!start.HasValue || slaves.Time >= start) &&
|
---|
215 | (!end.HasValue || slaves.Time < end) &&
|
---|
216 | (string.IsNullOrEmpty(userName) || users.Name == userName) &&
|
---|
217 | (string.IsNullOrEmpty(slaveId) || slaves.ClientId.ToString() == slaveId)
|
---|
218 | select new
|
---|
219 | {
|
---|
220 | SlaveID = slaves.ClientId,
|
---|
221 | Time = slaves.Time,
|
---|
222 | UsedCores = slaves.NumUsedCores,
|
---|
223 | TotalCores = slaves.NumTotalCores,
|
---|
224 | UsedMemory = slaves.UsedMemory,
|
---|
225 | TotalMemory = slaves.TotalMemory,
|
---|
226 | CPUUtilization = slaves.CpuUtilization
|
---|
227 | }).OrderByDescending(s => s.Time).GroupBy(s => s.SlaveID).ToList();
|
---|
228 |
|
---|
229 | return Json(data, JsonRequestBehavior.AllowGet);
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | private static TaskState GetTaskState(string taskState) {
|
---|
234 | TaskState state = TaskState.Finished;
|
---|
235 | switch (taskState)
|
---|
236 | {
|
---|
237 | case "Aborted":
|
---|
238 | state = TaskState.Aborted;
|
---|
239 | break;
|
---|
240 | case "Calculating":
|
---|
241 | state = TaskState.Calculating;
|
---|
242 | break;
|
---|
243 | case "Failed":
|
---|
244 | state = TaskState.Failed;
|
---|
245 | break;
|
---|
246 | case "Finished":
|
---|
247 | state = TaskState.Finished;
|
---|
248 | break;
|
---|
249 | case "Offline":
|
---|
250 | state = TaskState.Offline;
|
---|
251 | break;
|
---|
252 | case "Paused":
|
---|
253 | state = TaskState.Paused;
|
---|
254 | break;
|
---|
255 | case "Transferring":
|
---|
256 | state = TaskState.Transferring;
|
---|
257 | break;
|
---|
258 | case "Waiting":
|
---|
259 | state = TaskState.Waiting;
|
---|
260 | break;
|
---|
261 | }
|
---|
262 | return state;
|
---|
263 | }
|
---|
264 |
|
---|
265 | private static IOrderedQueryable<IGrouping<DateTime, FactClientInfo>> GetClientFacts(HiveDataContext db, DateTime? start = null, DateTime? end = null) {
|
---|
266 | start = start ?? DateTime.Now - DefaultDuration;
|
---|
267 | end = end ?? DateTime.Now;
|
---|
268 |
|
---|
269 | return from ci in db.FactClientInfos
|
---|
270 | where ci.Time >= start && ci.Time < end
|
---|
271 | group ci by ci.Time into timeGroup
|
---|
272 | orderby timeGroup.Key
|
---|
273 | select timeGroup;
|
---|
274 | }
|
---|
275 |
|
---|
276 | private static IEnumerable<IEnumerable<object[]>> CreateSeriesData<T>(IEnumerable<T> data, Func<T, DateTime> timeSelector, params Func<T, object>[] seriesSelectors) {
|
---|
277 | return seriesSelectors.Select(selector =>
|
---|
278 | data.Select(x => new[] {
|
---|
279 | timeSelector(x).ToUniversalTime().ToUnixTimestamp(),
|
---|
280 | selector(x)
|
---|
281 | })
|
---|
282 | );
|
---|
283 | }
|
---|
284 |
|
---|
285 | private static DateTime GetRecentDate(HiveDataContext db)
|
---|
286 | {
|
---|
287 | var mostRecent = from dates in db.FactClientInfos
|
---|
288 | group dates by dates.Time into startDate
|
---|
289 | select startDate.OrderByDescending(t=>t.Time).FirstOrDefault();
|
---|
290 | return mostRecent.Max(t=>t.Time);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | public static class DateTimeExtensions {
|
---|
295 | public static long ToUnixTimestamp(this DateTime dateTime) {
|
---|
296 | var duration = dateTime - new DateTime(1970, 1, 1, 0, 0, 0);
|
---|
297 |
|
---|
298 | return (long)duration.TotalMilliseconds;
|
---|
299 | }
|
---|
300 | }
|
---|
301 | }
|
---|