public async Task TrainingOps(bool labeled, bool useTokenCredential)
        {
            var client           = CreateFormTrainingClient(useTokenCredential);
            var trainingFilesUri = new Uri(TestEnvironment.BlobContainerSasUrl);

            TrainingOperation operation = await client.StartTrainingAsync(trainingFilesUri, labeled);

            await operation.WaitForCompletionAsync(PollingInterval);

            Assert.IsTrue(operation.HasValue);

            CustomFormModel trainedModel = operation.Value;

            CustomFormModel resultModel = await client.GetCustomModelAsync(trainedModel.ModelId);

            Assert.AreEqual(trainedModel.ModelId, resultModel.ModelId);
            Assert.AreEqual(trainedModel.Properties.IsComposedModel, resultModel.Properties.IsComposedModel);
            Assert.AreEqual(trainedModel.TrainingStartedOn, resultModel.TrainingStartedOn);
            Assert.AreEqual(trainedModel.TrainingCompletedOn, resultModel.TrainingCompletedOn);
            Assert.AreEqual(CustomFormModelStatus.Ready, resultModel.Status);
            Assert.AreEqual(trainedModel.Status, resultModel.Status);
            Assert.AreEqual(trainedModel.Errors.Count, resultModel.Errors.Count);

            for (int i = 0; i < resultModel.TrainingDocuments.Count; i++)
            {
                var tm = trainedModel.TrainingDocuments[i];
                var rm = resultModel.TrainingDocuments[i];

                Assert.AreEqual(tm.Name, rm.Name);
                Assert.AreEqual(tm.ModelId, rm.ModelId);
                Assert.AreEqual(tm.PageCount, rm.PageCount);
                Assert.AreEqual(TrainingStatus.Succeeded, rm.Status);
                Assert.AreEqual(tm.Status, rm.Status);
                Assert.AreEqual(tm.Errors.Count, rm.Errors.Count);
            }

            for (int i = 0; i < resultModel.Submodels.Count; i++)
            {
                Assert.AreEqual(trainedModel.Submodels[i].FormType, resultModel.Submodels[i].FormType);
                Assert.AreEqual(trainedModel.Submodels[i].ModelId, resultModel.Submodels[i].ModelId);
                foreach (var fields in resultModel.Submodels[i].Fields)
                {
                    Assert.AreEqual(trainedModel.Submodels[i].Fields[fields.Key].Name, fields.Value.Name);
                    if (labeled)
                    {
                        Assert.AreEqual(trainedModel.Submodels[i].Fields[fields.Key].Accuracy, fields.Value.Accuracy);
                    }
                    else
                    {
                        Assert.AreEqual(trainedModel.Submodels[i].Fields[fields.Key].Label, fields.Value.Label);
                    }
                }
            }

            CustomFormModelInfo modelInfo = client.GetCustomModelsAsync().ToEnumerableAsync().Result.FirstOrDefault();

            Assert.IsNotNull(modelInfo.ModelId);
            Assert.IsNotNull(modelInfo.TrainingStartedOn);
            Assert.IsNotNull(modelInfo.TrainingCompletedOn);
            Assert.IsNotNull(modelInfo.Status);
            Assert.IsNotNull(modelInfo.Properties);

            AccountProperties accountP = await client.GetAccountPropertiesAsync();

            Assert.IsNotNull(accountP.CustomModelCount);
            Assert.IsNotNull(accountP.CustomModelLimit);

            await client.DeleteModelAsync(trainedModel.ModelId);

            RequestFailedException ex = Assert.ThrowsAsync <RequestFailedException>(() => client.GetCustomModelAsync(trainedModel.ModelId));

            Assert.AreEqual("1022", ex.ErrorCode);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Run samples for Order By queries.
        /// </summary>
        /// <returns>a Task object.</returns>
        private async Task <RunSummary> ExecuteAsync(BenchmarkConfig config, string accountKey)
        {
            using (CosmosClient cosmosClient = config.CreateCosmosClient(accountKey))
            {
                if (config.CleanupOnStart)
                {
                    Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database);
                    await database.DeleteStreamAsync();
                }

                ContainerResponse containerResponse = await Program.CreatePartitionedContainerAsync(config, cosmosClient);

                Container container = containerResponse;

                int?currentContainerThroughput = await container.ReadThroughputAsync();

                Console.WriteLine($"Using container {config.Container} with {currentContainerThroughput} RU/s");

                int taskCount = config.GetTaskCount(currentContainerThroughput.Value);

                Console.WriteLine("Starting Inserts with {0} tasks", taskCount);
                Console.WriteLine();

                string partitionKeyPath = containerResponse.Resource.PartitionKeyPath;
                int    opsPerTask       = config.ItemCount / taskCount;

                // TBD: 2 clients SxS some overhead
                RunSummary runSummary;
                using (DocumentClient documentClient = config.CreateDocumentClient(accountKey))
                {
                    Func <IBenchmarkOperation> benchmarkOperationFactory = this.GetBenchmarkFactory(
                        config,
                        partitionKeyPath,
                        cosmosClient,
                        documentClient);

                    if (config.DisableCoreSdkLogging)
                    {
                        // Do it after client initialization (HACK)
                        Program.ClearCoreSdkListeners();
                    }

                    IExecutionStrategy execution = IExecutionStrategy.StartNew(config, benchmarkOperationFactory);
                    runSummary = await execution.ExecuteAsync(taskCount, opsPerTask, config.TraceFailures, 0.01);
                }

                if (config.CleanupOnFinish)
                {
                    Console.WriteLine($"Deleting Database {config.Database}");
                    Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database);
                    await database.DeleteStreamAsync();
                }

                runSummary.WorkloadType = config.WorkloadType;
                runSummary.id           = $"{DateTime.UtcNow.ToString("yyyy-MM-dd:HH-mm")}-{config.CommitId}";
                runSummary.Commit       = config.CommitId;
                runSummary.CommitDate   = config.CommitDate;
                runSummary.CommitTime   = config.CommitTime;

                runSummary.Date        = DateTime.UtcNow.ToString("yyyy-MM-dd");
                runSummary.Time        = DateTime.UtcNow.ToString("HH-mm");
                runSummary.BranchName  = config.BranchName;
                runSummary.TotalOps    = config.ItemCount;
                runSummary.Concurrency = taskCount;
                runSummary.Database    = config.Database;
                runSummary.Container   = config.Container;
                runSummary.AccountName = config.EndPoint;
                runSummary.pk          = config.ResultsPartitionKeyValue;

                string consistencyLevel = config.ConsistencyLevel;
                if (string.IsNullOrWhiteSpace(consistencyLevel))
                {
                    AccountProperties accountProperties = await cosmosClient.ReadAccountAsync();

                    consistencyLevel = accountProperties.Consistency.DefaultConsistencyLevel.ToString();
                }
                runSummary.ConsistencyLevel = consistencyLevel;


                if (config.PublishResults)
                {
                    Container resultsContainer = cosmosClient.GetContainer(config.Database, config.ResultsContainer);
                    await resultsContainer.CreateItemAsync(runSummary, new PartitionKey(runSummary.pk));
                }

                return(runSummary);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Executing benchmarks for V2/V3 cosmosdb SDK.
        /// </summary>
        /// <returns>a Task object.</returns>
        private async Task <RunSummary> ExecuteAsync(BenchmarkConfig config)
        {
            // V3 SDK client initialization
            using (CosmosClient cosmosClient = config.CreateCosmosClient(config.Key))
            {
                Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database);
                if (config.CleanupOnStart)
                {
                    await database.DeleteStreamAsync();
                }

                ContainerResponse containerResponse = await Program.CreatePartitionedContainerAsync(config, cosmosClient);

                Container container = containerResponse;

                int?currentContainerThroughput = await container.ReadThroughputAsync();

                if (!currentContainerThroughput.HasValue)
                {
                    // Container throughput is not configured. It is shared database throughput
                    ThroughputResponse throughputResponse = await database.ReadThroughputAsync(requestOptions : null);

                    throw new InvalidOperationException($"Using database {config.Database} with {throughputResponse.Resource.Throughput} RU/s. " +
                                                        $"Container {config.Container} must have a configured throughput.");
                }

                Console.WriteLine($"Using container {config.Container} with {currentContainerThroughput} RU/s");
                int taskCount = config.GetTaskCount(currentContainerThroughput.Value);

                Console.WriteLine("Starting Inserts with {0} tasks", taskCount);
                Console.WriteLine();

                string partitionKeyPath = containerResponse.Resource.PartitionKeyPath;
                int    opsPerTask       = config.ItemCount / taskCount;

                // TBD: 2 clients SxS some overhead
                RunSummary runSummary;

                // V2 SDK client initialization
                using (Microsoft.Azure.Documents.Client.DocumentClient documentClient = config.CreateDocumentClient(config.Key))
                {
                    Func <IBenchmarkOperation> benchmarkOperationFactory = this.GetBenchmarkFactory(
                        config,
                        partitionKeyPath,
                        cosmosClient,
                        documentClient);

                    if (config.DisableCoreSdkLogging)
                    {
                        // Do it after client initialization (HACK)
                        Program.ClearCoreSdkListeners();
                    }

                    IExecutionStrategy execution = IExecutionStrategy.StartNew(benchmarkOperationFactory);
                    runSummary = await execution.ExecuteAsync(config, taskCount, opsPerTask, 0.01);
                }

                if (config.CleanupOnFinish)
                {
                    Console.WriteLine($"Deleting Database {config.Database}");
                    await database.DeleteStreamAsync();
                }

                string consistencyLevel = config.ConsistencyLevel;
                if (string.IsNullOrWhiteSpace(consistencyLevel))
                {
                    AccountProperties accountProperties = await cosmosClient.ReadAccountAsync();

                    consistencyLevel = accountProperties.Consistency.DefaultConsistencyLevel.ToString();
                }
                runSummary.ConsistencyLevel = consistencyLevel;


                if (config.PublishResults)
                {
                    runSummary.Diagnostics = CosmosDiagnosticsLogger.GetDiagnostics();
                    await this.PublishResults(
                        config,
                        runSummary,
                        cosmosClient);
                }

                return(runSummary);
            }
        }
        public async Task ManageCustomModels()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            #region Snippet:FormRecognizerSampleManageCustomModels

            FormTrainingClient client = new FormTrainingClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            // Check number of models in the FormRecognizer account, and the maximum number of models that can be stored.
            AccountProperties accountProperties = client.GetAccountProperties();
            Console.WriteLine($"Account has {accountProperties.CustomModelCount} models.");
            Console.WriteLine($"It can have at most {accountProperties.CustomModelLimit} models.");

            // List the first ten or fewer models currently stored in the account.
            Pageable <CustomFormModelInfo> models = client.GetCustomModels();

            foreach (CustomFormModelInfo modelInfo in models.Take(10))
            {
                Console.WriteLine($"Custom Model Info:");
                Console.WriteLine($"  Model Id: {modelInfo.ModelId}");
                Console.WriteLine($"  Model name: {modelInfo.ModelName}");
                Console.WriteLine($"  Is composed model: {modelInfo.Properties.IsComposedModel}");
                Console.WriteLine($"  Model Status: {modelInfo.Status}");
                Console.WriteLine($"  Training model started on: {modelInfo.TrainingStartedOn}");
                Console.WriteLine($"  Training model completed on: {modelInfo.TrainingCompletedOn}");
            }

            // Create a new model to store in the account

