Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/Convert.cs @ 12768

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

#2388:

HeuristicLab.Services.Hive.DataAccess-3.3:

  • Removed old statistics tables
  • Updated SQL Scripts

HeuristicLab.Services.WebApp-3.3:
HeuristicLab.Services.WebApp.Status-3.3:
HeuristicLab.Services.WebApp.Statistics-3.3:

  • Minor changes
File size: 20.5 KB
RevLine 
[6983]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6983]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 DB = HeuristicLab.Services.Hive.DataAccess;
26using DT = HeuristicLab.Services.Hive.DataTransfer;
27
28namespace HeuristicLab.Services.Hive.DataTransfer {
29  public static class Convert {
30    #region Task
31    public static DT.Task ToDto(DB.Task source) {
32      if (source == null) return null;
33      return new DT.Task {
34        Id = source.TaskId,
35        CoresNeeded = source.CoresNeeded,
36        ExecutionTime = TimeSpan.FromMilliseconds(source.ExecutionTimeMs),
37        MemoryNeeded = source.MemoryNeeded,
38        ParentTaskId = source.ParentTaskId,
39        Priority = source.Priority,
40        PluginsNeededIds = (source.RequiredPlugins == null ? new List<Guid>() : source.RequiredPlugins.Select(x => x.PluginId).ToList()),
41        LastHeartbeat = source.LastHeartbeat,
42        State = Convert.ToDto(source.State),
43        StateLog = (source.StateLogs == null ? new List<DT.StateLog>() : source.StateLogs.Select(x => Convert.ToDto(x)).OrderBy(x => x.DateTime).ToList()),
44        IsParentTask = source.IsParentTask,
45        FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished,
46        Command = Convert.ToDto(source.Command),
47        LastTaskDataUpdate = (source.JobData == null ? DateTime.MinValue : source.JobData.LastUpdate),
48        JobId = source.JobId,
49        IsPrivileged = source.IsPrivileged
50      };
51    }
52
53    public static DB.Task ToEntity(DT.Task source) {
54      if (source == null) return null;
55      var entity = new DB.Task(); ToEntity(source, entity);
56      return entity;
57    }
58    public static void ToEntity(DT.Task source, DB.Task target) {
59      if ((source != null) && (target != null)) {
60        target.TaskId = source.Id;
61        target.CoresNeeded = source.CoresNeeded;
62        target.ExecutionTimeMs = source.ExecutionTime.TotalMilliseconds;
63        target.MemoryNeeded = source.MemoryNeeded;
64        target.ParentTaskId = source.ParentTaskId;
65        target.Priority = source.Priority;
66        target.LastHeartbeat = source.LastHeartbeat;
67        target.State = Convert.ToEntity(source.State);
68        foreach (DT.StateLog sl in source.StateLog.Where(x => x.Id == Guid.Empty)) {
69          target.StateLogs.Add(Convert.ToEntity(sl));
70        }
71        target.IsParentTask = source.IsParentTask;
72        target.FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished;
73        target.Command = Convert.ToEntity(source.Command);
74        // RequiredPlugins are added by Dao
75        target.JobId = source.JobId;
76        target.IsPrivileged = source.IsPrivileged;
77      }
78    }
[9266]79
80    public static void ToEntityTaskOnly(DT.Task source, DB.Task target) {
81      if ((source != null) && (target != null)) {
82        target.TaskId = source.Id;
83        target.CoresNeeded = source.CoresNeeded;
84        target.ExecutionTimeMs = source.ExecutionTime.TotalMilliseconds;
85        target.MemoryNeeded = source.MemoryNeeded;
86        target.ParentTaskId = source.ParentTaskId;
87        target.Priority = source.Priority;
88        target.LastHeartbeat = source.LastHeartbeat;
89        target.State = Convert.ToEntity(source.State);
90        target.IsParentTask = source.IsParentTask;
91        target.FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished;
92        target.Command = Convert.ToEntity(source.Command);
93        // RequiredPlugins are added by Dao
94        target.JobId = source.JobId;
95        target.IsPrivileged = source.IsPrivileged;
96      }
97    }
[6983]98    #endregion
99
100    #region TaskData
101    public static DT.TaskData ToDto(DB.TaskData source) {
102      if (source == null) return null;
[9665]103      return new DT.TaskData { TaskId = source.TaskId, Data = source.Data, LastUpdate = source.LastUpdate };
[6983]104    }
105    public static DB.TaskData ToEntity(DT.TaskData source) {
106      if (source == null) return null;
107      var entity = new DB.TaskData(); ToEntity(source, entity);
108      return entity;
109    }
110    public static void ToEntity(DT.TaskData source, DB.TaskData target) {
111      if ((source != null) && (target != null)) {
[9665]112        target.TaskId = source.TaskId;
113        target.Data = source.Data;
114        target.LastUpdate = source.LastUpdate;
[6983]115      }
116    }
117    #endregion
118
119    #region StateLog
120    public static DT.StateLog ToDto(DB.StateLog source) {
121      if (source == null) return null;
122      return new DT.StateLog { Id = source.StateLogId, DateTime = source.DateTime, Exception = source.Exception, TaskId = source.TaskId, SlaveId = source.SlaveId, State = Convert.ToDto(source.State), UserId = source.UserId };
123    }
124    public static DB.StateLog ToEntity(DT.StateLog source) {
125      if (source == null) return null;
126      var entity = new DB.StateLog(); ToEntity(source, entity);
127      return entity;
128    }
129    public static void ToEntity(DT.StateLog source, DB.StateLog target) {
130      if ((source != null) && (target != null)) {
131        target.StateLogId = source.Id; target.DateTime = source.DateTime; target.Exception = source.Exception; target.TaskId = source.TaskId; target.SlaveId = source.SlaveId; target.State = Convert.ToEntity(source.State); target.UserId = source.UserId;
132      }
133    }
134    #endregion
135
136    #region Downtimes
137    public static DT.Downtime ToDto(DB.Downtime source) {
138      if (source == null) return null;
[8957]139      return new DT.Downtime { Id = source.DowntimeId, AllDayEvent = source.AllDayEvent, EndDate = source.EndDate, Recurring = source.Recurring, RecurringId = source.RecurringId, ResourceId = source.ResourceId, StartDate = source.StartDate, DowntimeType = source.DowntimeType };
[6983]140    }
141    public static DB.Downtime ToEntity(DT.Downtime source) {
142      if (source == null) return null;
143      var entity = new DB.Downtime(); ToEntity(source, entity);
144      return entity;
145    }
146    public static void ToEntity(DT.Downtime source, DB.Downtime target) {
147      if ((source != null) && (target != null)) {
[8957]148        target.DowntimeId = 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; target.DowntimeType = source.DowntimeType;
[6983]149      }
150    }
151    #endregion
152
153    #region Job
154    public static DT.Job ToDto(DB.Job source) {
155      if (source == null) return null;
156      return new DT.Job { Id = source.JobId, Description = source.Description, Name = source.Name, OwnerUserId = source.OwnerUserId, DateCreated = source.DateCreated, ResourceNames = source.ResourceIds };
157    }
158    public static DB.Job ToEntity(DT.Job source) {
159      if (source == null) return null;
160      var entity = new DB.Job(); ToEntity(source, entity);
161      return entity;
162    }
163    public static void ToEntity(DT.Job source, DB.Job target) {
164      if ((source != null) && (target != null)) {
165        target.JobId = source.Id; target.Description = source.Description; target.Name = source.Name; target.OwnerUserId = source.OwnerUserId; target.DateCreated = source.DateCreated; target.ResourceIds = source.ResourceNames;
166      }
167    }
168    #endregion
169
170    #region JobPermission
171    public static DT.JobPermission ToDto(DB.JobPermission source) {
172      if (source == null) return null;
173      return new DT.JobPermission { JobId = source.JobId, GrantedUserId = source.GrantedUserId, GrantedByUserId = source.GrantedByUserId, Permission = Convert.ToDto(source.Permission) };
174    }
175    public static DB.JobPermission ToEntity(DT.JobPermission source) {
176      if (source == null) return null;
177      var entity = new DB.JobPermission(); ToEntity(source, entity);
178      return entity;
179    }
180    public static void ToEntity(DT.JobPermission source, DB.JobPermission target) {
181      if ((source != null) && (target != null)) {
182        target.JobId = source.JobId; target.GrantedUserId = source.GrantedUserId; target.GrantedByUserId = source.GrantedByUserId; target.Permission = Convert.ToEntity(source.Permission);
183      }
184    }
185    #endregion
186
187    #region Plugin
188    public static DT.Plugin ToDto(DB.Plugin source) {
189      if (source == null) return null;
190      return new DT.Plugin { Id = source.PluginId, Name = source.Name, Version = new Version(source.Version), UserId = source.UserId, DateCreated = source.DateCreated, Hash = source.Hash };
191    }
192    public static DB.Plugin ToEntity(DT.Plugin source) {
193      if (source == null) return null;
194      var entity = new DB.Plugin(); ToEntity(source, entity);
195      return entity;
196    }
197    public static void ToEntity(DT.Plugin source, DB.Plugin target) {
198      if ((source != null) && (target != null)) {
199        target.PluginId = source.Id; target.Name = source.Name; target.Version = source.Version.ToString(); target.UserId = source.UserId; target.DateCreated = source.DateCreated; target.Hash = source.Hash;
200      }
201    }
202    #endregion
203
204    #region PluginData
205    public static DT.PluginData ToDto(DB.PluginData source) {
206      if (source == null) return null;
207      return new DT.PluginData { Id = source.PluginDataId, PluginId = source.PluginId, Data = source.Data.ToArray(), FileName = source.FileName };
208    }
209    public static DB.PluginData ToEntity(DT.PluginData source) {
210      if (source == null) return null;
211      var entity = new DB.PluginData(); ToEntity(source, entity);
212      return entity;
213    }
214    public static void ToEntity(DT.PluginData source, DB.PluginData target) {
215      if ((source != null) && (target != null)) {
[9665]216        target.PluginDataId = source.Id;
217        target.PluginId = source.PluginId;
218        target.Data = source.Data;
219        target.FileName = source.FileName;
[6983]220      }
221    }
222    #endregion
223
[7916]224    #region Resource
225    public static DT.Resource ToDto(DB.Resource source) {
226      if (source == null) return null;
227      return new DT.Resource { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId, HbInterval = source.HbInterval, OwnerUserId = source.OwnerUserId };
228    }
229    public static DB.Resource ToEntity(DT.Resource source) {
230      if (source == null) return null;
231      var entity = new DB.Resource(); ToEntity(source, entity);
232      return entity;
233    }
234    public static void ToEntity(DT.Resource source, DB.Resource target) {
235      if ((source != null) && (target != null)) {
236        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId; target.HbInterval = source.HbInterval; target.OwnerUserId = source.OwnerUserId;
237      }
238    }
239    #endregion
240
241    #region SlaveGroup
242    public static DT.SlaveGroup ToDto(DB.SlaveGroup source) {
243      if (source == null) return null;
244      return new DT.SlaveGroup { Id = source.ResourceId, Name = source.Name, ParentResourceId = source.ParentResourceId, HbInterval = source.HbInterval, OwnerUserId = source.OwnerUserId };
245    }
246    public static DB.SlaveGroup ToEntity(DT.SlaveGroup source) {
247      if (source == null) return null;
248      var entity = new DB.SlaveGroup(); ToEntity(source, entity);
249      return entity;
250    }
251    public static void ToEntity(DT.SlaveGroup source, DB.SlaveGroup target) {
252      if ((source != null) && (target != null)) {
253        target.ResourceId = source.Id; target.Name = source.Name; target.ParentResourceId = source.ParentResourceId; target.HbInterval = source.HbInterval; target.OwnerUserId = source.OwnerUserId;
254      }
255    }
256    #endregion
257
[6983]258    #region Slave
259    public static DT.Slave ToDto(DB.Slave source) {
260      if (source == null) return null;
261      return new DT.Slave {
262        Id = source.ResourceId,
263        ParentResourceId = source.ParentResourceId,
264        Cores = source.Cores,
265        CpuSpeed = source.CpuSpeed,
266        FreeCores = source.FreeCores,
267        FreeMemory = source.FreeMemory,
268        IsAllowedToCalculate = source.IsAllowedToCalculate,
269        Memory = source.Memory,
270        Name = source.Name,
271        SlaveState = Convert.ToDto(source.SlaveState),
272        CpuArchitecture = Convert.ToDto(source.CpuArchitecture),
273        OperatingSystem = source.OperatingSystem,
274        LastHeartbeat = source.LastHeartbeat,
275        CpuUtilization = source.CpuUtilization,
[7857]276        HbInterval = source.HbInterval,
[7916]277        IsDisposable = source.IsDisposable,
278        OwnerUserId = source.OwnerUserId
[6983]279      };
280    }
281    public static DB.Slave ToEntity(DT.Slave source) {
282      if (source == null) return null;
283      var entity = new DB.Slave(); ToEntity(source, entity);
284      return entity;
285    }
286    public static void ToEntity(DT.Slave source, DB.Slave target) {
287      if ((source != null) && (target != null)) {
288        target.ResourceId = source.Id;
289        target.ParentResourceId = source.ParentResourceId;
290        target.Cores = source.Cores;
291        target.CpuSpeed = source.CpuSpeed;
292        target.FreeCores = source.FreeCores;
293        target.FreeMemory = source.FreeMemory;
294        target.IsAllowedToCalculate = source.IsAllowedToCalculate;
295        target.Memory = source.Memory;
296        target.Name = source.Name;
297        target.SlaveState = Convert.ToEntity(source.SlaveState);
298        target.CpuArchitecture = Convert.ToEntity(source.CpuArchitecture);
299        target.OperatingSystem = source.OperatingSystem;
300        target.LastHeartbeat = source.LastHeartbeat;
301        target.CpuUtilization = source.CpuUtilization;
302        target.HbInterval = source.HbInterval;
[7857]303        target.IsDisposable = source.IsDisposable;
[7916]304        target.OwnerUserId = source.OwnerUserId;
[6983]305      }
306    }
307    #endregion
308
[7916]309    #region ResourcePermission
310    public static DT.ResourcePermission ToDto(DB.ResourcePermission source) {
[6983]311      if (source == null) return null;
[7916]312      return new DT.ResourcePermission { ResourceId = source.ResourceId, GrantedUserId = source.GrantedUserId, GrantedByUserId = source.GrantedByUserId };
[6983]313    }
[7916]314    public static DB.ResourcePermission ToEntity(DT.ResourcePermission source) {
[6983]315      if (source == null) return null;
[7916]316      var entity = new DB.ResourcePermission(); ToEntity(source, entity);
[6983]317      return entity;
318    }
[7916]319    public static void ToEntity(DT.ResourcePermission source, DB.ResourcePermission target) {
[6983]320      if ((source != null) && (target != null)) {
[7916]321        target.ResourceId = source.ResourceId; target.GrantedUserId = source.GrantedUserId; target.GrantedByUserId = source.GrantedByUserId;
[6983]322      }
323    }
324    #endregion
325    #region TaskData
326    public static DT.TaskState ToDto(DB.TaskState source) {
327      if (source == DB.TaskState.Aborted) {
328        return TaskState.Aborted;
329      } else if (source == DB.TaskState.Calculating) {
330        return TaskState.Calculating;
331      } else if (source == DB.TaskState.Failed) {
332        return TaskState.Failed;
333      } else if (source == DB.TaskState.Finished) {
334        return TaskState.Finished;
335      } else if (source == DB.TaskState.Offline) {
336        return TaskState.Offline;
337      } else if (source == DB.TaskState.Paused) {
338        return TaskState.Paused;
339      } else if (source == DB.TaskState.Transferring) {
340        return TaskState.Transferring;
341      } else if (source == DB.TaskState.Waiting) {
342        return TaskState.Waiting;
343      } else
344        return TaskState.Failed;
345    }
346
347    public static DB.TaskState ToEntity(DT.TaskState source) {
348      if (source == DT.TaskState.Aborted) {
349        return DB.TaskState.Aborted;
350      } else if (source == DT.TaskState.Calculating) {
351        return DB.TaskState.Calculating;
352      } else if (source == DT.TaskState.Failed) {
353        return DB.TaskState.Failed;
354      } else if (source == DT.TaskState.Finished) {
355        return DB.TaskState.Finished;
356      } else if (source == DT.TaskState.Offline) {
357        return DB.TaskState.Offline;
358      } else if (source == DT.TaskState.Paused) {
359        return DB.TaskState.Paused;
360      } else if (source == DT.TaskState.Transferring) {
361        return DB.TaskState.Transferring;
362      } else if (source == DT.TaskState.Waiting) {
363        return DB.TaskState.Waiting;
364      } else
365        return DB.TaskState.Failed;
366    }
367    #endregion
368
369    #region Permission
370    public static DT.Permission ToDto(DB.Permission source) {
371      if (source == DB.Permission.Full) {
372        return Permission.Full;
373      } else if (source == DB.Permission.NotAllowed) {
374        return Permission.NotAllowed;
375      } else if (source == DB.Permission.Read) {
376        return Permission.Read;
377      } else
378        return Permission.NotAllowed;
379    }
380
381    public static DB.Permission ToEntity(DT.Permission source) {
382      if (source == DT.Permission.Full) {
383        return DB.Permission.Full;
384      } else if (source == DT.Permission.NotAllowed) {
385        return DB.Permission.NotAllowed;
386      } else if (source == DT.Permission.Read) {
387        return DB.Permission.Read;
388      } else
389        return DB.Permission.NotAllowed;
390    }
391    #endregion
392
393    #region Command
394    public static DT.Command? ToDto(DB.Command? source) {
395      if (source.HasValue) {
396        if (source.Value == DB.Command.Abort) {
397          return Command.Abort;
398        } else if (source.Value == DB.Command.Pause) {
399          return Command.Pause;
400        } else if (source.Value == DB.Command.Stop) {
401          return Command.Stop;
402        } else
403          return Command.Pause;
404      }
405      return null;
406    }
407
408    public static DB.Command? ToEntity(DT.Command? source) {
409      if (source.HasValue) {
410        if (source == DT.Command.Abort) {
411          return DB.Command.Abort;
412        } else if (source == DT.Command.Pause) {
413          return DB.Command.Pause;
414        } else if (source == DT.Command.Stop) {
415          return DB.Command.Stop;
416        } else
417          return DB.Command.Pause;
418      } else
419        return null;
420    }
421    #endregion
422
423    #region CpuArchiteture
424    public static DT.CpuArchitecture ToDto(DB.CpuArchitecture source) {
425      if (source == DB.CpuArchitecture.x64) {
426        return CpuArchitecture.x64;
427      } else if (source == DB.CpuArchitecture.x86) {
428        return CpuArchitecture.x86;
429      } else
430        return CpuArchitecture.x86;
431    }
432
433    public static DB.CpuArchitecture ToEntity(DT.CpuArchitecture source) {
434      if (source == DT.CpuArchitecture.x64) {
435        return DB.CpuArchitecture.x64;
436      } else if (source == DT.CpuArchitecture.x86) {
437        return DB.CpuArchitecture.x86;
438      } else
439        return DB.CpuArchitecture.x86;
440    }
441    #endregion
442
443    #region SlaveState
444    public static DT.SlaveState ToDto(DB.SlaveState source) {
445      if (source == DB.SlaveState.Calculating) {
446        return SlaveState.Calculating;
447      } else if (source == DB.SlaveState.Idle) {
448        return SlaveState.Idle;
449      } else if (source == DB.SlaveState.Offline) {
450        return SlaveState.Offline;
451      } else
452        return SlaveState.Offline;
453    }
454
455    public static DB.SlaveState ToEntity(DT.SlaveState source) {
456      if (source == DT.SlaveState.Calculating) {
457        return DB.SlaveState.Calculating;
458      } else if (source == DT.SlaveState.Idle) {
459        return DB.SlaveState.Idle;
460      } else if (source == DT.SlaveState.Offline) {
461        return DB.SlaveState.Offline;
462      } else
463        return DB.SlaveState.Offline;
464    }
465    #endregion
[9123]466
467    #region UserPriority
468    public static DT.UserPriority ToDto(DB.UserPriority source) {
469      if (source == null) return null;
470      return new DT.UserPriority() { Id = source.UserId, DateEnqueued = source.DateEnqueued };
471    }
472    public static DB.UserPriority ToEntity(DT.UserPriority source) {
473      if (source == null) return null;
474      var entity = new DB.UserPriority(); ToEntity(source, entity);
475      return entity;
476    }
477    public static void ToEntity(DT.UserPriority source, DB.UserPriority target) {
478      if ((source != null) && (target != null)) {
479        target.UserId = source.Id;
480        target.DateEnqueued = source.DateEnqueued;
481      }
482    }
483    #endregion
[6983]484  }
485}
Note: See TracBrowser for help on using the repository browser.