private static Tuple <ApplicationClientMetadata, ApplicationExtraMetadata> GetTransferMetadata(ApplicationSettings appSettings)
 {
     return(new Tuple <ApplicationClientMetadata, ApplicationExtraMetadata>(
                new ApplicationClientMetadata
     {
         ApplicationID = appSettings.ApplicationID,
         EventHubInteractionConnectionString = appSettings.InterEventHubSendConnectionString,
         EventHubObservationConnectionString = appSettings.ObserEventHubSendConnectionString,
         IsExplorationEnabled = appSettings.IsExplorationEnabled,
         ModelBlobUri = appSettings.ModelBlobUri,
         TrainArguments = appSettings.TrainArguments,
         AppInsightsKey = appSettings.AppInsightsKey,
         InitialExplorationEpsilon = appSettings.InitialExplorationEpsilon
     },
                new ApplicationExtraMetadata
     {
         AzureResourceGroupName = appSettings.AzureResourceGroupName,
         DecisionType = appSettings.DecisionType,
         ExperimentalUnitDuration = appSettings.ExperimentalUnitDuration,
         SubscriptionId = appSettings.SubscriptionId,
         ModelId = appSettings.ModelId,
         TrainFrequency = appSettings.TrainFrequency,
         SettingsTokenUri1 = appSettings.SettingsTokenUri1,
         SettingsTokenUri2 = appSettings.SettingsTokenUri2
     }
                ));
 }
        public static void CreateSettingsBlobIfNotExists(out string clSASTokenUri, out string webSASTokenUri)
        {
            clSASTokenUri  = null;
            webSASTokenUri = null;

            var telemetry = new TelemetryClient();

            var sasPolicy = new SharedAccessBlobPolicy
            {
                Permissions            = SharedAccessBlobPermissions.Read,
                SharedAccessStartTime  = DateTime.UtcNow.AddMinutes(-1),
                SharedAccessExpiryTime = DateTime.UtcNow.AddYears(1)
            };

            string azureStorageConnectionString = ConfigurationManager.AppSettings[AKConnectionString];
            var    storageAccount        = CloudStorageAccount.Parse(azureStorageConnectionString);
            var    blobClient            = storageAccount.CreateCloudBlobClient();
            var    settingsBlobContainer = blobClient.GetContainerReference(ApplicationBlobConstants.SettingsContainerName);

            try
            {
                var modelBlobContainer = blobClient.GetContainerReference(ApplicationBlobConstants.ModelContainerName);

                var clientSettingsBlob = settingsBlobContainer.GetBlockBlobReference(ApplicationBlobConstants.LatestClientSettingsBlobName);
                var extraSettingsBlob  = settingsBlobContainer.GetBlockBlobReference(ApplicationBlobConstants.LatestExtraSettingsBlobName);
                if (!clientSettingsBlob.Exists() || !extraSettingsBlob.Exists())
                {
                    telemetry.TrackTrace("Settings blob not found, creating new one.");

                    settingsBlobContainer.CreateIfNotExists();
                    modelBlobContainer.CreateIfNotExists();

                    // Create an empty model blob to generate SAS token for
                    var modelBlob = modelBlobContainer.GetBlockBlobReference(ApplicationBlobConstants.LatestModelBlobName);
                    modelBlob.UploadText(string.Empty);
                    var modelSASToken = modelBlob.GetSharedAccessSignature(sasPolicy);

                    var appSettings = new ApplicationSettings
                    {
                        ApplicationID                     = ConfigurationManager.AppSettings[AKAzureResourceGroup],
                        AzureResourceGroupName            = ConfigurationManager.AppSettings[AKAzureResourceGroup],
                        ConnectionString                  = ConfigurationManager.AppSettings[AKConnectionString],
                        ExperimentalUnitDuration          = Convert.ToInt32(ConfigurationManager.AppSettings[AKExpUnitDuration]),
                        InterEventHubSendConnectionString = ConfigurationManager.AppSettings[AKInterEHSendConnString],
                        ObserEventHubSendConnectionString = ConfigurationManager.AppSettings[AKObserEHSendConnString],
                        IsExplorationEnabled              = true,
                        SubscriptionId                    = ConfigurationManager.AppSettings[AKSubscriptionId],
                        DecisionType              = DecisionType.MultiActions, // TODO: update depending on deployment option
                        ModelId                   = ApplicationSettingConstants.UseLatestModelSetting,
                        TrainArguments            = ConfigurationManager.AppSettings[AKTrainArguments],
                        TrainFrequency            = TrainFrequency.High, // TODO: update depending on deployment option
                        ModelBlobUri              = modelBlob.Uri + modelSASToken,
                        AppInsightsKey            = ConfigurationManager.AppSettings[AKAppInsightsKey],
                        InitialExplorationEpsilon = Convert.ToSingle(ConfigurationManager.AppSettings[AKInitialExplorationEpsilon])
                    };
                    UpdateMetadata(clientSettingsBlob, extraSettingsBlob, appSettings);

                    var clSASToken  = clientSettingsBlob.GetSharedAccessSignature(sasPolicy);
                    var webSASToken = clientSettingsBlob.GetSharedAccessSignature(sasPolicy);

                    clSASTokenUri  = clientSettingsBlob.Uri + clSASToken;
                    webSASTokenUri = clientSettingsBlob.Uri + webSASToken;

                    appSettings.SettingsTokenUri1 = clSASTokenUri;
                    appSettings.SettingsTokenUri2 = webSASTokenUri;
                    UpdateMetadata(clientSettingsBlob, extraSettingsBlob, appSettings);

                    telemetry.TrackTrace($"Model blob uri: {appSettings.ModelBlobUri}");
                }
                else
                {
                    telemetry.TrackTrace("Settings blob already exists, skipping.");
                    var extraMetadata = JsonConvert.DeserializeObject <ApplicationExtraMetadata>(extraSettingsBlob.DownloadText());
                    clSASTokenUri  = extraMetadata.SettingsTokenUri1;
                    webSASTokenUri = extraMetadata.SettingsTokenUri2;
                }

                telemetry.TrackTrace($"Settings blob uri for client library: {clSASTokenUri}, for web api: {webSASTokenUri}");
            }
            catch (Exception ex)
            {
                telemetry.TrackException(ex);
            }
        }
 public static void UpdateMetadata(CloudBlockBlob clientSettingsBlob, CloudBlockBlob extraSettingsBlob, ApplicationSettings appSettings)
 {
     try
     {
         var metadata = GetTransferMetadata(appSettings);
         clientSettingsBlob.UploadText(JsonConvert.SerializeObject(metadata.Item1));
         extraSettingsBlob.UploadText(JsonConvert.SerializeObject(metadata.Item2));
     }
     catch (Exception ex)
     {
         Trace.TraceError(ex.ToString());
     }
 }