Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2582 Optimizing open file view by cutting down batches to 1. Logging system in navbar. Start authenticationCheck

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