#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using HeuristicLab.Services.Hive.DataAccess; namespace HeuristicLab.Services.Hive.Statistics.Controllers { //[OutputCache(Duration = 60 * 5)] public class ChartDataController : Controller { private static readonly TimeSpan DefaultDuration = new TimeSpan(1, 0, 0, 0); public JsonResult AverageCpuUtilization(DateTime? start = null, DateTime? end = null) { using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) { var data = from facts in GetClientFacts(db, start, end) select new { Time = facts.Key, CpuUtilization = facts.Average(x => x.CpuUtilization) }; return Json( CreateSeriesData(data.ToList(), x => x.Time, x => x.CpuUtilization), JsonRequestBehavior.AllowGet); } } public JsonResult UsedCores(DateTime? start = null, DateTime? end = null) { using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) { var data = from facts in GetClientFacts(db, start, end) select new { Time = facts.Key, UsedCores = facts.Sum(x => x.NumUsedCores), TotalCores = facts.Sum(x => x.NumTotalCores) }; return Json( CreateSeriesData(data.ToList(), x => x.Time, x => x.UsedCores, x => x.TotalCores), JsonRequestBehavior.AllowGet); } } public JsonResult UsedMemory(DateTime? start = null, DateTime? end = null) { using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) { var data = from facts in GetClientFacts(db, start, end) select new { Time = facts.Key, UsedMemory = facts.Sum(x => x.UsedMemory), TotalMemory = facts.Sum(x => x.TotalMemory) }; // Return values in GB return Json( CreateSeriesData(data.ToList(), x => x.Time, x => x.UsedMemory / 1000, x => x.TotalMemory / 1000), JsonRequestBehavior.AllowGet); } } private static IOrderedQueryable> GetClientFacts(HiveDataContext db, DateTime? start = null, DateTime? end = null) { start = start ?? DateTime.Now - DefaultDuration; end = end ?? DateTime.Now; return from ci in db.FactClientInfos where ci.Time >= start && ci.Time < end group ci by ci.Time into timeGroup orderby timeGroup.Key select timeGroup; } private static IEnumerable> CreateSeriesData(IEnumerable data, Func timeSelector, params Func[] seriesSelectors) { return seriesSelectors.Select(selector => data.Select(x => new[] { timeSelector(x).ToUniversalTime().ToUnixTimestamp(), selector(x) }) ); } } public static class DateTimeExtensions { public static long ToUnixTimestamp(this DateTime dateTime) { var duration = dateTime - new DateTime(1970, 1, 1, 0, 0, 0); return (long)duration.TotalMilliseconds; } } }