Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Services.Hive/3.3/Converter.cs @ 12584

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

#2388: Changed all files to connect to localhost / sqlexpress

HeuristicLab.Services.Hive-3.3:

  • Added Converter.cs and NewHiveService.cs, both will be integrated into existing HiveService.cs and Convert.cs when all methods are successfully implemented

HeuristicLab.Services.Hive.Web.Hive-3.3:

  • Added publish profiles

HeuristicLab.Services.WebApp.Statistics-3.3:

  • Added functionality to download TaskData as .hl file
File size: 9.0 KB
Line 
1using System;
2using System.Linq;
3using DA = HeuristicLab.Services.Hive.DataAccess;
4using DT = HeuristicLab.Services.Hive.DataTransfer;
5
6namespace HeuristicLab.Services.Hive {
7  public static class Converter {
8
9    #region Task
10    public static DT.Task ToDto(this DA.Task source) {
11      if (source == null) return null;
12      return new DT.Task {
13        Id = source.TaskId,
14        State = source.State.ToDto(),
15        ExecutionTime = TimeSpan.FromMilliseconds(source.ExecutionTimeMs),
16        LastHeartbeat = source.LastHeartbeat,
17        ParentTaskId = source.ParentTaskId,
18        Priority = source.Priority,
19        CoresNeeded = source.CoresNeeded,
20        MemoryNeeded = source.MemoryNeeded,
21        IsParentTask = source.IsParentTask,
22        FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished,
23        Command = source.Command.ToDto(),
24        JobId = source.JobId,
25        IsPrivileged = source.IsPrivileged,
26        PluginsNeededIds = source.RequiredPlugins.Select(x => x.PluginId).ToList(),
27        StateLog = source.StateLogs.Select(x => x.ToDto()).OrderBy(x => x.DateTime).ToList(),
28        LastTaskDataUpdate = source.JobData == null ? DateTime.MinValue : source.JobData.LastUpdate
29      };
30    }
31
32    public static DA.Task ToEntity(this DT.Task source) {
33      if (source == null) return null;
34      var result = new DA.Task();
35      source.CopyToEntity(result);
36      return result;
37    }
38
39    public static void CopyToEntity(this DT.Task source, DA.Task target) {
40      if ((source == null) || (target == null)) return;
41      target.TaskId = source.Id;
42      target.State = source.State.ToEntity();
43      target.ExecutionTimeMs = source.ExecutionTime.TotalMilliseconds;
44      target.LastHeartbeat = source.LastHeartbeat;
45      target.ParentTaskId = source.ParentTaskId;
46      target.Priority = source.Priority;
47      target.CoresNeeded = source.CoresNeeded;
48      target.MemoryNeeded = source.MemoryNeeded;
49      target.IsParentTask = source.IsParentTask;
50      target.FinishWhenChildJobsFinished = source.FinishWhenChildJobsFinished;
51      target.Command = source.Command.ToEntity();
52      target.JobId = source.JobId;
53      target.IsPrivileged = source.IsPrivileged;
54      var ids = target.RequiredPlugins.Select(x => x.PluginId);
55      target.RequiredPlugins.AddRange(source.PluginsNeededIds
56        .Where(x => !ids.Contains(x))
57        .Select(x => new DA.RequiredPlugin {
58          PluginId = x
59        })
60      );
61      target.StateLogs.AddRange(source.StateLog
62        .Where(x => x.Id == Guid.Empty)
63        .Select(x => x.ToEntity())
64      );
65      // result.JobData missing
66      // result.AssignedResources missing
67    }
68    #endregion
69
70    #region TaskData
71    public static DT.TaskData ToDto(this DA.TaskData source) {
72      if (source == null) return null;
73      return new DT.TaskData {
74        TaskId = source.TaskId,
75        Data = source.Data,
76        LastUpdate = source.LastUpdate
77      };
78    }
79
80    public static DA.TaskData ToEntity(this DT.TaskData source) {
81      if (source == null) return null;
82      var result = new DA.TaskData();
83      source.CopyToEntity(result);
84      return result;
85    }
86
87    public static void CopyToEntity(this DT.TaskData source, DA.TaskData target) {
88      if ((source == null) || (target == null)) return;
89      target.TaskId = source.TaskId;
90      target.Data = source.Data;
91      target.LastUpdate = source.LastUpdate;
92    }
93    #endregion
94
95    #region Job
96    public static DT.Job ToDto(this DA.Job source) {
97      return new DT.Job {
98        Id = source.JobId,
99        Description = source.Description,
100        Name = source.Name,
101        OwnerUserId = source.OwnerUserId,
102        DateCreated = source.DateCreated,
103        ResourceNames = source.ResourceIds
104      };
105    }
106
107    public static DA.Job ToEntity(this DT.Job source) {
108      if (source == null) return null;
109      var result = new DA.Job();
110      source.CopyToEntity(result);
111      return result;
112    }
113
114    public static void CopyToEntity(this DT.Job source, DA.Job target) {
115      if ((source == null) || (target == null)) return;
116      target.JobId = source.Id;
117      target.Description = source.Description;
118      target.Name = source.Name;
119      target.OwnerUserId = source.OwnerUserId;
120      target.DateCreated = source.DateCreated;
121      target.ResourceIds = source.ResourceNames;
122    }
123    #endregion
124
125    #region JobPermission
126    public static DT.JobPermission ToDto(this DA.JobPermission source) {
127      if (source == null) return null;
128      return new DT.JobPermission {
129        JobId = source.JobId,
130        GrantedUserId = source.GrantedUserId,
131        GrantedByUserId = source.GrantedByUserId,
132        Permission = source.Permission.ToDto()
133      };
134    }
135
136    public static DA.JobPermission ToEntity(this DT.JobPermission source) {
137      if (source == null) return null;
138      var result = new DA.JobPermission();
139      source.CopyToEntity(result);
140      return result;
141    }
142
143    public static void CopyToEntity(this DT.JobPermission source, DA.JobPermission target) {
144      if ((source == null) || (target == null)) return;
145      target.JobId = source.JobId;
146      target.GrantedUserId = source.GrantedUserId;
147      target.GrantedByUserId = source.GrantedByUserId;
148      target.Permission = source.Permission.ToEntity();
149    }
150    #endregion
151
152    #region State
153    public static DT.TaskState ToDto(this DA.TaskState source) {
154      switch (source) {
155        case DA.TaskState.Aborted: return DT.TaskState.Aborted;
156        case DA.TaskState.Calculating: return DT.TaskState.Calculating;
157        case DA.TaskState.Failed: return DT.TaskState.Failed;
158        case DA.TaskState.Finished: return DT.TaskState.Finished;
159        case DA.TaskState.Offline: return DT.TaskState.Offline;
160        case DA.TaskState.Paused: return DT.TaskState.Paused;
161        case DA.TaskState.Transferring: return DT.TaskState.Transferring;
162        case DA.TaskState.Waiting: return DT.TaskState.Waiting;
163        default: return DT.TaskState.Failed;
164      }
165    }
166
167    public static DA.TaskState ToEntity(this DT.TaskState source) {
168      switch (source) {
169        case DT.TaskState.Aborted: return DA.TaskState.Aborted;
170        case DT.TaskState.Calculating: return DA.TaskState.Calculating;
171        case DT.TaskState.Failed: return DA.TaskState.Failed;
172        case DT.TaskState.Finished: return DA.TaskState.Finished;
173        case DT.TaskState.Offline: return DA.TaskState.Offline;
174        case DT.TaskState.Paused: return DA.TaskState.Paused;
175        case DT.TaskState.Transferring: return DA.TaskState.Transferring;
176        case DT.TaskState.Waiting: return DA.TaskState.Waiting;
177        default: return DA.TaskState.Failed;
178      }
179    }
180    #endregion
181
182    #region StateLogs
183    public static DT.StateLog ToDto(this DA.StateLog source) {
184      return new DT.StateLog {
185        Id = source.StateLogId,
186        State = source.State.ToDto(),
187        DateTime = source.DateTime,
188        TaskId = source.TaskId,
189        UserId = source.UserId,
190        SlaveId = source.SlaveId,
191        Exception = source.Exception
192      };
193    }
194
195    public static DA.StateLog ToEntity(this DT.StateLog source) {
196      return new DA.StateLog {
197        StateLogId = source.Id,
198        State = source.State.ToEntity(),
199        DateTime = source.DateTime,
200        TaskId = source.TaskId,
201        UserId = source.UserId,
202        SlaveId = source.SlaveId,
203        Exception = source.Exception
204      };
205    }
206    #endregion
207
208    #region Command
209    public static DT.Command? ToDto(this DA.Command? source) {
210      if (source.HasValue) {
211        switch (source) {
212          case DA.Command.Abort: return DT.Command.Abort;
213          case DA.Command.Pause: return DT.Command.Pause;
214          case DA.Command.Stop: return DT.Command.Stop;
215          default: return DT.Command.Pause;
216        }
217      }
218      return null;
219    }
220
221    public static DA.Command? ToEntity(this DT.Command? source) {
222      if (source.HasValue) {
223        switch (source) {
224          case DT.Command.Abort: return DA.Command.Abort;
225          case DT.Command.Pause: return DA.Command.Pause;
226          case DT.Command.Stop: return DA.Command.Stop;
227          default: return DA.Command.Pause;
228        }
229      }
230      return null;
231    }
232    #endregion
233
234    #region Permission
235    public static DT.Permission ToDto(this DA.Permission source) {
236      switch (source) {
237        case DA.Permission.Full: return DT.Permission.Full;
238        case DA.Permission.NotAllowed: return DT.Permission.NotAllowed;
239        case DA.Permission.Read: return DT.Permission.Read;
240        default: return DT.Permission.NotAllowed;
241      }
242    }
243
244    public static DA.Permission ToEntity(this DT.Permission source) {
245      switch (source) {
246        case DT.Permission.Full: return DA.Permission.Full;
247        case DT.Permission.NotAllowed: return DA.Permission.NotAllowed;
248        case DT.Permission.Read: return DA.Permission.Read;
249        default: return DA.Permission.NotAllowed;
250      }
251    }
252    #endregion
253
254  }
255}
Note: See TracBrowser for help on using the repository browser.