#if SNIPPET
            Uri trainingFileUri = < trainingFileUri >;
#else
            Uri trainingFileUri = new Uri(TestEnvironment.BlobContainerSasUrlV2);
#endif
            TrainingOperation          operation         = client.StartTraining(trainingFileUri, useTrainingLabels: false, "My new model");
            Response <CustomFormModel> operationResponse = await operation.WaitForCompletionAsync();

            CustomFormModel model = operationResponse.Value;

            // Get the model that was just created
            CustomFormModel modelCopy = client.GetCustomModel(model.ModelId);

            Console.WriteLine($"Custom Model with Id {modelCopy.ModelId}  and name {modelCopy.ModelName} recognizes the following form types:");

            foreach (CustomFormSubmodel submodel in modelCopy.Submodels)
            {
                Console.WriteLine($"Submodel Form Type: {submodel.FormType}");
                foreach (CustomFormModelField field in submodel.Fields.Values)
                {
                    Console.Write($"  FieldName: {field.Name}");
                    if (field.Label != null)
                    {
                        Console.Write($", FieldLabel: {field.Label}");
                    }
                    Console.WriteLine("");
                }
            }

            // Delete the model from the account.
            client.DeleteModel(model.ModelId);

            #endregion
        }
Ejemplo n.º 5
0
        public async Task ManageModelsAsync()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            #region Snippet:FormRecognizerSampleManageModelsAsync

            var client = new DocumentModelAdministrationClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            // Check number of custom models in the FormRecognizer account, and the maximum number of models that can be stored.
            AccountProperties accountProperties = await client.GetAccountPropertiesAsync();

            Console.WriteLine($"Account has {accountProperties.DocumentModelCount} models.");
            Console.WriteLine($"It can have at most {accountProperties.DocumentModelLimit} models.");

            // List the first ten or fewer models currently stored in the account.
            AsyncPageable <DocumentModelInfo> models = client.GetModelsAsync();

            int count = 0;
            await foreach (DocumentModelInfo modelInfo in models)
            {
                Console.WriteLine($"Custom Model Info:");
                Console.WriteLine($"  Model Id: {modelInfo.ModelId}");
                if (string.IsNullOrEmpty(modelInfo.Description))
                {
                    Console.WriteLine($"  Model description: {modelInfo.Description}");
                }
                Console.WriteLine($"  Created on: {modelInfo.CreatedOn}");
                if (++count == 10)
                {
                    break;
                }
            }

            // Create a new model to store in the account
