private Task ProduceAccountActionsAsync(TrackActionQuery query, ITargetBlock <Action[]> actionQueue)
 {
     return(ProduceParticipantActionsAsync(
                () => _searchServices.FindAccountsAsync(query.AccountQuery),
                accounts => _searchServices.FindActionsByAccountsAsync(query.ActionQuery, accounts),
                actionQueue,
                _batchOptions.Value.AccountActionsDatabaseSearchMaxDegreeOfParallelism
                ).Unwrap());
 }
        public async Task <Action[]> FindActionsAsync(TrackActionQuery query)
        {
            var actionQueue = new BufferBlock <Action[]>();

            var produceTask = ProduceActionsAsync(query, actionQueue).Unwrap();

            var mergeActionsTask = MergeActionsAsync(actionQueue, query.ActionQuery.Top);

            await Task.WhenAll(mergeActionsTask, produceTask.ContinueWith(_ => actionQueue.Complete()));

            return(mergeActionsTask.Result);
        }
        private async Task <Task> ProduceActionsAsync(TrackActionQuery query, ITargetBlock <Action[]> actionQueue)
        {
            if (query.AccountQuery == null && query.ContactQuery == null)
            {
                bool sendStatus = await actionQueue.SendAsync(await _searchServices.FindActionsAsync(query.ActionQuery));

                if (!sendStatus)
                {
                    throw new InvalidOperationException("Revisit bottleneck");
                }

                return(Task.CompletedTask);
            }

            var produceAccountActionTask = query.AccountQuery != null?ProduceAccountActionsAsync(query, actionQueue) : Task.CompletedTask;

            var produceContactActionTask = query.ContactQuery != null?ProduceContactActionsAsync(query, actionQueue) : Task.CompletedTask;

            return(Task.WhenAll(produceAccountActionTask, produceContactActionTask));
        }