1 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Optimization;
|
---|
4 | using Microsoft.AspNet.Hosting;
|
---|
5 | using Microsoft.AspNet.Mvc;
|
---|
6 | using System;
|
---|
7 | using System.Collections.Generic;
|
---|
8 | using System.IO;
|
---|
9 | using System.Linq;
|
---|
10 | using System.Threading;
|
---|
11 | using System.Threading.Tasks;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Clients.Hive.WebJobManager.Services
|
---|
14 | {
|
---|
15 | /// <summary>
|
---|
16 | /// Used for opening a file and uploading this to Hive.
|
---|
17 | /// Saves all relevant Job information needed for uploading.
|
---|
18 | /// Outsourced to selectedJob keep the current job loaded
|
---|
19 | /// </summary>
|
---|
20 | public class FileOpeningService
|
---|
21 | {
|
---|
22 | public Guid UserId { get; set; }
|
---|
23 |
|
---|
24 | private WebLoginService weblog;
|
---|
25 | public RefreshableJob Job { get; set; }
|
---|
26 | public FileOpeningViewModel vm { get; set; }
|
---|
27 | public IHostingEnvironment env { get; set; }
|
---|
28 | public List<Guid> previousids {
|
---|
29 | get; set; }
|
---|
30 | public List<int> previousLogs { get; set; }
|
---|
31 |
|
---|
32 |
|
---|
33 | public FileOpeningService(Guid u)
|
---|
34 | {
|
---|
35 | weblog = WebLoginService.Instance;
|
---|
36 | UserId = u;
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | /// Clears loaded jobs and start new one
|
---|
42 | /// </summary>
|
---|
43 | public void NewModel()
|
---|
44 | {
|
---|
45 | vm = new FileOpeningViewModel();
|
---|
46 | Job = new RefreshableJob();
|
---|
47 |
|
---|
48 | Job.Job.Name = "init";
|
---|
49 |
|
---|
50 | }
|
---|
51 | /// <summary>
|
---|
52 | /// Attaches tasks from viewmodel to job
|
---|
53 | /// </summary>
|
---|
54 | public void setTasks()
|
---|
55 | {
|
---|
56 |
|
---|
57 | Job.HiveTasks.Add(vm.SelectedTask);
|
---|
58 | }
|
---|
59 | /// <summary>
|
---|
60 | /// Refresh all info from a job from the server
|
---|
61 | /// </summary>
|
---|
62 | public void refreshJob()
|
---|
63 | {
|
---|
64 | weblog.getClientWeb(UserId).LoadJob(Job);
|
---|
65 | }
|
---|
66 | /// <summary>
|
---|
67 | /// Adds loaded job from vm onto server
|
---|
68 | /// </summary>
|
---|
69 | /// <returns>Job being uploaded</returns>
|
---|
70 | public RefreshableJob AddCurrentModelToHive()
|
---|
71 | {
|
---|
72 | if (vm != null)
|
---|
73 | {
|
---|
74 | var clientWeb = weblog.getClientWeb(UserId);
|
---|
75 | clientWeb.CurrentEnv = env;
|
---|
76 | clientWeb.Refresh();
|
---|
77 | clientWeb.StartJob((ex) => { throw ex; }, Job, CancellationToken.None);
|
---|
78 | return Job;
|
---|
79 | }
|
---|
80 | return null;
|
---|
81 | }
|
---|
82 | public void SaveToFile(string file)
|
---|
83 | {
|
---|
84 | if(vm != null)
|
---|
85 | {
|
---|
86 | var uploads = Path.Combine(env.WebRootPath, "uploads", weblog.getServiceLocator(UserId).getHiveServiceClient().ClientCredentials.UserName.UserName,
|
---|
87 | "HiveChanged");
|
---|
88 | Directory.CreateDirectory(uploads);
|
---|
89 | var cont = Job.HiveTasks.First().ItemTask.Item;
|
---|
90 | if (!file.EndsWith(".hl"))
|
---|
91 | file += ".hl";
|
---|
92 | var fileloc = uploads + "\\" + file;
|
---|
93 | if(cont is Experiment)
|
---|
94 | ContentManager.Save((Experiment)cont, fileloc, false);
|
---|
95 | else if(cont is BatchRun)
|
---|
96 | ContentManager.Save((BatchRun)cont, fileloc, false);
|
---|
97 | else if(cont is Algorithm)
|
---|
98 | {
|
---|
99 | IAlgorithm t = (IAlgorithm)Job.HiveTasks.First().ItemTask.Item;
|
---|
100 | ContentManager.Save((IStorableContent)t, fileloc, false);
|
---|
101 | }
|
---|
102 |
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | }
|
---|
107 | }
|
---|