#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Data.Linq; using System.Linq; namespace HeuristicLab.Services.Hive.DataAccess.Daos.HiveStatistics { public class FactTaskDao : GenericDao { private static readonly TaskState[] CompletedStates = { TaskState.Finished, TaskState.Aborted, TaskState.Failed }; private Table DimClientTable { get { return DataContext.GetTable(); } } private Table DimJobTable { get { return DataContext.GetTable(); } } public FactTaskDao(DataContext dataContext) : base(dataContext) { } public override FactTask GetById(Guid id) { return GetByIdQuery(DataContext, id); } public IQueryable GetNotFinishedTasks() { return Table.Where(x => x.EndTime == null); } public IQueryable GetByJobId(Guid id) { return Table.Where(x => x.JobId == id); } public DateTime? GetLastCompletedTaskFromJob(Guid id) { return GetLastCompletedTaskFromJobQuery(DataContext, id); } public IQueryable GetByClientId(Guid id) { return Table.Where(x => x.LastClientId == id); } public IQueryable GetTasksWithException() { return Table.Where(x => x.Exception.Trim() != string.Empty); } public IQueryable GetByGroupId(Guid id) { return from factTask in Table join client in DimClientTable on factTask.LastClientId equals client.Id where client.ResourceGroupId == id select factTask; } public IQueryable GetByUserId(Guid id) { return from factTask in Table join job in DimJobTable on factTask.JobId equals job.JobId where job.UserId == id select factTask; } public IQueryable GetCompletedTasks() { return Table.Where(x => CompletedStates.Contains(x.TaskState)); } public override void Delete(IEnumerable ids) { string paramIds = string.Join(",", ids.Select(x => string.Format("'{0}'", x))); if (!string.IsNullOrWhiteSpace(paramIds)) { string query = string.Format(BatchDeleteQuery, paramIds); DataContext.ExecuteCommand(query); } } public void DeleteByJobId(Guid jobId) { DataContext.ExecuteCommand(DeleteByJobIdQuery, jobId); } #region Compiled queries private static readonly Func GetByIdQuery = CompiledQuery.Compile((DataContext db, Guid id) => (from factTask in db.GetTable() where factTask.TaskId == id select factTask).SingleOrDefault()); private static readonly Func GetLastCompletedTaskFromJobQuery = CompiledQuery.Compile((DataContext db, Guid id) => (from factTask in db.GetTable() where factTask.JobId == id && factTask.EndTime != null select factTask.EndTime).Max()); #endregion #region String queries private const string BatchDeleteQuery = @"DELETE FROM [statistics].[FactTask] WHERE TaskId IN ({0});"; private const string DeleteByJobIdQuery = @"DELETE FROM [statistics].[FactTask] WHERE JobId = {0};"; #endregion } }