source: branches/HiveStatistics/sources/HeuristicLab.Services.WebApp.Maintenance/3.3/WebApi/FactTaskController.cs @ 12858

Last change on this file since 12858 was 12858, checked in by ascheibe, 7 years ago

#2388

  • prevent disposing of hive data context
  • more cleanups
File size: 4.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.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      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    [HttpPost]
61    public void AggregateJob(Guid id) {
62      var pm = PersistenceManager;
63      AggregateJob(pm, id);
64    }
65
66    [HttpPost]
67    public void AggregateAllJobs(DateTime start, DateTime end) {
68      var pm = PersistenceManager;
69      var dimJobDao = pm.DimJobDao;
70      var jobIds = pm.UseTransaction(() => dimJobDao.GetAll()
71        .Where(x => x.DateCreated >= start && x.DateCreated <= end)
72        .Select(x => x.JobId)
73        .ToList()
74      );
75      foreach (var id in jobIds) {
76        AggregateJob(pm, id);
77      }
78    }
79
80    private void AggregateJob(IPersistenceManager pm, Guid id) {
81      var jobDao = pm.JobDao;
82      var factTaskDao = pm.FactTaskDao;
83      pm.UseTransaction(() => {
84        if (jobDao.Exists(id)) return;
85        var jobTimeData = factTaskDao.GetByJobId(id).GroupBy(x => x.JobId).Select(x => new {
86          CalculatingTime = x.Sum(y => y.CalculatingTime),
87          WaitingTime = x.Sum(y => y.WaitingTime),
88          TransferTime = x.Sum(y => y.TransferTime),
89          InitialWaitingTime = x.Sum(y => y.InitialWaitingTime),
90          StartTime = x.Min(y => y.StartTime),
91          EndTime = x.Max(y => y.EndTime)
92        }).SingleOrDefault();
93        if (jobTimeData != null) {
94          factTaskDao.DeleteByJobId(id);
95          factTaskDao.Save(new FactTask {
96            TaskId = Guid.NewGuid(),
97            JobId = id,
98            CalculatingTime = jobTimeData.CalculatingTime,
99            WaitingTime = jobTimeData.WaitingTime,
100            TransferTime = jobTimeData.TransferTime,
101            InitialWaitingTime = jobTimeData.InitialWaitingTime,
102            StartTime = jobTimeData.StartTime,
103            EndTime = jobTimeData.EndTime,
104            NumCalculationRuns = 0,
105            NumRetries = 0,
106            CoresRequired = 0,
107            MemoryRequired = 0,
108            Priority = 0,
109            TaskState = TaskState.Finished
110          });
111        }
112        pm.SubmitChanges();
113      });
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.