Skip to content

Commit

Permalink
feat: allow disabled ogc-client cache per request
Browse files Browse the repository at this point in the history
  • Loading branch information
AlitaBernachot committed Mar 6, 2025
1 parent a16ba66 commit fda5a34
Show file tree
Hide file tree
Showing 24 changed files with 427 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ describe('ChartViewComponent', () => {
it('creates a dataset reader once from the link', () => {
expect(dataService.getDataset).toHaveBeenCalledTimes(1)
expect(dataService.getDataset).toHaveBeenCalledWith(
aSetOfLinksFixture().dataCsv()
aSetOfLinksFixture().dataCsv(),
true
)
})
it('choses the first string property for X', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ export class ChartViewComponent {
switchMap((link) => {
this.error = null
this.loading = true
return this.dataService.getDataset(link).pipe(
const cacheActive = true // TODO implement whether should be true or false
return this.dataService.getDataset(link, cacheActive).pipe(
catchError((error) => {
this.handleError(error)
return EMPTY
Expand Down
149 changes: 95 additions & 54 deletions libs/feature/dataviz/src/lib/service/data.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ jest.mock('@geonetwork-ui/data-fetcher', () => ({

describe('DataService', () => {
let service: DataService
const cacheActive = true

beforeEach(() => {
jest.clearAllMocks()
Expand Down Expand Up @@ -649,11 +650,14 @@ describe('DataService', () => {
it('returns an observable that errors with a relevant error', async () => {
try {
await lastValueFrom(
service.getDataset({
url: new URL('http://error.parse/geojson'),
mimeType: 'application/geo+json',
type: 'download',
})
service.getDataset(
{
url: new URL('http://error.parse/geojson'),
mimeType: 'application/geo+json',
type: 'download',
},
cacheActive
)
)
} catch (e) {
expect(e).toStrictEqual({
Expand All @@ -667,11 +671,14 @@ describe('DataService', () => {
it('returns an observable that errors with a relevant error', async () => {
try {
await lastValueFrom(
service.getDataset({
url: new URL('http://error.network/xls'),
mimeType: 'application/vnd.ms-excel',
type: 'download',
})
service.getDataset(
{
url: new URL('http://error.network/xls'),
mimeType: 'application/vnd.ms-excel',
type: 'download',
},
cacheActive
)
)
} catch (e) {
expect(e).toStrictEqual({
Expand All @@ -685,11 +692,14 @@ describe('DataService', () => {
it('returns an observable that errors with a relevant error', async () => {
try {
await lastValueFrom(
service.getDataset({
url: new URL('http://error.http/csv'),
mimeType: 'text/csv',
type: 'download',
})
service.getDataset(
{
url: new URL('http://error.http/csv'),
mimeType: 'text/csv',
type: 'download',
},
cacheActive
)
)
} catch (e) {
expect(e).toStrictEqual({
Expand All @@ -704,11 +714,14 @@ describe('DataService', () => {
it('returns an observable that errors with a relevant error', async () => {
try {
await lastValueFrom(
service.getDataset({
url: new URL('http://error/xls'),
mimeType: 'application/vnd.ms-excel',
type: 'download',
})
service.getDataset(
{
url: new URL('http://error/xls'),
mimeType: 'application/vnd.ms-excel',
type: 'download',
},
cacheActive
)
)
} catch (e) {
expect(e).toStrictEqual({
Expand All @@ -719,23 +732,31 @@ describe('DataService', () => {
})
describe('valid file', () => {
it('calls DataFetcher.openDataset', () => {
service.getDataset({
url: new URL('http://sample/geojson'),
mimeType: 'text/csv',
type: 'download',
})
service.getDataset(
{
url: new URL('http://sample/geojson'),
mimeType: 'text/csv',
type: 'download',
},
cacheActive
)
expect(openDataset).toHaveBeenCalledWith(
'http://sample/geojson',
'csv'
'csv',
undefined,
true
)
})
it('returns an observable that emits the array of features', async () => {
const result = await lastValueFrom(
service.getDataset({
url: new URL('http://sample/csv'),
mimeType: 'text/csv',
type: 'download',
})
service.getDataset(
{
url: new URL('http://sample/csv'),
mimeType: 'text/csv',
type: 'download',
},
cacheActive
)
)
await expect(result.read()).resolves.toEqual(SAMPLE_GEOJSON.features)
})
Expand All @@ -746,11 +767,14 @@ describe('DataService', () => {
describe('valid file', () => {
it('returns an observable that emits the feature collection', async () => {
const result = await lastValueFrom(
service.readAsGeoJson({
url: new URL('http://sample/geojson'),
mimeType: 'application/geo+json',
type: 'download',
})
service.readAsGeoJson(
{
url: new URL('http://sample/geojson'),
mimeType: 'application/geo+json',
type: 'download',
},
cacheActive
)
)
expect(result).toEqual(SAMPLE_GEOJSON)
})
Expand Down Expand Up @@ -803,39 +827,56 @@ describe('DataService', () => {
)
})
it('calls DataFetcher.openDataset with a proxied url', () => {
service.getDataset({
url: new URL('http://esri.rest/local'),
accessServiceProtocol: 'esriRest',
type: 'service',
})
service.getDataset(
{
url: new URL('http://esri.rest/local'),
accessServiceProtocol: 'esriRest',
type: 'service',
},
cacheActive
)
expect(openDataset).toHaveBeenCalledWith(
'http://proxy.local/?url=http%3A%2F%2Fesri.rest%2Flocal%2Fquery%3Ff%3Dgeojson%26where%3D1%3D1%26outFields%3D*',
'geojson'
'geojson',
undefined,
true
)
})
})

describe('#readGeoJsonDataset', () => {
it('calls DataFetcher.openDataset with a proxied url', () => {
service.getDataset({
url: new URL('http://sample/geojson'),
mimeType: 'text/csv',
type: 'download',
})
service.getDataset(
{
url: new URL('http://sample/geojson'),
mimeType: 'text/csv',
type: 'download',
},
cacheActive
)
expect(openDataset).toHaveBeenCalledWith(
'http://proxy.local/?url=http%3A%2F%2Fsample%2Fgeojson',
'csv'
'csv',
undefined,
true
)
})
it('does not apply the proxy twice', () => {
service.getDataset({
url: new URL('http://proxy.local/?url=http%3A%2F%2Fsample%2Fgeojson'),
mimeType: 'text/csv',
type: 'download',
})
service.getDataset(
{
url: new URL(
'http://proxy.local/?url=http%3A%2F%2Fsample%2Fgeojson'
),
mimeType: 'text/csv',
type: 'download',
},
cacheActive
)
expect(openDataset).toHaveBeenCalledWith(
'http://proxy.local/?url=http%3A%2F%2Fsample%2Fgeojson',
'csv'
'csv',
undefined,
true
)
})
})
Expand Down
31 changes: 22 additions & 9 deletions libs/feature/dataviz/src/lib/service/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,11 @@ export class DataService {
}))
}

readAsGeoJson(link: DatasetOnlineResource): Observable<FeatureCollection> {
return this.getDataset(link).pipe(
readAsGeoJson(
link: DatasetOnlineResource,
cacheActive: boolean
): Observable<FeatureCollection> {
return this.getDataset(link, cacheActive).pipe(
switchMap((dataset) => dataset.selectAll().read()),
map((features) => ({
type: 'FeatureCollection',
Expand All @@ -252,13 +255,21 @@ export class DataService {
)
}

getDataset(link: DatasetOnlineResource): Observable<BaseReader> {
getDataset(
link: DatasetOnlineResource,
cacheActive: boolean
): Observable<BaseReader> {
if (link.type === 'service' && link.accessServiceProtocol === 'wfs') {
const wfsUrlEndpoint = this.proxy.getProxiedUrl(link.url.toString())
return from(
openDataset(wfsUrlEndpoint, 'wfs', {
wfsFeatureType: link.name,
})
openDataset(
wfsUrlEndpoint,
'wfs',
{
wfsFeatureType: link.name,
},
cacheActive
)
)
} else if (link.type === 'download') {
const linkProxifiedUrl = this.proxy.getProxiedUrl(link.url.toString())
Expand All @@ -267,7 +278,9 @@ export class DataService {
SupportedTypes.indexOf(format as any) > -1
? (format as SupportedType)
: undefined
return from(openDataset(linkProxifiedUrl, supportedType)).pipe()
return from(
openDataset(linkProxifiedUrl, supportedType, undefined, cacheActive)
).pipe()
} else if (
link.type === 'service' &&
link.accessServiceProtocol === 'esriRest'
Expand All @@ -276,15 +289,15 @@ export class DataService {
link.url.toString(),
'geojson'
)
return from(openDataset(url, 'geojson')).pipe()
return from(openDataset(url, 'geojson', undefined, cacheActive)).pipe()
} else if (
link.type === 'service' &&
link.accessServiceProtocol === 'ogcFeatures'
) {
return from(this.getDownloadUrlsFromOgcApi(link.url.href)).pipe(
switchMap((collectionInfo) => {
const geojsonUrl = collectionInfo.jsonDownloadLink
return openDataset(geojsonUrl, 'geojson')
return openDataset(geojsonUrl, 'geojson', undefined, cacheActive)
}),
tap((url) => {
if (url === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ describe('TableViewComponent', () => {

it('loads the data from the first available link', () => {
expect(dataService.getDataset).toHaveBeenCalledWith(
aSetOfLinksFixture().dataCsv()
aSetOfLinksFixture().dataCsv(),
true
)
})

Expand Down Expand Up @@ -144,7 +145,8 @@ describe('TableViewComponent', () => {
}))
it('loads data from selected link', () => {
expect(dataService.getDataset).toHaveBeenCalledWith(
aSetOfLinksFixture().geodataJson()
aSetOfLinksFixture().geodataJson(),
true
)
})
it('displays mocked data in the table', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export class TableViewComponent {
) {}

getDatasetReader(link: DatasetOnlineResource): Observable<BaseReader> {
return this.dataService.getDataset(link)
const cacheActive = true // TODO implement whether should be true or false
return this.dataService.getDataset(link, cacheActive)
}

onTableSelect(event) {
Expand Down
3 changes: 2 additions & 1 deletion libs/feature/record/src/lib/map-view/map-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ export class MapViewComponent implements AfterViewInit {
link.accessServiceProtocol === 'ogcFeatures')) ||
link.type === 'download'
) {
return this.dataService.readAsGeoJson(link).pipe(
const cacheActive = true // TODO implement whether should be true or false
return this.dataService.readAsGeoJson(link, cacheActive).pipe(
map((data) => ({
type: 'geojson',
data,
Expand Down
16 changes: 15 additions & 1 deletion libs/util/data-fetcher/src/lib/data-fetcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,26 @@ describe('data-fetcher', () => {
'geojson'
)
})
it('uses cache', () => {
it('uses cache by default', () => {
expect(useCache).toHaveBeenCalledTimes(1)
})
it('avoids identical concurrent requests', () => {
expect(sharedFetch).toHaveBeenCalledTimes(1)
})
})
describe('when no use of ogc-client cache', () => {
beforeEach(() => {
const cacheActive = false
readDataset(
'http://localfile/fixtures/perimetre-des-epci-concernes-par-un-contrat-de-ville.geojson',
'geojson',
undefined,
cacheActive
)
})
it('does not use ogc-client cache', () => {
expect(useCache).not.toHaveBeenCalled()
})
})
})
})
Loading

0 comments on commit fda5a34

Please sign in to comment.