#if SNIPPET
            Uri trainingFileUri = new Uri("<trainingFileUri>");
#else
            Uri trainingFileUri = new Uri(TestEnvironment.BlobContainerSasUrl);
#endif
            BuildModelOperation operation = await client.StartBuildModelAsync(trainingFileUri, DocumentBuildMode.Template);

            Response <DocumentModel> operationResponse = await operation.WaitForCompletionAsync();

            DocumentModel model = operationResponse.Value;

            // Get the model that was just created
            DocumentModel newCreatedModel = await client.GetModelAsync(model.ModelId);

            Console.WriteLine($"Custom Model with Id {newCreatedModel.ModelId} has the following information:");

            Console.WriteLine($"  Model Id: {newCreatedModel.ModelId}");
            if (string.IsNullOrEmpty(newCreatedModel.Description))
            {
                Console.WriteLine($"  Model description: {newCreatedModel.Description}");
            }
            Console.WriteLine($"  Created on: {newCreatedModel.CreatedOn}");

            // Delete the model from the account.
            await client.DeleteModelAsync(newCreatedModel.ModelId);

            #endregion
        }
        /// <summary>
        /// The Get Account Create Operation Status operation returns the
        /// status of the account creation operation. After calling an
        /// asynchronous operation, you can call this method to determine
        /// whether the operation has succeeded, failed, or is still in
        /// progress.
        /// </summary>
        /// <param name='operationStatusLink'>
        /// Required. Location value returned by the BeginCreating operation.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Values returned by the Create operation.
        /// </returns>
        public async Task <BatchAccountCreateResponse> GetAccountCreateOperationStatusAsync(string operationStatusLink, CancellationToken cancellationToken)
        {
            // Validate
            if (operationStatusLink == null)
            {
                throw new ArgumentNullException("operationStatusLink");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("operationStatusLink", operationStatusLink);
                TracingAdapter.Enter(invocationId, this, "GetAccountCreateOperationStatusAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + operationStatusLink;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2015-07-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK && statusCode != HttpStatusCode.Accepted)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    BatchAccountCreateResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK || statusCode == HttpStatusCode.Accepted)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new BatchAccountCreateResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            AccountResource resourceInstance = new AccountResource();
                            result.Resource = resourceInstance;

                            JToken idValue = responseDoc["id"];
                            if (idValue != null && idValue.Type != JTokenType.Null)
                            {
                                string idInstance = ((string)idValue);
                                resourceInstance.Id = idInstance;
                            }

                            JToken typeValue = responseDoc["type"];
                            if (typeValue != null && typeValue.Type != JTokenType.Null)
                            {
                                string typeInstance = ((string)typeValue);
                                resourceInstance.Type = typeInstance;
                            }

                            JToken nameValue = responseDoc["name"];
                            if (nameValue != null && nameValue.Type != JTokenType.Null)
                            {
                                string nameInstance = ((string)nameValue);
                                resourceInstance.Name = nameInstance;
                            }

                            JToken locationValue = responseDoc["location"];
                            if (locationValue != null && locationValue.Type != JTokenType.Null)
                            {
                                string locationInstance = ((string)locationValue);
                                resourceInstance.Location = locationInstance;
                            }

                            JToken tagsSequenceElement = ((JToken)responseDoc["tags"]);
                            if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null)
                            {
                                foreach (JProperty property in tagsSequenceElement)
                                {
                                    string tagsKey   = ((string)property.Name);
                                    string tagsValue = ((string)property.Value);
                                    resourceInstance.Tags.Add(tagsKey, tagsValue);
                                }
                            }

                            JToken propertiesValue = responseDoc["properties"];
                            if (propertiesValue != null && propertiesValue.Type != JTokenType.Null)
                            {
                                AccountProperties propertiesInstance = new AccountProperties();
                                resourceInstance.Properties = propertiesInstance;

                                JToken accountEndpointValue = propertiesValue["accountEndpoint"];
                                if (accountEndpointValue != null && accountEndpointValue.Type != JTokenType.Null)
                                {
                                    string accountEndpointInstance = ((string)accountEndpointValue);
                                    propertiesInstance.AccountEndpoint = accountEndpointInstance;
                                }

                                JToken provisioningStateValue = propertiesValue["provisioningState"];
                                if (provisioningStateValue != null && provisioningStateValue.Type != JTokenType.Null)
                                {
                                    AccountProvisioningState provisioningStateInstance = ((AccountProvisioningState)Enum.Parse(typeof(AccountProvisioningState), ((string)provisioningStateValue), true));
                                    propertiesInstance.ProvisioningState = provisioningStateInstance;
                                }
                            }

                            BatchManagementError errorInstance = new BatchManagementError();
                            result.Error = errorInstance;

                            JToken codeValue = responseDoc["code"];
                            if (codeValue != null && codeValue.Type != JTokenType.Null)
                            {
                                string codeInstance = ((string)codeValue);
                                errorInstance.Code = codeInstance;
                            }

                            JToken messageValue = responseDoc["message"];
                            if (messageValue != null && messageValue.Type != JTokenType.Null)
                            {
                                string messageInstance = ((string)messageValue);
                                errorInstance.Message = messageInstance;
                            }

                            JToken targetValue = responseDoc["target"];
                            if (targetValue != null && targetValue.Type != JTokenType.Null)
                            {
                                string targetInstance = ((string)targetValue);
                                errorInstance.Target = targetInstance;
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }
                    if (result.Resource != null && result.Resource.Properties != null && result.Resource.Properties.ProvisioningState == AccountProvisioningState.Succeeded)
                    {
                        result.Status = OperationStatus.Succeeded;
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
Ejemplo n.º 7
0
        public override void ExecuteCmdlet()
        {
            if (ServiceType != StorageServiceType.Table)
            {
                ServiceProperties currentServiceProperties = Channel.GetStorageServiceProperties(ServiceType, GetRequestOptions(ServiceType), OperationContext);

                // Premium Account not support classic metrics and logging
                if ((MetricsType == ServiceMetricsType.Hour && currentServiceProperties.HourMetrics == null) ||
                    (MetricsType == ServiceMetricsType.Minute && currentServiceProperties.MinuteMetrics == null))
                {
                    AccountProperties accountProperties = Channel.GetAccountProperties();
                    if (accountProperties.SkuName.Contains("Premium"))
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "This Storage account doesn't support Classic Metrics, since it’s a Premium Storage account: {0}", Channel.StorageContext.StorageAccountName));
                    }
                }

                ServiceProperties serviceProperties = new ServiceProperties();
                serviceProperties.Clean();

                bool isHourMetrics = false;

                switch (MetricsType)
                {
                case ServiceMetricsType.Hour:
                    serviceProperties.HourMetrics = currentServiceProperties.HourMetrics;
                    UpdateServiceProperties(serviceProperties.HourMetrics);
                    isHourMetrics = true;
                    break;

                case ServiceMetricsType.Minute:
                    serviceProperties.MinuteMetrics = currentServiceProperties.MinuteMetrics;
                    UpdateServiceProperties(serviceProperties.MinuteMetrics);
                    isHourMetrics = false;
                    break;
                }

                Channel.SetStorageServiceProperties(ServiceType, serviceProperties,
                                                    GetRequestOptions(ServiceType), OperationContext);

                if (PassThru)
                {
                    if (isHourMetrics)
                    {
                        WriteObject(serviceProperties.HourMetrics);
                    }
                    else
                    {
                        WriteObject(serviceProperties.MinuteMetrics);
                    }
                }
            }
            else //Table use old XSCL
            {
                StorageTableManagement   tableChannel             = new StorageTableManagement(Channel.StorageContext);
                XTable.ServiceProperties currentServiceProperties = tableChannel.GetStorageTableServiceProperties(GetTableRequestOptions(), TableOperationContext);

                // Premium Account not support classic metrics and logging
                if ((MetricsType == ServiceMetricsType.Hour && currentServiceProperties.HourMetrics == null) ||
                    (MetricsType == ServiceMetricsType.Minute && currentServiceProperties.MinuteMetrics == null))
                {
                    AccountProperties accountProperties = Channel.GetAccountProperties();
                    if (accountProperties.SkuName.Contains("Premium"))
                    {
                        throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "This Storage account doesn't support Classic Metrics, since it’s a Premium Storage account: {0}", Channel.StorageContext.StorageAccountName));
                    }
                }

                XTable.ServiceProperties serviceProperties = new XTable.ServiceProperties();
                serviceProperties.Clean();

                bool isHourMetrics = false;

                switch (MetricsType)
                {
                case ServiceMetricsType.Hour:
                    serviceProperties.HourMetrics = currentServiceProperties.HourMetrics;
                    UpdateServiceProperties(serviceProperties.HourMetrics);
                    isHourMetrics = true;
                    break;

                case ServiceMetricsType.Minute:
                    serviceProperties.MinuteMetrics = currentServiceProperties.MinuteMetrics;
                    UpdateServiceProperties(serviceProperties.MinuteMetrics);
                    isHourMetrics = false;
                    break;
                }

                tableChannel.SetStorageTableServiceProperties(serviceProperties,
                                                              GetTableRequestOptions(), TableOperationContext);

                if (PassThru)
                {
                    if (isHourMetrics)
                    {
                        WriteObject(serviceProperties.HourMetrics);
                    }
                    else
                    {
                        WriteObject(serviceProperties.MinuteMetrics);
                    }
                }
            }
        }
