Skip to content

Commit

Permalink
record: added a function to convert "total hits" for Elasticsearch
Browse files Browse the repository at this point in the history
This function allows to convert the total hits to keep the compatibility between versions 6 and 7 of Elasticsearch.

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
Garfield-fr committed Sep 11, 2020
1 parent dd20af4 commit 00445b6
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 15 deletions.
4 changes: 2 additions & 2 deletions projects/rero/ng-core/src/lib/record/record.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ import { AddFieldEditorComponent } from './editor/add-field-editor/add-field-edi
import { ArrayTypeComponent } from './editor/array-type/array-type.component';
import { DropdownLabelEditorComponent } from './editor/dropdown-label-editor/dropdown-label-editor.component';
import { EditorComponent } from './editor/editor.component';
import { hooksFormlyExtension, registerTranslateExtension, fieldIdGenerator } from './editor/extensions';
import { fieldIdGenerator, hooksFormlyExtension, registerTranslateExtension } from './editor/extensions';
import { HorizontalWrapperComponent } from './editor/horizontal-wrapper/horizontal-wrapper.component';
import { MultiSchemaTypeComponent } from './editor/multischema/multischema.component';
import { ObjectTypeComponent } from './editor/object-type/object-type.component';
import { RemoteTypeaheadComponent } from './editor/remote-typeahead/remote-typeahead.component';
import { SwitchComponent } from './editor/switch/switch.component';
import { ToggleWrapperComponent } from './editor/toggle-wrapper/toggle-wrappers.component';
import { DatepickerTypeComponent } from './editor/type/datepicker-type.component';
Expand All @@ -53,7 +54,6 @@ import { RecordSearchComponent } from './search/record-search.component';
import { JsonComponent } from './search/result/item/json.component';
import { RecordSearchResultComponent } from './search/result/record-search-result.component';
import { RecordSearchResultDirective } from './search/result/record-search-result.directive';
import { RemoteTypeaheadComponent } from './editor/remote-typeahead/remote-typeahead.component';

