Example #1
0
        /// <summary>
        /// Replaces Time Series instances asynchronously.
        /// </summary>
        /// <param name="timeSeriesInstances">The Time Series instances to replaced.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// List of objects corresponding by position to the <paramref name="timeSeriesInstances"/> array in the request. Instance object
        /// is set when operation is successful and error object is set when operation is unsuccessful.
        /// </returns>
        /// <remarks>
        /// For more samples, see <see href="https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/timeseriesinsights/Azure.IoT.TimeSeriesInsights/samples">our repo samples</see>.
        /// </remarks>
        /// <example>
        /// <code snippet="Snippet:TimeSeriesInsightsReplaceInstance">
        /// // Get Time Series Insights instances by Id
        /// // tsId is created above using `TimeSeriesIdHelper.CreateTimeSeriesId`.
        /// var instanceIdsToGet = new List&lt;TimeSeriesId&gt;
        /// {
        ///     tsId,
        /// };
        ///
        /// Response&lt;InstancesOperationResult[]&gt; getInstancesByIdResult = await client.Instances.GetAsync(instanceIdsToGet);
        ///
        /// TimeSeriesInstance instanceResult = getInstancesByIdResult.Value[0].Instance;
        /// Console.WriteLine($&quot;Retrieved Time Series Insights instance with Id &apos;{instanceResult.TimeSeriesId}&apos; and name &apos;{instanceResult.Name}&apos;.&quot;);
        ///
        /// // Now let&apos;s replace the instance with an updated name
        /// instanceResult.Name = &quot;newInstanceName&quot;;
        ///
        /// var instancesToReplace = new List&lt;TimeSeriesInstance&gt;
        /// {
        ///     instanceResult,
        /// };
        ///
        /// Response&lt;InstancesOperationResult[]&gt; replaceInstancesResult = await client.Instances.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 &lt; replaceInstancesResult.Value.Length; i++)
        /// {
        ///     TimeSeriesId tsiId = instancesToReplace[i].TimeSeriesId;
        ///
        ///     TimeSeriesOperationError currentError = replaceInstancesResult.Value[i].Error;
        ///
        ///     if (currentError != null)
        ///     {
        ///         Console.WriteLine($&quot;Failed to replace Time Series Insights instance with Id &apos;{tsiId}&apos;,&quot; +
        ///             $&quot; Error Message: &apos;{currentError.Message}&apos;, Error code: &apos;{currentError.Code}&apos;.&quot;);
        ///     }
        ///     else
        ///     {
        ///         Console.WriteLine($&quot;Replaced Time Series Insights instance with Id &apos;{tsiId}&apos;.&quot;);
        ///     }
        /// }
        /// </code>
        /// </example>
        /// <exception cref="ArgumentNullException">
        /// The exception is thrown when <paramref name="timeSeriesInstances"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// The exception is thrown when <paramref name="timeSeriesInstances"/> is empty.
        /// </exception>
        public virtual async Task <Response <InstancesOperationResult[]> > ReplaceAsync(
            IEnumerable <TimeSeriesInstance> timeSeriesInstances,
            CancellationToken cancellationToken = default)
        {
            using DiagnosticScope scope = _clientDiagnostics
                                          .CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(Replace)}");
            scope.Start();

            try
            {
                Argument.AssertNotNullOrEmpty(timeSeriesInstances, nameof(timeSeriesInstances));

                var batchRequest = new InstancesBatchRequest();

                foreach (TimeSeriesInstance instance in timeSeriesInstances)
                {
                    batchRequest.Update.Add(instance);
                }

                Response <InstancesBatchResponse> executeBatchResponse = await _instancesRestClient
                                                                         .ExecuteBatchAsync(batchRequest, null, cancellationToken)
                                                                         .ConfigureAwait(false);

                return(Response.FromValue(executeBatchResponse.Value.Update.ToArray(), executeBatchResponse.GetRawResponse()));
            }
            catch (Exception ex)
            {
                scope.Failed(ex);
                throw;
            }
        }