Ejemplo n.º 8
0
        public async Task TrainingOps(bool labeled)
        {
            var client           = CreateInstrumentedFormTrainingClient();
            var trainingFilesUri = new Uri(TestEnvironment.BlobContainerSasUrl);
            TrainingOperation operation;

            // TODO: sanitize body and enable body recording here.
            using (Recording.DisableRequestBodyRecording())
            {
                operation = await client.StartTrainingAsync(trainingFilesUri, labeled);
            }

            await operation.WaitForCompletionAsync();

            Assert.IsTrue(operation.HasValue);

            CustomFormModel trainedModel = operation.Value;

            CustomFormModel resultModel = await client.GetCustomModelAsync(trainedModel.ModelId);

            Assert.AreEqual(trainedModel.ModelId, resultModel.ModelId);
            Assert.AreEqual(trainedModel.CreatedOn, resultModel.CreatedOn);
            Assert.AreEqual(trainedModel.LastModified, resultModel.LastModified);
            Assert.AreEqual(CustomFormModelStatus.Ready, resultModel.Status);
            Assert.AreEqual(trainedModel.Status, resultModel.Status);
            Assert.AreEqual(trainedModel.Errors.Count, resultModel.Errors.Count);

            for (int i = 0; i < resultModel.TrainingDocuments.Count; i++)
            {
                var tm = trainedModel.TrainingDocuments[i];
                var rm = resultModel.TrainingDocuments[i];

                Assert.AreEqual(tm.DocumentName, rm.DocumentName);
                Assert.AreEqual(tm.PageCount, rm.PageCount);
                Assert.AreEqual(TrainingStatus.Succeeded, rm.Status);
                Assert.AreEqual(tm.Status, rm.Status);
                Assert.AreEqual(tm.Errors.Count, rm.Errors.Count);
            }

            for (int i = 0; i < resultModel.Models.Count; i++)
            {
                Assert.AreEqual(trainedModel.Models[i].FormType, resultModel.Models[i].FormType);

                foreach (var fields in resultModel.Models[i].Fields)
                {
                    Assert.AreEqual(trainedModel.Models[i].Fields[fields.Key].Name, fields.Value.Name);
                    if (labeled)
                    {
                        Assert.AreEqual(trainedModel.Models[i].Fields[fields.Key].Accuracy, fields.Value.Accuracy);
                    }
                    else
                    {
                        Assert.AreEqual(trainedModel.Models[i].Fields[fields.Key].Label, fields.Value.Label);
                    }
                }
            }

            CustomFormModelInfo modelInfo = client.GetCustomModelsAsync().ToEnumerableAsync().Result.FirstOrDefault();

            Assert.IsNotNull(modelInfo.ModelId);
            Assert.IsNotNull(modelInfo.CreatedOn);
            Assert.IsNotNull(modelInfo.LastModified);
            Assert.IsNotNull(modelInfo.Status);

            AccountProperties accountP = await client.GetAccountPropertiesAsync();

            Assert.IsNotNull(accountP.CustomModelCount);
            Assert.IsNotNull(accountP.CustomModelLimit);

            await client.DeleteModelAsync(trainedModel.ModelId);

            Assert.ThrowsAsync <RequestFailedException>(() => client.GetCustomModelAsync(trainedModel.ModelId));
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="AccountPropertiesInfo" /> class.
 /// </summary>
 /// <param name="metaId">The account property meta id</param>
 /// <param name="accountProperties">The account properties</param>
 public AccountPropertiesInfo(string metaId, AccountProperties accountProperties)
 {
     MetaId            = metaId;
     AccountProperties = accountProperties;
 }
Ejemplo n.º 10
0
 public AccountViewModel(Account account, AccountProperties properties, Balances balances) : base()
 {
     AccountNumber      = account;
     _accountProperties = properties;
     _balances          = balances;
 }
        public static async Task <AccountProperties> GetDatabaseAccountFromAnyLocationsAsync(
            Uri defaultEndpoint, IList <string> locations, Func <Uri, Task <AccountProperties> > getDatabaseAccountFn)
        {
            ExceptionDispatchInfo capturedException = null;

            try
            {
                AccountProperties databaseAccount = await getDatabaseAccountFn(defaultEndpoint);

                return(databaseAccount);
            }
            catch (Exception e)
            {
                DefaultTrace.TraceInformation("Fail to reach global gateway {0}, {1}", defaultEndpoint, e.ToString());

                if (IsNonRetriableException(e))
                {
                    DefaultTrace.TraceInformation("Exception is not retriable");
                    throw;
                }

                if (locations.Count == 0)
                {
                    throw;
                }

                // Save the exception and rethrow it at the end after trying all regions
                capturedException = ExceptionDispatchInfo.Capture(e);
            }

            for (int index = 0; index < locations.Count; index++)
            {
                try
                {
                    AccountProperties databaseAccount = await getDatabaseAccountFn(LocationHelper.GetLocationEndpoint(defaultEndpoint, locations[index]));

                    return(databaseAccount);
                }
                catch (Exception e)
                {
                    DefaultTrace.TraceInformation("Fail to reach location {0}, {1}", locations[index], e.ToString());

                    // if is the last region, throw exception
                    if (index == locations.Count - 1)
                    {
                        // The reason for rethrowing the first exception is that the locations list might contain invalid regions,
                        // so the last exception would be some generic exception ("The remote name could not be resolved") instead of the real exception.
                        // Location list containing invalid regions is quite common when SetCurrentLocation is used since it will add all Azure regions to the list
                        if (capturedException != null)
                        {
                            capturedException.Throw();
                        }

                        throw;
                    }
                }
            }

            // we should never reach here. Make compiler happy
            throw new Exception();
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            if (ShouldProcess(this.Name, "Add Cognitive Services Account NetworkRules"))
            {
                var account = this.CognitiveServicesClient.Accounts.Get(
                    this.ResourceGroupName,
                    this.Name);
                NetworkRuleSet accountACL = account.Properties.NetworkAcls;

                if (accountACL == null)
                {
                    accountACL = new NetworkRuleSet();
                    // Deny is the default action value from server side,
                    // Specifically make default action Deny in client side as server side might want this value to be always provided in future.
                    accountACL.DefaultAction = NetworkRuleAction.Deny;
                }

                switch (ParameterSetName)
                {
                case NetWorkRuleStringParameterSet:
                    if (accountACL.VirtualNetworkRules == null)
                    {
                        accountACL.VirtualNetworkRules = new List <VirtualNetworkRule>();
                    }

                    foreach (string s in VirtualNetworkResourceId)
                    {
                        VirtualNetworkRule rule = new VirtualNetworkRule(s, null, true);
                        accountACL.VirtualNetworkRules.Add(rule);
                    }
                    break;

                case IpRuleStringParameterSet:
                    if (accountACL.IpRules == null)
                    {
                        accountACL.IpRules = new List <IpRule>();
                    }

                    foreach (string s in IpAddressOrRange)
                    {
                        IpRule rule = new IpRule(s);
                        accountACL.IpRules.Add(rule);
                    }
                    break;

                case NetworkRuleObjectParameterSet:
                    if (accountACL.VirtualNetworkRules == null)
                    {
                        accountACL.VirtualNetworkRules = new List <VirtualNetworkRule>();
                    }

                    foreach (PSVirtualNetworkRule rule in VirtualNetworkRule)
                    {
                        accountACL.VirtualNetworkRules.Add(rule.ToVirtualNetworkRule());
                    }
                    break;

                case IpRuleObjectParameterSet:
                    if (accountACL.IpRules == null)
                    {
                        accountACL.IpRules = new List <IpRule>();
                    }

                    foreach (PSIpRule rule in IpRule)
                    {
                        accountACL.IpRules.Add(rule.ToIpRule());
                    }
                    break;
                }

                var properties = new AccountProperties();
                properties.NetworkAcls = accountACL;
                this.CognitiveServicesClient.Accounts.Update(
                    this.ResourceGroupName,
                    this.Name,
                    new Account()
                {
                    Properties = properties
                }
                    );

                account = this.CognitiveServicesClient.Accounts.Get(this.ResourceGroupName, this.Name);

                switch (ParameterSetName)
                {
                case NetWorkRuleStringParameterSet:
                case NetworkRuleObjectParameterSet:
                    WriteObject(PSNetworkRuleSet.Create(account.Properties.NetworkAcls).VirtualNetworkRules);
                    break;

                case IpRuleStringParameterSet:
                case IpRuleObjectParameterSet:
                    WriteObject(PSNetworkRuleSet.Create(account.Properties.NetworkAcls).IpRules);
                    break;
                }
            }
        }