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