1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web.Http;
|
---|
5 | using HeuristicLab.Services.Hive;
|
---|
6 | using HeuristicLab.Services.Hive.DataAccess;
|
---|
7 | using DAL = HeuristicLab.Services.Hive.DataAccess;
|
---|
8 | using DTO = HeuristicLab.Services.WebApp.Status.WebApi.DataTransfer;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Services.WebApp.Status.WebApi {
|
---|
11 | public class DataController : ApiController {
|
---|
12 |
|
---|
13 | // start temporary quickfix
|
---|
14 | private const string SQL_USER_TASK_STATUS =
|
---|
15 | @"WITH UserTasks AS (
|
---|
16 | SELECT Job.OwnerUserId AS UserId, TaskState, COUNT(Task.TaskId) AS Count
|
---|
17 | FROM Task, Job
|
---|
18 | WHERE Task.JobId = Job.JobId AND TaskState IN ('Calculating', 'Waiting')
|
---|
19 | GROUP BY Job.OwnerUserId, TaskState
|
---|
20 | )
|
---|
21 | SELECT
|
---|
22 | DISTINCT UserId,
|
---|
23 | ISNULL((SELECT Count FROM UserTasks WHERE TaskState = 'Calculating' AND UserId = ut.UserId), 0) AS CalculatingTasks,
|
---|
24 | ISNULL((SELECT Count FROM UserTasks WHERE TaskState = 'Waiting' AND UserId = ut.UserId), 0) AS WaitingTasks
|
---|
25 | FROM UserTasks ut;";
|
---|
26 |
|
---|
27 |
|
---|
28 | private class UserTaskStatus {
|
---|
29 | public Guid UserId { get; set; }
|
---|
30 | public int CalculatingTasks { get; set; }
|
---|
31 | public int WaitingTasks { get; set; }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public IEnumerable<DTO.TaskStatus> GetTaskStatus(HiveDataContext db) {
|
---|
35 | var query = db.ExecuteQuery<UserTaskStatus>(SQL_USER_TASK_STATUS).ToList();
|
---|
36 | return query.Select(uts => new DTO.TaskStatus {
|
---|
37 | User = new DTO.User {
|
---|
38 | Id = uts.UserId.ToString(),
|
---|
39 | Name = ServiceLocator.Instance.UserManager.GetUserById(uts.UserId).UserName
|
---|
40 | },
|
---|
41 | CalculatingTasks = uts.CalculatingTasks,
|
---|
42 | WaitingTasks = uts.WaitingTasks
|
---|
43 | });
|
---|
44 | }
|
---|
45 | // end temporary quickfix
|
---|
46 |
|
---|
47 | public DTO.Status GetStatus() {
|
---|
48 | using (var db = new HiveDataContext()) {
|
---|
49 | var onlineSlaves = (from slave in db.Resources.OfType<DAL.Slave>()
|
---|
50 | where slave.SlaveState == SlaveState.Calculating || slave.SlaveState == SlaveState.Idle
|
---|
51 | select slave).ToList();
|
---|
52 | return new DTO.Status {
|
---|
53 | CoreStatus = new DTO.CoreStatus {
|
---|
54 | TotalCores = onlineSlaves.Sum(s => s.Cores ?? 0),
|
---|
55 | AvailableCores = onlineSlaves.Where(s => s.IsAllowedToCalculate).Sum(s => s.Cores ?? 0),
|
---|
56 | FreeCores = onlineSlaves.Sum(s => s.FreeCores ?? 0)
|
---|
57 | },
|
---|
58 | CpuUtilizationStatus = new DTO.CpuUtilizationStatus {
|
---|
59 | TotalCpuUtilization = onlineSlaves.Any()
|
---|
60 | ? Math.Round(onlineSlaves.Average(s => s.CpuUtilization), 2)
|
---|
61 | : 0.0,
|
---|
62 | UsedCpuUtilization = onlineSlaves.Any(x => x.IsAllowedToCalculate)
|
---|
63 | ? Math.Round(onlineSlaves.Where(x => x.IsAllowedToCalculate).Average(s => s.CpuUtilization), 2)
|
---|
64 | : 0.0
|
---|
65 | },
|
---|
66 | MemoryStatus = new DTO.MemoryStatus {
|
---|
67 | TotalMemory = onlineSlaves.Any() ? (int)onlineSlaves.Sum(s => s.Memory) / 1024 : 0,
|
---|
68 | FreeMemory = onlineSlaves.Any() ? (int)onlineSlaves.Sum(s => s.FreeMemory) / 1024 : 0
|
---|
69 | },
|
---|
70 | TasksStatus = GetTaskStatus(db),
|
---|
71 | SlavesCpuStatus = onlineSlaves.Select(x => new DTO.SlaveCpuStatus {
|
---|
72 | CpuUtilization = Math.Round(x.CpuUtilization, 2),
|
---|
73 | Slave = new DTO.Slave {
|
---|
74 | Id = x.ResourceId.ToString(),
|
---|
75 | Name = x.Name
|
---|
76 | }
|
---|
77 | }),
|
---|
78 | Timestamp = JavascriptUtils.ToTimestamp(DateTime.Now)
|
---|
79 | };
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | public IEnumerable<DTO.Status> GetStatusHistory(DateTime start, DateTime end) {
|
---|
84 | using (var db = new HiveDataContext()) {
|
---|
85 | var statistics = db.Statistics.Where(s => s.Timestamp >= start && s.Timestamp <= end)
|
---|
86 | .OrderBy(x => x.Timestamp)
|
---|
87 | .ToList();
|
---|
88 | foreach (var statistic in statistics) {
|
---|
89 | yield return new DTO.Status {
|
---|
90 | CoreStatus = new DTO.CoreStatus {
|
---|
91 | TotalCores = statistic.SlaveStatistics.Sum(x => x.Cores),
|
---|
92 | AvailableCores = 0,
|
---|
93 | FreeCores = statistic.SlaveStatistics.Sum(x => x.FreeCores)
|
---|
94 | },
|
---|
95 | CpuUtilizationStatus = new DTO.CpuUtilizationStatus {
|
---|
96 | TotalCpuUtilization = 0.0,
|
---|
97 | UsedCpuUtilization = statistic.SlaveStatistics.Any() ? statistic.SlaveStatistics.Average(x => x.CpuUtilization) : 0.0
|
---|
98 | },
|
---|
99 | MemoryStatus = new DTO.MemoryStatus {
|
---|
100 | TotalMemory = statistic.SlaveStatistics.Sum(x => x.Memory) / 1024,
|
---|
101 | FreeMemory = statistic.SlaveStatistics.Sum(x => x.FreeMemory) / 1024
|
---|
102 | },
|
---|
103 | Timestamp = JavascriptUtils.ToTimestamp(statistic.Timestamp)
|
---|
104 | };
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | } |
---|