Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/FactTaskDao.cs @ 13780

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

#2429: Worked on the maintenance WebApp plugin:

  • Space Usage Page: Displays the number of rows and allocated disk space for every database table
  • Plugin Page: Shows unused plugins and provides functionality to delete all and specific plugins
  • FactTask Page: Allows to aggregate all Job Tasks to a single task for a given job or jobs within an selected time period
  • FactClientInfo Page: Allows to aggregate consecutive FactClientInfo entries with the same state and isallowedtocalculate flag
File size: 4.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.Data.Linq;
25using System.Linq;
26
27namespace HeuristicLab.Services.Hive.DataAccess.Daos.HiveStatistics {
28  public class FactTaskDao : GenericDao<Guid, FactTask> {
29
30    private static readonly TaskState[] CompletedStates = { TaskState.Finished, TaskState.Aborted, TaskState.Failed };
31
32    private Table<DimClient> DimClientTable {
33      get { return DataContext.GetTable<DimClient>(); }
34    }
35    private Table<DimJob> DimJobTable {
36      get { return DataContext.GetTable<DimJob>(); }
37    }
38
39    public FactTaskDao(DataContext dataContext) : base(dataContext) { }
40
41    public override FactTask GetById(Guid id) {
42      return GetByIdQuery(DataContext, id);
43    }
44
45    public IQueryable<FactTask> GetNotFinishedTasks() {
46      return Table.Where(x => x.EndTime == null);
47    }
48
49    public IQueryable<FactTask> GetByJobId(Guid id) {
50      return Table.Where(x => x.JobId == id);
51    }
52
53    public DateTime? GetLastCompletedTaskFromJob(Guid id) {
54      return GetLastCompletedTaskFromJobQuery(DataContext, id);
55    }
56
57    public IQueryable<FactTask> GetByClientId(Guid id) {
58      return Table.Where(x => x.LastClientId == id);
59    }
60
61    public IQueryable<FactTask> GetTasksWithException() {
62      return Table.Where(x => x.Exception.Trim() != string.Empty);
63    }
64
65    public IQueryable<FactTask> GetByGroupId(Guid id) {
66      return from factTask in Table
67             join client in DimClientTable on factTask.LastClientId equals client.Id
68             where client.ResourceGroupId == id
69             select factTask;
70    }
71
72    public IQueryable<FactTask> GetByUserId(Guid id) {
73      return from factTask in Table
74             join job in DimJobTable on factTask.JobId equals job.JobId
75             where job.UserId == id
76             select factTask;
77    }
78
79    public IQueryable<FactTask> GetCompletedTasks() {
80      return Table.Where(x => CompletedStates.Contains(x.TaskState));
81    }
82
83    public override void Delete(IEnumerable<Guid> ids) {
84      string paramIds = string.Join(",", ids.Select(x => string.Format("'{0}'", x)));
85      if (!string.IsNullOrWhiteSpace(paramIds)) {
86        string query = string.Format(BatchDeleteQuery, paramIds);
87        DataContext.ExecuteCommand(query);
88      }
89    }
90
91    public void DeleteByJobId(Guid jobId) {
92      DataContext.ExecuteCommand(DeleteByJobIdQuery, jobId);
93    }
94
95    #region Compiled queries
96    private static readonly Func<DataContext, Guid, FactTask> GetByIdQuery =
97      CompiledQuery.Compile((DataContext db, Guid id) =>
98        (from factTask in db.GetTable<FactTask>()
99         where factTask.TaskId == id
100         select factTask).SingleOrDefault());
101
102
103    private static readonly Func<DataContext, Guid, DateTime?> GetLastCompletedTaskFromJobQuery =
104      CompiledQuery.Compile((DataContext db, Guid id) =>
105        (from factTask in db.GetTable<FactTask>()
106         where factTask.JobId == id && factTask.EndTime != null
107         select factTask.EndTime).Max());
108    #endregion
109
110    #region String queries
111    private const string BatchDeleteQuery =
112      @"DELETE FROM [statistics].[FactTask]
113         WHERE TaskId IN ({0});";
114
115    private const string DeleteByJobIdQuery =
116      @"DELETE FROM [statistics].[FactTask]
117         WHERE JobId = {0};";
118    #endregion
119  }
120}
Note: See TracBrowser for help on using the repository browser.