private async Task UpdateBulk(ICollection <int> deleteIdSet, ICollection <QnAElement> addSet, ICollection <QnAElementUpdate> updateSet) { var qnaRequest = new QnAMakerUpdateRequest(); if (deleteIdSet?.Count > 0) { qnaRequest.Delete = new QnAMakerDeleteSet { Ids = deleteIdSet } } ; if (addSet?.Count > 0) { qnaRequest.Add = new QnAMakerAddSet { QnaList = addSet } } ; if (updateSet?.Count > 0) { qnaRequest.Update = new QnAMakerUpdateSet { Name = null, QnaList = updateSet } } ; if (qnaRequest.Delete != null || qnaRequest.Add != null || qnaRequest.Update != null) { await Update(qnaRequest); } }
public async Task Update(QnAMakerUpdateRequest request) { var payload = JsonConvert.SerializeObject(request); var method = "/knowledgebases/"; string uri = host + service + method + _knowledgebaseId; var response = await Patch(uri, payload); response.EnsureSuccess(); var operation = response.Headers.TryGetValues("Location", out var locationHeaders) ? locationHeaders.FirstOrDefault() : null; if (string.IsNullOrEmpty(operation)) { return; } var done = false; while (true != done) { response = await GetStatus(operation); var state = JToken.Parse(response.Body)?.Value <string>("operationState") ?? string.Empty; if (state.CompareTo("Running") == 0 || state.CompareTo("NotStarted") == 0) { var wait = response.Headers.TryGetValues("Retry-After", out var retryHeaders) ? retryHeaders.FirstOrDefault() : null; var waitInSeconds = int.TryParse(wait ?? string.Empty, out var waitParse) ? waitParse : 30; Thread.Sleep(waitInSeconds * 1000); } else { done = true; } } }
private async Task UpdateBatchwise(ICollection <int> deleteIdSet, ICollection <QnAElement> addSet, ICollection <QnAElementUpdate> updateSet) { var delCount = deleteIdSet?.Count ?? 0; var addCount = addSet?.Count ?? 0; var updCount = updateSet?.Count ?? 0; var totalCount = delCount + addCount + updCount; if (totalCount == 0) { return; } if (totalCount <= updateBatchSize) { await UpdateBulk(deleteIdSet, addSet, updateSet); return; } var counter = 0; while (counter < totalCount) { var setSize = updateBatchSize; var qnaRequest = new QnAMakerUpdateRequest(); if (setSize > 0 && delCount > counter) { var set = deleteIdSet.Skip(counter).Take(setSize).ToArray(); setSize -= set.Length; counter += set.Length; qnaRequest.Delete = new QnAMakerDeleteSet { Ids = set }; } if (setSize > 0 && delCount + addCount > counter) { var set = addSet.Skip(counter - delCount).Take(setSize).ToArray(); setSize -= set.Length; counter += set.Length; qnaRequest.Add = new QnAMakerAddSet { QnaList = set }; } if (setSize > 0 && delCount + addCount + updCount > counter) { var set = updateSet.Skip(counter - delCount - addCount).Take(setSize).ToArray(); setSize -= set.Length; counter += set.Length; qnaRequest.Update = new QnAMakerUpdateSet { Name = null, QnaList = set }; } await Update(qnaRequest); } }