Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.Statistics/3.3/Controllers/AdminDataController.cs @ 11252

Last change on this file since 11252 was 11246, checked in by mroscoe, 10 years ago
File size: 6.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.Mvc;
6using HeuristicLab.Services.Hive.DataAccess;
7
8namespace HeuristicLab.Services.Hive.Statistics.Controllers {
9  public class AdminDataController : Controller {
10
11    public JsonResult UserList()
12    {
13      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
14      {
15        var allUsers = (from users in db.DimUsers
16                            select new { users.Name })
17                            .ToList();
18
19        List<string> UserNames = new List<string>();
20        allUsers.ForEach(u => UserNames.Add(u.Name));
21
22        return Json(UserNames, JsonRequestBehavior.AllowGet);
23      }
24    }
25
26    public JsonResult TaskStats(DateTime start, DateTime end, string userName=null) {
27      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString)) {
28        var taskStats = (from tasks in db.FactTasks
29                        join jobs in db.DimJobs
30                          on tasks.JobId equals jobs.JobId
31                        where tasks.StartTime >= start &&
32                        tasks.EndTime < end &&
33                        (string.IsNullOrEmpty(userName) || jobs.UserName == userName)
34                        select new { tasks.TotalWaitingTime, tasks.TotalTransferTime, tasks.TotalRuntime, tasks.NumCalculationRuns,
35                          tasks.CoresRequired, tasks.MemoryRequired})
36                          .ToList();
37
38        List<KeyValuePair<string, double>> FullStats = new List<KeyValuePair<string, double>>();
39        FullStats.Add(new KeyValuePair<string, double>("Total Waiting Time",taskStats.Sum(w => w.TotalWaitingTime)));
40        FullStats.Add(new KeyValuePair<string, double>("Total Transfer Time", taskStats.Sum(t => t.TotalTransferTime)));
41        FullStats.Add(new KeyValuePair<string, double>("Total Runtime", taskStats.Sum(r => r.TotalRuntime)));
42        FullStats.Add(new KeyValuePair<string, double>("Number Calculation Runs", taskStats.Sum(n => n.NumCalculationRuns)));
43        FullStats.Add(new KeyValuePair<string, double>("Cores Required", taskStats.Sum(c => c.CoresRequired)));
44        FullStats.Add(new KeyValuePair<string, double>("Memory Required", taskStats.Sum(m => m.MemoryRequired)));
45
46        return Json(FullStats, JsonRequestBehavior.AllowGet);
47      }
48    }
49
50    public JsonResult MoreTaskInfo(string taskId) {
51      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
52      {
53        var moreInfo = (from tasks in db.FactTasks
54                         where tasks.TaskId.ToString() == taskId
55                         select new
56                         {
57                           tasks.TotalWaitingTime,
58                           tasks.TotalTransferTime,
59                           tasks.TotalRuntime,
60                           tasks.NumCalculationRuns,
61                           tasks.CoresRequired,
62                           tasks.MemoryRequired,
63                         })
64                          .ToList();
65
66        List<KeyValuePair<string, double>> AllInfo = new List<KeyValuePair<string, double>>();
67        AllInfo.Add(new KeyValuePair<string, double>("Number Calculation Runs", moreInfo.Sum(n => n.NumCalculationRuns)));
68        AllInfo.Add(new KeyValuePair<string, double>("Cores Required", moreInfo.Sum(c => c.CoresRequired)));
69        AllInfo.Add(new KeyValuePair<string, double>("Memory Required", moreInfo.Sum(m => m.MemoryRequired)));
70
71        return Json(AllInfo, JsonRequestBehavior.AllowGet);
72      }
73    }
74
75    public JsonResult MoreSlaveInfo(string slaveId, DateTime? start = null, DateTime? end = null) {
76      using (var db = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString))
77      {
78        var moreInfo = (from slaves in db.FactClientInfos
79                        where slaves.ClientId.ToString() == slaveId &&
80                        (!start.HasValue || slaves.Time >= start) &&
81                        (!end.HasValue || slaves.Time < end)
82                        select new
83                        {
84                          slaves.Time,
85                          slaves.TotalTimeIdle,
86                          slaves.TotalTimeCalculating,
87                          slaves.TotalTimeTransferring,
88                          slaves.TotalTimeOffline,
89                          slaves.TotalTimeUnavailable
90                        }).ToList();
91
92        List<List<KeyValuePair<string, string>>> AllInfo = new List<List<KeyValuePair<string, string>>>();
93        List<List<KeyValuePair<string, string>>> DistinctInfo = new List<List<KeyValuePair<string, string>>>();
94        for(var i=0; i < moreInfo.Count; i++) {
95          List<KeyValuePair<string, string>> newRow = new List<KeyValuePair<string, string>>();
96          newRow.Add(new KeyValuePair<string, string>("Time", moreInfo.ElementAt(i).Time.ToShortDateString() + " " + moreInfo.ElementAt(i).Time.ToShortTimeString()));
97          newRow.Add(new KeyValuePair<string, string>("Time Idle", moreInfo.ElementAt(i).TotalTimeIdle.ToString()));
98          newRow.Add(new KeyValuePair<string, string>("Time Calculating", moreInfo.ElementAt(i).TotalTimeCalculating.ToString()));
99          newRow.Add(new KeyValuePair<string, string>("Time Transferring", moreInfo.ElementAt(i).TotalTimeTransferring.ToString()));
100          newRow.Add(new KeyValuePair<string, string>("Time Offline", moreInfo.ElementAt(i).TotalTimeOffline.ToString()));
101          newRow.Add(new KeyValuePair<string, string>("Time Unavailable", moreInfo.ElementAt(i).TotalTimeUnavailable.ToString()));
102          AllInfo.Add(newRow);
103        }
104
105        //Ensures that only records with changes in Traffic or Time fields are included
106        List<KeyValuePair<string, string>> lastRecord = null;
107        //Initilized to true to ensure first record is automatically included
108        bool isDistinct = true;
109        foreach (List<KeyValuePair<string, string>> row in AllInfo) {
110          //Ignores check for first row
111          if (lastRecord != null) {
112            isDistinct = false;
113            //Cycles through values of current row and lastRecord
114            for (var j=0; j < row.Count; j++) {
115              //Disregards time field from distinct checks
116              if (row.ElementAt(j).Key != "Time") {
117                //If values differ then row is distinct, break loop
118                if (row.ElementAt(j).Value != lastRecord.ElementAt(j).Value) {
119                  isDistinct = true;
120                  break;
121                }
122              }
123            }
124          }
125          lastRecord = row;
126          if (isDistinct) {
127            DistinctInfo.Add(row);
128          }
129        }
130
131        return Json(DistinctInfo, JsonRequestBehavior.AllowGet);
132      }
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.