Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9625 was 9625, checked in by pfleck, 11 years ago

#2063:
Insert charts on index page for CPU utilization, cores and memory usage.
Added html helper for generating chart javascript.
Added ChartDataController for generating JSON data for the charts.

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Services.Hive.DataAccess;
27
28namespace HeuristicLab.Services.Hive.Statistics.Controllers {
29  public class ChartDataController : Controller {
30    private static readonly TimeSpan DefaultDuration = new TimeSpan(1, 0, 0, 0);
31
32    public JsonResult AverageCpuUtilization(DateTime? start = null, DateTime? end = null) {
33      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
34        var data =
35          from facts in GetClientFacts(db, start, end)
36          select new {
37            Time = facts.Key,
38            CpuUtilization = facts.Average(x => x.CpuUtilization)
39          };
40
41        return Json(
42          CreateSeriesData(data.ToList(), x => x.Time, x => x.CpuUtilization),
43          JsonRequestBehavior.AllowGet);
44      }
45    }
46
47    public JsonResult UsedCores(DateTime? start = null, DateTime? end = null) {
48      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
49        var data =
50          from facts in GetClientFacts(db, start, end)
51          select new {
52            Time = facts.Key,
53            UsedCores = facts.Sum(x => x.NumUsedCores),
54            TotalCores = facts.Sum(x => x.NumTotalCores)
55          };
56
57        return Json(
58          CreateSeriesData(data.ToList(), x => x.Time, x => x.UsedCores, x => x.TotalCores),
59          JsonRequestBehavior.AllowGet);
60      }
61    }
62
63    public JsonResult UsedMemory(DateTime? start = null, DateTime? end = null) {
64      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
65        var data =
66          from facts in GetClientFacts(db, start, end)
67          select new {
68            Time = facts.Key,
69            UsedMemory = facts.Sum(x => x.UsedMemory),
70            TotalMemory = facts.Sum(x => x.TotalMemory)
71          };
72
73        return Json(
74          CreateSeriesData(data.ToList(), x => x.Time, x => x.UsedMemory, x => x.TotalMemory),
75          JsonRequestBehavior.AllowGet);
76      }
77    }
78
79    private static IOrderedQueryable<IGrouping<DateTime, FactClientInfo>> GetClientFacts(HiveDataContext db, DateTime? start = null, DateTime? end = null) {
80      start = start ?? DateTime.Now - DefaultDuration;
81      end = end ?? DateTime.Now;
82
83      return from ci in db.FactClientInfos
84             where ci.DimTime.Time > start && ci.DimTime.Time < end
85             group ci by ci.DimTime.Time into timeGroup
86             orderby timeGroup.Key
87             select timeGroup;
88    }
89
90    private static IEnumerable<IEnumerable<object[]>> CreateSeriesData<T>(IEnumerable<T> data, Func<T, DateTime> timeSelector, params Func<T, object>[] selectors) {
91      return selectors.Select(selector =>
92        data.Select(x => new[] {
93          timeSelector(x).ToString("yyyy-MM-dd HH:mm"),
94          selector(x)
95        })
96      );
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.