Skip to content

Commit

Permalink
Use link in hits to build after param
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbet committed Jul 17, 2019
1 parent a73fbe5 commit c361a69
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 70 deletions.
41 changes: 23 additions & 18 deletions src/contributors/MapContributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
Filter, Page, FeatureCollection
} from 'arlas-api';
import { OnMoveResult, ElementIdentifier, triggerType, PageEnum } from '../models/models';
import { getElementFromJsonObject, appendIdToSort, invertSortDirection, getAfter, removePageFromIndex } from '../utils/utils';
import { getElementFromJsonObject, appendIdToSort, invertSortDirection, removePageFromIndex } from '../utils/utils';
import { decode_bbox, bboxes } from 'ngeohash';
import jsonSchema from '../jsonSchemas/mapContributorConf.schema.json';

Expand Down Expand Up @@ -680,30 +680,35 @@ export class MapContributor extends Contributor {
}

public getPage(reference: Map<string, string | number | Date>, sort: string, whichPage: PageEnum, maxPages: number): void {
let after;
if (whichPage === PageEnum.previous) {
sort = invertSortDirection(sort);
after = reference.get('_previousAfter');
} else {
after = reference.get('_nextAfter');
}
const idSortOrder = (whichPage === PageEnum.next) ? 'asc' : 'desc';
const sortWithId = appendIdToSort(sort, idSortOrder, this.idFieldName);
const after = getAfter(sortWithId, reference);
this.fetchDataGeoSearch(this.includeFeaturesFields, sortWithId, after)
.pipe(
map(f => this.computeDataTileSearch(f)),
map(f => {
if (maxPages !== -1) {
(whichPage === PageEnum.next) ? f.forEach(d => { this.geojsondata.features.push(d); })
: f.forEach(d => { this.geojsondata.features.unshift(d); });
(whichPage === PageEnum.next) ? removePageFromIndex(0, this.geojsondata.features, this.searchSize, maxPages) :
removePageFromIndex(this.geojsondata.features.length - this.searchSize, this.geojsondata.features,
this.searchSize, maxPages);
} else {
if (whichPage === PageEnum.next) {
f.forEach(d => { this.geojsondata.features.push(d); });
if (after !== undefined) {
this.fetchDataGeoSearch(this.includeFeaturesFields, sortWithId, after)
.pipe(
map(f => this.computeDataTileSearch(f)),
map(f => {
if (maxPages !== -1) {
(whichPage === PageEnum.next) ? f.forEach(d => { this.geojsondata.features.push(d); })
: f.forEach(d => { this.geojsondata.features.unshift(d); });
(whichPage === PageEnum.next) ? removePageFromIndex(0, this.geojsondata.features, this.searchSize, maxPages) :
removePageFromIndex(this.geojsondata.features.length - this.searchSize, this.geojsondata.features,
this.searchSize, maxPages);
} else {
if (whichPage === PageEnum.next) {
f.forEach(d => { this.geojsondata.features.push(d); });
}
}
}
})
);
})
);

}
}
public fetchDataGeoSearch(includeFeaturesFields: Set<string>, sort: string,
afterParam?: string, fromParam?): Observable<FeatureCollection> {
Expand Down
96 changes: 62 additions & 34 deletions src/contributors/ResultListContributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
Projection, Hits,
Filter, Aggregation, Expression, Hit
} from 'arlas-api';
import { getElementFromJsonObject, isArray, download, appendIdToSort, invertSortDirection, getAfter, removePageFromIndex } from '../utils/utils';
import { getElementFromJsonObject, isArray, download, appendIdToSort, invertSortDirection, removePageFromIndex } from '../utils/utils';
import { Action, ElementIdentifier, SortEnum, Column, Detail, Field, FieldsConfiguration, PageEnum } from '../models/models';
import jp from 'jsonpath/jsonpath.min';
import jsonSchema from '../jsonSchemas/resultlistContributorConf.schema.json';
Expand Down Expand Up @@ -461,40 +461,47 @@ export class ResultListContributor extends Contributor {
* @param whichPage Whether to fetch next or previous page.
*/
public getPage(reference: Map<string, string | number | Date>, whichPage: PageEnum): void {
let sort = (this.geoOrderSort) ? this.geoOrderSort : this.sort;
const sort = (this.geoOrderSort) ? this.geoOrderSort : this.sort;
let after;
if (whichPage === PageEnum.previous) {
sort = invertSortDirection(sort);
after = reference.get('_previousAfter');
} else {
after = reference.get('_nextAfter');
}
const idSortOrder = (whichPage === PageEnum.next) ? 'asc' : 'desc';
const sortWithId = appendIdToSort(sort, idSortOrder, this.fieldsConfiguration.idFieldName);
const after = getAfter(sortWithId, reference);
this.getHitsObservable(this.includesvalues, sortWithId, after)
.pipe(
map(f => this.computeData(f)),
map(f => {
/**
* if maxPages === -1 then we keep adding data to the list without removing old data
*/
if (this.maxPages !== -1) {
(whichPage === PageEnum.next) ? f.forEach(d => { this.data.push(d); }) : f.forEach(d => { this.data.unshift(d); });
(whichPage === PageEnum.next) ? removePageFromIndex(0, this.data, this.pageSize, this.maxPages) :
removePageFromIndex(this.data.length - this.pageSize, this.data, this.pageSize, this.maxPages);
if (f.length === 0) {
/** notifies the end of fetching up-items or down-items */
this.fetchState = { endListUp: whichPage === PageEnum.previous, endListDown: whichPage === PageEnum.next };
const sortWithId = appendIdToSort(sort, sort, this.fieldsConfiguration.idFieldName);
if (after !== undefined) {
this.getHitsObservable(this.includesvalues, sortWithId, after, null, whichPage)
.pipe(
map(f => this.computeData(f)),
map(f => {
/**
* if maxPages === -1 then we keep adding data to the list without removing old data
*/
if (this.maxPages !== -1) {
(whichPage === PageEnum.next) ? f.forEach(d => { this.data.push(d); }) :
f.reverse().forEach(d => { this.data.unshift(d); });
(whichPage === PageEnum.next) ? removePageFromIndex(0, this.data, this.pageSize, this.maxPages) :
removePageFromIndex(this.data.length - this.pageSize, this.data, this.pageSize, this.maxPages);
if (f.length === 0) {
/** notifies the end of fetching up-items or down-items */
this.fetchState = { endListUp: whichPage === PageEnum.previous, endListDown: whichPage === PageEnum.next };
} else {
this.fetchState = { endListUp: false, endListDown: false };
}
} else {
this.fetchState = { endListUp: false, endListDown: false };
}
} else {
if (whichPage === PageEnum.next) {
f.forEach(d => { this.data.push(d); });
if (whichPage === PageEnum.next) {
f.forEach(d => { this.data.push(d); });
}
this.fetchState = { endListUp: true, endListDown: f.length === 0 };
}
this.fetchState = { endListUp: true, endListDown: f.length === 0 };
}
return f;
})
)
.subscribe(data => data);
return f;
})
)
.subscribe(data => data);
} else {
this.fetchState = { endListUp: whichPage === PageEnum.previous, endListDown: whichPage === PageEnum.next };
}

}

public fetchData(collaborationEvent: CollaborationEvent): Observable<Hits> {
Expand All @@ -511,9 +518,25 @@ export class ResultListContributor extends Contributor {

public computeData(hits: Hits): Array<Map<string, string | number | Date>> {
const listResult = new Array<Map<string, string | number | Date>>();
const next = hits.links.next;
const previous = hits.links.previous;
let nextAfter;
let previousAfter;
if (next) {
nextAfter = new URL(next.href).searchParams.get('after');
}
if (previous) {
previousAfter = new URL(previous.href).searchParams.get('before');
}
if (hits.nbhits > 0) {
hits.hits.forEach(h => {
const fieldValueMap = new Map<string, string | number | Date>();
if (next) {
fieldValueMap.set('_nextAfter', nextAfter);
}
if (previous) {
fieldValueMap.set('_previousAfter', previousAfter);
}
this.fieldsList.forEach(element => {
const result: string = getElementFromJsonObject(h.data, element.fieldName);
const process: string = this.columnsProcess[element.columnName];
Expand Down Expand Up @@ -620,16 +643,21 @@ export class ResultListContributor extends Contributor {
* @param reference comma seperated field values from which next/previous data is fetched
* @param origin (page.from in arlas api) an offset from which fetching hits starts. It's ignored if reference is set.
*/
private getHitsObservable(includesvalues: Array<string>, sort?: string, reference?: string, origin?: number): Observable<Hits> {
private getHitsObservable(includesvalues: Array<string>, sort?: string, reference?: string,
origin?: number, whichPage?: PageEnum): Observable<Hits> {
const projection: Projection = {};
const search: Search = { page: { size: this.pageSize } };
if (sort) {
search.page.sort = sort;
}
if (reference) {
search.page.after = reference;
if (whichPage === PageEnum.previous) {
search.page.before = reference;
} else {
search.page.after = reference;
}
} else {
if (from !== undefined && from !== null) {
if (origin !== undefined && origin !== null) {
search.page.from = origin;
}
}
Expand Down
19 changes: 1 addition & 18 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,7 @@ export function invertSortDirection(sortString: string): string {
}

/**
* @param sortString comma separated fields on which sort is applied
* @param reference reference data from which next/previous data is fetched
*/
export function getAfter(sortString: string, reference: Map<string, string | number | Date>) {
const fields = Array.from(sortString.split(',')).map(f => {
if (f.startsWith('-')) {
f = f.substring(1, f.length);
}
return f;
});
const afterList = [];
fields.forEach(f => afterList.push(reference.get(f).toString()));
return afterList.join(',');
}


/**
*
*
* @param fromIndex remove `pageSize` elements from `data` array starting from `fromIndex`
* @param data the data list from which pages are removed
* @param pageSize how many hits/features are inside each page
Expand Down

0 comments on commit c361a69

Please sign in to comment.