Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2582 RC2 migration fixed. OKB query implemented. Preparing for OKB manager

File size: 17.7 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
22
23using HeuristicLab.Clients.Hive.WebJobManager.Services;
24using HeuristicLab.Optimization;
25using Microsoft.AspNetCore.SignalR;
26using System;
27using System.Linq;
28using System.Reflection;
29using HeuristicLab.Core;
30
31namespace HeuristicLab.Clients.Hive.WebJobManager
32{
33    /// <summary>
34    /// SignalR Hub for displaying the progress when uploading a Job.
35    /// Also used to change child distribution and priority for inner tasks.
36    /// </summary>
37    public class ProgressHub : Hub
38    {
39        private WebLoginService weblog;
40        private Guid userId;
41        private FileOpeningService fileopener;
42        private void loader()
43        {
44            weblog = WebLoginService.Instance;
45            string uid = Context.QueryString["userid"];
46            if (uid == null || uid == "" || Guid.Parse(uid) == Guid.Empty)
47            {
48                userId = Guid.Empty;
49            }
50            else
51            {
52                userId = Guid.Parse(uid);
53                fileopener = weblog.getFileOpener(userId);
54            }
55        }
56
57        /// <summary>
58        /// First message from client
59        /// </summary>
60        /// <param name="receivedString">Client message</param>
61        public void HandleMessage(string receivedString)
62        {
63            loader();
64            Clients.Caller.processMessage("Connection Established");
65            fileopener.Job.Progress.StatusChanged += runHub;
66        }
67        /// <summary>
68        /// Changes name for the current job to be created
69        /// </summary>
70        /// <param name="name">Job name</param>
71        public void ChangeNameResource(string name, string resource)
72        {
73            loader();
74            if (name != null)
75                fileopener.Job.Job.Name = name;
76            if (resource != null && resource != "")
77            {
78                fileopener.Job.Job.ResourceNames += "/" + resource;
79            }
80        }
81        /// <summary>
82        /// Toggles distribute child task for a current task
83        /// </summary>
84        /// <param name="arr">2-dimensional int array.
85        /// First dimension is depth (arr.length == 4 means the item is 4 nodes deep)
86        /// Second contains expirement info or batch info. (arr[][0] contains index to select the right experiment sub task,
87        /// arr[][1] contains index to select right batch run subtask)</param>
88        public void ToggleChild(int[][] arr)
89        {
90            loader();
91            bool change = false;
92            string name = "";
93            HiveTask current = fileopener.Job.HiveTasks.ToList()[0];
94            if (arr.Length == 0)
95            {//check if upper job
96                current.ItemTask.ComputeInParallel = !current.ItemTask.ComputeInParallel;
97                change = current.ItemTask.ComputeInParallel;
98                name = current.ItemTask.Name;
99            }
100            else
101            {
102                for (var i = 0; i < arr.Length; i++)
103                {
104                    //loop for depth
105
106                    if (i == arr.Length - 1)//end of depth loop, right current is selected
107                    {
108                        if (current.ItemTask.Item is BatchRun)
109                        {
110                            current.ChildHiveTasks[arr[i][1]].ItemTask.ComputeInParallel = !current.ChildHiveTasks[arr[i][1]].ItemTask.ComputeInParallel;
111                            change = current.ChildHiveTasks[arr[i][1]].ItemTask.ComputeInParallel;
112                            name = current.ChildHiveTasks[arr[i][1]].ItemTask.Name;
113                        }
114                        else if (current.ItemTask.Item is Experiment)
115                        {
116                            current.ChildHiveTasks[arr[i][0]].ItemTask.ComputeInParallel = !current.ChildHiveTasks[arr[i][0]].ItemTask.ComputeInParallel;
117                            change = current.ChildHiveTasks[arr[i][0]].ItemTask.ComputeInParallel;
118                            name = current.ChildHiveTasks[arr[i][0]].ItemTask.Name;
119                        }
120
121                    }
122                    else
123                    {//not deep enough, select right path
124                        if (current.ItemTask.Item is BatchRun)
125                            current = current.ChildHiveTasks[arr[i][1]]; // select right batch
126                        else if (current.ItemTask.Item is Experiment)
127                            current = current.ChildHiveTasks[arr[i][0]]; // select right sub task from experiment
128                    }
129                }
130            }
131            if (change)
132                Clients.Caller.saveComplete("Child distribution activated for " + name);
133            else
134                Clients.Caller.saveComplete("Child distribution deactivated for " + name);
135        }
136        public void changeName(int[][] arr, string name, int idname)
137        {
138           
139            if (name != "" && name != null)
140            {
141                loader();
142                string old = "";
143                HiveTask current = fileopener.Job.HiveTasks.ToList()[0];
144                if (arr.Length == 0)
145                {//check if upper job
146                    old = current.ItemTask.Name;
147                    current.ItemTask.Name = name;
148
149                }
150                else
151                {
152                    for (var i = 0; i < arr.Length; i++)
153                    {//loop for depth
154                        if (i == arr.Length - 1)
155                        {//Right depth reached, change name for current
156                            if (current.ItemTask.Item is BatchRun)
157                            {
158                                old = current.ChildHiveTasks[arr[i][1]].ItemTask.Name;
159                                current.ChildHiveTasks[arr[i][1]].ItemTask.Name = name;
160                            }
161                            else if (current.ItemTask.Item is Experiment)
162                            {
163                                old = current.ChildHiveTasks[arr[i][0]].ItemTask.Name;
164                                current.ChildHiveTasks[arr[i][0]].ItemTask.Name = name;
165                            }
166
167                        }
168                        else
169                        {//not deep enough, choose right path
170                            if (current.ItemTask.Item is BatchRun)
171                                current = current.ChildHiveTasks[arr[i][1]]; // select right batch
172                            else if (current.ItemTask.Item is Experiment)
173                                current = current.ChildHiveTasks[arr[i][0]]; // select right sub task from experiment
174                        }
175                    }
176                }
177                Clients.Caller.processName(name, idname);
178                Clients.Caller.saveComplete( old + " renamed to " + name);
179            }
180        }
181        public void changeRepit(int[][] arr, int repit)
182        {
183            loader();
184            HiveTask current = fileopener.Job.HiveTasks.ToList()[0];
185            string Name = "failed";
186            if (arr.Length == 0)
187            {//check if upper job
188                if (current.ItemTask.Item is BatchRun)
189                {
190                    var b = (BatchRun)current.ItemTask.Item;
191                    b.Repetitions = repit;
192                    Name = current.ItemTask.Name;
193                }
194
195            }
196            else
197            {
198                for (var i = 0; i < arr.Length; i++)
199                {//loop for depth
200                    if (i == arr.Length - 1)
201                    {//Right depth reached, change name for current
202                        if (current.ChildHiveTasks[arr[i][0]].ItemTask.Item is BatchRun)
203                        {
204                            var b = (BatchRun)current.ChildHiveTasks[arr[i][0]].ItemTask.Item;
205                            b.Repetitions = repit;
206                            Name = current.ChildHiveTasks[arr[i][0]].ItemTask.Name;
207                        }
208                    }
209                    else
210                    {//not deep enough, choose right path
211                        if (current.ItemTask.Item is BatchRun)
212                            current = current.ChildHiveTasks[arr[i][1]]; // select right batch
213                        else if (current.ItemTask.Item is Experiment)
214                            current = current.ChildHiveTasks[arr[i][0]]; // select right sub task from experiment
215                    }
216                }
217            }
218            if (Name == "failed")
219                Clients.Caller.createAlert("Something went wrong while changing the repititions, please try again", "danger");
220            else
221                Clients.Caller.saveComplete("Repititions changed to "+repit+ " for " + Name);
222
223        }
224        /// <summary>
225        /// Change priority for a node
226        /// </summary>
227        /// <param name="arr">2-dimensional int array.
228        /// First dimension is depth (arr.length == 4 means the item is 4 nodes deep)
229        /// Second contains expirement info or batch info. (arr[][0] contains index to select the right experiment sub task,
230        /// arr[][1] contains index to select right batch run subtask)</param>
231        /// <param name="prior">Selected priority</param>
232        public void ChangePriority(int[][] arr, int prior)
233        {
234            string name = "";
235            loader();
236            HiveTask current = fileopener.Job.HiveTasks.ToList()[0];
237            if (arr.Length == 0)
238            {//check if upper job
239                current.Task.Priority = prior;
240                name = current.ItemTask.Name;
241            }
242            else
243            {
244                for (var i = 0; i < arr.Length; i++)
245                {//loop for depth
246
247
248                    if (i == arr.Length - 1)
249                    {//Right depth reached, change priority for current
250                        if (current.ItemTask.Item is BatchRun)
251                        {
252                            current.ChildHiveTasks[arr[i][1]].Task.Priority = prior;
253                            name = current.ChildHiveTasks[arr[i][0]].ItemTask.Name;
254                        }
255                        else if (current.ItemTask.Item is Experiment)
256                        {
257                            current.ChildHiveTasks[arr[i][0]].Task.Priority = prior;
258                            name = current.ChildHiveTasks[arr[i][0]].ItemTask.Name;
259                        }
260
261                    }
262                    else
263                    {//not deep enough, choose right path
264                        if (current.ItemTask.Item is BatchRun)
265                            current = current.ChildHiveTasks[arr[i][1]]; // select right batch
266                        else if (current.ItemTask.Item is Experiment)
267                            current = current.ChildHiveTasks[arr[i][0]]; // select right sub task from experiment
268                    }
269                }
270            }
271            Clients.Caller.saveComplete("Priority changed for " + name);
272        }
273        /// <summary>
274        /// Used by event 'ProgressChanged' from current uploading Job
275        /// </summary>
276        /// <param name="sender"></param>
277        /// <param name="e"></param>
278        public void runHub(object sender, EventArgs e)
279        {
280            loader();
281            int value = 0;
282
283            switch (fileopener.Job.Progress.Status)
284            {
285                case "Connecting to server...":
286                    value = 0;
287                    break;
288                case "Uploading Job...":
289                    value = 10;
290                    break;
291                case "Uploading plugins...":
292                    value = 30;
293                    break;
294                case "Uploading tasks...":
295                    value = 50;
296                    break;
297                case "Upload finished":
298                    value = 100;
299                    break;
300                default://Tasks are uploading individually
301                    value = (int)(50 + (40 * fileopener.Job.Progress.ProgressValue));
302                    break;
303
304            }
305            //send info to client
306            Clients.Caller.processMessage(fileopener.Job.Progress.Status, value);
307
308        }
309        public void paraEdit(int[][] arr, string problem, string name, string type, string value)
310        {
311
312
313            loader();
314            HiveTask current = fileopener.Job.HiveTasks.ToList()[0];
315            if (arr.Length == 0)
316            {//check if upper job
317                if (current.ItemTask.Item is Algorithm)
318                {
319                    searchEditPara(current, problem, name, type, value);
320                }
321            }
322            else
323            {
324                for (var i = 0; i < arr.Length; i++)
325                {//loop for depth
326                    if (i == arr.Length - 1)
327                    {//Right depth reached, change priority for current
328                        if (current.ItemTask.Item is BatchRun)
329                        {
330                            current = current.ChildHiveTasks[arr[i][1]];
331                        }
332                        else if (current.ItemTask.Item is Experiment)
333                        {
334                            current = current.ChildHiveTasks[arr[i][0]];
335                        }
336                        if (current.ItemTask.Item is Algorithm)
337                        {
338                            searchEditPara(current, problem, name, type, value);
339                        }
340                    }
341                    else
342                    {//not deep enough, choose right path
343                        if (current.ItemTask.Item is BatchRun)
344                            current = current.ChildHiveTasks[arr[i][1]]; // select right batch
345                        else if (current.ItemTask.Item is Experiment)
346                            current = current.ChildHiveTasks[arr[i][0]]; // select right sub task from experiment
347                    }
348                }
349            }
350        }
351        /// <summary>
352        /// Generic parameter setter
353        /// </summary>
354        /// <param name="curr">Current task to edit </param>
355        /// <param name="problem">Problem parameter or normal parameter</param>
356        /// <param name="name">Property name</param>
357        /// <param name="type">Datatype</param>
358        /// <param name="value">New value</param>
359        private void searchEditPara(HiveTask curr, string problem, string name, string type, string value)
360        {
361            var prob = problem == "True" ? true : false;
362
363            var dattype = Type.GetType(type);
364            if (dattype == null)
365            {//Use assemblies to find correct datatype
366                foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
367                {
368                    dattype = a.GetType(type);
369                    if (dattype != null)
370                        break;
371                }
372            }
373            //find parse method
374            var parse = dattype.GetMethod("Parse", new[] { typeof(string) });
375
376            if (parse != null)
377            {
378                //Parses string to correct datatype
379                var val = parse.Invoke(null, new object[] { value });
380
381                //BUGGERS
382                // if (name == "DominateOnEqualQualities" || name == "PlusSelection" || name == "ReevaluteElites")
383                //   val = ((BoolValue)val).Value;
384
385                // if (name == "ReduceToPopulationSize")
386                //name += "Parameter";
387                if (val != null)
388                {
389                    try
390                    {
391                        IAlgorithm alg = (IAlgorithm)curr.ItemTask.Item;
392                        if (prob)
393                            alg.Problem.Parameters[name].ActualValue = (IItem)val;
394                        else
395                        {
396                            alg.Parameters[name].ActualValue = (IItem)val;
397                        }
398                        Clients.Caller.saveComplete("Parameter saved for " + name);
399                    }
400                    catch (NullReferenceException e)
401                    {
402                        Console.WriteLine("NullRefException: " + name + " - " + type);
403                    }
404                    catch (TargetException e)
405                    {
406                        Console.WriteLine("TargetException: " + name + " - " + type);
407                    }
408                    catch (NotSupportedException e)
409                    {
410                        Clients.Caller.createAlert("Parameter " + name + " could not be changed for it is a fixed parameter. All changes done in this view are not saved to Hive.", "danger");
411                    }
412                }
413                else
414                    Clients.Caller.createAlert("Format wrong for " + name + " of type " + type + ". Make sure you follow the right format pattern.", "warning");
415
416
417            }
418
419        }
420
421    }
422}
Note: See TracBrowser for help on using the repository browser.