Пример #1
0
        /// <summary>
        /// List conversations in the channelId.
        /// </summary>
        /// <param name="channelId">Channel Id.</param>
        /// <param name="continuationToken">Continuatuation token to page through results.</param>
        /// <returns>A <see cref="Task"/> A task that represents the work queued to execute.</returns>
        public async Task<PagedResult<TranscriptInfo>> ListTranscriptsAsync(string channelId, string continuationToken = null)
        {
            if (string.IsNullOrEmpty(channelId))
            {
                throw new ArgumentNullException($"missing {nameof(channelId)}");
            }

            var dirName = GetDirName(channelId);
            var dir = this.Container.Value.GetDirectoryReference(dirName);
            var pageSize = 20;
            BlobContinuationToken token = null;
            List<TranscriptInfo> conversations = new List<TranscriptInfo>();
            do
            {
                var segment = await dir.ListBlobsSegmentedAsync(false, BlobListingDetails.Metadata, null, token, null, null).ConfigureAwait(false);

                foreach (var blob in segment.Results.Where(c => c is CloudBlobDirectory).Cast<CloudBlobDirectory>())
                {
                    // Unescape the Id we escaped when we saved it
                    var conversation = new TranscriptInfo() { Id = Uri.UnescapeDataString(blob.Prefix.Split('/').Where(s => s.Length > 0).Last()), ChannelId = channelId };
                    if (continuationToken != null)
                    {
                        if (conversation.Id == continuationToken)
                        {
                            // we found continuation token
                            continuationToken = null;
                        }

                        // skip record
                    }
                    else
                    {
                        conversations.Add(conversation);
                        if (conversations.Count == pageSize)
                        {
                            break;
                        }
                    }
                }

                if (segment.ContinuationToken != null)
                {
                    token = segment.ContinuationToken;
                }
            }
            while (token != null && conversations.Count < pageSize);

            var pagedResult = new PagedResult<TranscriptInfo>();
            pagedResult.Items = conversations.ToArray();

            if (pagedResult.Items.Length == 20)
            {
                pagedResult.ContinuationToken = pagedResult.Items.Last().Id;
            }

            return pagedResult;
        }
        /// <summary>
        /// List conversations in the channelId.
        /// </summary>
        /// <param name="channelId">Channel Id.</param>
        /// <param name="continuationToken">Continuatuation token to page through results.</param>
        /// <returns>A <see cref="Task"/> A task that represents the work queued to execute.</returns>
        public async Task <PagedResult <TranscriptInfo> > ListTranscriptsAsync(string channelId, string continuationToken = null)
        {
            const int PageSize = 20;

            if (string.IsNullOrEmpty(channelId))
            {
                throw new ArgumentNullException($"missing {nameof(channelId)}");
            }

            string token = null;
            List <TranscriptInfo> conversations = new List <TranscriptInfo>();

            do
            {
                var resultSegment = _containerClient.Value.GetBlobsAsync(BlobTraits.Metadata, prefix: $"{SanitizeKey(channelId)}/")
                                    .AsPages(token).ConfigureAwait(false);
                token = null;

                await foreach (var blobPage in resultSegment)
                {
                    foreach (BlobItem blobItem in blobPage.Values)
                    {
                        // Unescape the Id we escaped when we saved it
                        var conversation = new TranscriptInfo()
                        {
                            Id = Uri.UnescapeDataString(blobItem.Name.Split('/').Last(s => s.Length > 0)), ChannelId = channelId
                        };
                        if (continuationToken != null)
                        {
                            if (conversation.Id == continuationToken)
                            {
                                // we found continuation token
                                continuationToken = null;
                            }

                            // skip record
                        }
                        else
                        {
                            conversations.Add(conversation);
                            if (conversations.Count == PageSize)
                            {
                                break;
                            }
                        }
                    }
                }
            }while (!string.IsNullOrEmpty(token) && conversations.Count < PageSize);

            var pagedResult = new PagedResult <TranscriptInfo>()
            {
                Items = conversations.ToArray()
            };

            if (pagedResult.Items.Length == PageSize)
            {
                pagedResult.ContinuationToken = pagedResult.Items.Last().Id;
            }

            return(pagedResult);
        }
        /// <summary>
        /// List conversations in the channelId.
        /// </summary>
        /// <param name="channelId">Channel Id.</param>
        /// <param name="continuationToken">Continuation token to page through results.</param>
        /// <returns>A <see cref="Task"/> A task that represents the work queued to execute.</returns>
        public async Task <PagedResult <TranscriptInfo> > ListTranscriptsAsync(string channelId, string continuationToken = null)
        {
            if (string.IsNullOrEmpty(channelId))
            {
                throw new ArgumentNullException($"missing {nameof(channelId)}");
            }

            int pageStart = 0;
            int pageSize  = 20;

            // Ensure Initialization has been run
            await InitializeAsync().ConfigureAwait(false);

            if (continuationToken != null)
            {
                bool result = int.TryParse(continuationToken, out pageStart);

                if (!result)
                {
                    throw new InvalidOperationException(nameof(result));
                }
            }

            List <TranscriptInfo> conversations = new List <TranscriptInfo>();

            var pagedResult = new PagedResult <TranscriptInfo>();

            var searchResponse = await elasticClient.SearchAsync <DocumentItem>(s => s
                                                                                .Index(indexName)
                                                                                .Sort(ss => ss
                                                                                      .Ascending(p => p.Timestamp))
                                                                                .From(pageStart)
                                                                                .Size(pageSize)
                                                                                .Query(q => q
                                                                                       .MatchPhrase(m => m
                                                                                                    .Field(f => f.ChannelId)
                                                                                                    .Query(channelId)))).ConfigureAwait(false);

            var documentItems = searchResponse.Documents;

            foreach (var documentItem in documentItems)
            {
                var conversation = new TranscriptInfo {
                    Id = documentItem.Id, ChannelId = documentItem.ChannelId, Created = (DateTimeOffset)documentItem.Timestamp
                };
                conversations.Add(conversation);
            }

            pagedResult.Items = conversations.ToArray();

            if (pagedResult.Items.Length == pageSize)
            {
                pagedResult.ContinuationToken = $"{pageStart + pageSize}";
            }
            else
            {
                pagedResult.ContinuationToken = null;
            }

            return(pagedResult);
        }