@NgModule({
declarations: [
Expand Down
3 changes: 1 addition & 2 deletions projects/rero/ng-core/src/lib/record/record.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { HttpErrorResponse } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { getTestBed, TestBed } from '@angular/core/testing';
import { TranslateFakeLoader, TranslateLoader, TranslateModule } from '@ngx-translate/core';
Expand Down Expand Up @@ -74,7 +73,7 @@ describe('RecordService', () => {
};

service.getRecords('documents', '', 1, 10, [{ key: 'author', values: ['John doe'] }]).subscribe((data: Record) => {
expect(data.hits.total).toBe(2);
expect(service.totalHits(data.hits.total)).toBe(2);
});

const req = httpMock.expectOne(request => request.method === 'GET' && request.url === url + '/');
Expand Down
27 changes: 25 additions & 2 deletions projects/rero/ng-core/src/lib/record/record.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class RecordService {
query += ` NOT pid:${excludePid}`;
}
return this.getRecords(recordType, query, 1, 1).pipe(
map((res: Record) => res.hits.total),
map((res: Record) => this.totalHits(res.hits.total)),
map((total) => (total ? { alreadyTaken: value } : null)),
debounceTime(1000)
);
Expand Down Expand Up @@ -282,12 +282,35 @@ export class RecordService {
query += ` NOT pid:${excludePid}`;
}
return this.getRecords(recordType, query, 1, 1).pipe(
map((res: Record) => res.hits.total),
map((res: Record) => this.totalHits(res.hits.total)),
map((total) => (total ? { alreadyTaken: value } : null)),
debounceTime(500)
);
}

/**
* Transform a total value string or object representation
* (ES compatibility v6 and v7)
* @param total - string or object
* @param relation - boolean
* @return integer, text or null
*/
totalHits(total: any, relation = false): any {
switch (typeof total) {
case 'object':
if (relation) {
return `${this._translateService.instant(total.relation)} ${total.value.toString()}`;
}
return Number(total.value);
case 'number':
return total;
case 'string':
return Number(total);
default:
return null;
}
}

/**
* Error handling during api call process.
* @param error - HttpErrorResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class RecordSearchComponent implements OnInit, OnDestroy {
key: string,
label: string,
component?: Component,
total?: number,
total?: any,
canAdd?: any,
canUpdate?: any,
canDelete?: any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ describe('RecordSearchComponent', () => {
links: {}
};

const recordServiceSpy = jasmine.createSpyObj('RecordService', ['getRecords', 'delete']);
const recordServiceSpy = jasmine.createSpyObj('RecordService', ['getRecords', 'delete', 'totalHits']);
recordServiceSpy.getRecords.and.returnValue(of(emptyRecords));
recordServiceSpy.delete.and.returnValue(of({}));
recordServiceSpy.totalHits.and.returnValue(10);

const recordUiServiceSpy = jasmine.createSpyObj('RecordUiService', [
'getResourceConfig',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy {
key: string,
label: string,
component?: Component,
total?: number,
total?: any,
canAdd?: any,
canUpdate?: any,
canDelete?: any,
Expand Down Expand Up @@ -199,6 +199,7 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy {
* Loads total count of records for each resource.
*/
ngOnInit() {

// Subscribe on aggregation filters changes and do search.
this._aggregationsFiltersSubscription = this._recordSearchService.aggregationsFilters.subscribe(
(aggregationsFilters: Array<AggregationsFilter>) => {
Expand Down Expand Up @@ -228,7 +229,7 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy {
type.key, '', 1, 1, [],
this._config.preFilters || {},
this._config.listHeaders || null).subscribe((records: Record) => {
type.total = records.hits.total;
type.total = this._recordService.totalHits(records.hits.total);
});
}
}
Expand Down Expand Up @@ -329,7 +330,7 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy {
* @return integer representing the number of results.
*/
get total(): number {
return this.hits && this.hits.total ? this.hits.total : 0;
return this.hits && this.hits.total ? this._recordService.totalHits(this.hits.total) : 0;
}

/**
Expand All @@ -341,7 +342,8 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy {
if (this._config.resultsText) {
return this._config.resultsText(this.hits);
}
return this._translateService.stream('{{ total }} results', { total: this.total });
const total = this._recordService.totalHits(this.total);
return this._translateService.stream('{{ total }} results', { total });
}

/**
Expand Down
7 changes: 4 additions & 3 deletions projects/rero/ng-core/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from './lib/core-config.service';
export * from './lib/core.module';
export * from './lib/dialog/dialog.component';
export * from './lib/dialog/dialog.service';
export * from './lib/error/error.component';
export * from './lib/pipe/callback-array-filter.pipe';
export * from './lib/pipe/default.pipe';
export * from './lib/pipe/get-record.pipe';
Expand All @@ -37,6 +38,8 @@ export * from './lib/record/editor/editor.service';
export * from './lib/record/editor/extensions';
export * from './lib/record/editor/multischema/multischema.component';
export * from './lib/record/editor/object-type/object-type.component';
export * from './lib/record/editor/remote-typeahead/remote-typeahead.component';
export * from './lib/record/editor/remote-typeahead/remote-typeahead.service';
export * from './lib/record/editor/switch/switch.component';
export * from './lib/record/editor/toggle-wrapper/toggle-wrappers.component';
export * from './lib/record/editor/type/datepicker-type.component';
Expand All @@ -61,6 +64,4 @@ export * from './lib/utils/sort-by-keys';
export * from './lib/utils/utils';
export * from './lib/validator/time.validator';
export * from './lib/validator/unique.validator';
export * from './lib/error/error.component';
export * from './lib/record/editor/remote-typeahead/remote-typeahead.service';
export * from './lib/record/editor/remote-typeahead/remote-typeahead.component';

0 comments on commit 00445b6

Please sign in to comment.