Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2024.02.xx] - Counter widget sends double requests, the first one has viewport as undefined #10674 #10683 #10705

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('wpsChart enhancer', () => {
document.body.innerHTML = '';
setTimeout(done);
});
it('wpsChart data retrival', (done) => {
it('wpsCounter data retrieval', (done) => {
const Sink = wpsCounter(createSink( ({data, loading} = {}) => {
if (!loading) {
expect(data).toExist();
Expand All @@ -44,7 +44,7 @@ describe('wpsChart enhancer', () => {
};
ReactDOM.render(<Sink {...props} />, document.getElementById("container"));
});
it('wpsChart error management', (done) => {
it('wpsCounter error management', (done) => {
const Sink = wpsCounter(createSink( ({error, loading} = {}) => {
if (!loading && error) {
expect(error).toExist();
Expand All @@ -64,4 +64,28 @@ describe('wpsChart enhancer', () => {
};
ReactDOM.render(<Sink {...props} />, document.getElementById("container"));
});
it('wpsCounter with mapSync and dependencies', (done) => {
const Sink = wpsCounter(createSink( ({data, loading} = {}) => {
if (!loading) {
expect(data).toExist();
done();
}
}));
const props = {
mapSync: true,
dependencies: {
viewport: "..."
},
layer: {
name: "test",
url: 'base/web/client/test-resources/widgetbuilder/aggregate',
wpsUrl: 'base/web/client/test-resources/widgetbuilder/aggregate',
search: {url: 'base/web/client/test-resources/widgetbuilder/aggregate'}},
options: {
aggregateFunction: "Count",
aggregationAttribute: "test"
}
};
ReactDOM.render(<Sink {...props} />, document.getElementById("container"));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,28 @@ describe('triggerFetch stream', () => {
}
);
});
it('triggerFetch with mapSync and dependencies.viewport', (done) => {
const base = {
layer: { name: "TEST" },
mapSync: true
};
const propsChanges = [
base, // does not trigger fetch
{...base, dependencies: { viewport: true }}, // triggers fetch (p1)
{...base, dependencies: { viewport: false }}, // does not trigger fetch
{...base, mapSync: false, filter: "changed"} // triggers fetch (p2) (the filter changes due to the viewport)
];
triggerFetch(Rx.Observable.from(propsChanges))
.bufferCount(4)
.subscribe(
([p1, p2, p3, p4]) => {
expect(p1?.dependencies?.viewport).toBe(true);
expect(p2).toExist();
expect(p3).toNotExist();
expect(p4).toNotExist();
done();
}
);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ const sameSortOptions = (o1 = {}, o2 = {}) =>
* @return {Observable} Stream of props to trigger the data fetch
*/
export default ($props) =>
$props.filter(({ layer = {} }) => layer.name )
$props.filter(({ layer = {}, mapSync, dependencies }) => {
// Check if mapSync is enabled (true) and dependencies.viewport is null or falsy
// If this condition is true, return false to filter out the event.
// This prevents an extra API call from being triggered when the viewport is not available.
if (mapSync && !dependencies?.viewport) {
return false;
}
return layer.name;
} )
.distinctUntilChanged(
({ layer = {}, options = {}, filter, sortOptions }, newProps) =>
/* getSearchUrl(layer) === getSearchUrl(layer) && */
Expand Down
10 changes: 9 additions & 1 deletion web/client/components/widgets/enhancers/wpsCounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ import { getWpsUrl } from '../../../utils/LayersUtils';
*/
const dataStreamFactory = ($props) =>
$props
.filter(({layer = {}, options}) => layer.name && getWpsUrl(layer) && options && options.aggregateFunction && options.aggregationAttribute)
.filter(({layer = {}, options, dependencies, mapSync}) => {
// Check if mapSync is enabled (true) and dependencies.viewport is null or falsy
// If this condition is true, return false to filter out the event.
// This prevents an extra API call from being triggered when the viewport is not available.
if (mapSync && !dependencies?.viewport) {
return false;
}
return layer.name && getWpsUrl(layer) && options && options.aggregateFunction && options.aggregationAttribute;
})
.distinctUntilChanged(
({layer = {}, options = {}, filter}, newProps) =>
(newProps.layer && layer.name === newProps.layer.name && layer.loadingError === newProps.layer.loadingError)
Expand Down