Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12584 was 12584, checked in by dglaser, 9 years ago

#2388: Changed all files to connect to localhost / sqlexpress

HeuristicLab.Services.Hive-3.3:

  • Added Converter.cs and NewHiveService.cs, both will be integrated into existing HiveService.cs and Convert.cs when all methods are successfully implemented

HeuristicLab.Services.Hive.Web.Hive-3.3:

  • Added publish profiles

HeuristicLab.Services.WebApp.Statistics-3.3:

  • Added functionality to download TaskData as .hl file
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  public class TaskController : ApiController {
38    private IPersistenceManager PersistenceManager {
39      get { return ServiceLocator.Instance.PersistenceManager; }
40    }
41
42    private IUserManager UserManager {
43      get { return ServiceLocator.Instance.UserManager; }
44    }
45
46    private IRoleVerifier RoleVerifier {
47      get { return ServiceLocator.Instance.RoleVerifier; }
48    }
49
50    [HttpPost]
51    public DT.TaskPage GetTasksByJobId(Guid id, int page, int size, IEnumerable<string> states) {
52      using (var pm = PersistenceManager) {
53        var dimJobDao = pm.DimJobDao;
54        var dimClientDao = pm.DimClientDao;
55        var factTaskDao = pm.FactTaskDao;
56
57        DA.DimJob job = pm.UseTransaction(() => dimJobDao.GetById(id));
58        if (job == null) {
59          throw new ArgumentException("invalid job id");
60        }
61        if (job.UserId != UserManager.CurrentUserId) {
62          RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
63        }
64
65        return pm.UseTransaction(() => {
66          var tasks = factTaskDao.GetByJobId(id).Where(x => states.Contains(x.TaskState.ToString()));
67          return new DT.TaskPage {
68            TotalTasks = tasks.Count(),
69            Tasks = (from factTask in tasks
70                     join dimJob in dimJobDao.GetAll() on factTask.JobId equals dimJob.JobId
71                     join dimClient in dimClientDao.GetAll() on factTask.LastClientId equals
72                       dimClient.Id into taskClientJoin
73                     from a in taskClientJoin.DefaultIfEmpty()
74                     let startTime = factTask.StartTime ?? DateTime.Now
75                     let endTime = factTask.EndTime ?? DateTime.Now
76                     select new DT.Task {
77                       Id = factTask.TaskId,
78                       JobId = factTask.JobId,
79                       JobName = dimJob.JobName,
80                       TotalTime = (long)(endTime - startTime).TotalSeconds,
81                       CalculatingTime = factTask.CalculatingTime,
82                       WaitingTime = factTask.WaitingTime,
83                       TransferTime = factTask.TransferTime,
84                       InitialWaitingTime = factTask.InitialWaitingTime,
85                       NumCalculationRuns = factTask.NumCalculationRuns,
86                       NumRetries = factTask.NumRetries,
87                       CoresRequired = factTask.CoresRequired,
88                       MemoryRequired = factTask.MemoryRequired,
89                       Priority = factTask.Priority,
90                       State = factTask.TaskState.ToString(),
91                       LastClientId = factTask.LastClientId,
92                       LastClientName = a != null ? a.Name : string.Empty,
93                       UserId = dimJob.UserId,
94                       UserName = dimJob.UserName,
95                       StartTime = factTask.StartTime,
96                       EndTime = factTask.EndTime,
97                       Exception = factTask.Exception
98                     })
99              .Skip((page - 1) * size)
100              .Take(size)
101              .ToList()
102          };
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      using (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
160    public HttpResponseMessage GetTaskDataById(Guid id) {
161      using (var pm = PersistenceManager) {
162        var taskDataDao = pm.TaskDataDao;
163        return pm.UseTransaction(() => {
164          var taskData = taskDataDao.GetById(id);
165          if (taskData == null)
166            return new HttpResponseMessage(HttpStatusCode.NotFound);
167          HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
168          var stream = new MemoryStream(taskData.Data);
169          result.Content = new StreamContent(stream);
170          result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
171          result.Content.Headers.ContentDisposition =
172            new ContentDispositionHeaderValue("attachment") {
173              FileName = string.Format("{0}.hl", id)
174            };
175          return result;
176        });
177      }
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.