public async Task <bool> CheckActiveReindexJobsAsync(CancellationToken cancellationToken) { try { var query = _queryFactory.Create <CosmosReindexJobRecordWrapper>( _containerScope.Value, new CosmosQueryContext( new QueryDefinition(CheckActiveJobsByStatusQuery), new QueryRequestOptions { PartitionKey = new PartitionKey(CosmosDbReindexConstants.ReindexJobPartitionKey) })); FeedResponse <CosmosReindexJobRecordWrapper> result = await query.ExecuteNextAsync(); if (result.Any()) { return(true); } return(false); } catch (CosmosException dce) { if (dce.StatusCode == HttpStatusCode.TooManyRequests) { throw new RequestRateExceededException(dce.RetryAfter); } _logger.LogError(dce, "Failed to check if any reindex jobs are active."); throw; } }
public async Task <(bool, string)> CheckActiveReindexJobsAsync(CancellationToken cancellationToken) { try { var query = _queryFactory.Create <CosmosReindexJobRecordWrapper>( _containerScope.Value, new CosmosQueryContext( new QueryDefinition(CheckActiveJobsByStatusQuery), new QueryRequestOptions { PartitionKey = new PartitionKey(CosmosDbReindexConstants.ReindexJobPartitionKey) })); FeedResponse <CosmosReindexJobRecordWrapper> result = await query.ExecuteNextAsync(); if (result.Any()) { return(true, result.FirstOrDefault().JobRecord.Id); } return(false, string.Empty); } catch (CosmosException dce) { if (dce.IsRequestRateExceeded()) { throw; } _logger.LogError(dce, "Failed to check if any reindex jobs are active."); throw; } }
public async Task <bool> CheckActiveReindexJobsAsync(CancellationToken cancellationToken) { try { IDocumentQuery <int> query = _documentClientScope.Value.CreateDocumentQuery <int>( CollectionUri, new SqlQuerySpec(CheckActiveJobsByStatusQuery), new FeedOptions { PartitionKey = new PartitionKey(CosmosDbReindexConstants.ReindexJobPartitionKey) }) .AsDocumentQuery(); FeedResponse <CosmosReindexJobRecordWrapper> result = await query.ExecuteNextAsync <CosmosReindexJobRecordWrapper>(); if (result.Any()) { return(true); } return(false); } catch (DocumentClientException dce) { if (dce.StatusCode == HttpStatusCode.TooManyRequests) { throw new RequestRateExceededException(dce.RetryAfter); } _logger.LogError(dce, "Failed to check if any reindex jobs are active."); throw; } }
public async Task <PagedList <DocumentDetails> > GetPagedCorrespondenceByInvestorId(int investorId, int pageNumber = 1, int pageSize = 10) { var cacheKey = string.Format($"customer_corro_{investorId}"); if (_cache.Get(cacheKey) != null) { var list = _cache.Get(cacheKey) as List <DocumentDetails>; if (list != null) { return(new PagedList <DocumentDetails>(list.AsQueryable(), pageNumber, pageSize)); } } var result = new List <DocumentDetails>(); using (var client = new CosmosClient(_functionSettings.CosmosDBEndpoint, _functionSettings.CosmosDBMasterKey)) { var container = client.GetContainer(_functionSettings.CosmosDBName, SQLStatements.InvestorDocuments.ContainerName); var sql = SQLStatements.InvestorDocuments.ListInvestorDocs; var query = new QueryDefinition(sql); query.WithParameter("@InvestorId", investorId); var iterator = container .GetItemQueryIterator <InvestorDocument>(query, requestOptions: new QueryRequestOptions() { MaxConcurrency = 1 }); while (iterator.HasMoreResults) { var task = await iterator.ReadNextAsync(); FeedResponse <InvestorDocument> response = task; if (response.Any()) { foreach (var item in response) { result.AddRange(item.Corro); } var expireTime = DateTimeOffset.Now.AddMinutes(_functionSettings.CacheInMinutes); _cache.Set(cacheKey, result, expireTime); } } } return(new PagedList <DocumentDetails>(result.AsQueryable(), pageNumber, pageSize)); }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns>Document</returns> public static async Task <Document> GetTodoItemById(Guid id) { // Best way if selfLink is not available... IDocumentQuery <Document> query = ( from doc in client.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId)) where doc.Id.Equals(id.ToString()) select doc ).AsDocumentQuery(); Document documentToReturn = null; while (query.HasMoreResults) { FeedResponse <Document> res = await query.ExecuteNextAsync <Document>(); if (res.Any()) { documentToReturn = res.Single(); break; } } return(documentToReturn); //FeedOptions queryOptions = new FeedOptions { MaxItemCount = 1 }; // Find by Id //IQueryable<TodoItem> todoQuery = client.CreateDocumentQuery<TodoItem>( // UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), queryOptions) // .Where(todo => todo.Id.Equals(id.ToString())).FirstOrDefault(); //FeedResponse<TodoItem> feedResponse = //IDocumentQuery<TodoItem> query = await client.CreateDocumentQuery<TodoItem>( // UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), queryOptions) // .Where(todo => todo.Id.Equals(id.ToString())).AsDocumentQuery(); //using (var todoQuery = client.CreateDocumentQuery<TodoItem>( // UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), queryOptions) // .Where(todo => todo.Id.Equals(id.ToString())).FirstOrDefault() // .AsDocumentQuery().ExecuteNextAsync(); //{ // return await todoQuery.ExecuteNextAsync().Result; //} }
public async Task <List <PointOfInterest> > GetPointsOfInterestsNearby(Point p) { var items = new List <PointOfInterest>(); var query = client.CreateDocumentQuery <PointOfInterest>(collectionLink, new FeedOptions { MaxItemCount = -1 }) .Where(poi => poi.Location.Distance(p) < 50000) .AsDocumentQuery(); while (query.HasMoreResults) { FeedResponse <PointOfInterest> res = await query.ExecuteNextAsync <PointOfInterest>(); if (res.Any()) { items.AddRange(res.ToList()); break; } } return(items); }
/// <inheritdoc/> public async Task <InstanceQueryResponse> GetInstancesOfApplication( Dictionary <string, StringValues> queryParams, string continuationToken, int size) { InstanceQueryResponse queryResponse = new InstanceQueryResponse(); FeedOptions feedOptions = new FeedOptions { EnableCrossPartitionQuery = true, MaxItemCount = size, }; if (continuationToken != null) { feedOptions.RequestContinuation = continuationToken; } IQueryable <Instance> queryBuilder = _client.CreateDocumentQuery <Instance>(_collectionUri, feedOptions); try { queryBuilder = BuildQueryFromParameters(queryParams, queryBuilder); } catch (Exception e) { queryResponse.Exception = e.Message; return(queryResponse); } try { IDocumentQuery <Instance> documentQuery = queryBuilder.AsDocumentQuery(); FeedResponse <Instance> feedResponse = await documentQuery.ExecuteNextAsync <Instance>(); if (!feedResponse.Any()) { queryResponse.Count = 0; queryResponse.TotalHits = 0; return(queryResponse); } string nextContinuationToken = feedResponse.ResponseContinuation; logger.LogInformation($"continuation token: {nextContinuationToken}"); // this migth be expensive feedOptions.RequestContinuation = null; int totalHits = queryBuilder.Count(); queryResponse.TotalHits = totalHits; List <Instance> instances = feedResponse.ToList <Instance>(); PostProcess(instances); queryResponse.Instances = instances; queryResponse.ContinuationToken = nextContinuationToken; queryResponse.Count = instances.Count; } catch (Exception e) { logger.LogError("error: {e}"); queryResponse.Exception = e.Message; } return(queryResponse); }
public async Task <Guid> InsertUpdateDocument(InvestorDocumentFlat document) { using (var client = new CosmosClient(_functionSettings.CosmosDBEndpoint, _functionSettings.CosmosDBMasterKey)) { var container = client.GetContainer(_functionSettings.CosmosDBName, SQLStatements.InvestorDocuments.ContainerName); var sql = SQLStatements.InvestorDocuments.ListInvestorDocs; var query = new QueryDefinition(sql); query.WithParameter("@InvestorId", document.InvestorId); var iterator = container .GetItemQueryIterator <InvestorDocument>(query, requestOptions: new QueryRequestOptions() { MaxConcurrency = 1 }); var docDetails = new DocumentDetails() { RefId = document.RefId, Type = document.Type, AccountNo = document.AccountNo, Date = document.Date, ProductCode = document.ProductCode, ProductName = document.ProductName, Link = document.Link }; while (iterator.HasMoreResults) { var task = iterator.ReadNextAsync(); task.Wait(); FeedResponse <InvestorDocument> response = task.Result; if (response.Any()) { var item = response.FirstOrDefault(); if (item != null) { var d = item.Corro.Where(x => x.RefId == document.RefId).FirstOrDefault(); if (d == null) { item.Corro.Add(docDetails); } else { //Only update the link to document d.Link = document.Link; } await container.ReplaceItemAsync(item, item.id.ToString(), new PartitionKey(item.InvestorId)); return(item.id); } } else { break; } } var doc = new InvestorDocument() { InvestorId = document.InvestorId, id = Guid.NewGuid(), Corro = new List <DocumentDetails>() }; doc.Corro.Add(docDetails); var insertResponse = await container.CreateItemAsync(doc, new PartitionKey(doc.InvestorId)); document.id = insertResponse.Resource.id; } return(document.id); }