Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.DataAccess/3.4/Convert.cs @ 5786

Last change on this file since 5786 was 5786, checked in by cneumuel, 14 years ago

#1233

  • implemented correct numbering of BatchRuns
  • improvements in ExperimentManager
  • fixed bug in server (jobs were scheduled multiple times)
  • added exception handling for task in slave
  • improved timeout handling of jobs (LifecycleManager)
File size: 12.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Data.Linq;
24using System.Linq;
25using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
26
27namespace HeuristicLab.Services.Hive.DataAccess {
28  public static class Convert {
29    #region Job
30    public static DT.Job ToDto(Job source) {
31      if (source == null) return null;
32      return new DT.Job {
33        Id = source.JobId,
34        CoresNeeded = source.CoresNeeded,
35        ExecutionTime = string.IsNullOrEmpty(source.ExecutionTime) ? TimeSpan.Zero : TimeSpan.Parse(source.ExecutionTime),
36        MemoryNeeded = source.MemoryNeeded,
37        ParentJobId = source.ParentJobId,
38        Priority = source.Priority,
39        PluginsNeededIds = source.RequiredPlugins.Select(x => x.PluginId).ToList(),
40        LastHeartbeat = source.LastHeartbeat,
41        State = source.State,
42        StateLog = source.StateLogs.Select(x => Convert.ToDto(x)).OrderBy(x => x.DateTime).ToList(),
43        IsParentJob = source.IsParentJob,
44        FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished,
45        Command = source.Command,
46        LastJobDataUpdate = source.JobData.LastUpdate
47      };
48    }
49    public static Job ToEntity(DT.Job source) {
50      if (source == null) return null;
51      var entity = new Job(); ToEntity(source, entity);
52      return entity;
53    }
54    public static void ToEntity(DT.Job source, Job target) {
55      if ((source != null) && (target != null)) {
56        target.JobId = source.Id;
57        target.CoresNeeded = source.CoresNeeded;
58        target.ExecutionTime = source.ExecutionTime.ToString();
59        target.MemoryNeeded = source.MemoryNeeded;
60        target.ParentJobId = source.ParentJobId;
61        target.Priority = source.Priority;
62        target.LastHeartbeat = source.LastHeartbeat;
63        target.State = source.State;
64        if (target.StateLogs == null) target.StateLogs = new EntitySet<StateLog>();
65        foreach (DT.StateLog sl in source.StateLog.Where(x => x.Id == Guid.Empty)) {
66          target.StateLogs.Add(Convert.ToEntity(sl));
67        }
68        //target.StateLogs.AddRange(source.StateLog.Select(x => Convert.ToEntity(x)).OrderBy(x => x.DateTime));
69        target.IsParentJob = source.IsParentJob;
70        target.FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished;
71        target.Command = source.Command;
72        // RequiredPlugins are added by Dao
73      }
74    }
75    #endregion
76
77    #region JobData
78    public static DT.JobData ToDto(JobData source) {
79      if (source == null) return null;
80      return new DT.JobData { JobId = source.JobId, Data = source.Data.ToArray(), LastUpdate = source.LastUpdate };
81    }
82    public static JobData ToEntity(DT.JobData source) {
83      if (source == null) return null;
84      var entity = new JobData(); ToEntity(source, entity);
85      return entity;
86    }
87    public static void ToEntity(DT.JobData source, JobData target) {
88      if ((source != null) && (target != null)) {
89        target.JobId = source.JobId; target.Data = new Binary(source.Data); target.LastUpdate = source.LastUpdate;
90      }
91    }
92    #endregion
93
94    #region StateLog
95    public static DT.StateLog ToDto(StateLog source) {
96      if (source == null) return null;
97      return new DT.StateLog { Id = source.StateLogId, DateTime = source.DateTime, Exception = source.Exception, JobId = source.JobId, SlaveId = source.SlaveId, State = source.State, UserId = source.UserId };
98    }
99    public static StateLog ToEntity(DT.StateLog source) {
100      if (source == null) return null;
101      var entity = new StateLog(); ToEntity(source, entity);
102      return entity;
103    }
104    public static void ToEntity(DT.StateLog source, StateLog target) {
105      if ((source != null) && (target != null)) {
106        target.StateLogId = source.Id; target.DateTime = source.DateTime; target.Exception = source.Exception; target.JobId = source.JobId; target.SlaveId = source.SlaveId; target.State = source.State; target.UserId = source.UserId;
107      }
108    }
109    #endregion
110
111    #region Appointment
112    public static DT.Appointment ToDto(UptimeCalendar source) {
113      if (source == null) return null;
114      return new DT.Appointment { Id = source.UptimeCalendarId, AllDayEvent = source.AllDayEvent, EndDate = source.EndDate, Recurring = source.Recurring, RecurringId = source.RecurringId, ResourceId = source.ResourceId, StartDate = source.StartDate };
115    }
116    public static UptimeCalendar ToEntity(DT.Appointment source) {
117      if (source == null) return null;
118      var entity = new UptimeCalendar(); ToEntity(source, entity);
119      return entity;
120    }
121    public static void ToEntity(DT.Appointment source, UptimeCalendar target) {
122      if ((source != null) && (target != null)) {
123        target.UptimeCalendarId = source.Id; target.AllDayEvent = source.AllDayEvent; target.EndDate = source.EndDate; target.Recurring = source.Recurring; target.RecurringId = source.RecurringId; target.ResourceId = source.ResourceId; target.StartDate = source.StartDate;
124      }
125    }
126    #endregion
127
128    #region HiveExperiment
129    public static DT.HiveExperiment ToDto(HiveExperiment source) {
130      if (source == null) return null;
131      return new DT.HiveExperiment { Id = source.HiveExperimentId, Description = source.Description, Name = source.Name, RootJobId = source.RootJobId, OwnerUserId = source.OwnerUserId, DateCreated = source.DateCreated, ResourceNames = source.ResourceIds, LastAccessed = source.LastAccessed };
132    }
133    public static HiveExperiment ToEntity(DT.HiveExperiment source) {
134      if (source == null) return null;
135      var entity = new HiveExperiment(); ToEntity(source, entity);
136      return entity;
137    }
138    public static void ToEntity(DT.HiveExperiment source, HiveExperiment target) {
139      if ((source != null) && (target != null)) {
140        target.HiveExperimentId = source.Id; target.Description = source.Description; target.Name = source.Name; target.RootJobId = source.RootJobId; target.OwnerUserId = source.OwnerUserId; target.DateCreated = source.DateCreated; target.ResourceIds = source.ResourceNames; target.LastAccessed = source.LastAccessed;
141      }
142    }
143    #endregion
144
145    #region HiveExperimentPermission
146    public static DT.HiveExperimentPermission ToDto(HiveExperimentPermission source) {
147      if (source == null) return null;
148      return new DT.HiveExperimentPermission { HiveExperimentId = source.HiveExperimentId, GrantedUserId = source.GrantedUserId, GrantedByUserId = source.GrantedByUserId, Permission = source.Permission };
149    }
150    public static HiveExperimentPermission ToEntity(DT.HiveExperimentPermission source) {
151      if (source == null) return null;
152      var entity = new HiveExperimentPermission(); ToEntity(source, entity);
153      return entity;
154    }
155    public static void ToEntity(DT.HiveExperimentPermission source, HiveExperimentPermission target) {
156      if ((source != null) && (target != null)) {
157        target.HiveExperimentId = source.HiveExperimentId; target.GrantedUserId = source.GrantedUserId; target.GrantedByUserId = source.GrantedByUserId; target.Permission = source.Permission;
158      }
159    }
160    #endregion
161
162    #region Plugin
163    public static DT.Plugin ToDto(Plugin source) {
164      if (source == null) return null;
165      return new DT.Plugin { Id = source.PluginId, Name = source.Name, Version = new Version(source.Version), UserId = source.UserId, IsLocal = source.IsLocal, DateCreated = source.DateCreated };
166    }
167    public static Plugin ToEntity(DT.Plugin source) {
168      if (source == null) return null;
169      var entity = new Plugin(); ToEntity(source, entity);
170      return entity;
171    }
172    public static void ToEntity(DT.Plugin source, Plugin target) {
173      if ((source != null) && (target != null)) {
174        target.PluginId = source.Id; target.Name = source.Name; target.Version = source.Version.ToString(); target.UserId = source.UserId; target.IsLocal = source.IsLocal; target.DateCreated = source.DateCreated;
175      }
176    }
177    #endregion
178
179    #region PluginData
180    public static DT.PluginData ToDto(PluginData source) {
181      if (source == null) return null;
182      return new DT.PluginData { Id = source.PluginDataId, PluginId = source.PluginId, Data = source.Data.ToArray(), FileName = source.FileName };
183    }
184    public static PluginData ToEntity(DT.PluginData source) {
185      if (source == null) return null;
186      var entity = new PluginData(); ToEntity(source, entity);
187      return entity;
188    }
189    public static void ToEntity(DT.PluginData source, PluginData target) {
190      if ((source != null) && (target != null)) {
191        target.PluginDataId = source.Id; target.PluginId = source.PluginId; target.Data = new Binary(source.Data); target.FileName = source.FileName;
192      }
193    }
194    #endregion
195
196    #region Slave
197    public static DT.Slave ToDto(Slave source) {
198      if (source == null) return null;
199      return new DT.Slave {
200        Id = source.ResourceId,
201        ParentResourceId = source.ParentResourceId,
202        Cores = source.Cores,
203        CpuSpeed = source.CpuSpeed,
204        FreeCores = source.FreeCores,
205        FreeMemory = source.FreeMemory,
206        IsAllowedToCalculate = source.IsAllowedToCalculate,
207        Memory = source.Memory,
208        Name = source.Name,
209        SlaveState = source.SlaveState,
210        CpuArchitecture = source.CpuArchitecture,
211        OperatingSystem = source.OperatingSystem,
212        LastHeartbeat = source.LastHeartbeat
213      };
214    }
215    public static Slave ToEntity(DT.Slave source) {
216      if (source == null) return null;
217      var entity = new Slave(); ToEntity(source, entity);
218      return entity;
219    }
220    public static void ToEntity(DT.Slave source, Slave target) {
221      if ((source != null) && (target != null)) {
222        target.ResourceId = source.Id;
223        target.ParentResourceId = source.ParentResourceId;
224        target.Cores = source.Cores;
225        target.CpuSpeed = source.CpuSpeed;
226        target.FreeCores = source.FreeCores;
227        target.FreeMemory = source.FreeMemory;
228        target.IsAllowedToCalculate = source.IsAllowedToCalculate;
229        target.Memory = source.Memory;
230        target.Name = source.Name;
231        target.SlaveState = source.SlaveState;
232        target.CpuArchitecture = source.CpuArchitecture;
233        target.OperatingSystem = source.OperatingSystem;
234        target.LastHeartbeat = source.LastHeartbeat;
235      }
236    }
237    #endregion
238
239    #region SlaveGroup
240    public static DT.SlaveGroup ToDto(SlaveGroup source) {
241      if (source == null) return null;
242      return new DT.SlaveGroup { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId };
243    }
244    public static SlaveGroup ToEntity(DT.SlaveGroup source) {
245      if (source == null) return null;
246      var entity = new SlaveGroup(); ToEntity(source, entity);
247      return entity;
248    }
249    public static void ToEntity(DT.SlaveGroup source, SlaveGroup target) {
250      if ((source != null) && (target != null)) {
251        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId;
252      }
253    }
254    #endregion
255
256    #region Resource
257    public static DT.Resource ToDto(Resource source) {
258      if (source == null) return null;
259      return new DT.Resource { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId };
260    }
261    public static Resource ToEntity(DT.Resource source) {
262      if (source == null) return null;
263      var entity = new Resource(); ToEntity(source, entity);
264      return entity;
265    }
266    public static void ToEntity(DT.Resource source, Resource target) {
267      if ((source != null) && (target != null)) {
268        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId;
269      }
270    }
271    #endregion
272  }
273}
Note: See TracBrowser for help on using the repository browser.