Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Hive.Web/Hive-3.3/Status.aspx.cs @ 13508

Last change on this file since 13508 was 9363, checked in by spimming, 12 years ago

#1888:

  • Merged revisions from trunk
File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
26using System.Web.UI;
27using System.Web.UI.WebControls;
28using HeuristicLab.Services.Hive.DataAccess;
29using HeuristicLab.Services.Hive;
30using HeuristicLab.Services.Hive.DataTransfer;
31using System.Text;
32using System.Web.UI.DataVisualization.Charting;
33using DA = HeuristicLab.Services.Hive.DataAccess;
34using DT = HeuristicLab.Services.Hive.DataTransfer;
35
36public partial class Status : System.Web.UI.Page {
37  protected void Page_Load(object sender, EventArgs e) {
38    var dao = ServiceLocator.Instance.HiveDao;
39    var transactionManager = ServiceLocator.Instance.TransactionManager;
40    var resourceName = Request.QueryString["resource"];
41    IEnumerable<Guid> resourceIds = new List<Guid>();
42    IEnumerable<DT.Slave> onlineSlaves = new List<DT.Slave>();
43    int currentlyJobsWaiting = 0;
44    Dictionary<Guid, int> calculatingTasksByUser = new Dictionary<Guid,int>();
45    Dictionary<Guid, int> waitingTasksByUser = new Dictionary<Guid, int>();
46    List<DT.Resource> groups = new List<DT.Resource>();
47
48    transactionManager.UseTransaction(() => {
49       groups = dao.GetResources(x => x.ResourceType == "GROUP").ToList();
50    }, false, false);   
51
52    if (!string.IsNullOrEmpty(resourceName)) {
53        transactionManager.UseTransaction(() =>
54        {
55            var resId = dao.GetResources(x => x.Name == resourceName).Single().Id;
56            resourceIds = dao.GetChildResources(resId).Select(x => x.Id).Union(new List<Guid> { resId });
57            calculatingTasksByUser = dao.GetCalculatingTasksByUserForResources(resourceIds.ToList());
58            waitingTasksByUser = dao.GetWaitingTasksByUserForResources(resourceIds.ToList());
59        }, false, false);       
60    } else {
61        transactionManager.UseTransaction(() =>
62        {
63             resourceIds = dao.GetResources(x => true).Select(y => y.Id);
64             calculatingTasksByUser = dao.GetCalculatingTasksByUser();
65             waitingTasksByUser = dao.GetWaitingTasksByUser();           
66        }, false, false);
67    }   
68
69    transactionManager.UseTransaction(() =>
70    {                     
71        onlineSlaves = dao.GetSlaves(x => (x.SlaveState == DA.SlaveState.Calculating || x.SlaveState == DA.SlaveState.Idle) && resourceIds.Contains(x.ResourceId));
72        currentlyJobsWaiting = dao.GetLightweightTasks(x => x.State == DA.TaskState.Waiting).Count();           
73    }, false, false);
74
75    int overallCurrentlyAvailableCores = onlineSlaves.Where(s => s.Cores.HasValue).Sum(s => s.Cores.Value);
76    int currentlyAvailableCores = onlineSlaves.Where(s => s.Cores.HasValue && s.IsAllowedToCalculate).Sum(s => s.Cores.Value);
77    int currentlyUsedCores = overallCurrentlyAvailableCores - onlineSlaves.Where(s => s.FreeCores.HasValue).Sum(s => s.FreeCores.Value);
78   
79    this.overallAvailableCoresLabel.Text = overallCurrentlyAvailableCores.ToString();
80    this.availableCoresLabel.Text = currentlyAvailableCores.ToString();
81    this.usedCoresLabel.Text = currentlyUsedCores.ToString();
82    this.waitingJobsLabel.Text = currentlyJobsWaiting.ToString();
83
84    slavesLabel.Text = string.Join(", ", onlineSlaves.Select(x => string.Format("<a href=\"?resource={0}\">{0}</a> ({1} %)", x.Name, Math.Round(x.CpuUtilization, 2))));
85    groupsLabel.Text = "<a href=\"Status.aspx\">All</a>, ";
86    groupsLabel.Text += string.Join(", ", groups.Select(x => string.Format("<a href=\"?resource={0}\">{0}</a>", x.Name)));
87
88    overallCpuUtilizationLabel.Text = (onlineSlaves.Count() > 0 ? Math.Round(onlineSlaves.Average(s => s.CpuUtilization), 2).ToString() : "0.0") + " %";
89    cpuUtilizationLabel.Text = (onlineSlaves.Count() > 0 && onlineSlaves.Where(x => x.IsAllowedToCalculate).Count() > 0 ? Math.Round(onlineSlaves.Where(x => x.IsAllowedToCalculate).Average(s => s.CpuUtilization), 2).ToString() : "0.0") + " %";
90
91    DT.Statistics[] stats = new DT.Statistics[0];   
92    transactionManager.UseTransaction(() =>
93    {   
94        if (daysDropDownList.SelectedValue == "All") {
95          stats = dao.GetStatistics(x => true).OrderBy(x => x.TimeStamp).ToArray();
96        } else {
97          stats = dao.GetStatistics(x => x.Timestamp >= DateTime.Now.Subtract(TimeSpan.FromDays(int.Parse(daysDropDownList.SelectedValue)))).OrderBy(x => x.TimeStamp).ToArray();
98        }
99    }, false, false);
100   
101    for (int i = 0; i < stats.Length; i++) {
102      var s = stats[i];
103      var slaveStats = s.SlaveStatistics.Where(x => resourceIds.Contains(x.SlaveId));
104
105      var averageCpuUtilization = slaveStats.Count() > 0 ? slaveStats.Average(x => x.CpuUtilization) : 0.0;
106      cpuUtilizationChart.Series[0].Points.Add(new DataPoint(s.TimeStamp.ToOADate(), averageCpuUtilization));
107
108      var cores = slaveStats.Sum(x => x.Cores);
109
110      var usedCores = cores - slaveStats.Sum(x => x.FreeCores);
111      coresChart.Series[0].Points.AddXY(s.TimeStamp.ToOADate(), cores);
112      coresChart.Series[1].Points.AddXY(s.TimeStamp.ToOADate(), usedCores);
113
114      var memory = slaveStats.Sum(x => x.Memory);
115      var usedMemory = memory - slaveStats.Sum(x => x.FreeMemory);
116      memoryChart.Series[0].Points.AddXY(s.TimeStamp.ToOADate(), memory / 1024.0);
117      memoryChart.Series[1].Points.AddXY(s.TimeStamp.ToOADate(), usedMemory / 1024.0);
118    }
119
120    GenerateTasksByUserTable(waitingTasksByUser, waitingTasksByUserTable); 
121    GenerateTasksByUserTable(calculatingTasksByUser, calculatingTasksByUserTable); 
122  }
123
124  private void GenerateTasksByUserTable(Dictionary<Guid, int> tasksByUser, Table table) {
125    foreach (var kvp in tasksByUser) {
126      TableRow curRow = new TableRow();
127      TableCell cellUser = new TableCell();
128      cellUser.Text = ServiceLocator.Instance.UserManager.GetUserById(kvp.Key).UserName;
129      TableCell cellCnt = new TableCell();
130      cellCnt.Text = kvp.Value.ToString();
131
132      curRow.Cells.Add(cellUser);
133      curRow.Cells.Add(cellCnt);
134      table.Rows.Add(curRow);
135    }
136    if (tasksByUser.Count() > 0) {
137      TableRow sumRow = new TableRow();       
138      TableCell sumCell = new TableCell();
139      sumCell.BorderWidth = Unit.Pixel(3);
140      sumCell.Text = tasksByUser.Sum(x => x.Value).ToString();
141      sumRow.Cells.Add(new TableCell());
142      sumRow.Cells.Add(sumCell);
143      table.Rows.Add(sumRow);
144    }
145  } 
146}
Note: See TracBrowser for help on using the repository browser.