Example #2
0
        /// <summary>
        /// Deletes Time Series instances from the environment by Time Series Ids synchronously.
        /// </summary>
        /// <param name="timeSeriesIds">List of Ids of the Time Series instances to delete.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// List of error objects corresponding by position to the array in the request. Null means the instance has been deleted, or did not exist.
        /// Error object is set when operation is unsuccessful.
        /// </returns>
        /// <seealso cref="DeleteAsync(IEnumerable{TimeSeriesId}, CancellationToken)">
        /// See the asynchronous version of this method for examples.
        /// </seealso>
        /// <exception cref="ArgumentNullException">
        /// The exception is thrown when <paramref name="timeSeriesIds"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// The exception is thrown when <paramref name="timeSeriesIds"/> is empty.
        /// </exception>
        public virtual Response <TimeSeriesOperationError[]> Delete(
            IEnumerable <TimeSeriesId> timeSeriesIds,
            CancellationToken cancellationToken = default)
        {
            using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(Delete)}");
            scope.Start();

            try
            {
                Argument.AssertNotNullOrEmpty(timeSeriesIds, nameof(timeSeriesIds));

                var batchRequest = new InstancesBatchRequest
                {
                    Delete = new InstancesRequestBatchGetOrDelete()
                };

                foreach (TimeSeriesId timeSeriesId in timeSeriesIds)
                {
                    batchRequest.Delete.TimeSeriesIds.Add(timeSeriesId);
                }

                Response <InstancesBatchResponse> executeBatchResponse = _instancesRestClient
                                                                         .ExecuteBatch(batchRequest, null, cancellationToken);

                return(Response.FromValue(executeBatchResponse.Value.Delete.ToArray(), executeBatchResponse.GetRawResponse()));
            }
            catch (Exception ex)
            {
                scope.Failed(ex);
                throw;
            }
        }
Example #3
0
        /// <summary>
        /// Creates Time Series instances synchronously. If a provided instance is already in use, then this will attempt to replace the existing
        /// instance with the provided Time Series Instance.
        /// </summary>
        /// <param name="timeSeriesInstances">The Time Series instances to be created or replaced.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// List of error objects corresponding by position to the <paramref name="timeSeriesInstances"/> array in the request.
        /// A <seealso cref="TimeSeriesOperationError"/> object will be set when operation is unsuccessful.
        /// </returns>
        /// <seealso cref="CreateOrReplaceAsync(IEnumerable{TimeSeriesInstance}, CancellationToken)">
        /// See the asynchronous version of this method for examples.
        /// </seealso>
        /// <exception cref="ArgumentNullException">
        /// The exception is thrown when <paramref name="timeSeriesInstances"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// The exception is thrown when <paramref name="timeSeriesInstances"/> is empty.
        /// </exception>
        public virtual Response <TimeSeriesOperationError[]> CreateOrReplace(
            IEnumerable <TimeSeriesInstance> timeSeriesInstances,
            CancellationToken cancellationToken = default)
        {
            using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(CreateOrReplace)}");
            scope.Start();

            try
            {
                Argument.AssertNotNullOrEmpty(timeSeriesInstances, nameof(timeSeriesInstances));

                var batchRequest = new InstancesBatchRequest();

                foreach (TimeSeriesInstance instance in timeSeriesInstances)
                {
                    batchRequest.Put.Add(instance);
                }

                Response <InstancesBatchResponse> executeBatchResponse = _instancesRestClient
                                                                         .ExecuteBatch(batchRequest, null, cancellationToken);

                // Extract the errors array from the response. If there was an error with creating or replacing one of the instances,
                // it will be placed at the same index location that corresponds to its place in the input array.
                IEnumerable <TimeSeriesOperationError> errorResults = executeBatchResponse.Value.Put.Select((result) => result.Error);

                return(Response.FromValue(errorResults.ToArray(), executeBatchResponse.GetRawResponse()));
            }
            catch (Exception ex)
            {
                scope.Failed(ex);
                throw;
            }
        }