source: branches/OaaS/HeuristicLab.Services.Optimization.Controller/Azure/DAL.cs @ 9362

Last change on this file since 9362 was 9362, checked in by fschoepp, 9 years ago

#1888:

  • Added IVisualExtensionDao to the backend representing js extension that create new viewable elements of an algorithm.
  • Started to upgrade the frontend to render those new javascript UI extensions.
File size: 15.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Services.Optimization.ControllerService.Interfaces;
6using Microsoft.WindowsAzure;
7using Microsoft.WindowsAzure.StorageClient;
8using Microsoft.WindowsAzure.ServiceRuntime;
9using System.Diagnostics;
10using HeuristicLab.Services.Optimization.ControllerService.Model;
11using HeuristicLab.Services.Optimization.ControllerService.Parsers;
12
13namespace HeuristicLab.Services.Optimization.ControllerService.Azure {
14  public static class AzureConstants {
15    public static readonly string SCENARIO_TABLE = "Scenario";
16    public static readonly string SCENARIO_BLOB_CONTAINER = "scenario";
17    public static readonly string EXPERIMENT_TABLE = "Experiment";
18    public static readonly string EXPERIMENT_BLOB_CONTAINER = "experiment";
19    public static readonly string VISUAL_BLOB_CONTAINER = "visualextensions";
20    public static readonly string CLOUD_SETTINGS_KEY = "Cloudia.WindowsAzure.Storage";
21
22   
23  }
24
25  public class ScenarioDao : IScenarioDao {   
26    public CloudTableClient TableClient { get; set; }
27
28    public bool Add(ScenarioEntity entity) {
29      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
30      TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
31      ScenarioEntity dbEntity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
32                               where e.RowKey == entity.RowKey
33                               select e).FirstOrDefault();
34      if (dbEntity != null)
35        return false;
36
37      serviceContext.AddObject(AzureConstants.SCENARIO_TABLE, entity);
38      serviceContext.SaveChanges();
39      return true;
40    }
41
42    public bool DeleteByName(string scenarioName) {
43      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
44      TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
45      ScenarioEntity entity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
46                               where e.RowKey == scenarioName
47                               select e).FirstOrDefault();
48      if (entity == null)
49        return false;
50
51      serviceContext.DeleteObject(entity);
52      serviceContext.SaveChangesWithRetries();
53      return true;
54    }
55
56    public ScenarioEntity FindByName(string scenarioName) {
57      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
58      TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
59      ScenarioEntity entity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
60                               where e.RowKey == scenarioName
61                               select e).FirstOrDefault();
62      return entity;
63    }
64
65
66    public IEnumerable<ScenarioEntity> GetAllEntities() {
67      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
68      TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
69      return (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
70             select e).AsEnumerable();
71    }
72  }
73
74  public class BlobDao : IBlobDao {
75    public CloudBlobClient BlobClient { get; set; }
76
77    public bool Add(StringEntry entry) {
78      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
79      container.CreateIfNotExist();
80      var blob = container.GetBlobReference(entry.Key);
81      blob.UploadText(entry.Text);
82      return true;
83    }
84
85    public bool DeleteByKey(string entryKey) {
86      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
87      container.CreateIfNotExist();
88      var blob = container.GetBlobReference(entryKey);
89      return blob.DeleteIfExists();
90    }
91
92    public StringEntry FindByKey(string entryKey) {
93      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
94      container.CreateIfNotExist();
95      var blob = container.GetBlobReference(entryKey);
96      return new StringEntry() { Key = entryKey, Text = blob.DownloadText() };     
97    }
98  }
99
100  internal sealed class ExperimentEntity : TableServiceEntity {
101    public ExperimentEntity() {
102    }
103
104    public ExperimentEntity(string user, string experimentName, string experimentId, string experimentUrl) {
105      PartitionKey = "ScenarioPartition";
106      RowKey = user + "_" + experimentName;     
107      User = user;
108      ExperimentId = experimentId;
109      ExperimentJsonUrl = experimentUrl;
110    }
111
112    public string ExperimentId { get; set; }
113
114    public string User { get; set; }
115
116    public string ExperimentJsonUrl { get; set; }
117
118  }
119
120  public class ExperimentDao : IExperimentDao {
121    public CloudBlobClient BlobClient { get; set; }
122    public CloudTableClient TableClient { get; set; }
123
124    public bool Add(string username, Model.Experiment experiment) {
125      if (FindByName(username, experiment.Name) != null)
126        return false;
127
128      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
129      container.CreateIfNotExist();
130      // For now we store it as JSON element in the blob store
131      var experimentJson = AlgorithmConverter.ConvertExperimentToJson(experiment);
132      Guid experimentJsonGuid = Guid.NewGuid();
133      var experimentJsonId = experiment.Name + "_" + experimentJsonGuid.ToString();
134      experimentJson["nodeId"] = experimentJsonId.ToString();
135      var blob = container.GetBlobReference(experimentJsonId);     
136      blob.UploadText(experimentJson.ToString());
137      experiment.Id = experimentJsonId;
138
139      TableServiceContext serviceContext = TableClient.GetDataServiceContext();     
140      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
141      var entity = new ExperimentEntity(username, experiment.Name, experiment.Id, blob.Uri.ToString());
142      serviceContext.AddObject(AzureConstants.EXPERIMENT_TABLE, entity);
143      serviceContext.SaveChangesWithRetries();
144      return true;
145    }
146
147    public bool Update(string username, Experiment experiment) {
148      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
149      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
150      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
151                    where e.ExperimentId == experiment.Id
152                    select e).FirstOrDefault();
153      if (entity == null) {
154        return false;
155      }
156
157      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
158      container.CreateIfNotExist();
159      var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
160      var experimentJson = AlgorithmConverter.ConvertExperimentToJson(experiment).ToString();
161      blob.UploadText(experimentJson);
162      return true;
163    }
164
165    public bool Delete(string username, string experimentId) {
166      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
167      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
168      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
169                    where e.ExperimentId == experimentId
170                    select e).FirstOrDefault();
171
172      if (entity == null)
173        return false;
174
175      if (entity.ExperimentJsonUrl != null) {
176        CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
177        container.CreateIfNotExist();
178        var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
179        blob.DeleteIfExists();
180      }
181
182      serviceContext.DeleteObject(entity);
183      serviceContext.SaveChangesWithRetries();
184      return true;
185    }
186
187    public bool DeleteByName(string username, string experiment) {
188      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
189      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
190      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
191                    where e.RowKey == (username + "_" + experiment)
192                    select e).FirstOrDefault();
193     
194      if (entity == null)
195        return false;
196
197      if (entity.ExperimentJsonUrl != null) {
198        CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
199        container.CreateIfNotExist();
200        var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
201        blob.DeleteIfExists();
202      }
203
204      serviceContext.DeleteObject(entity);
205      serviceContext.SaveChangesWithRetries();
206      return true;
207    }
208
209    public Model.Experiment FindByName(string username, string experiment) {
210      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
211      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
212      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
213                    where e.RowKey == (username + "_" + experiment)
214                    select e).FirstOrDefault();
215     
216      if (entity == null) {
217        return null;
218      }
219
220      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
221      container.CreateIfNotExist();
222      var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
223      return AlgorithmConverter.ConvertJsonToExperiment(blob.DownloadText());
224    }
225
226    //private Experiment Convert(ExperimentEntity entity, string entityJson) {
227    //  // TODO: Read the whole experiment, not just the names!
228    //  var exp = new Experiment() { Name = entity.RowKey.Split('_')[1] };
229    //  foreach (var scenarioName in entity.Algorithms.Split(','))
230    //    exp.Algorithm.Add(new Algorithm() { Name = scenarioName });
231    //  return exp;
232    //}
233
234    public IEnumerable<Model.Experiment> GetExperiments(string user, bool namesOnly=false) {
235      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
236      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
237      var entites = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
238                    where e.User == user
239                    select e).ToList();
240      var experiments = new List<Experiment>();
241      if (namesOnly) {
242        return (from e in entites select new Model.Experiment() { Id = e.ExperimentId, Name = e.RowKey.Split('_')[1] });
243      }
244      else {
245        CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
246        container.CreateIfNotExist();
247        foreach (var entity in entites) {
248          var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
249          experiments.Add(AlgorithmConverter.ConvertJsonToExperiment(blob.DownloadText()));
250        }
251        return experiments;
252      }
253    }
254
255
256    public Experiment GetExperimentByName(string username, string scenario) {
257      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
258      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
259      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
260                      where e.RowKey == username + "_" + scenario
261                      select e).FirstOrDefault();
262      if (entity == null || entity.ExperimentJsonUrl == null) {
263        return null;
264      }
265
266      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
267      container.CreateIfNotExist();
268      var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
269      var exp = AlgorithmConverter.ConvertJsonToExperiment(blob.DownloadText());
270      return exp;
271    }
272
273
274    public Experiment GetExperimentById(User user, string nodeId) {
275      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
276      TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE);
277      var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE)
278                    where e.ExperimentId == nodeId
279                    select e).FirstOrDefault();
280      if (entity == null || entity.ExperimentJsonUrl == null) {
281        return null;
282      }
283
284      if (entity.User != user.Username)
285        return null;
286
287      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.EXPERIMENT_BLOB_CONTAINER);
288      container.CreateIfNotExist();
289      var blob = container.GetBlobReference(entity.ExperimentJsonUrl);
290      return AlgorithmConverter.ConvertJsonToExperiment(blob.DownloadText());
291    }
292  }
293
294  public class VisualExtensionDao : IVisualExtensionDao {
295    public CloudBlobClient BlobClient { get; set; }
296
297    public bool Add(string algorithmId, string script) {
298      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.VISUAL_BLOB_CONTAINER);
299      container.CreateIfNotExist();
300      var blob = container.GetBlobReference(algorithmId);
301      blob.UploadText(script);
302      return true;
303    }
304
305    public bool DeleteById(string algorithmId) {
306      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.VISUAL_BLOB_CONTAINER);
307      container.CreateIfNotExist();
308      var blob = container.GetBlobReference(algorithmId);
309      return blob.DeleteIfExists();
310    }
311
312    public string FindById(string algorithmId) {
313      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.VISUAL_BLOB_CONTAINER);
314      container.CreateIfNotExist();
315      var blob = container.GetBlobReference(algorithmId);
316      return blob.DownloadText();
317    }
318  }
319
320  public class AzureDataAccessLayer : IDataAccessLayer {
321    private IScenarioDao scenarioDao;
322    private IBlobDao blobDao;
323    private IExperimentDao expDao;
324    private IVisualExtensionDao visualDao;
325
326    private CloudStorageAccount storageAccount;
327
328    private CloudStorageAccount StorageAccount {
329      get {
330        if (storageAccount == null) {
331          try {
332            storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(AzureConstants.CLOUD_SETTINGS_KEY));
333          }
334          catch (Exception ex) {
335            Trace.WriteLine(ex.Message);
336            storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=optimizationstorage1;AccountKey=n7Leom8ZFWkof/VQ2a4aRSvwOlX+Gwr3uojQF9CFJw1osmGCV0WwaNC8s7nkZ+qteLduAgW2l75WFpbXrkvG4Q==");
337          }
338        }
339        return storageAccount;
340      }     
341    }
342
343    public IScenarioDao ScenarioDao {
344      get {
345        if (scenarioDao == null) {
346          scenarioDao = new ScenarioDao() { TableClient = StorageAccount.CreateCloudTableClient() };
347        }
348        return scenarioDao;
349      }
350    }
351
352    public IBlobDao BlobDao {
353      get {
354        if (blobDao == null) {
355          blobDao = new BlobDao() { BlobClient = StorageAccount.CreateCloudBlobClient() };
356        }
357        return blobDao;
358      }
359    }
360
361    public IExperimentDao ExperimentDao {
362      get {
363        if (expDao == null) {
364          expDao = new ExperimentDao() { TableClient = StorageAccount.CreateCloudTableClient(), BlobClient = StorageAccount.CreateCloudBlobClient() };
365        }
366        return expDao;
367      }
368    }
369
370
371    public IVisualExtensionDao VisualExtensionDao {
372      get {
373        if (visualDao == null) {
374          visualDao = new VisualExtensionDao() { BlobClient = StorageAccount.CreateCloudBlobClient() };
375        }
376        return visualDao;
377      }
378    }
379  }
380}
Note: See TracBrowser for help on using the repository browser.