1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
34 | [Authorize]
|
---|
35 | public class JobController : ApiController {
|
---|
36 | private IPersistenceManager PersistenceManager {
|
---|
37 | get { return ServiceLocator.Instance.PersistenceManager; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | private IUserManager UserManager {
|
---|
41 | get { return ServiceLocator.Instance.UserManager; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | private IRoleVerifier RoleVerifier {
|
---|
45 | get { return ServiceLocator.Instance.RoleVerifier; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public DT.JobDetails GetJobDetails(Guid id) {
|
---|
49 | var pm = PersistenceManager;
|
---|
50 | var dimJobDao = pm.DimJobDao;
|
---|
51 | var factTaskDao = pm.FactTaskDao;
|
---|
52 | return pm.UseTransaction(() => {
|
---|
53 | var job = dimJobDao.GetById(id);
|
---|
54 | if (job != null) {
|
---|
55 | var timeData = factTaskDao.GetByJobId(id).GroupBy(x => x.JobId).Select(x => new {
|
---|
56 | AvgTransferringTime = (long)x.Average(y => y.TransferTime),
|
---|
57 | MinCalculatingTime = (long)x.Min(y => y.CalculatingTime),
|
---|
58 | MaxCalculatingTime = (long)x.Max(y => y.CalculatingTime),
|
---|
59 | AvgCalculatingTime = (long)x.Average(y => y.CalculatingTime),
|
---|
60 | TotalCalculatingTime = (long)x.Sum(y => y.CalculatingTime),
|
---|
61 | TotalWaitingTime = (long)x.Sum(y => y.WaitingTime)
|
---|
62 | }).FirstOrDefault();
|
---|
63 | DateTime endTime = job.DateCompleted ?? DateTime.Now;
|
---|
64 | long totalTime = (long)(endTime - job.DateCreated).TotalSeconds;
|
---|
65 | return new DT.JobDetails {
|
---|
66 | Id = job.JobId,
|
---|
67 | Name = job.JobName,
|
---|
68 | UserId = job.UserId,
|
---|
69 | UserName = job.UserName,
|
---|
70 | DateCreated = job.DateCreated,
|
---|
71 | DateCompleted = job.DateCompleted,
|
---|
72 | TotalTasks = job.TotalTasks,
|
---|
73 | CompletedTasks = job.CompletedTasks,
|
---|
74 | AvgTransferringTime = timeData != null ? timeData.AvgTransferringTime : 0,
|
---|
75 | AvgCalculatingTime = timeData != null ? timeData.AvgCalculatingTime : 0,
|
---|
76 | MinCalculatingTime = timeData != null ? timeData.MinCalculatingTime : 0,
|
---|
77 | MaxCalculatingTime = timeData != null ? timeData.MaxCalculatingTime : 0,
|
---|
78 | TotalCalculatingTime = timeData != null ? timeData.TotalCalculatingTime : 0,
|
---|
79 | TotalWaitingTime = timeData != null ? timeData.TotalWaitingTime : 0,
|
---|
80 | TotalTime = totalTime,
|
---|
81 | TasksStates = factTaskDao.GetByJobId(id)
|
---|
82 | .GroupBy(x => x.TaskState)
|
---|
83 | .Select(x => new DT.TaskStateCount {
|
---|
84 | State = x.Key.ToString(),
|
---|
85 | Count = x.Count()
|
---|
86 | }).ToList()
|
---|
87 | };
|
---|
88 | }
|
---|
89 | return null;
|
---|
90 | });
|
---|
91 | }
|
---|
92 |
|
---|
93 | public DT.JobPage GetJobs(int page, int size, bool completed = false) {
|
---|
94 | return GetJobsByUserId(UserManager.CurrentUserId, page, size, completed);
|
---|
95 | }
|
---|
96 |
|
---|
97 | public DT.JobPage GetJobsByUserId(Guid id, int page, int size, bool completed = false) {
|
---|
98 | if (id != UserManager.CurrentUserId) {
|
---|
99 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
100 | }
|
---|
101 | var pm = PersistenceManager;
|
---|
102 | var dimJobDao = pm.DimJobDao;
|
---|
103 | return pm.UseTransaction(() => {
|
---|
104 | var jobs = dimJobDao.GetByUserId(id).Where(x => completed ? (x.DateCompleted != null) : (x.DateCompleted == null));
|
---|
105 | return new DT.JobPage {
|
---|
106 | TotalJobs = jobs.Count(),
|
---|
107 | Jobs = jobs.OrderByDescending(x => x.DateCompleted ?? DateTime.MaxValue)
|
---|
108 | .Skip((page - 1) * size)
|
---|
109 | .Take(size)
|
---|
110 | .Select(x => ConvertToDT(x))
|
---|
111 | .ToList()
|
---|
112 | };
|
---|
113 | });
|
---|
114 | }
|
---|
115 |
|
---|
116 | public IEnumerable<DT.Job> GetAllJobs(bool completed) {
|
---|
117 | return GetAllJobsByUserId(UserManager.CurrentUserId, completed);
|
---|
118 | }
|
---|
119 |
|
---|
120 | public IEnumerable<DT.Job> GetAllJobsByUserId(Guid id, bool completed) {
|
---|
121 | if (id != UserManager.CurrentUserId) {
|
---|
122 | RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
|
---|
123 | }
|
---|
124 | var pm = PersistenceManager;
|
---|
125 | var dimJobDao = pm.DimJobDao;
|
---|
126 | return pm.UseTransaction(() => {
|
---|
127 | return dimJobDao.GetByUserId(id)
|
---|
128 | .Where(x => completed ? (x.DateCompleted != null) : (x.DateCompleted == null))
|
---|
129 | .Select(x => ConvertToDT(x))
|
---|
130 | .ToList();
|
---|
131 | });
|
---|
132 | }
|
---|
133 |
|
---|
134 | [Authorize(Roles = HiveRoles.Administrator)]
|
---|
135 | public IEnumerable<DT.Job> GetAllActiveJobsFromAllUsers() {
|
---|
136 | var pm = PersistenceManager;
|
---|
137 | var dimJobDao = pm.DimJobDao;
|
---|
138 | return pm.UseTransaction(() => {
|
---|
139 | return dimJobDao.GetAll()
|
---|
140 | .Where(x => x.DateCompleted == null)
|
---|
141 | .OrderByDescending(x => x.DateCreated)
|
---|
142 | .Select(x => ConvertToDT(x))
|
---|
143 | .ToList();
|
---|
144 | });
|
---|
145 | }
|
---|
146 |
|
---|
147 | private DT.Job ConvertToDT(DA.DimJob job) {
|
---|
148 | return new DT.Job {
|
---|
149 | Id = job.JobId,
|
---|
150 | Name = job.JobName,
|
---|
151 | UserId = job.UserId,
|
---|
152 | UserName = job.UserName,
|
---|
153 | DateCreated = job.DateCreated,
|
---|
154 | DateCompleted = job.DateCompleted,
|
---|
155 | TotalTasks = job.TotalTasks,
|
---|
156 | CompletedTasks = job.CompletedTasks
|
---|
157 | };
|
---|
158 | }
|
---|
159 | }
|
---|
160 | } |
---|