Esempio n. 1
0
        /// <summary>
        /// </summary>
        public async Task RunSamplesAsync(TimeSeriesInsightsClient client)
        {
            PrintHeader("TIME SERIES INSIGHTS INSTANCES SAMPLE");

            // Figure out how many keys make up the Time Series Id
            TimeSeriesModelSettings modelSettings = await client.ModelSettings.GetAsync().ConfigureAwait(false);

            TimeSeriesId instanceId = modelSettings.TimeSeriesIdProperties.Count switch
            {
                1 => new TimeSeriesId("key1"),
                2 => new TimeSeriesId("key1", "key2"),
                3 => new TimeSeriesId("key1", "key2", "key3"),
                _ => throw new Exception($"Invalid number of Time Series Insights Id properties."),
            };

            #region Snippet:TimeSeriesInsightsSampleCreateInstance

            // Create a Time Series Instance object with the default Time Series Insights type Id.
            // The default type Id can be obtained programmatically by using the ModelSettings client.
            var instance = new TimeSeriesInstance(instanceId, "1be09af9-f089-4d6b-9f0b-48018b5f7393")
            {
                Name = "instance1",
            };

            var tsiInstancesToCreate = new List <TimeSeriesInstance>
            {
                instance,
            };

            Response <TimeSeriesOperationError[]> createInstanceErrors = await client
                                                                         .Instances
                                                                         .CreateOrReplaceAsync(tsiInstancesToCreate)
                                                                         .ConfigureAwait(false);

            // The response of calling the API contains a list of error objects corresponding by position to the input parameter
            // array in the request. If the error object is set to null, this means the operation was a success.
            if (createInstanceErrors.Value[0] == null)
            {
                Console.WriteLine($"Created Time Series Insights instance with Id '{instanceId}'.");
            }
            else
            {
                Console.WriteLine($"Failed to create a Time Series Insights instance: {createInstanceErrors.Value[0].Message}.");
            }

            #endregion Snippet:TimeSeriesInsightsSampleCreateInstance

            #region Snippet:TimeSeriesInsightsGetAllInstances

            // Get all instances for the Time Series Insigths environment
            AsyncPageable <TimeSeriesInstance> tsiInstances = client.Instances.GetAsync();
            await foreach (TimeSeriesInstance tsiInstance in tsiInstances)
            {
                Console.WriteLine($"Retrieved Time Series Insights instance with Id '{tsiInstance.TimeSeriesId}' and name '{tsiInstance.Name}'.");
            }

            #endregion Snippet:TimeSeriesInsightsGetAllInstances

            #region Snippet:TimeSeriesInsightsReplaceInstance

            // Get Time Series Insights instances by Id
            var instanceIdsToGet = new List <TimeSeriesId>
            {
                instanceId,
            };

            Response <InstancesOperationResult[]> getInstancesByIdResult = await client.Instances.GetAsync(instanceIdsToGet).ConfigureAwait(false);

            TimeSeriesInstance instanceResult = getInstancesByIdResult.Value[0].Instance;
            Console.WriteLine($"Retrieved Time Series Insights instance with Id '{instanceResult.TimeSeriesId}' and name '{instanceResult.Name}'.");

            // Now let's replace the instance with an updated name
            instanceResult.Name = "newInstanceName";

            var instancesToReplace = new List <TimeSeriesInstance>
            {
                instanceResult,
            };

            Response <InstancesOperationResult[]> replaceInstancesResult = await client.Instances.ReplaceAsync(instancesToReplace).ConfigureAwait(false);

            // The response of calling the API contains a list of error objects corresponding by position to the input parameter
            // array in the request. If the error object is set to null, this means the operation was a success.
            if (replaceInstancesResult.Value[0].Error != null)
            {
                Console.WriteLine($"Failed to retrieve a Time Series Insights instnace with Id '{replaceInstancesResult.Value[0].Error.Message}'.");
            }

            #endregion Snippet:TimeSeriesInsightsReplaceInstance

            #region Snippet:TimeSeriesInsightsGetnstancesById

            // Get Time Series Insights instances by Id
            var timeSeriesIds = new List <TimeSeriesId>
            {
                instanceId,
            };

            Response <InstancesOperationResult[]> getInstancesByNameResult = await client.Instances.GetAsync(timeSeriesIds).ConfigureAwait(false);

            /// The response of calling the API contains a list of instance or error objects corresponding by position to the array in the request.
            /// Instance object is set when operation is successful and error object is set when operation is unsuccessful.
            InstancesOperationResult getInstanceByIdResult = getInstancesByNameResult.Value[0];
            if (getInstanceByIdResult.Instance != null)
            {
                Console.WriteLine($"Retrieved Time Series Insights instance with Id '{getInstanceByIdResult.Instance.TimeSeriesId}' and name '{getInstanceByIdResult.Instance.Name}'.");
            }
            else if (getInstanceByIdResult.Error != null)
            {
                Console.WriteLine($"Failed to retrieve a Time Series Insights instnace with Id '{getInstanceByIdResult.Error.Message}'.");
            }

            #endregion Snippet:TimeSeriesInsightsGetnstancesById

            // Clean up
            try
            {
                #region Snippet:TimeSeriesInsightsSampleDeleteInstance

                Response <TimeSeriesOperationError[]> deleteInstanceErrors = await client
                                                                             .Instances
                                                                             .DeleteAsync(new List <TimeSeriesId> {
                    instanceId
                })
                                                                             .ConfigureAwait(false);

                // The response of calling the API contains a list of error objects corresponding by position to the input parameter
                // array in the request. If the error object is set to null, this means the operation was a success.
                if (deleteInstanceErrors.Value[0] == null)
                {
                    Console.WriteLine($"Deleted Time Series Insights instance with Id '{instanceId}'.");
                }
                else
                {
                    Console.WriteLine($"Failed to delete a Time Series Insights instance: {deleteInstanceErrors.Value[0].Message}.");
                }

                #endregion Snippet:TimeSeriesInsightsSampleDeleteInstance
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to delete Time Series Insights instance: {ex.Message}");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a Time Series Insights instance
        /// Gets all instances for the environment
        /// Gets a specific instance by Id
        /// Replaces an instance
        /// Deletes an instance.
        /// </summary>
        public async Task RunSamplesAsync(TimeSeriesInsightsClient client)
        {
            PrintHeader("TIME SERIES INSIGHTS INSTANCES SAMPLE");

            // Figure out what keys make up the Time Series Id
            TimeSeriesInsightsModelSettings modelSettingsClient = client.GetModelSettingsClient();
            TimeSeriesInsightsInstances     instancesClient     = client.GetInstancesClient();
            TimeSeriesModelSettings         modelSettings       = await modelSettingsClient.GetAsync();

            TimeSeriesId tsId          = TimeSeriesIdHelper.CreateTimeSeriesId(modelSettings);
            string       defaultTypeId = modelSettings.DefaultTypeId;

            #region Snippet:TimeSeriesInsightsSampleCreateInstance

            // Create a Time Series Instance object with the default Time Series Insights type Id.
            // The default type Id can be obtained programmatically by using the ModelSettings client.
            // tsId is created above using `TimeSeriesIdHelper.CreateTimeSeriesId`.
            var instance = new TimeSeriesInstance(tsId, defaultTypeId)
            {
                Name = "instance1",
            };

            var tsiInstancesToCreate = new List <TimeSeriesInstance>
            {
                instance,
            };

            Response <TimeSeriesOperationError[]> createInstanceErrors = await instancesClient
                                                                         .CreateOrReplaceAsync(tsiInstancesToCreate);

            // The response of calling the API contains a list of error objects corresponding by position to the input parameter
            // array in the request. If the error object is set to null, this means the operation was a success.
            for (int i = 0; i < createInstanceErrors.Value.Length; i++)
            {
                TimeSeriesId tsiId = tsiInstancesToCreate[i].TimeSeriesId;

                if (createInstanceErrors.Value[i] == null)
                {
                    Console.WriteLine($"Created Time Series Insights instance with Id '{tsiId}'.");
                }
                else
                {
                    Console.WriteLine($"Failed to create a Time Series Insights instance with Id '{tsiId}', " +
                                      $"Error Message: '{createInstanceErrors.Value[i].Message}, " +
                                      $"Error code: '{createInstanceErrors.Value[i].Code}'.");
                }
            }

            #endregion Snippet:TimeSeriesInsightsSampleCreateInstance

            #region Snippet:TimeSeriesInsightsGetAllInstances

            // Get all instances for the Time Series Insights environment
            AsyncPageable <TimeSeriesInstance> tsiInstances = instancesClient.GetAsync();
            await foreach (TimeSeriesInstance tsiInstance in tsiInstances)
            {
                Console.WriteLine($"Retrieved Time Series Insights instance with Id '{tsiInstance.TimeSeriesId}' and name '{tsiInstance.Name}'.");
            }

            #endregion Snippet:TimeSeriesInsightsGetAllInstances

            #region Snippet:TimeSeriesInsightsReplaceInstance

            // Get Time Series Insights instances by Id
            // tsId is created above using `TimeSeriesIdHelper.CreateTimeSeriesId`.
            var instanceIdsToGet = new List <TimeSeriesId>
            {
                tsId,
            };

            Response <InstancesOperationResult[]> getInstancesByIdResult = await instancesClient.GetAsync(instanceIdsToGet);

            TimeSeriesInstance instanceResult = getInstancesByIdResult.Value[0].Instance;
            Console.WriteLine($"Retrieved Time Series Insights instance with Id '{instanceResult.TimeSeriesId}' and name '{instanceResult.Name}'.");

            // Now let's replace the instance with an updated name
            instanceResult.Name = "newInstanceName";

            var instancesToReplace = new List <TimeSeriesInstance>
            {
                instanceResult,
            };

            Response <InstancesOperationResult[]> replaceInstancesResult = await instancesClient.ReplaceAsync(instancesToReplace);

            // The response of calling the API contains a list of error objects corresponding by position to the input parameter.
            // array in the request. If the error object is set to null, this means the operation was a success.
            for (int i = 0; i < replaceInstancesResult.Value.Length; i++)
            {
                TimeSeriesId tsiId = instancesToReplace[i].TimeSeriesId;

                TimeSeriesOperationError currentError = replaceInstancesResult.Value[i].Error;

                if (currentError != null)
                {
                    Console.WriteLine($"Failed to replace Time Series Insights instance with Id '{tsiId}'," +
                                      $" Error Message: '{currentError.Message}', Error code: '{currentError.Code}'.");
                }
                else
                {
                    Console.WriteLine($"Replaced Time Series Insights instance with Id '{tsiId}'.");
                }
            }

            #endregion Snippet:TimeSeriesInsightsReplaceInstance

            #region Snippet:TimeSeriesInsightsGetnstancesById

            // Get Time Series Insights instances by Id
            // tsId is created above using `TimeSeriesIdHelper.CreateTimeSeriesId`.
            var timeSeriesIds = new List <TimeSeriesId>
            {
                tsId,
            };

            Response <InstancesOperationResult[]> getByIdsResult = await instancesClient.GetAsync(timeSeriesIds);

            // The response of calling the API contains a list of instance or error objects corresponding by position to the array in the request.
            // Instance object is set when operation is successful and error object is set when operation is unsuccessful.
            for (int i = 0; i < getByIdsResult.Value.Length; i++)
            {
                InstancesOperationResult currentOperationResult = getByIdsResult.Value[i];

                if (currentOperationResult.Instance != null)
                {
                    Console.WriteLine($"Retrieved Time Series Insights instance with Id '{currentOperationResult.Instance.TimeSeriesId}' and name '{currentOperationResult.Instance.Name}'.");
                }
                else if (currentOperationResult.Error != null)
                {
                    Console.WriteLine($"Failed to retrieve a Time Series Insights instance with Id '{timeSeriesIds[i]}'. Error message: '{currentOperationResult.Error.Message}'.");
                }
            }

            #endregion Snippet:TimeSeriesInsightsGetnstancesById

            // Clean up
            try
            {
                #region Snippet:TimeSeriesInsightsSampleDeleteInstanceById

                // tsId is created above using `TimeSeriesIdHelper.CreateTimeSeriesId`.
                var instancesToDelete = new List <TimeSeriesId>
                {
                    tsId,
                };

                Response <TimeSeriesOperationError[]> deleteInstanceErrors = await instancesClient
                                                                             .DeleteAsync(instancesToDelete);

                // The response of calling the API contains a list of error objects corresponding by position to the input parameter
                // array in the request. If the error object is set to null, this means the operation was a success.
                for (int i = 0; i < deleteInstanceErrors.Value.Length; i++)
                {
                    TimeSeriesId tsiId = instancesToDelete[i];

                    if (deleteInstanceErrors.Value[i] == null)
                    {
                        Console.WriteLine($"Deleted Time Series Insights instance with Id '{tsiId}'.");
                    }
                    else
                    {
                        Console.WriteLine($"Failed to delete a Time Series Insights instance with Id '{tsiId}'. Error Message: '{deleteInstanceErrors.Value[i].Message}'");
                    }
                }

                #endregion Snippet:TimeSeriesInsightsSampleDeleteInstanceById
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to delete Time Series Insights instance: {ex.Message}");
            }
        }