Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2582 Distribution childs and priority done. Display current jobs and start graphs

File size: 12.2 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                ViewBag.Title = vm.selectedJob.Job.Name + " - Jobs";
73                return View("Index", vm);
74            }
75            else
76            {
77                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
78                return RedirectToAction("Index", "Home");
79            }
80        }
81        public IActionResult Delete(Guid id)// delete a job
82        {
83            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
84            {
85
86                vm.message = client.GetJob(id).Name + " deleted";
87                client.DeleteJob(id);
88                HiveClientWeb.Instance.Refresh();
89
90                vm.userJobs = HiveClientWeb.Instance.Jobs.ToList();
91                ViewBag.Title = "Jobs";
92                return View("Index", vm);
93            }
94            else
95            {
96                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
97                return RedirectToAction("Index", "Home");
98            }
99        }
100        #endregion
101
102        #region Uploads
103        public IActionResult Uploads()
104        {
105            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
106            {
107                UploadedJobViewModel upper = new UploadedJobViewModel();
108                fillUploadsPaths(upper, -1);
109
110                ViewBag.Title = "Uploaded files";
111                return View("Uploads", upper);
112            }
113            else
114            {
115                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
116                return RedirectToAction("Index", "Home");
117            }
118        }
119        public IActionResult UploadDir(int index)
120        {
121            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
122            {
123                UploadedJobViewModel upper = new UploadedJobViewModel();
124                fillUploadsPaths(upper, index);
125
126                ViewBag.Title = "Uploaded files";
127                return View("Uploads", upper);
128            }
129            else
130            {
131                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
132                return RedirectToAction("Index", "Home");
133            }
134        }
135        private void fillUploadsPaths(UploadedJobViewModel vm, int index)
136        {
137            var tempdex = index; //Fix when maps gets deleted
138            var start = Path.Combine(_environment.WebRootPath, "uploads", client.ClientCredentials.UserName.UserName);
139            var dirs = Directory.GetDirectories(start);
140            foreach (string dir in dirs)
141            {
142                if (Directory.GetFiles(dir).Length == 0 && Directory.GetDirectories(dir).Length == 0)
143                {
144                    Directory.Delete(dir, false);
145                    tempdex = -1;
146                }
147                else {
148                    vm.FullDatePaths.Add(dir);
149                    var temp = dir.Split('\\');
150                    vm.DisplayDatePaths.Add(temp[temp.Length - 1]);
151                }
152            }
153            if (tempdex != -1)
154            {
155                vm.SelectedIndex = tempdex;
156                dirs = Directory.GetFiles(vm.FullDatePaths[tempdex]);
157                foreach (string dir in dirs)
158                {
159                    vm.FullFilesPaths.Add(dir);
160                    var temp = dir.Split('\\');
161                    vm.DisplayFilesPaths.Add(temp[temp.Length - 1]);
162                }
163            }
164        }
165        public IActionResult DeleteFile(int index, int filedex)
166        {
167            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
168            {
169                UploadedJobViewModel upper = new UploadedJobViewModel();
170                fillUploadsPaths(upper, index);
171                System.IO.File.Delete(upper.FullFilesPaths[filedex]);
172                var message = upper.DisplayFilesPaths[filedex] + " has been deleted";
173
174                upper = new UploadedJobViewModel();
175                fillUploadsPaths(upper, index);
176                upper.message = message;
177                ViewBag.Title = "Uploaded files";
178
179                return View("Uploads", upper);
180            }
181            else
182            {
183                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
184                return RedirectToAction("Index", "Home");
185            }
186        }
187
188        public IActionResult OpenFile(int index, int filedex)
189        {
190            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
191            {
192                UploadedJobViewModel upper = new UploadedJobViewModel();
193                fillUploadsPaths(upper, index);
194
195                FileOpeningService serve = FileOpeningService.Instance;
196                serve.NewModel();
197                serve.env = _environment;
198
199                var ioptimizer = ContentManager.Load(upper.FullFilesPaths[filedex]);
200
201                serve.vm.SelectedTask = new OptimizerHiveTask((IOptimizer)ioptimizer);
202                if (serve.vm.SelectedTask.ItemTask.Item is IAlgorithm)
203                {
204                    serve.vm.SelectedAlgorithm = (IAlgorithm)serve.vm.SelectedTask.ItemTask.Item;
205                }
206                else if (serve.vm.SelectedTask.ItemTask.Item is BatchRun)
207                {
208                    serve.vm.SelectedBatchRun = (BatchRun)serve.vm.SelectedTask.ItemTask.Item;
209                }
210                else if (serve.vm.SelectedTask.ItemTask.Item is Experiment)
211                {
212                    serve.vm.SelectedExperiment = (Experiment)serve.vm.SelectedTask.ItemTask.Item;
213                }
214                serve.setTasks();
215                ViewBag.Title = serve.vm.SelectedTask.ItemTask.Name + " - Open file";
216
217                return View("OpenFile", serve.vm);
218            }
219            else
220            {
221                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
222                return RedirectToAction("Index", "Home");
223            }
224        }
225
226        public IActionResult AddToHive()
227        {
228            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
229            {
230                    var job = FileOpeningService.Instance.AddCurrentModelToHive();
231                    while (job.Progress.ProgressValue != 1)
232                    { }
233
234                    Thread.Sleep(1000);
235                    job.Progress.Status = "Upload finished";
236                    Thread.Sleep(2000);
237                    return RedirectToAction("Index", "Job");
238            }
239            else
240            {
241                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
242                return RedirectToAction("Index", "Home");
243            }
244        }
245        //TODO Work in progress
246        /* public async Task<IActionResult> DownloadFile(int index, int filedex)
247         {
248             if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
249             {
250                 UploadedJobViewModel upper = new UploadedJobViewModel();
251                 fillUploadsPaths(upper, index);
252
253                HttpContext.Response.ContentType = "APPLICATION/OCTET-STREAM";
254                 String Header = "Attachment; Filename=" + upper.DisplayFilesPaths[filedex];
255                 HttpContext.Response.Headers.Add("Content-Disposition", Header);
256                 System.IO.FileInfo Dfile = new System.IO.FileInfo(upper.FullFilesPaths[filedex]);
257                 await HttpContext.Response.WriteAsync(Dfile.FullName);
258                 //HttpContext.Response.End();
259
260                 var message = upper.DisplayFilesPaths[filedex] + " has been downloaded";
261
262                 upper.message = message;
263                 ViewBag.Title = "Downloaded file";
264
265                 return View("Uploads", upper);
266             }
267             else
268             {
269                 HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
270                 return RedirectToAction("Index", "Home");
271             }
272         }*/
273
274        #endregion
275
276        #region Uploader
277        public IActionResult Uploader()
278        {
279            if (((HiveServiceLocatorWebManagerService)(HiveServiceLocatorWebManagerService.Instance)).CheckLogin())
280            {
281                ViewBag.Title = "Upload Jobs";
282                ViewBag.Name = client.ClientCredentials.UserName.UserName;
283                return View("Uploader");
284            }
285            else
286            {
287                HiveServiceLocatorWebManagerService.SetLoginErrorMessage();
288                return RedirectToAction("Index", "Home");
289            }
290        }
291        [HttpPost]
292        public async Task<IActionResult> Uploader(ICollection<IFormFile> files, string directory)
293        {
294
295            UploadedJobViewModel upper = new UploadedJobViewModel();
296            var uploads = Path.Combine(_environment.WebRootPath, "uploads", client.ClientCredentials.UserName.UserName,
297                directory);
298            Directory.CreateDirectory(uploads);
299            foreach (var file in files)
300            {
301                if (file.Length > 0)
302                {
303                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
304                    await file.SaveAsAsync(Path.Combine(uploads, fileName));
305                    // var ioptimizer = ContentManager.Load(Path.Combine(uploads, fileName));
306                    //OptimizerHiveTask task = new OptimizerHiveTask((IOptimizer)ioptimizer);
307                    //upper.Tasks.Add(task);
308                }
309            }
310            ViewBag.Title = "Upload complete";
311            return RedirectToAction("Uploads", "Job");
312
313        }
314        #endregion
315    }
316}
Note: See TracBrowser for help on using the repository browser.