Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.WebApp.Maintenance/3.3/WebApi/CleanupController.cs @ 12761

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

#2429: Worked on the maintenance WebApp plugin:

  • Space Usage Page: Displays the number of rows and allocated disk space for every database table
  • Plugin Page: Shows unused plugins and provides functionality to delete all and specific plugins
  • FactTask Page: Allows to aggregate all Job Tasks to a single task for a given job or jobs within an selected time period
  • FactClientInfo Page: Allows to aggregate consecutive FactClientInfo entries with the same state and isallowedtocalculate flag
File size: 7.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web.Http;
5using HeuristicLab.Services.Hive;
6using HeuristicLab.Services.Hive.DataAccess;
7using HeuristicLab.Services.Hive.DataAccess.Interfaces;
8using DT = HeuristicLab.Services.WebApp.Maintenance.WebApi.DataTransfer;
9
10namespace HeuristicLab.Services.WebApp.Maintenance.WebApi {
11  public class CleanupController : ApiController {
12
13    private IPersistenceManager PersistenceManager {
14      get { return ServiceLocator.Instance.PersistenceManager; }
15    }
16
17    [HttpGet, HttpPost]
18    public bool CleanupJob(Guid jobId) {
19      using (var pm = PersistenceManager) {
20        var jobDao = pm.JobDao;
21        var factTaskDao = pm.FactTaskDao;
22        return pm.UseTransaction(() => {
23          if (jobDao.Exists(jobId)) return false;
24          var jobTimeData = factTaskDao.GetByJobId(jobId).GroupBy(x => x.JobId).Select(x => new {
25            CalculatingTime = x.Sum(y => y.CalculatingTime),
26            WaitingTime = x.Sum(y => y.WaitingTime),
27            TransferTime = x.Sum(y => y.TransferTime),
28            InitialWaitingTime = x.Sum(y => y.InitialWaitingTime),
29            StartTime = x.Min(y => y.StartTime),
30            EndTime = x.Max(y => y.EndTime)
31          }).SingleOrDefault();
32          factTaskDao.DeleteByJobId(jobId);
33          factTaskDao.Save(new FactTask {
34            TaskId = Guid.NewGuid(),
35            JobId = jobId,
36            CalculatingTime = jobTimeData.CalculatingTime,
37            WaitingTime = jobTimeData.WaitingTime,
38            TransferTime = jobTimeData.TransferTime,
39            InitialWaitingTime = jobTimeData.InitialWaitingTime,
40            StartTime = jobTimeData.StartTime,
41            EndTime = jobTimeData.EndTime,
42            NumCalculationRuns = 0,
43            NumRetries = 0,
44            CoresRequired = 0,
45            MemoryRequired = 0,
46            Priority = 0,
47            TaskState = TaskState.Finished
48          });
49          pm.SubmitChanges();
50          return true;
51        });
52      }
53    }
54
55    [HttpGet, HttpPost]
56    public void AggregateClient(Guid clientId, int entries) {
57      using (var pm = PersistenceManager) {
58        var factClientInfoDao = pm.FactClientInfoDao;
59        var clientInfos = pm.UseTransaction(() => factClientInfoDao.GetByClientId(clientId).OrderBy(x => x.Time).ToList());
60        FactClientInfo lastClientInfo = null, sumClientInfo = new FactClientInfo();
61        int counter = 0;
62        var aggregatedClientInfos = new List<FactClientInfo>();
63        foreach (var clientInfo in clientInfos) {
64          if (lastClientInfo != null) {
65            if (lastClientInfo.SlaveState != clientInfo.SlaveState ||
66                lastClientInfo.IsAllowedToCalculate != clientInfo.IsAllowedToCalculate) {
67              // update into 'lastClientInfo'
68              if (aggregatedClientInfos.Any()) {
69                counter = aggregatedClientInfos.Count();
70                pm.UseTransaction(() => {
71                  factClientInfoDao.Delete(aggregatedClientInfos);
72                  lastClientInfo.NumUsedCores = sumClientInfo.NumUsedCores / counter;
73                  lastClientInfo.NumTotalCores = sumClientInfo.NumTotalCores / counter;
74                  lastClientInfo.UsedMemory = sumClientInfo.UsedMemory / counter;
75                  lastClientInfo.TotalMemory = sumClientInfo.TotalMemory / counter;
76                  lastClientInfo.CpuUtilization = sumClientInfo.CpuUtilization / counter;
77                  lastClientInfo.IdleTime = sumClientInfo.IdleTime;
78                  lastClientInfo.OfflineTime = sumClientInfo.OfflineTime;
79                  lastClientInfo.UnavailableTime = sumClientInfo.UnavailableTime;
80                  pm.SubmitChanges();
81                });
82              }
83              aggregatedClientInfos.Clear();
84              counter = 0;
85              sumClientInfo = new FactClientInfo();
86            } else {
87              // sum up
88              sumClientInfo.NumUsedCores += clientInfo.NumUsedCores;
89              sumClientInfo.NumTotalCores += clientInfo.NumTotalCores;
90              sumClientInfo.UsedMemory += clientInfo.UsedMemory;
91              sumClientInfo.TotalMemory += clientInfo.TotalMemory;
92              sumClientInfo.CpuUtilization += clientInfo.CpuUtilization;
93              sumClientInfo.IdleTime += clientInfo.IdleTime;
94              sumClientInfo.OfflineTime += clientInfo.OfflineTime;
95              sumClientInfo.UnavailableTime += clientInfo.UnavailableTime;
96              counter++;
97              if (counter >= entries) {
98                pm.UseTransaction(() => {
99                  factClientInfoDao.Delete(aggregatedClientInfos);
100                  clientInfo.NumUsedCores = sumClientInfo.NumUsedCores / counter;
101                  clientInfo.NumTotalCores = sumClientInfo.NumTotalCores / counter;
102                  clientInfo.UsedMemory = sumClientInfo.UsedMemory / counter;
103                  clientInfo.TotalMemory = sumClientInfo.TotalMemory / counter;
104                  clientInfo.CpuUtilization = sumClientInfo.CpuUtilization / counter;
105                  clientInfo.IdleTime = sumClientInfo.IdleTime;
106                  clientInfo.OfflineTime = sumClientInfo.OfflineTime;
107                  clientInfo.UnavailableTime = sumClientInfo.UnavailableTime;
108                  pm.SubmitChanges();
109                });
110                aggregatedClientInfos.Clear();
111                counter = 0;
112                sumClientInfo = new FactClientInfo();
113              } else {
114                aggregatedClientInfos.Add(clientInfo);
115              }
116            }
117          }
118          lastClientInfo = clientInfo;
119        }
120      }
121    }
122
123    public IEnumerable<DT.Plugin> GetUnusedPlugins() {
124      using (var pm = PersistenceManager) {
125        var pluginDao = pm.PluginDao;
126        var requiredPluginDao = pm.RequiredPluginDao;
127        var taskDao = pm.TaskDao;
128        return pm.UseTransaction(() => {
129          var taskIds = taskDao.GetAll().Select(x => x.TaskId);
130          var usedPluginIds = requiredPluginDao.GetAll()
131            .Where(x => taskIds.Contains(x.TaskId))
132            .Select(x => x.PluginId)
133            .Distinct();
134          return pluginDao.GetAll()
135            .Where(x => !usedPluginIds.Any(y => y == x.PluginId))
136            .Select(x => new DT.Plugin {
137              Name = x.Name,
138              Version = x.Version,
139              DateCreated = x.DateCreated
140            })
141            .ToList();
142        });
143      }
144    }
145
146    public void DeletePlugin(Guid id) {
147      using (var pm = PersistenceManager) {
148        var pluginDao = pm.PluginDao;
149        pm.UseTransaction(() => {
150          pluginDao.Delete(id);
151          pm.SubmitChanges();
152        });
153      }
154    }
155
156    public void DeleteUnusedPlugins() {
157      using (var pm = PersistenceManager) {
158        var pluginDao = pm.PluginDao;
159        pm.UseTransaction(() => {
160          pluginDao.DeleteUnusedPlugins();
161          pm.SubmitChanges();
162        });
163      }
164    }
165
166  }
167}
Note: See TracBrowser for help on using the repository browser.