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

Last change on this file since 9643 was 9643, checked in by pfleck, 6 years ago

#2063:
Added time selection for charts.
Implemented redrawing of charts using Ajax.

File size: 4.1 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  //[OutputCache(Duration = 60 * 5)]
30  public class ChartDataController : Controller {
31    private static readonly TimeSpan DefaultDuration = new TimeSpan(1, 0, 0, 0);
32
33    public JsonResult AverageCpuUtilization(DateTime? start = null, DateTime? end = null) {
34      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
35        var data =
36          from facts in GetClientFacts(db, start, end)
37          select new {
38            Time = facts.Key,
39            CpuUtilization = facts.Average(x => x.CpuUtilization)
40          };
41
42        return Json(
43          CreateSeriesData(data.ToList(), x => x.Time, x => x.CpuUtilization),
44          JsonRequestBehavior.AllowGet);
45      }
46    }
47
48    public JsonResult UsedCores(DateTime? start = null, DateTime? end = null) {
49      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
50        var data =
51          from facts in GetClientFacts(db, start, end)
52          select new {
53            Time = facts.Key,
54            UsedCores = facts.Sum(x => x.NumUsedCores),
55            TotalCores = facts.Sum(x => x.NumTotalCores)
56          };
57
58        return Json(
59          CreateSeriesData(data.ToList(), x => x.Time, x => x.UsedCores, x => x.TotalCores),
60          JsonRequestBehavior.AllowGet);
61      }
62    }
63
64    public JsonResult UsedMemory(DateTime? start = null, DateTime? end = null) {
65      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
66        var data =
67          from facts in GetClientFacts(db, start, end)
68          select new {
69            Time = facts.Key,
70            UsedMemory = facts.Sum(x => x.UsedMemory),
71            TotalMemory = facts.Sum(x => x.TotalMemory)
72          };
73
74        // Return values in GB
75        return Json(
76          CreateSeriesData(data.ToList(), x => x.Time, x => x.UsedMemory / 1000, x => x.TotalMemory / 1000),
77          JsonRequestBehavior.AllowGet);
78      }
79    }
80
81    private static IOrderedQueryable<IGrouping<DateTime, FactClientInfo>> GetClientFacts(HiveDataContext db, DateTime? start = null, DateTime? end = null) {
82      start = start ?? DateTime.Now - DefaultDuration;
83      end = end ?? DateTime.Now;
84
85      return from ci in db.FactClientInfos
86             where ci.Time > start && ci.Time < end
87             group ci by ci.Time into timeGroup
88             orderby timeGroup.Key
89             select timeGroup;
90    }
91
92    private static IEnumerable<IEnumerable<object[]>> CreateSeriesData<T>(IEnumerable<T> data, Func<T, DateTime> timeSelector, params Func<T, object>[] selectors) {
93      return selectors.Select(selector =>
94        data.Select(x => new[] {
95          //timeSelector(x).ToString("yyyy-MM-dd HH:mm"),
96          timeSelector(x).ToUnixTimestamp(),
97          selector(x)
98        })
99      );
100    }
101  }
102
103  public static class DateTimeExtensions {
104    public static long ToUnixTimestamp(this DateTime d) {
105      var duration = d - new DateTime(1970, 1, 1, 0, 0, 0);
106
107      return (long)duration.TotalMilliseconds;
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.