Skip to content

Commit 3d06017

Browse files
#347 add scroll api functionality to requesting logs from OS
1 parent 4de10b2 commit 3d06017

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

src/Dorc.OpenSearchData/Sources/DeploymentLogService.cs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,57 @@ public void EnrichDeploymentResultsWithLogs(IEnumerable<DeploymentResultApiModel
4444
private IEnumerable<DeployOpenSearchLogModel> GetLogsFromOpenSearch(List<int> requestIds, List<int> deploymentResultIds)
4545
{
4646
var logs = new List<DeployOpenSearchLogModel>();
47+
const string scrollTimeout = "1m";
4748

48-
for (int pageNumber = 1; ; pageNumber++)
49+
var searchResponse = _openSearchClient.Search<DeployOpenSearchLogModel>(s => s
50+
.Index(_deploymentResultIndex)
51+
.Query(q => q
52+
.Bool(b => b
53+
.Must(must => must
54+
.Terms(t => t
55+
.Field(field => field.deployment_result_id)
56+
.Terms(deploymentResultIds)),
57+
must => must
58+
.Terms(t => t
59+
.Field(field => field.request_id)
60+
.Terms(requestIds)))))
61+
.Size(_pageSize)
62+
.Scroll(scrollTimeout)
63+
);
64+
65+
if (!searchResponse.IsValid)
4966
{
67+
_logger.Error($"OpenSearch query exception: {searchResponse.OriginalException?.Message}.{Environment.NewLine}Request information: {searchResponse.DebugInformation}");
68+
return logs;
69+
}
5070

51-
var searchResult = _openSearchClient.Search<DeployOpenSearchLogModel>(s => s
52-
.Index(_deploymentResultIndex)
53-
.Query(q => q
54-
.Bool(b => b
55-
.Must(must => must
56-
.Terms(t => t
57-
.Field(field => field.deployment_result_id)
58-
.Terms(deploymentResultIds)),
59-
must => must
60-
.Terms(t => t
61-
.Field(field => field.request_id)
62-
.Terms(requestIds)))))
63-
.From((pageNumber - 1) * _pageSize)
64-
.Size(_pageSize));
65-
66-
if (!searchResult.IsValid)
71+
logs.AddRange(searchResponse.Documents);
72+
73+
var scrollId = searchResponse.ScrollId;
74+
while (!string.IsNullOrEmpty(scrollId))
75+
{
76+
var scrollResponse = _openSearchClient.Scroll<DeployOpenSearchLogModel>(scrollTimeout, scrollId);
77+
78+
if (!scrollResponse.IsValid)
6779
{
68-
_logger.Error($"OpenSearch query exception: {searchResult.OriginalException?.Message}.{Environment.NewLine}Request information: {searchResult.DebugInformation}");
69-
return logs;
80+
_logger.Error($"OpenSearch scroll query exception: {scrollResponse.OriginalException?.Message}.{Environment.NewLine}Request information: {scrollResponse.DebugInformation}");
81+
break;
7082
}
7183

72-
if (searchResult.Documents != null && searchResult.Documents.Any())
73-
logs.AddRange(searchResult.Documents);
84+
if (scrollResponse.Documents != null && scrollResponse.Documents.Any())
85+
{
86+
logs.AddRange(scrollResponse.Documents);
87+
scrollId = scrollResponse.ScrollId;
88+
}
7489
else
90+
{
7591
break;
92+
}
93+
}
94+
95+
if (!string.IsNullOrEmpty(scrollId))
96+
{
97+
_openSearchClient.ClearScroll(c => c.ScrollId(scrollId));
7698
}
7799

78100
return logs;

0 commit comments

Comments
 (0)