Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.WebApp.Maintenance/3.3/WebApi/FactTaskController.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: 4.3 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.Linq;
24using System.Web.Http;
25using HeuristicLab.Services.Hive;
26using HeuristicLab.Services.Hive.DataAccess;
27using HeuristicLab.Services.Hive.DataAccess.Interfaces;
28using DT = HeuristicLab.Services.WebApp.Maintenance.WebApi.DataTransfer;
29
30namespace HeuristicLab.Services.WebApp.Maintenance.WebApi {
31  public class FactTaskController : ApiController {
32    private IPersistenceManager PersistenceManager {
33      get { return ServiceLocator.Instance.PersistenceManager; }
34    }
35
36    public DT.JobPage GetJobs(DateTime start, DateTime end, int page, int size) {
37      using (var pm = PersistenceManager) {
38        var dimJobDao = pm.DimJobDao;
39        var factTaskDao = pm.FactTaskDao;
40        return pm.UseTransaction(() => {
41          var query = dimJobDao.GetAll().Where(x => x.DateCreated >= start && x.DateCreated <= end);
42          return new DT.JobPage {
43            TotalJobs = query.Count(),
44            Jobs = query.OrderBy(x => x.DateCreated)
45              .Skip((page - 1) * size)
46              .Take(size)
47              .Select(x => new DT.Job {
48                Id = x.JobId,
49                Name = x.JobName,
50                Username = x.UserName,
51                DateCreated = x.DateCreated,
52                Tasks = x.TotalTasks,
53                Aggregated = !(factTaskDao.GetAll().Count(y => y.JobId == x.JobId) > 1)
54              })
55              .ToList()
56          };
57        });
58      }
59    }
60
61    [HttpPost]
62    public void AggregateJob(Guid id) {
63      using (var pm = PersistenceManager) {
64        AggregateJob(pm, id);
65      }
66    }
67
68    [HttpPost]
69    public void AggregateAllJobs(DateTime start, DateTime end) {
70      using (var pm = PersistenceManager) {
71        var dimJobDao = pm.DimJobDao;
72        var jobIds = pm.UseTransaction(() => dimJobDao.GetAll()
73          .Where(x => x.DateCreated >= start && x.DateCreated <= end)
74          .Select(x => x.JobId)
75          .ToList()
76        );
77        foreach (var id in jobIds) {
78          AggregateJob(pm, id);
79        }
80      }
81    }
82
83    private void AggregateJob(IPersistenceManager pm, Guid id) {
84      var jobDao = pm.JobDao;
85      var factTaskDao = pm.FactTaskDao;
86      pm.UseTransaction(() => {
87        if (jobDao.Exists(id)) return;
88        var jobTimeData = factTaskDao.GetByJobId(id).GroupBy(x => x.JobId).Select(x => new {
89          CalculatingTime = x.Sum(y => y.CalculatingTime),
90          WaitingTime = x.Sum(y => y.WaitingTime),
91          TransferTime = x.Sum(y => y.TransferTime),
92          InitialWaitingTime = x.Sum(y => y.InitialWaitingTime),
93          StartTime = x.Min(y => y.StartTime),
94          EndTime = x.Max(y => y.EndTime)
95        }).SingleOrDefault();
96        if (jobTimeData != null) {
97          factTaskDao.DeleteByJobId(id);
98          factTaskDao.Save(new FactTask {
99            TaskId = Guid.NewGuid(),
100            JobId = id,
101            CalculatingTime = jobTimeData.CalculatingTime,
102            WaitingTime = jobTimeData.WaitingTime,
103            TransferTime = jobTimeData.TransferTime,
104            InitialWaitingTime = jobTimeData.InitialWaitingTime,
105            StartTime = jobTimeData.StartTime,
106            EndTime = jobTimeData.EndTime,
107            NumCalculationRuns = 0,
108            NumRetries = 0,
109            CoresRequired = 0,
110            MemoryRequired = 0,
111            Priority = 0,
112            TaskState = TaskState.Finished
113          });
114        }
115        pm.SubmitChanges();
116      });
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.