#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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 {
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 Json(
CreateSeriesData(data.ToList(), x => x.Time, x => x.UsedMemory, x => x.TotalMemory),
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.DimTime.Time > start && ci.DimTime.Time < end
group ci by ci.DimTime.Time into timeGroup
orderby timeGroup.Key
select timeGroup;
}
private static IEnumerable> CreateSeriesData(IEnumerable data, Func timeSelector, params Func[] selectors) {
return selectors.Select(selector =>
data.Select(x => new[] {
timeSelector(x).ToString("yyyy-MM-dd HH:mm"),
selector(x)
})
);
}
}
}