Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2394: Fixed UserTaskQuery and GetStatusHistory in the WebApp.Status plugin

File size: 8.3 KB
RevLine 
[12435]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;
[12428]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,
[12443]44          ISNULL((SELECT Count FROM UserTasks WHERE TaskState = 'Calculating' AND UserId = ut.UserId), 0) AS CalculatingTasks,
45          ISNULL((SELECT Count FROM UserTasks WHERE TaskState = 'Waiting' AND UserId = ut.UserId), 0) AS WaitingTasks
[12428]46        FROM UserTasks ut;";
47
48    private class UserTaskStatus {
49      public Guid UserId { get; set; }
50      public int CalculatingTasks { get; set; }
51      public int WaitingTasks { get; set; }
52    }
53
54    public IEnumerable<DTO.TaskStatus> GetTaskStatus(HiveDataContext db) {
55      var query = db.ExecuteQuery<UserTaskStatus>(SQL_USER_TASK_STATUS).ToList();
56      return query.Select(uts => new DTO.TaskStatus {
57        User = new DTO.User {
58          Id = uts.UserId.ToString(),
59          Name = ServiceLocator.Instance.UserManager.GetUserById(uts.UserId).UserName
60        },
61        CalculatingTasks = uts.CalculatingTasks,
62        WaitingTasks = uts.WaitingTasks
[12435]63      }).OrderBy(x => x.User.Name);
[12428]64    }
65    // end temporary quickfix
66
67    public DTO.Status GetStatus() {
68      using (var db = new HiveDataContext()) {
69        var onlineSlaves = (from slave in db.Resources.OfType<DAL.Slave>()
70                            where slave.SlaveState == SlaveState.Calculating || slave.SlaveState == SlaveState.Idle
71                            select slave).ToList();
[12435]72        var activeSlaves = onlineSlaves.Where(s => s.IsAllowedToCalculate).ToList();
73        var calculatingSlaves = activeSlaves.Where(s => s.SlaveState == SlaveState.Calculating).ToList();
74
75        int calculatingMemory = calculatingSlaves.Any() ? (int)calculatingSlaves.Sum(s => s.Memory) / 1024 : 0;
76        int freeCalculatingMemory = calculatingSlaves.Any() ? (int)calculatingSlaves.Sum(s => s.FreeMemory) / 1024 : 0;
77
[12428]78        return new DTO.Status {
79          CoreStatus = new DTO.CoreStatus {
80            TotalCores = onlineSlaves.Sum(s => s.Cores ?? 0),
[12435]81            FreeCores = onlineSlaves.Sum(s => s.FreeCores ?? 0), // temporary for old chart data
82            ActiveCores = activeSlaves.Sum(s => s.Cores ?? 0),
83            CalculatingCores = calculatingSlaves.Sum(s => s.Cores ?? 0)
[12428]84          },
85          CpuUtilizationStatus = new DTO.CpuUtilizationStatus {
86            TotalCpuUtilization = onlineSlaves.Any()
87                                  ? Math.Round(onlineSlaves.Average(s => s.CpuUtilization), 2)
88                                  : 0.0,
[12435]89            ActiveCpuUtilization = activeSlaves.Any()
90                                   ? Math.Round(activeSlaves.Average(s => s.CpuUtilization), 2)
91                                   : 0.0,
92            CalculatingCpuUtilization = calculatingSlaves.Any()
93                                        ? Math.Round(calculatingSlaves.Average(s => s.CpuUtilization), 2)
94                                        : 0.0
[12428]95          },
96          MemoryStatus = new DTO.MemoryStatus {
97            TotalMemory = onlineSlaves.Any() ? (int)onlineSlaves.Sum(s => s.Memory) / 1024 : 0,
[12435]98            FreeMemory = onlineSlaves.Any() ? (int)onlineSlaves.Sum(s => s.FreeMemory) / 1024 : 0,
99            ActiveMemory = activeSlaves.Any() ? (int)activeSlaves.Sum(s => s.Memory) / 1024 : 0,
100            UsedMemory = calculatingMemory - freeCalculatingMemory
[12428]101          },
102          TasksStatus = GetTaskStatus(db),
[12435]103          SlavesStatus = onlineSlaves.Select(x => new DTO.SlaveStatus {
[12428]104            Slave = new DTO.Slave {
105              Id = x.ResourceId.ToString(),
106              Name = x.Name
[12435]107            },
108            CpuUtilization = x.CpuUtilization,
109            Cores = x.Cores ?? 0,
110            FreeCores = x.FreeCores ?? 0,
111            Memory = (x.Memory ?? 0) / 1024,
112            FreeMemory = (x.FreeMemory ?? 0) / 1024,
113            IsAllowedToCalculate = x.IsAllowedToCalculate,
114            State = x.SlaveState.ToString()
115          }).OrderBy(x => x.Slave.Name),
[12428]116          Timestamp = JavascriptUtils.ToTimestamp(DateTime.Now)
117        };
118      }
119    }
120
121    public IEnumerable<DTO.Status> GetStatusHistory(DateTime start, DateTime end) {
[12435]122      TimeSpan ts = end - start;
123      int increment = 1;
124      double totalMinutes = ts.TotalMinutes;
125      while (totalMinutes > 5761) {
126        totalMinutes -= 5761;
127        increment += 5;
128      }
[12428]129      using (var db = new HiveDataContext()) {
[12443]130        var statistics = db.Statistics.Where(s => s.Timestamp >= start && s.Timestamp <= end)
131                                      .OrderBy(s => s.Timestamp);
[12435]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;
[12428]140        foreach (var statistic in statistics) {
[12435]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          }
[12428]173        }
174      }
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.