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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Web.Http;
|
---|
26 | using HeuristicLab.Services.Access;
|
---|
27 | using HeuristicLab.Services.Hive;
|
---|
28 | using HeuristicLab.Services.Hive.DataAccess.Interfaces;
|
---|
29 | using DA = HeuristicLab.Services.Hive.DataAccess;
|
---|
30 | using DT = HeuristicLab.Services.WebApp.Statistics.WebApi.DataTransfer;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Services.WebApp.Statistics.WebApi {
|
---|
33 | public class TaskController : ApiController {
|
---|
34 | private IPersistenceManager PersistenceManager {
|
---|
35 | get { return ServiceLocator.Instance.PersistenceManager; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | private IUserManager UserManager {
|
---|
39 | get { return ServiceLocator.Instance.UserManager; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | private IRoleVerifier RoleVerifier {
|
---|
43 | get { return ServiceLocator.Instance.RoleVerifier; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [HttpPost]
|
---|
47 | public DT.TaskPage GetTasksByJobId(Guid id, int page, int size, IEnumerable<string> states) {
|
---|
48 | using (var pm = PersistenceManager) {
|
---|
49 | var dimJobDao = pm.DimJobDao;
|
---|
50 | var dimClientDao = pm.DimClientDao;
|
---|
51 | var factTaskDao = pm.FactTaskDao;
|
---|
52 |
|
---|
53 | DA.DimJob job = pm.UseTransaction(() => dimJobDao.GetById(id));
|
---|
54 | if (job == null) {
|
---|
55 | throw new ArgumentException("invalid job id");
|
---|
56 | }
|
---|
57 | if (job.UserId != UserManager.CurrentUserId) {
|
---|
58 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
59 | }
|
---|
60 |
|
---|
61 | return pm.UseTransaction(() => {
|
---|
62 | var tasks = factTaskDao.GetByJobId(id).Where(x => states.Contains(x.TaskState.ToString()));
|
---|
63 | return new DT.TaskPage {
|
---|
64 | TotalTasks = tasks.Count(),
|
---|
65 | Tasks = (from factTask in tasks
|
---|
66 | join dimJob in dimJobDao.GetAll() on factTask.JobId equals dimJob.JobId
|
---|
67 | join dimClient in dimClientDao.GetAll() on factTask.LastClientId equals
|
---|
68 | dimClient.Id into taskClientJoin
|
---|
69 | from a in taskClientJoin.DefaultIfEmpty()
|
---|
70 | let startTime = factTask.StartTime ?? DateTime.Now
|
---|
71 | let endTime = factTask.EndTime ?? DateTime.Now
|
---|
72 | select new DT.Task {
|
---|
73 | Id = factTask.TaskId,
|
---|
74 | JobId = factTask.JobId,
|
---|
75 | JobName = dimJob.JobName,
|
---|
76 | TotalTime = (long)(endTime - startTime).TotalSeconds,
|
---|
77 | CalculatingTime = factTask.CalculatingTime,
|
---|
78 | WaitingTime = factTask.WaitingTime,
|
---|
79 | TransferTime = factTask.TransferTime,
|
---|
80 | InitialWaitingTime = factTask.InitialWaitingTime,
|
---|
81 | NumCalculationRuns = factTask.NumCalculationRuns,
|
---|
82 | NumRetries = factTask.NumRetries,
|
---|
83 | CoresRequired = factTask.CoresRequired,
|
---|
84 | MemoryRequired = factTask.MemoryRequired,
|
---|
85 | Priority = factTask.Priority,
|
---|
86 | State = factTask.TaskState.ToString(),
|
---|
87 | LastClientId = factTask.LastClientId,
|
---|
88 | LastClientName = a != null ? a.Name : string.Empty,
|
---|
89 | UserId = dimJob.UserId,
|
---|
90 | UserName = dimJob.UserName,
|
---|
91 | StartTime = factTask.StartTime,
|
---|
92 | EndTime = factTask.EndTime,
|
---|
93 | Exception = factTask.Exception
|
---|
94 | })
|
---|
95 | .Skip((page - 1) * size)
|
---|
96 | .Take(size)
|
---|
97 | .ToList()
|
---|
98 | };
|
---|
99 | });
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | [HttpPost]
|
---|
104 | public DT.TaskPage GetTasksByClientId(Guid id, int page, int size, IEnumerable<string> states, Guid userId = default(Guid)) {
|
---|
105 | bool isAdministrator = User.IsInRole(HiveRoles.Administrator);
|
---|
106 | using (var pm = PersistenceManager) {
|
---|
107 | var dimJobDao = pm.DimJobDao;
|
---|
108 | var dimClientDao = pm.DimClientDao;
|
---|
109 | var factTaskDao = pm.FactTaskDao;
|
---|
110 | return pm.UseTransaction(() => {
|
---|
111 | var tasks = factTaskDao.GetByClientId(id).Where(x => states.Contains(x.TaskState.ToString()));
|
---|
112 | if (userId != Guid.Empty) {
|
---|
113 | tasks = tasks.Where(x => x.DimJob.UserId == userId);
|
---|
114 | }
|
---|
115 | return new DT.TaskPage {
|
---|
116 | TotalTasks = tasks.Count(),
|
---|
117 | Tasks = (from factTask in tasks
|
---|
118 | join dimJob in dimJobDao.GetAll() on factTask.JobId equals dimJob.JobId
|
---|
119 | join dimClient in dimClientDao.GetAll() on factTask.LastClientId equals
|
---|
120 | dimClient.Id into taskClientJoin
|
---|
121 | from a in taskClientJoin.DefaultIfEmpty()
|
---|
122 | let startTime = factTask.StartTime ?? DateTime.Now
|
---|
123 | let endTime = factTask.EndTime ?? DateTime.Now
|
---|
124 | select new DT.Task {
|
---|
125 | Id = isAdministrator ? factTask.TaskId : default(Guid),
|
---|
126 | JobId = isAdministrator ? factTask.JobId : default(Guid),
|
---|
127 | JobName = isAdministrator ? dimJob.JobName : string.Empty,
|
---|
128 | TotalTime = (long)(endTime - startTime).TotalSeconds,
|
---|
129 | CalculatingTime = factTask.CalculatingTime,
|
---|
130 | WaitingTime = factTask.WaitingTime,
|
---|
131 | TransferTime = factTask.TransferTime,
|
---|
132 | InitialWaitingTime = factTask.InitialWaitingTime,
|
---|
133 | NumCalculationRuns = factTask.NumCalculationRuns,
|
---|
134 | NumRetries = factTask.NumRetries,
|
---|
135 | CoresRequired = factTask.CoresRequired,
|
---|
136 | MemoryRequired = factTask.MemoryRequired,
|
---|
137 | Priority = factTask.Priority,
|
---|
138 | State = factTask.TaskState.ToString(),
|
---|
139 | LastClientId = factTask.LastClientId,
|
---|
140 | LastClientName = a != null ? a.Name : string.Empty,
|
---|
141 | UserId = isAdministrator ? dimJob.UserId : default(Guid),
|
---|
142 | UserName = isAdministrator ? dimJob.UserName : string.Empty,
|
---|
143 | StartTime = factTask.StartTime,
|
---|
144 | EndTime = factTask.EndTime,
|
---|
145 | Exception = isAdministrator ? factTask.Exception : string.Empty
|
---|
146 | })
|
---|
147 | .OrderByDescending(x => x.EndTime ?? DateTime.MaxValue)
|
---|
148 | .Skip((page - 1) * size)
|
---|
149 | .Take(size)
|
---|
150 | .ToList()
|
---|
151 | };
|
---|
152 | });
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|