Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Controllers/JobController.cs @ 13714

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

#2582 Implement graphs(Pie and line per task) + live job overview with SignalR

File size: 12.4 KB
Line 
1using Microsoft.AspNet.Mvc;
2using HeuristicLab.Clients.Hive.WebJobManager.Services;
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Threading.Tasks;
7using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
8using System.ServiceModel.Security;
9using Microsoft.AspNet.Http;
10using System.IO;
11using Microsoft.Net.Http.Headers;
12using Microsoft.AspNet.Hosting;
13using HeuristicLab.Common;
14using HeuristicLab.Core;
15using HeuristicLab.Optimization;
16using System.Threading;
17
18namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
19{
20    public class JobController : Controller
21    {
22        private HiveServiceClient client;
23        private JobViewModel vm;
24        private IHostingEnvironment _environment;
25
26        public JobController(IHostingEnvironment env)
27        {
28            HiveServiceLocatorWebManagerService hiveServiceLocator = (HiveServiceLocatorWebManagerService)HiveServiceLocatorWebManagerService.Instance;
29            client = hiveServiceLocator.getHiveServiceClient();
30            vm = new JobViewModel();
31
32
33            _environment = env;
34        }
35        #region Jobs
36        public IActionResult Index()
37        {
38            try
39            {
40                HiveClientWeb.Instance.Refresh();
41
42                vm.userJobs = HiveClientWeb.Instance.Jobs.ToList();
43            }
44            catch (Exception e)
45            {
46                if (e is MessageSecurityException || e is InvalidOperationException)
47                {
48                    HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
49                    return RedirectToAction("Index", "Home");
50                }
51                throw;
52            }
53            ViewBag.Title = "Jobs";
54            return View(vm);
55        }
56        public IActionResult Selected(Guid id)
57        {
58            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
59            {
60                HiveClientWeb.Instance.Refresh();
61               
62                vm.userJobs = HiveClientWeb.Instance.Jobs.ToList();
63                foreach(var j in vm.userJobs)
64                {
65                    if(j.Id == id)
66                    {
67                        vm.selectedJob = j;
68                    }
69                }
70                vm.selectedJob.RefreshAutomatically = true;
71                 HiveClientWeb.LoadJob(vm.selectedJob);
72                FileOpeningService.Instance.NewModel();
73                FileOpeningService.Instance.Job = vm.selectedJob;
74                ViewBag.Title = vm.selectedJob.Job.Name + " - Jobs";
75                return View("Index", vm);
76            }
77            else
78            {
79                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
80                return RedirectToAction("Index", "Home");
81            }
82        }
83        public IActionResult Delete(Guid id)// delete a job
84        {
85            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
86            {
87
88                vm.message = client.GetJob(id).Name + " deleted";
89                client.DeleteJob(id);
90                HiveClientWeb.Instance.Refresh();
91
92                vm.userJobs = HiveClientWeb.Instance.Jobs.ToList();
93                ViewBag.Title = "Jobs";
94                return View("Index", vm);
95            }
96            else
97            {
98                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
99                return RedirectToAction("Index", "Home");
100            }
101        }
102        #endregion
103
104        #region Uploads
105        public IActionResult Uploads()
106        {
107            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
108            {
109                UploadedJobViewModel upper = new UploadedJobViewModel();
110                fillUploadsPaths(upper, -1);
111
112                ViewBag.Title = "Uploaded files";
113                return View("Uploads", upper);
114            }
115            else
116            {
117                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
118                return RedirectToAction("Index", "Home");
119            }
120        }
121        public IActionResult UploadDir(int index)
122        {
123            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
124            {
125                UploadedJobViewModel upper = new UploadedJobViewModel();
126                fillUploadsPaths(upper, index);
127
128                ViewBag.Title = "Uploaded files";
129                return View("Uploads", upper);
130            }
131            else
132            {
133                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
134                return RedirectToAction("Index", "Home");
135            }
136        }
137        private void fillUploadsPaths(UploadedJobViewModel vm, int index)
138        {
139            var tempdex = index; //Fix when maps gets deleted
140            var start = Path.Combine(_environment.WebRootPath, "uploads", client.ClientCredentials.UserName.UserName);
141            var dirs = Directory.GetDirectories(start);
142            foreach (string dir in dirs)
143            {
144                if (Directory.GetFiles(dir).Length == 0 && Directory.GetDirectories(dir).Length == 0)
145                {
146                    Directory.Delete(dir, false);
147                    tempdex = -1;
148                }
149                else {
150                    vm.FullDatePaths.Add(dir);
151                    var temp = dir.Split('\\');
152                    vm.DisplayDatePaths.Add(temp[temp.Length - 1]);
153                }
154            }
155            if (tempdex != -1)
156            {
157                vm.SelectedIndex = tempdex;
158                dirs = Directory.GetFiles(vm.FullDatePaths[tempdex]);
159                foreach (string dir in dirs)
160                {
161                    vm.FullFilesPaths.Add(dir);
162                    var temp = dir.Split('\\');
163                    vm.DisplayFilesPaths.Add(temp[temp.Length - 1]);
164                }
165            }
166        }
167        public IActionResult DeleteFile(int index, int filedex)
168        {
169            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
170            {
171                UploadedJobViewModel upper = new UploadedJobViewModel();
172                fillUploadsPaths(upper, index);
173                System.IO.File.Delete(upper.FullFilesPaths[filedex]);
174                var message = upper.DisplayFilesPaths[filedex] + " has been deleted";
175
176                upper = new UploadedJobViewModel();
177                fillUploadsPaths(upper, index);
178                upper.message = message;
179                ViewBag.Title = "Uploaded files";
180
181                return View("Uploads", upper);
182            }
183            else
184            {
185                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
186                return RedirectToAction("Index", "Home");
187            }
188        }
189
190        public IActionResult OpenFile(int index, int filedex)
191        {
192            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
193            {
194                UploadedJobViewModel upper = new UploadedJobViewModel();
195                fillUploadsPaths(upper, index);
196
197                FileOpeningService serve = FileOpeningService.Instance;
198                serve.NewModel();
199                serve.env = _environment;
200
201                var ioptimizer = ContentManager.Load(upper.FullFilesPaths[filedex]);
202
203                serve.vm.SelectedTask = new OptimizerHiveTask((IOptimizer)ioptimizer);
204                if (serve.vm.SelectedTask.ItemTask.Item is IAlgorithm)
205                {
206                    serve.vm.SelectedAlgorithm = (IAlgorithm)serve.vm.SelectedTask.ItemTask.Item;
207                }
208                else if (serve.vm.SelectedTask.ItemTask.Item is BatchRun)
209                {
210                    serve.vm.SelectedBatchRun = (BatchRun)serve.vm.SelectedTask.ItemTask.Item;
211                }
212                else if (serve.vm.SelectedTask.ItemTask.Item is Experiment)
213                {
214                    serve.vm.SelectedExperiment = (Experiment)serve.vm.SelectedTask.ItemTask.Item;
215                }
216                serve.setTasks();
217                ViewBag.Title = serve.vm.SelectedTask.ItemTask.Name + " - Open file";
218
219                return View("OpenFile", serve.vm);
220            }
221            else
222            {
223                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
224                return RedirectToAction("Index", "Home");
225            }
226        }
227
228        public IActionResult AddToHive()
229        {
230            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
231            {
232                    var job = FileOpeningService.Instance.AddCurrentModelToHive();
233                    while (job.Progress.ProgressValue != 1)
234                    { }
235
236                    Thread.Sleep(1000);
237                    job.Progress.Status = "Upload finished";
238                    Thread.Sleep(2000);
239                    return RedirectToAction("Index", "Job");
240            }
241            else
242            {
243                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
244                return RedirectToAction("Index", "Home");
245            }
246        }
247        //TODO Work in progress
248        /* public async Task<IActionResult> DownloadFile(int index, int filedex)
249         {
250             if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
251             {
252                 UploadedJobViewModel upper = new UploadedJobViewModel();
253                 fillUploadsPaths(upper, index);
254
255                HttpContext.Response.ContentType = "APPLICATION/OCTET-STREAM";
256                 String Header = "Attachment; Filename=" + upper.DisplayFilesPaths[filedex];
257                 HttpContext.Response.Headers.Add("Content-Disposition", Header);
258                 System.IO.FileInfo Dfile = new System.IO.FileInfo(upper.FullFilesPaths[filedex]);
259                 await HttpContext.Response.WriteAsync(Dfile.FullName);
260                 //HttpContext.Response.End();
261
262                 var message = upper.DisplayFilesPaths[filedex] + " has been downloaded";
263
264                 upper.message = message;
265                 ViewBag.Title = "Downloaded file";
266
267                 return View("Uploads", upper);
268             }
269             else
270             {
271                 HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
272                 return RedirectToAction("Index", "Home");
273             }
274         }*/
275
276        #endregion
277
278        #region Uploader
279        public IActionResult Uploader()
280        {
281            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
282            {
283                ViewBag.Title = "Upload Jobs";
284                ViewBag.Name = client.ClientCredentials.UserName.UserName;
285                return View("Uploader");
286            }
287            else
288            {
289                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
290                return RedirectToAction("Index", "Home");
291            }
292        }
293        [HttpPost]
294        public async Task<IActionResult> Uploader(ICollection<IFormFile> files, string directory)
295        {
296
297            UploadedJobViewModel upper = new UploadedJobViewModel();
298            var uploads = Path.Combine(_environment.WebRootPath, "uploads", client.ClientCredentials.UserName.UserName,
299                directory);
300            Directory.CreateDirectory(uploads);
301            foreach (var file in files)
302            {
303                if (file.Length > 0)
304                {
305                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
306                    await file.SaveAsAsync(Path.Combine(uploads, fileName));
307                    // var ioptimizer = ContentManager.Load(Path.Combine(uploads, fileName));
308                    //OptimizerHiveTask task = new OptimizerHiveTask((IOptimizer)ioptimizer);
309                    //upper.Tasks.Add(task);
310                }
311            }
312            ViewBag.Title = "Upload complete";
313            return RedirectToAction("Uploads", "Job");
314
315        }
316        #endregion
317    }
318}
Note: See TracBrowser for help on using the repository browser.