Skip to content

Commit

Permalink
Merge pull request #632 from geonetwork/fix-10k-records-limit
Browse files Browse the repository at this point in the history
Datahub: show the correct amount of records on the news page
  • Loading branch information
jahow authored Sep 22, 2023
2 parents f491bdb + daf93f1 commit 24bf196
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
30 changes: 28 additions & 2 deletions libs/api/repository/src/lib/gn4/gn4-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ElasticsearchServiceMock {
class SearchApiServiceMock {
search = jest.fn((bucket, payload) => {
const body = JSON.parse(payload)
const count = body.size ?? 20
const count = body.size || 1234
const result: EsSearchResponse = {
hits: {
hits: DATASET_RECORDS,
Expand Down Expand Up @@ -122,6 +122,32 @@ describe('Gn4Repository', () => {
expect(results.records).toStrictEqual(DATASET_RECORDS)
})
})
describe('getMatchesCount', () => {
let count: number
beforeEach(async () => {
count = await lastValueFrom(
repository.getMatchesCount({
field1: '1234',
field2: {
abcd: true,
},
})
)
})
it('builds a payload with the specified uuid', () => {
expect(gn4Helper.getSearchRequestBody).toHaveBeenCalledWith(
{},
0,
0,
undefined,
undefined,
{ field1: '1234', field2: { abcd: true } }
)
})
it('returns the result count', () => {
expect(count).toStrictEqual(1234)
})
})
describe('getByUniqueIdentifier', () => {
let record: CatalogRecord
beforeEach(async () => {
Expand Down Expand Up @@ -204,7 +230,7 @@ describe('Gn4Repository', () => {
expect(gn4Helper.buildAutocompletePayload).toHaveBeenCalledWith('blargz')
})
it('returns the given results as records', () => {
expect(results.count).toBe(20)
expect(results.count).toBe(1234)
expect(results.records).toStrictEqual(DATASET_RECORDS)
})
})
Expand Down
20 changes: 20 additions & 0 deletions libs/api/repository/src/lib/gn4/gn4-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
Aggregations,
AggregationsParams,
FieldFilters,
} from '@geonetwork-ui/common/domain/search'
import { map } from 'rxjs/operators'
import {
Expand Down Expand Up @@ -62,6 +63,25 @@ export class Gn4Repository implements RecordsRepositoryInterface {
)
}

getMatchesCount(filters: FieldFilters): Observable<number> {
return this.gn4SearchApi
.search(
'records-count',
JSON.stringify({
...this.gn4SearchHelper.getSearchRequestBody(
{},
0,
0,
undefined,
undefined,
filters
),
track_total_hits: true,
})
)
.pipe(map((results: Gn4SearchResults) => results.hits.total?.value || 0))
}

getByUniqueIdentifier(
uniqueIdentifier: string
): Observable<CatalogRecord | null> {
Expand Down
2 changes: 2 additions & 0 deletions libs/common/domain/src/lib/records-repository.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { Observable } from 'rxjs'
import {
Aggregations,
AggregationsParams,
FieldFilters,
SearchParams,
SearchResults,
} from './search'
import { CatalogRecord } from './record/metadata.model'

export abstract class RecordsRepositoryInterface {
abstract search(params: SearchParams): Observable<SearchResults>
abstract getMatchesCount(filters: FieldFilters): Observable<number>
abstract getByUniqueIdentifier(
uniqueIdentifier: string
): Observable<CatalogRecord | null>
Expand Down
8 changes: 3 additions & 5 deletions libs/feature/catalog/src/lib/records/records.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { TestBed } from '@angular/core/testing'
import { RecordsService } from './records.service'
import { SAMPLE_SEARCH_RESULTS } from '@geonetwork-ui/common/fixtures'
import { of, throwError } from 'rxjs'
import { RecordsRepositoryInterface } from '@geonetwork-ui/common/domain/records-repository.interface'

class RecordsRepositoryMock {
search = jest.fn(() => of(SAMPLE_SEARCH_RESULTS))
getMatchesCount = jest.fn(() => of(123))
}

describe('RecordsService', () => {
Expand All @@ -32,13 +30,13 @@ describe('RecordsService', () => {
service.recordsCount$.subscribe()
service.recordsCount$.subscribe()
service.recordsCount$.subscribe()
expect(repository.search).toHaveBeenCalledTimes(1)
expect(repository.getMatchesCount).toHaveBeenCalledTimes(1)
})
})

describe('when the request does not behave as expected', () => {
beforeEach(() => {
repository.search = () => throwError(() => 'blargz')
repository.getMatchesCount = () => throwError(() => 'blargz')
service = new RecordsService(repository) // create a new service to enable the changed repository behaviour
})
it('emits 0', () => {
Expand Down
8 changes: 2 additions & 6 deletions libs/feature/catalog/src/lib/records/records.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { Injectable } from '@angular/core'
import { Observable, of } from 'rxjs'
import { catchError, map, shareReplay } from 'rxjs/operators'
import { catchError, shareReplay } from 'rxjs/operators'
import { RecordsRepositoryInterface } from '@geonetwork-ui/common/domain/records-repository.interface'

@Injectable({
providedIn: 'root',
})
export class RecordsService {
recordsCount$: Observable<number> = this.recordsRepository
.search({
limit: 0,
offset: 0,
})
.getMatchesCount({})
.pipe(
map((response) => response.count),
shareReplay(1),
catchError(() => of(0))
)
Expand Down

0 comments on commit 24bf196

Please sign in to comment.