source: branches/HiveStatistics/sources/HeuristicLab.Services.WebApp.Statistics/3.3/WebApi/TaskController.cs @ 12768

Last change on this file since 12768 was 12768, checked in by dglaser, 6 years ago

#2388:

HeuristicLab.Services.Hive.DataAccess-3.3:

  • Removed old statistics tables
  • Updated SQL Scripts

HeuristicLab.Services.WebApp-3.3:
HeuristicLab.Services.WebApp.Status-3.3:
HeuristicLab.Services.WebApp.Statistics-3.3:

  • Minor changes
File size: 8.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.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      using (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
108    [HttpPost]
109    public DT.TaskPage GetTasksByClientId(Guid id, int page, int size, IEnumerable<string> states, Guid userId = default(Guid)) {
110      bool isAdministrator = User.IsInRole(HiveRoles.Administrator);
111      using (var pm = PersistenceManager) {
112        var dimJobDao = pm.DimJobDao;
113        var dimClientDao = pm.DimClientDao;
114        var factTaskDao = pm.FactTaskDao;
115        return pm.UseTransaction(() => {
116          var tasks = factTaskDao.GetByClientId(id).Where(x => states.Contains(x.TaskState.ToString()));
117          if (userId != Guid.Empty) {
118            tasks = tasks.Where(x => x.DimJob.UserId == userId);
119          }
120          return new DT.TaskPage {
121            TotalTasks = tasks.Count(),
122            Tasks = (from factTask in tasks
123                     join dimJob in dimJobDao.GetAll() on factTask.JobId equals dimJob.JobId
124                     join dimClient in dimClientDao.GetAll() on factTask.LastClientId equals
125                       dimClient.Id into taskClientJoin
126                     from a in taskClientJoin.DefaultIfEmpty()
127                     let startTime = factTask.StartTime ?? DateTime.Now
128                     let endTime = factTask.EndTime ?? DateTime.Now
129                     select new DT.Task {
130                       Id = isAdministrator ? factTask.TaskId : default(Guid),
131                       JobId = isAdministrator ? factTask.JobId : default(Guid),
132                       JobName = isAdministrator ? dimJob.JobName : string.Empty,
133                       TotalTime = (long)(endTime - startTime).TotalSeconds,
134                       CalculatingTime = factTask.CalculatingTime,
135                       WaitingTime = factTask.WaitingTime,
136                       TransferTime = factTask.TransferTime,
137                       InitialWaitingTime = factTask.InitialWaitingTime,
138                       NumCalculationRuns = factTask.NumCalculationRuns,
139                       NumRetries = factTask.NumRetries,
140                       CoresRequired = factTask.CoresRequired,
141                       MemoryRequired = factTask.MemoryRequired,
142                       Priority = factTask.Priority,
143                       State = factTask.TaskState.ToString(),
144                       LastClientId = factTask.LastClientId,
145                       LastClientName = a != null ? a.Name : string.Empty,
146                       UserId = isAdministrator ? dimJob.UserId : default(Guid),
147                       UserName = isAdministrator ? dimJob.UserName : string.Empty,
148                       StartTime = factTask.StartTime,
149                       EndTime = factTask.EndTime,
150                       Exception = isAdministrator ? factTask.Exception : string.Empty
151                     })
152                  .OrderByDescending(x => x.EndTime ?? DateTime.MaxValue)
153                  .Skip((page - 1) * size)
154                  .Take(size)
155                  .ToList()
156          };
157        });
158      }
159    }
160
161    public HttpResponseMessage GetTaskDataById(Guid id) {
162      using (var pm = PersistenceManager) {
163        var taskDataDao = pm.TaskDataDao;
164        return pm.UseTransaction(() => {
165          var taskData = taskDataDao.GetById(id);
166          if (taskData == null)
167            return new HttpResponseMessage(HttpStatusCode.NotFound);
168          HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
169          var stream = new MemoryStream(taskData.Data);
170          result.Content = new StreamContent(stream);
171          result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
172          result.Content.Headers.ContentDisposition =
173            new ContentDispositionHeaderValue("attachment") {
174              FileName = string.Format("{0}.hl", id)
175            };
176          return result;
177        });
178      }
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.