Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Hubs/ProgressHub.cs @ 13827

Last change on this file since 13827 was 13827, checked in by jlodewyc, 8 years ago

#2582 Parameter changing busy, save file, download file and email on pass reset

File size: 11.3 KB
Line 
1using HeuristicLab.Clients.Hive.WebJobManager.Services;
2using HeuristicLab.Optimization;
3using Microsoft.AspNet.SignalR;
4using System;
5using System.Collections.Generic;
6using System.Linq;
7using System.Threading.Tasks;
8using HeuristicLab.Data;
9using System.Reflection;
10
11namespace HeuristicLab.Clients.Hive.WebJobManager
12{
13    /// <summary>
14    /// SignalR Hub for displaying the progress when uploading a Job.
15    /// Also used to change child distribution and priority for inner tasks.
16    /// </summary>
17    public class ProgressHub : Hub
18    {
19        private WebLoginService weblog;
20        private Guid userId;
21        private FileOpeningService fileopener;
22        private void loader()
23        {
24            weblog = WebLoginService.Instance;
25            string uid = Context.QueryString["userid"];
26            if (uid == null || uid == "" || Guid.Parse(uid) == Guid.Empty)
27            {
28                userId = Guid.Empty;
29            }
30            else
31            {
32                userId = Guid.Parse(uid);
33                fileopener = weblog.getFileOpener(userId);
34            }
35        }
36
37        /// <summary>
38        /// First message from client
39        /// </summary>
40        /// <param name="receivedString">Client message</param>
41        public void HandleMessage(string receivedString)
42        {
43            loader();
44            Clients.Caller.processMessage("Connection Established");
45            fileopener.Job.Progress.StatusChanged += runHub;
46        }
47        /// <summary>
48        /// Changes name for the current job to be created
49        /// </summary>
50        /// <param name="name">Job name</param>
51        public void ChangeNameResource(string name, string resource)
52        {
53            loader();
54            if (name != null)
55                fileopener.Job.Job.Name = name;
56            if (resource != null && resource != "")
57            {
58                fileopener.Job.Job.ResourceNames += "/" + resource;
59            }
60        }
61        /// <summary>
62        /// Toggles distribute child task for a current task
63        /// </summary>
64        /// <param name="arr">2-dimensional int array.
65        /// First dimension is depth (arr.length == 4 means the item is 4 nodes deep)
66        /// Second contains expirement info or batch info. (arr[][0] contains index to select the right experiment sub task,
67        /// arr[][1] contains index to select right batch run subtask)</param>
68        public void ToggleChild(int[][] arr)
69        {
70            loader();
71            HiveTask current = fileopener.Job.HiveTasks.ToList()[0];
72            if (arr.Length == 0)
73            {//check if upper job
74                current.ItemTask.ComputeInParallel = !current.ItemTask.ComputeInParallel;
75
76            }
77            else
78            {
79                for (var i = 0; i < arr.Length; i++)
80                {
81                    //loop for depth
82
83                    if (i == arr.Length - 1)//end of depth loop, right current is selected
84                    {
85                        if (current.ItemTask.Item is BatchRun)
86                        {
87                            current.ChildHiveTasks[arr[i][1]].ItemTask.ComputeInParallel = !current.ChildHiveTasks[arr[i][1]].ItemTask.ComputeInParallel;
88                        }
89                        else if (current.ItemTask.Item is Experiment)
90                        {
91                            current.ChildHiveTasks[arr[i][0]].ItemTask.ComputeInParallel = !current.ChildHiveTasks[arr[i][0]].ItemTask.ComputeInParallel;
92                        }
93
94                    }
95                    else
96                    {//not deep enough, select right path
97                        if (current.ItemTask.Item is BatchRun)
98                            current = current.ChildHiveTasks[arr[i][1]]; // select right batch
99                        else if (current.ItemTask.Item is Experiment)
100                            current = current.ChildHiveTasks[arr[i][0]]; // select right sub task from experiment
101                    }
102                }
103            }
104        }
105        /// <summary>
106        /// Change priority for a node
107        /// </summary>
108        /// <param name="arr">2-dimensional int array.
109        /// First dimension is depth (arr.length == 4 means the item is 4 nodes deep)
110        /// Second contains expirement info or batch info. (arr[][0] contains index to select the right experiment sub task,
111        /// arr[][1] contains index to select right batch run subtask)</param>
112        /// <param name="prior">Selected priority</param>
113        public void ChangePriority(int[][] arr, int prior)
114        {
115            loader();
116            HiveTask current = fileopener.Job.HiveTasks.ToList()[0];
117            if (arr.Length == 0)
118            {//check if upper job
119                current.Task.Priority = prior;
120
121            }
122            else
123            {
124                for (var i = 0; i < arr.Length; i++)
125                {//loop for depth
126
127
128                    if (i == arr.Length - 1)
129                    {//Right depth reached, change priority for current
130                        if (current.ItemTask.Item is BatchRun)
131                        {
132                            current.ChildHiveTasks[arr[i][1]].Task.Priority = prior;
133                        }
134                        else if (current.ItemTask.Item is Experiment)
135                        {
136                            current.ChildHiveTasks[arr[i][0]].Task.Priority = prior;
137                        }
138
139                    }
140                    else
141                    {//not deep enough, choose right path
142                        if (current.ItemTask.Item is BatchRun)
143                            current = current.ChildHiveTasks[arr[i][1]]; // select right batch
144                        else if (current.ItemTask.Item is Experiment)
145                            current = current.ChildHiveTasks[arr[i][0]]; // select right sub task from experiment
146                    }
147                }
148            }
149        }
150        /// <summary>
151        /// Used by event 'ProgressChanged' from current uploading Job
152        /// </summary>
153        /// <param name="sender"></param>
154        /// <param name="e"></param>
155        public void runHub(object sender, EventArgs e)
156        {
157            loader();
158            int value = 0;
159
160            switch (fileopener.Job.Progress.Status)
161            {
162                case "Connecting to server...":
163                    value = 0;
164                    break;
165                case "Uploading Job...":
166                    value = 10;
167                    break;
168                case "Uploading plugins...":
169                    value = 30;
170                    break;
171                case "Uploading tasks...":
172                    value = 50;
173                    break;
174                case "Upload finished":
175                    value = 100;
176                    break;
177                default://Tasks are uploading individually
178                    value = (int)(50 + (40 * fileopener.Job.Progress.ProgressValue));
179                    break;
180
181            }
182            //send info to client
183            Clients.Caller.processMessage(fileopener.Job.Progress.Status, value);
184
185        }
186        public void paraEdit(int[][] arr, string problem, string name, string type, string value)
187        {
188
189
190            loader();
191            HiveTask current = fileopener.Job.HiveTasks.ToList()[0];
192            if (arr.Length == 0)
193            {//check if upper job
194                if (current.ItemTask.Item is Algorithm)
195                {
196                    searchEditPara(current, problem, name, type, value);
197                }
198            }
199            else
200            {
201                for (var i = 0; i < arr.Length; i++)
202                {//loop for depth
203                    if (i == arr.Length - 1)
204                    {//Right depth reached, change priority for current
205                        if (current.ItemTask.Item is BatchRun)
206                        {
207                            current = current.ChildHiveTasks[arr[i][1]];
208                        }
209                        else if (current.ItemTask.Item is Experiment)
210                        {
211                            current = current.ChildHiveTasks[arr[i][0]];
212                        }
213                        if (current.ItemTask.Item is Algorithm)
214                        {
215                            searchEditPara(current, problem, name, type, value);
216                        }
217                    }
218                    else
219                    {//not deep enough, choose right path
220                        if (current.ItemTask.Item is BatchRun)
221                            current = current.ChildHiveTasks[arr[i][1]]; // select right batch
222                        else if (current.ItemTask.Item is Experiment)
223                            current = current.ChildHiveTasks[arr[i][0]]; // select right sub task from experiment
224                    }
225                }
226            }
227        }
228        /// <summary>
229        /// Generic parameter setter
230        /// </summary>
231        /// <param name="curr">Current task to edit </param>
232        /// <param name="problem">Problem parameter or normal parameter</param>
233        /// <param name="name">Property name</param>
234        /// <param name="type">Datatype</param>
235        /// <param name="value">New value</param>
236        private void searchEditPara(HiveTask curr, string problem, string name, string type, string value)
237        {
238            var prob = problem == "True" ? true : false;
239
240            var dattype = Type.GetType(type);
241            if (dattype == null)
242            {//Use assemblies to find correct datatype
243                foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
244                {
245                    dattype = a.GetType(type);
246                    if (dattype != null)
247                        break;
248                }
249            }
250            //find parse method
251            var parse = dattype.GetMethod("Parse", new[] { typeof(string) });
252
253            if (parse != null)
254            {
255                //Parses string to correct datatype
256                var val = parse.Invoke(null, new object[] { value });
257
258                //BUGGERS
259                if (name == "ReevaluateElites")
260                    name = "ReevaluteElites";
261                if (name == "DominateOnEqualQualities" || name == "PlusSelection" || name == "ReevaluteElites")
262                    val = ((BoolValue)val).Value;
263               
264               // if (name == "ReduceToPopulationSize")
265                    //name += "Parameter";
266
267                try
268                {
269                    Algorithm alg = (Algorithm)curr.ItemTask.Item;
270                    if (prob)
271                        alg.Problem.GetType().GetProperty(name).SetValue(alg, val);
272                    else
273                        alg.GetType().GetProperty(name).SetValue(alg, val);
274                }
275                catch (NullReferenceException e)
276                {
277                    Console.WriteLine("NullRefException: " + name + " - " + type);
278                }
279                catch(TargetException e)
280                {
281                    Console.WriteLine("TargetException: " + name + " - " + type);
282                }
283            }
284
285        }
286
287    }
288}
Note: See TracBrowser for help on using the repository browser.