Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Services.WebApp.Statistics/3.3/WebApi/TaskController.cs @ 16605

Last change on this file since 16605 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.IO;
25using System.Linq;
26using System.Net;
27using System.Net.Http;
28using System.Net.Http.Headers;
29using System.Web.Http;
30using HeuristicLab.Services.Access;
31using HeuristicLab.Services.Hive;
32using HeuristicLab.Services.Hive.DataAccess.Interfaces;
33using DA = HeuristicLab.Services.Hive.DataAccess;
34using DT = HeuristicLab.Services.WebApp.Statistics.WebApi.DataTransfer;
35
36namespace HeuristicLab.Services.WebApp.Statistics.WebApi {
37  [Authorize]
38  public class TaskController : ApiController {
39    private IPersistenceManager PersistenceManager {
40      get { return ServiceLocator.Instance.PersistenceManager; }
41    }
42
43    private IUserManager UserManager {
44      get { return ServiceLocator.Instance.UserManager; }
45    }
46
47    private IRoleVerifier RoleVerifier {
48      get { return ServiceLocator.Instance.RoleVerifier; }
49    }
50
51    [HttpPost]
52    public DT.TaskPage GetTasksByJobId(Guid id, int page, int size, IEnumerable<string> states) {
53      var pm = PersistenceManager;
54      var dimJobDao = pm.DimJobDao;
55      var dimClientDao = pm.DimClientDao;
56      var factTaskDao = pm.FactTaskDao;
57
58      DA.DimJob job = pm.UseTransaction(() => dimJobDao.GetById(id));
59      if (job == null) {
60        throw new ArgumentException("invalid job id");
61      }
62      if (job.UserId != UserManager.CurrentUserId) {
63        RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
64      }
65
66      return pm.UseTransaction(() => {
67        var tasks = factTaskDao.GetByJobId(id).Where(x => states.Contains(x.TaskState.ToString()));
68        return new DT.TaskPage {
69          TotalTasks = tasks.Count(),
70          Tasks = (from factTask in tasks
71                   join dimJob in dimJobDao.GetAll() on factTask.JobId equals dimJob.JobId
72                   join dimClient in dimClientDao.GetAll() on factTask.LastClientId equals
73                     dimClient.Id into taskClientJoin
74                   from a in taskClientJoin.DefaultIfEmpty()
75                   let startTime = factTask.StartTime ?? DateTime.Now
76                   let endTime = factTask.EndTime ?? DateTime.Now
77                   select new DT.Task {
78                     Id = factTask.TaskId,
79                     JobId = factTask.JobId,
80                     JobName = dimJob.JobName,
81                     TotalTime = (long)(endTime - startTime).TotalSeconds,
82                     CalculatingTime = factTask.CalculatingTime,
83                     WaitingTime = factTask.WaitingTime,
84                     TransferTime = factTask.TransferTime,
85                     InitialWaitingTime = factTask.InitialWaitingTime,
86                     NumCalculationRuns = factTask.NumCalculationRuns,
87                     NumRetries = factTask.NumRetries,
88                     CoresRequired = factTask.CoresRequired,
89                     MemoryRequired = factTask.MemoryRequired,
90                     Priority = factTask.Priority,
91                     State = factTask.TaskState.ToString(),
92                     LastClientId = factTask.LastClientId,
93                     LastClientName = a != null ? a.Name : string.Empty,
94                     UserId = dimJob.UserId,
95                     UserName = dimJob.UserName,
96                     StartTime = factTask.StartTime,
97                     EndTime = factTask.EndTime,
98                     Exception = factTask.Exception
99                   })
100            .Skip((page - 1) * size)
101            .Take(size)
102            .ToList()
103        };
104      });
105    }
106
107    [HttpPost]
108    public DT.TaskPage GetTasksByClientId(Guid id, int page, int size, IEnumerable<string> states, Guid userId = default(Guid)) {
109      bool isAdministrator = User.IsInRole(HiveRoles.Administrator);
110      var pm = PersistenceManager;
111      var dimJobDao = pm.DimJobDao;
112      var dimClientDao = pm.DimClientDao;
113      var factTaskDao = pm.FactTaskDao;
114      return pm.UseTransaction(() => {
115        var tasks = factTaskDao.GetByClientId(id).Where(x => states.Contains(x.TaskState.ToString()));
116        if (userId != Guid.Empty) {
117          tasks = tasks.Where(x => x.DimJob.UserId == userId);
118        }
119        return new DT.TaskPage {
120          TotalTasks = tasks.Count(),
121          Tasks = (from factTask in tasks
122                   join dimJob in dimJobDao.GetAll() on factTask.JobId equals dimJob.JobId
123                   join dimClient in dimClientDao.GetAll() on factTask.LastClientId equals
124                     dimClient.Id into taskClientJoin
125                   from a in taskClientJoin.DefaultIfEmpty()
126                   let startTime = factTask.StartTime ?? DateTime.Now
127                   let endTime = factTask.EndTime ?? DateTime.Now
128                   select new DT.Task {
129                     Id = isAdministrator ? factTask.TaskId : default(Guid),
130                     JobId = isAdministrator ? factTask.JobId : default(Guid),
131                     JobName = isAdministrator ? dimJob.JobName : string.Empty,
132                     TotalTime = (long)(endTime - startTime).TotalSeconds,
133                     CalculatingTime = factTask.CalculatingTime,
134                     WaitingTime = factTask.WaitingTime,
135                     TransferTime = factTask.TransferTime,
136                     InitialWaitingTime = factTask.InitialWaitingTime,
137                     NumCalculationRuns = factTask.NumCalculationRuns,
138                     NumRetries = factTask.NumRetries,
139                     CoresRequired = factTask.CoresRequired,
140                     MemoryRequired = factTask.MemoryRequired,
141                     Priority = factTask.Priority,
142                     State = factTask.TaskState.ToString(),
143                     LastClientId = factTask.LastClientId,
144                     LastClientName = a != null ? a.Name : string.Empty,
145                     UserId = isAdministrator ? dimJob.UserId : default(Guid),
146                     UserName = isAdministrator ? dimJob.UserName : string.Empty,
147                     StartTime = factTask.StartTime,
148                     EndTime = factTask.EndTime,
149                     Exception = isAdministrator ? factTask.Exception : string.Empty
150                   })
151                .OrderByDescending(x => x.EndTime ?? DateTime.MaxValue)
152                .Skip((page - 1) * size)
153                .Take(size)
154                .ToList()
155        };
156      });
157    }
158
159    public HttpResponseMessage GetTaskDataById(Guid id) {
160      var pm = PersistenceManager;
161      var taskDataDao = pm.TaskDataDao;
162      return pm.UseTransaction(() => {
163        var taskData = taskDataDao.GetById(id);
164        if (taskData == null)
165          return new HttpResponseMessage(HttpStatusCode.NotFound);
166        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
167        var stream = new MemoryStream(taskData.Data);
168        result.Content = new StreamContent(stream);
169        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
170        result.Content.Headers.ContentDisposition =
171          new ContentDispositionHeaderValue("attachment") {
172            FileName = string.Format("{0}.hl", id)
173          };
174        return result;
175      });
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.