/// <summary> /// Gets the response headers for the context. /// </summary> /// <returns>The response headers for the context.</returns> public INameValueCollection GetResponseHeaders() { StringKeyValueCollection responseHeaders = new StringKeyValueCollection(); responseHeaders[HttpConstants.HttpHeaders.Continuation] = this.ContinuationToken; if (this.ContinuationToken == "[]") { throw new InvalidOperationException("Somehow a document query execution context returned an empty array of continuations."); } this.SetQueryMetrics(); IReadOnlyDictionary <string, QueryMetrics> groupedQueryMetrics = this.GetQueryMetrics(); if (groupedQueryMetrics != null && groupedQueryMetrics.Count != 0) { responseHeaders[HttpConstants.HttpHeaders.QueryMetrics] = QueryMetrics .CreateFromIEnumerable(groupedQueryMetrics.Values) .ToDelimitedString(); } responseHeaders[HttpConstants.HttpHeaders.RequestCharge] = this.requestChargeTracker .GetAndResetCharge() .ToString(CultureInfo.InvariantCulture); return(responseHeaders); }
/// <summary> /// Gets the response headers for the context. /// </summary> /// <returns>The response headers for the context.</returns> public CosmosQueryResponseMessageHeaders GetResponseHeaders() { string continuationToken = this.ContinuationToken; if (continuationToken == "[]") { throw new InvalidOperationException("Somehow a document query execution context returned an empty array of continuations."); } CosmosQueryResponseMessageHeaders responseHeaders = new CosmosQueryResponseMessageHeaders(continuationToken, null, this.ResourceTypeEnum, this.ResourceLink); this.SetQueryMetrics(); IReadOnlyDictionary <string, QueryMetrics> groupedQueryMetrics = this.GetQueryMetrics(); if (groupedQueryMetrics != null && groupedQueryMetrics.Count != 0) { responseHeaders[HttpConstants.HttpHeaders.QueryMetrics] = QueryMetrics .CreateFromIEnumerable(groupedQueryMetrics.Values) .ToDelimitedString(); } responseHeaders.RequestCharge = this.requestChargeTracker .GetAndResetCharge(); return(responseHeaders); }
/// <summary> /// Adds all QueryMetrics in a list along with the current instance. /// </summary> /// <param name="queryMetricsList">The list to sum up.</param> /// <returns>A new QueryMetrics instance that is the sum of the current instance and the list.</returns> internal QueryMetrics Add(params QueryMetrics[] queryMetricsList) { List <QueryMetrics> combinedQueryMetricsList = new List <QueryMetrics>(queryMetricsList.Length + 1) { this }; combinedQueryMetricsList.AddRange(queryMetricsList); return(QueryMetrics.CreateFromIEnumerable(combinedQueryMetricsList)); }
/// <summary> /// Since query metrics are being aggregated asynchronously to the feed reponses as explained in the member documentation, /// this function allows us to take a snapshot of the query metrics. /// </summary> private void SetQueryMetrics() { this.groupedQueryMetrics = Interlocked.Exchange(ref this.partitionedQueryMetrics, new ConcurrentBag <Tuple <string, QueryMetrics> >()) .GroupBy(tuple => tuple.Item1, tuple => tuple.Item2) .ToDictionary(group => group.Key, group => QueryMetrics.CreateFromIEnumerable(group)); }