Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.WebApp.Statistics/3.3/WebApi/JobController.cs @ 12525

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

#2388:

HeuristicLab.Services.WebApp.Statistics-3.3:

  • added groups page
  • improved jobs, clients and users pages

HeuristicLab.Services.WebApp-3.3:

  • merged from trunk
File size: 6.4 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.Linq;
25using System.Web.Http;
26using HeuristicLab.Services.Access;
27using HeuristicLab.Services.Hive;
28using HeuristicLab.Services.Hive.DataAccess.Interfaces;
29using DA = HeuristicLab.Services.Hive.DataAccess;
30using DT = HeuristicLab.Services.WebApp.Statistics.WebApi.DataTransfer;
31
32namespace 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      using (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
94    public DT.JobPage GetJobs(int page, int size, bool completed = false) {
95      return GetJobsByUserId(UserManager.CurrentUserId, page, size, completed);
96    }
97
98    public DT.JobPage GetJobsByUserId(Guid id, int page, int size, bool completed = false) {
99      if (id != UserManager.CurrentUserId) {
100        RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
101      }
102      using (var pm = PersistenceManager) {
103        var dimJobDao = pm.DimJobDao;
104        return pm.UseTransaction(() => {
105          var jobs = dimJobDao.GetByUserId(id).Where(x => completed ? (x.DateCompleted != null) : (x.DateCompleted == null));
106          return new DT.JobPage {
107            TotalJobs = jobs.Count(),
108            Jobs = jobs.OrderByDescending(x => x.DateCompleted ?? DateTime.MaxValue)
109              .Skip((page - 1) * size)
110              .Take(size)
111              .Select(x => ConvertToDT(x))
112              .ToList()
113          };
114        });
115      }
116    }
117
118    public IEnumerable<DT.Job> GetAllJobs(bool completed) {
119      return GetAllJobsByUserId(UserManager.CurrentUserId, completed);
120    }
121
122    public IEnumerable<DT.Job> GetAllJobsByUserId(Guid id, bool completed) {
123      if (id != UserManager.CurrentUserId) {
124        RoleVerifier.AuthenticateForAnyRole(HiveRoles.Administrator);
125      }
126      using (var pm = PersistenceManager) {
127        var dimJobDao = pm.DimJobDao;
128        return pm.UseTransaction(() => {
129          return dimJobDao.GetByUserId(id)
130            .Where(x => completed ? (x.DateCompleted != null) : (x.DateCompleted == null))
131            .Select(x => ConvertToDT(x))
132            .ToList();
133        });
134      }
135    }
136
137    [Authorize(Roles = HiveRoles.Administrator)]
138    public IEnumerable<DT.Job> GetAllActiveJobsFromAllUsers() {
139      using (var pm = PersistenceManager) {
140        var dimJobDao = pm.DimJobDao;
141        return pm.UseTransaction(() => {
142          return dimJobDao.GetAll()
143            .Where(x => x.DateCompleted == null)
144            .OrderByDescending(x => x.DateCreated)
145            .Select(x => ConvertToDT(x))
146            .ToList();
147        });
148      }
149    }
150
151    private DT.Job ConvertToDT(DA.DimJob job) {
152      return new DT.Job {
153        Id = job.JobId,
154        Name = job.JobName,
155        UserId = job.UserId,
156        UserName = job.UserName,
157        DateCreated = job.DateCreated,
158        DateCompleted = job.DateCompleted,
159        TotalTasks = job.TotalTasks,
160        CompletedTasks = job.CompletedTasks
161      };
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.