Skip to content

Commit 1831f44

Browse files
fix: ensure payload filter returns all request results
(cherry picked from commit e1f4209)
1 parent 94c05eb commit 1831f44

File tree

3 files changed

+224
-10
lines changed

3 files changed

+224
-10
lines changed

gravitee-apim-repository/gravitee-apim-repository-elasticsearch/src/main/java/io/gravitee/repository/elasticsearch/log/ElasticLogRepository.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public class ElasticLogRepository extends AbstractElasticsearchRepository implem
7171
@Autowired
7272
protected RepositoryConfiguration configuration;
7373

74+
private static final int MAX_RESULT_WINDOW = 10000;
75+
7476
@Override
7577
public TabularResponse query(final QueryContext queryContext, final TabularQuery query) throws AnalyticsException {
7678
final Long from = query.timeRange().range().from();
@@ -89,8 +91,8 @@ public TabularResponse query(final QueryContext queryContext, final TabularQuery
8991
} else {
9092
final TabularQuery logQuery = tabular()
9193
.timeRange(query.timeRange().range(), query.timeRange().interval())
92-
.page(query.page())
93-
.size(query.size())
94+
.page(1)
95+
.size(MAX_RESULT_WINDOW)
9496
.query(logQueryString)
9597
.build();
9698
final String sQuery = this.createElasticsearchJsonQuery(logQuery);
@@ -114,7 +116,7 @@ public TabularResponse query(final QueryContext queryContext, final TabularQuery
114116
final String requestQuery = isEmpty(queryString) ? logIdsQuery : format("(%s) AND (%s)", queryString, logIdsQuery);
115117
final TabularQueryBuilder requestQueryBuilder = tabular()
116118
.timeRange(query.timeRange().range(), query.timeRange().interval())
117-
.page(1)
119+
.page(query.page())
118120
.size(query.size())
119121
.sort(query.sort())
120122
.query(requestQuery);
@@ -129,12 +131,7 @@ public TabularResponse query(final QueryContext queryContext, final TabularQuery
129131
}
130132

131133
SearchResponse searchResponseRequest = result.blockingGet();
132-
return this.toTabularResponse(
133-
searchResponseRequest,
134-
searchResponseRequest.getSearchHits().getTotal().getValue() == query.size() || query.page() > 1
135-
? searchResponseLog.getSearchHits().getTotal().getValue()
136-
: searchResponseRequest.getSearchHits().getTotal().getValue()
137-
);
134+
return this.toTabularResponse(searchResponseRequest, searchResponseRequest.getSearchHits().getTotal().getValue());
138135
}
139136
} catch (final Exception eex) {
140137
logger.error("Impossible to perform log request", eex);

gravitee-apim-repository/gravitee-apim-repository-elasticsearch/src/test/java/io/gravitee/repository/elasticsearch/log/ElasticsearchLogRepositoryTest.java

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,221 @@ public void testTabular_withoutQuery() throws Exception {
134134
assertThat(response).isNotNull();
135135
assertThat(response.getLogs().size()).isEqualTo(response.getSize());
136136
}
137+
138+
@Test
139+
public void testTabular_withCombinedFilters_statusAndBody() throws Exception {
140+
TabularResponse response = logRepository.query(
141+
queryContext,
142+
tabular()
143+
.timeRange(lastDays(60), hours(1))
144+
.query("status:200 AND client-response.body:*not valid or is expired*")
145+
.page(1)
146+
.size(10)
147+
.build()
148+
);
149+
150+
assertThat(response).isNotNull();
151+
assertThat(response.getSize()).isLessThanOrEqualTo(6);
152+
assertThat(response.getLogs()).hasSizeLessThanOrEqualTo((int) response.getSize());
153+
}
154+
155+
@Test
156+
public void testTabular_withCombinedFilters_pagination_page1() throws Exception {
157+
TabularResponse response = logRepository.query(
158+
queryContext,
159+
tabular()
160+
.timeRange(lastDays(60), hours(1))
161+
.query("status:200 AND client-response.body:*not valid or is expired*")
162+
.page(1)
163+
.size(3)
164+
.build()
165+
);
166+
167+
assertThat(response).isNotNull();
168+
long totalSize = response.getSize();
169+
assertThat(response.getLogs()).hasSizeLessThanOrEqualTo(3);
170+
assertThat(totalSize).isGreaterThanOrEqualTo(response.getLogs().size());
171+
}
172+
173+
@Test
174+
public void testTabular_withCombinedFilters_pagination_page2() throws Exception {
175+
TabularResponse responsePage1 = logRepository.query(
176+
queryContext,
177+
tabular()
178+
.timeRange(lastDays(60), hours(1))
179+
.query("status:200 AND client-response.body:*not valid or is expired*")
180+
.page(1)
181+
.size(3)
182+
.build()
183+
);
184+
185+
TabularResponse responsePage2 = logRepository.query(
186+
queryContext,
187+
tabular()
188+
.timeRange(lastDays(60), hours(1))
189+
.query("status:200 AND client-response.body:*not valid or is expired*")
190+
.page(2)
191+
.size(3)
192+
.build()
193+
);
194+
195+
assertThat(responsePage2).isNotNull();
196+
assertThat(responsePage2.getSize()).isEqualTo(responsePage1.getSize());
197+
int expectedPage2Size = (int) Math.max(0, responsePage1.getSize() - 3);
198+
assertThat(responsePage2.getLogs()).hasSizeLessThanOrEqualTo(expectedPage2Size);
199+
}
200+
201+
@Test
202+
public void testTabular_withCombinedFilters_differentPageSizes() throws Exception {
203+
TabularResponse responseSize10 = logRepository.query(
204+
queryContext,
205+
tabular()
206+
.timeRange(lastDays(60), hours(1))
207+
.query("status:200 AND client-response.body:*not valid or is expired*")
208+
.page(1)
209+
.size(10)
210+
.build()
211+
);
212+
213+
TabularResponse responseSize15 = logRepository.query(
214+
queryContext,
215+
tabular()
216+
.timeRange(lastDays(60), hours(1))
217+
.query("status:200 AND client-response.body:*not valid or is expired*")
218+
.page(1)
219+
.size(15)
220+
.build()
221+
);
222+
223+
TabularResponse responseSize100 = logRepository.query(
224+
queryContext,
225+
tabular()
226+
.timeRange(lastDays(60), hours(1))
227+
.query("status:200 AND client-response.body:*not valid or is expired*")
228+
.page(1)
229+
.size(100)
230+
.build()
231+
);
232+
assertThat(responseSize10.getSize()).isEqualTo(responseSize15.getSize()).isEqualTo(responseSize100.getSize());
233+
}
234+
235+
@Test
236+
public void testTabular_bodyFilterOnly_pagination_consistent() throws Exception {
237+
String bodyQuery = "client-response.body:*not valid or is expired*";
238+
239+
TabularResponse response1 = logRepository.query(
240+
queryContext,
241+
tabular().timeRange(lastDays(60), hours(1)).query(bodyQuery).page(1).size(10).build()
242+
);
243+
244+
TabularResponse response2 = logRepository.query(
245+
queryContext,
246+
tabular().timeRange(lastDays(60), hours(1)).query(bodyQuery).page(1).size(15).build()
247+
);
248+
249+
// Total should be consistent
250+
assertThat(response1.getSize()).isEqualTo(response2.getSize());
251+
assertThat(response1.getSize()).isEqualTo(6);
252+
}
253+
254+
@Test
255+
public void testTabular_emptyResults_withCombinedFilters() throws Exception {
256+
TabularResponse response = logRepository.query(
257+
queryContext,
258+
tabular()
259+
.timeRange(lastDays(60), hours(1))
260+
.query("status:999 AND client-response.body:*nonexistent-text-12345*")
261+
.page(1)
262+
.size(10)
263+
.build()
264+
);
265+
266+
assertThat(response).isNotNull();
267+
assertThat(response.getSize()).isZero();
268+
assertThat(response.getLogs()).isEmpty();
269+
}
270+
271+
@Test
272+
public void testTabular_statusFilterOnly() throws Exception {
273+
TabularResponse response = logRepository.query(
274+
queryContext,
275+
tabular().timeRange(lastDays(60), hours(1)).query("status:200").page(1).size(10).build()
276+
);
277+
278+
assertThat(response).isNotNull();
279+
assertThat(response.getSize()).isGreaterThan(0);
280+
assertThat(response.getLogs()).isNotEmpty();
281+
}
282+
283+
@Test
284+
public void testTabular_multipleStatusFilters_withBody() throws Exception {
285+
TabularResponse response = logRepository.query(
286+
queryContext,
287+
tabular()
288+
.timeRange(lastDays(60), hours(1))
289+
.query("status:200 AND client-response.body:*not valid or is expired*")
290+
.page(1)
291+
.size(10)
292+
.build()
293+
);
294+
295+
assertThat(response).isNotNull();
296+
assertThat(response.getLogs()).hasSizeLessThanOrEqualTo((int) response.getSize());
297+
}
298+
299+
@Test
300+
public void testTabular_paginationBeyondResults() throws Exception {
301+
TabularResponse responsePage1 = logRepository.query(
302+
queryContext,
303+
tabular().timeRange(lastDays(60), hours(1)).query("client-response.body:*not valid or is expired*").page(1).size(10).build()
304+
);
305+
306+
// Request page 10 when we only have 6 results
307+
TabularResponse responsePage10 = logRepository.query(
308+
queryContext,
309+
tabular().timeRange(lastDays(60), hours(1)).query("client-response.body:*not valid or is expired*").page(10).size(10).build()
310+
);
311+
312+
assertThat(responsePage10).isNotNull();
313+
assertThat(responsePage10.getSize()).isEqualTo(responsePage1.getSize()); // Total should be same
314+
assertThat(responsePage10.getLogs()).isEmpty(); // No results on page 10
315+
}
316+
317+
@Test
318+
public void testTabular_largePaginationSize() throws Exception {
319+
TabularResponse response = logRepository.query(
320+
queryContext,
321+
tabular().timeRange(lastDays(60), hours(1)).query("client-response.body:*not valid or is expired*").page(1).size(1000).build()
322+
);
323+
324+
assertThat(response).isNotNull();
325+
assertThat(response.getSize()).isEqualTo(6);
326+
assertThat(response.getLogs()).hasSize(6); // Should return all available results
327+
}
328+
329+
@Test
330+
public void testTabular_caseInsensitiveBodySearch() throws Exception {
331+
TabularResponse responseLower = logRepository.query(
332+
queryContext,
333+
tabular().timeRange(lastDays(60), hours(1)).query("client-response.body:*not valid*").page(1).size(10).build()
334+
);
335+
336+
TabularResponse responseUpper = logRepository.query(
337+
queryContext,
338+
tabular().timeRange(lastDays(60), hours(1)).query("client-response.body:*NOT VALID*").page(1).size(10).build()
339+
);
340+
assertThat(responseLower).isNotNull();
341+
assertThat(responseUpper).isNotNull();
342+
}
343+
344+
@Test
345+
public void testTabular_complexQuery_multipleConditions() throws Exception {
346+
TabularResponse response = logRepository.query(
347+
queryContext,
348+
tabular().timeRange(lastDays(60), hours(1)).query("status:200 AND client-response.body:*valid*").page(1).size(10).build()
349+
);
350+
351+
assertThat(response).isNotNull();
352+
assertThat(response.getLogs()).hasSizeLessThanOrEqualTo((int) response.getSize());
353+
}
137354
}

gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/LogsServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public SearchLogResponse<ApiRequestItem> findByApi(final ExecutionContext execut
158158
logResponse.setLogs(response.getLogs().stream().map(this::toApiRequestItem).collect(Collectors.toList()));
159159

160160
// Add metadata (only if they are results)
161-
if (response.getSize() > 0) {
161+
if (response.getLogs() != null && !response.getLogs().isEmpty()) {
162162
Map<String, Map<String, String>> metadata = new HashMap<>();
163163

164164
logResponse

0 commit comments

Comments
 (0)