Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.WebApp.Status/3.3/WebApi/DataController.cs @ 12435

Last change on this file since 12435 was 12435, checked in by dglaser, 9 years ago

#2394: Improved PluginManager and updated hive status monitor.

File size: 8.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Http;
26using HeuristicLab.Services.Hive;
27using HeuristicLab.Services.Hive.DataAccess;
28using DAL = HeuristicLab.Services.Hive.DataAccess;
29using DTO = HeuristicLab.Services.WebApp.Status.WebApi.DataTransfer;
30
31namespace HeuristicLab.Services.WebApp.Status.WebApi {
32  public class DataController : ApiController {
33
34    // start temporary quickfix
35    private const string SQL_USER_TASK_STATUS =
36      @"WITH UserTasks AS (
37          SELECT Job.OwnerUserId AS UserId, TaskState, COUNT(Task.TaskId) AS Count
38          FROM Task, Job
39          WHERE Task.JobId = Job.JobId AND TaskState IN ('Calculating', 'Waiting')
40          GROUP BY Job.OwnerUserId, TaskState
41        )
42        SELECT
43          DISTINCT UserId,
44          (SELECT Count FROM UserTasks WHERE TaskState = 'Calculating' AND UserId = ut.UserId) AS CalculatingTasks,
45          (SELECT Count FROM UserTasks WHERE TaskState = 'Waiting' AND UserId = ut.UserId) AS WaitingTasks
46        FROM UserTasks ut;";
47
48
49    private class UserTaskStatus {
50      public Guid UserId { get; set; }
51      public int CalculatingTasks { get; set; }
52      public int WaitingTasks { get; set; }
53    }
54
55    public IEnumerable<DTO.TaskStatus> GetTaskStatus(HiveDataContext db) {
56      var query = db.ExecuteQuery<UserTaskStatus>(SQL_USER_TASK_STATUS).ToList();
57      return query.Select(uts => new DTO.TaskStatus {
58        User = new DTO.User {
59          Id = uts.UserId.ToString(),
60          Name = ServiceLocator.Instance.UserManager.GetUserById(uts.UserId).UserName
61        },
62        CalculatingTasks = uts.CalculatingTasks,
63        WaitingTasks = uts.WaitingTasks
64      }).OrderBy(x => x.User.Name);
65    }
66    // end temporary quickfix
67
68    public DTO.Status GetStatus() {
69      using (var db = new HiveDataContext()) {
70        var onlineSlaves = (from slave in db.Resources.OfType<DAL.Slave>()
71                            where slave.SlaveState == SlaveState.Calculating || slave.SlaveState == SlaveState.Idle
72                            select slave).ToList();
73        var activeSlaves = onlineSlaves.Where(s => s.IsAllowedToCalculate).ToList();
74        var calculatingSlaves = activeSlaves.Where(s => s.SlaveState == SlaveState.Calculating).ToList();
75
76        int calculatingMemory = calculatingSlaves.Any() ? (int)calculatingSlaves.Sum(s => s.Memory) / 1024 : 0;
77        int freeCalculatingMemory = calculatingSlaves.Any() ? (int)calculatingSlaves.Sum(s => s.FreeMemory) / 1024 : 0;
78
79        return new DTO.Status {
80          CoreStatus = new DTO.CoreStatus {
81            TotalCores = onlineSlaves.Sum(s => s.Cores ?? 0),
82            FreeCores = onlineSlaves.Sum(s => s.FreeCores ?? 0), // temporary for old chart data
83            ActiveCores = activeSlaves.Sum(s => s.Cores ?? 0),
84            CalculatingCores = calculatingSlaves.Sum(s => s.Cores ?? 0)
85          },
86          CpuUtilizationStatus = new DTO.CpuUtilizationStatus {
87            TotalCpuUtilization = onlineSlaves.Any()
88                                  ? Math.Round(onlineSlaves.Average(s => s.CpuUtilization), 2)
89                                  : 0.0,
90            ActiveCpuUtilization = activeSlaves.Any()
91                                   ? Math.Round(activeSlaves.Average(s => s.CpuUtilization), 2)
92                                   : 0.0,
93            CalculatingCpuUtilization = calculatingSlaves.Any()
94                                        ? Math.Round(calculatingSlaves.Average(s => s.CpuUtilization), 2)
95                                        : 0.0
96          },
97          MemoryStatus = new DTO.MemoryStatus {
98            TotalMemory = onlineSlaves.Any() ? (int)onlineSlaves.Sum(s => s.Memory) / 1024 : 0,
99            FreeMemory = onlineSlaves.Any() ? (int)onlineSlaves.Sum(s => s.FreeMemory) / 1024 : 0,
100            ActiveMemory = activeSlaves.Any() ? (int)activeSlaves.Sum(s => s.Memory) / 1024 : 0,
101            UsedMemory = calculatingMemory - freeCalculatingMemory
102          },
103          TasksStatus = GetTaskStatus(db),
104          SlavesStatus = onlineSlaves.Select(x => new DTO.SlaveStatus {
105            Slave = new DTO.Slave {
106              Id = x.ResourceId.ToString(),
107              Name = x.Name
108            },
109            CpuUtilization = x.CpuUtilization,
110            Cores = x.Cores ?? 0,
111            FreeCores = x.FreeCores ?? 0,
112            Memory = (x.Memory ?? 0) / 1024,
113            FreeMemory = (x.FreeMemory ?? 0) / 1024,
114            IsAllowedToCalculate = x.IsAllowedToCalculate,
115            State = x.SlaveState.ToString()
116          }).OrderBy(x => x.Slave.Name),
117          Timestamp = JavascriptUtils.ToTimestamp(DateTime.Now)
118        };
119      }
120    }
121
122    public IEnumerable<DTO.Status> GetStatusHistory(DateTime start, DateTime end) {
123      TimeSpan ts = end - start;
124      int increment = 1;
125      double totalMinutes = ts.TotalMinutes;
126      while (totalMinutes > 5761) {
127        totalMinutes -= 5761;
128        increment += 5;
129      }
130      using (var db = new HiveDataContext()) {
131        var statistics = db.Statistics.Where(s => s.Timestamp >= start && s.Timestamp <= end);
132        var status = new DTO.Status {
133          CoreStatus = new DTO.CoreStatus(),
134          CpuUtilizationStatus = new DTO.CpuUtilizationStatus(),
135          MemoryStatus = new DTO.MemoryStatus()
136        };
137        int freeCores = 0;
138        int freeMemory = 0;
139        int i = 1;
140        foreach (var statistic in statistics) {
141          status.CoreStatus.TotalCores += statistic.SlaveStatistics.Sum(x => x.Cores);
142          freeCores += statistic.SlaveStatistics.Sum(x => x.FreeCores);
143          status.CpuUtilizationStatus.TotalCpuUtilization += statistic.SlaveStatistics.Any()
144                                                             ? statistic.SlaveStatistics.Average(x => x.CpuUtilization)
145                                                             : 0.0;
146          status.MemoryStatus.TotalMemory += statistic.SlaveStatistics.Sum(x => x.Memory) / 1024;
147          freeMemory += statistic.SlaveStatistics.Sum(x => x.FreeMemory) / 1024;
148          if (i >= increment) {
149            status.Timestamp = JavascriptUtils.ToTimestamp(statistic.Timestamp);
150            status.CoreStatus.TotalCores /= i;
151            freeCores /= i;
152            status.CpuUtilizationStatus.TotalCpuUtilization /= i;
153            status.MemoryStatus.TotalMemory /= i;
154            freeMemory /= i;
155            status.CoreStatus.ActiveCores = status.CoreStatus.TotalCores;
156            status.MemoryStatus.ActiveMemory = status.MemoryStatus.TotalMemory;
157            status.CpuUtilizationStatus.ActiveCpuUtilization = status.CpuUtilizationStatus.TotalCpuUtilization;
158            status.CpuUtilizationStatus.CalculatingCpuUtilization = status.CpuUtilizationStatus.CalculatingCpuUtilization;
159            status.CoreStatus.CalculatingCores = status.CoreStatus.TotalCores - freeCores;
160            status.MemoryStatus.UsedMemory = status.MemoryStatus.TotalMemory - freeMemory;
161            yield return status;
162            status = new DTO.Status {
163              CoreStatus = new DTO.CoreStatus(),
164              CpuUtilizationStatus = new DTO.CpuUtilizationStatus(),
165              MemoryStatus = new DTO.MemoryStatus()
166            };
167            freeCores = 0;
168            freeMemory = 0;
169            i = 1;
170          } else {
171            i++;
172          }
173        }
174      }
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.