Skip to content

Commit 6a54a34

Browse files
authored
Corrected the counting of the total number of items in the result list (#125)
Corrected the counting of the total number of items in the result list.
1 parent 39345a0 commit 6a54a34

File tree

5 files changed

+27
-60
lines changed

5 files changed

+27
-60
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
### Fixed
1717

1818
- Fixed the broken production version due to a missing package (#123).
19+
- Corrected the counting of the total number of items in the result list (#120).
1920

2021
## [1.2.0] - 2024-05-07
2122

cypress/e2e/total-items.cy.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
describe("Total items", () => {
2+
it("The total number of results must be correct, even if not all SPARQL query variables used in the WHERE clause are selected for display in the SELECT clause", () => {
3+
cy.visit("/");
4+
cy.contains("For testing only").click();
5+
cy.contains("A test on counting the total number results").click();
6+
cy.contains("1-10 of 20");
7+
});
8+
});

public/queries/so_distinct.rq

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT DISTINCT ?s ?o WHERE {
2+
?s ?p ?o
3+
}

src/config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,16 @@
279279
"http://localhost:8080/example/dup2"
280280
]
281281
}
282+
},
283+
{
284+
"id": "9050",
285+
"queryGroupId": "c-tst",
286+
"queryLocation": "so_distinct.rq",
287+
"name": "A test on counting the total number results",
288+
"description": "Query a source containing ?s ?p1 ?o and ?s ?p2 ?o with p1 != p2 and request only ?s ?o",
289+
"comunicaContext": {
290+
"sources": ["http://localhost:8080/example/dup1"]
291+
}
282292
}
283293
]
284294
}

src/dataProvider/SparqlDataProvider.js

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ configManager.on('configChanged', onConfigChanged);
3535

3636
export default {
3737
getList: async function getList(resource, { pagination, sort, filter, meta }) {
38-
3938
const query = findQueryWithId(resource);
40-
query.limit = pagination.perPage;
41-
query.offset = (pagination.page - 1) * pagination.perPage;
39+
const limit = pagination.perPage;
40+
const offset = (pagination.page - 1) * pagination.perPage;
4241
query.sort = sort;
4342

4443
handleComunicaContextCreation(query);
@@ -53,6 +52,8 @@ export default {
5352
}
5453

5554
let results = await executeQuery(query);
55+
let totalItems = results.length;
56+
results = results.slice(offset, offset + limit);
5657

5758
if (Object.keys(filter).length > 0) {
5859
results = results.filter((result) => {
@@ -64,7 +65,7 @@ export default {
6465

6566
return {
6667
data: results,
67-
total: query.totalItems
68+
total: totalItems
6869
};
6970
},
7071
getOne: async function getOne() {
@@ -125,16 +126,6 @@ async function fetchQuery(query) {
125126
if (!query.variableOntology) {
126127
query.variableOntology = findPredicates(parsedQuery);
127128
}
128-
if (parsedQuery.limit !== undefined && query.offset + query.limit > parsedQuery.limit) {
129-
parsedQuery.limit = parsedQuery.limit - query.offset;
130-
} else {
131-
parsedQuery.limit = query.limit;
132-
}
133-
if (parsedQuery.offset) {
134-
parsedQuery.offset += query.offset;
135-
} else {
136-
parsedQuery.offset = query.offset;
137-
}
138129
if (!parsedQuery.order && query.sort && query.sort.field !== "id") {
139130
const { field, order } = query.sort;
140131
parsedQuery.order = [
@@ -290,7 +281,6 @@ async function handleQueryExecution(execution, query) {
290281
const resultType = execution.resultType;
291282
if (execution.resultType !== "boolean") {
292283
const metadata = await execution.metadata();
293-
query.totalItems = await countQueryResults(query);
294284
variables = metadata.variables.map((val) => {
295285
return val.value;
296286
});
@@ -301,51 +291,6 @@ async function handleQueryExecution(execution, query) {
301291
}
302292
}
303293

304-
/**
305-
* Predict the total number of elements in the result of a query
306-
* @param {object} query - the query element from the configuration
307-
* @returns {number} the actual number of results in the query, if it were executed
308-
*/
309-
async function countQueryResults(query) {
310-
const parser = new Parser();
311-
const parsedQuery = parser.parse(query.rawText);
312-
const distinctInitial = parsedQuery.distinct;
313-
const offsetInitial = parsedQuery.offset;
314-
const limitInitial = parsedQuery.limit;
315-
parsedQuery.queryType = "SELECT";
316-
parsedQuery.distinct = false;
317-
parsedQuery.offset = 0;
318-
if (parsedQuery.limit) {
319-
delete parsedQuery.limit;
320-
}
321-
parsedQuery.variables = [
322-
{
323-
expression: {
324-
type: "aggregate",
325-
aggregation: "count",
326-
expression: { termType: "Wildcard", value: "*" },
327-
distinct: distinctInitial
328-
},
329-
variable: { termType: "Variable", value: "totalItems" },
330-
},
331-
];
332-
const generator = new Generator();
333-
const countQuery = generator.stringify(parsedQuery);
334-
const bindings = await myEngine.queryBindings(countQuery, {
335-
sources: query.comunicaContext.sources,
336-
fetch: fetch,
337-
httpProxyHandler: proxyHandler,
338-
});
339-
let totalItems = parseInt((await bindings.toArray())[0].get("totalItems").value);
340-
if (offsetInitial) {
341-
totalItems -= offsetInitial;
342-
}
343-
if (limitInitial && totalItems > limitInitial) {
344-
totalItems = limitInitial;
345-
}
346-
return totalItems;
347-
}
348-
349294
const queryTypeHandlers = {
350295
bindings: configureBindingStream,
351296
quads: configureQuadStream,

0 commit comments

Comments
 (0)