Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.Hive/3.3/Converter.cs @ 12902

Last change on this file since 12902 was 12878, checked in by ascheibe, 9 years ago

#2388 merged hive statistics branch into trunk

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