Skip to content

Commit

Permalink
fix: LowIndex updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfaron committed Nov 6, 2023
1 parent 71d002e commit 86d537b
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 35 deletions.
3 changes: 2 additions & 1 deletion projects/aas-lib/src/lib/aas-table/aas-table.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { TranslateService } from '@ngx-translate/core';
@Injectable()
export class AASTableEffects {
private readonly store: Store<AASTableFeatureState>;
private readonly defaultLimit = 10;

constructor(
private readonly actions: Actions,
Expand All @@ -44,7 +45,7 @@ export class AASTableEffects {
return EMPTY;
}

return this.api.getDocuments({ previous: null, limit: 10 });
return this.api.getDocuments({ previous: null, limit: this.defaultLimit });
}),
map(page => AASTableActions.setPage({ page })))));
});
Expand Down
9 changes: 7 additions & 2 deletions projects/aas-portal/src/app/start/start.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { ViewMode } from 'projects/aas-lib/src/public-api';
export enum StartActionType {
SET_VIEW_MODE = '[Start] set View Mode',
APPLY_FILTER = '[Start] apply filter',
SET_FILTER = '[Start] set filter'
SET_FILTER = '[Start] set filter',
SET_LIMIT = '[Start] set limit',
}

export const setViewMode = createAction(
Expand All @@ -21,4 +22,8 @@ export const setViewMode = createAction(

export const setFilter = createAction(
StartActionType.SET_FILTER,
props<{ filter: string }>());
props<{ filter: string }>());

export const setLimit = createAction(
StartActionType.SET_FILTER,
props<{ limit: number }>());
4 changes: 2 additions & 2 deletions projects/aas-portal/src/app/start/start.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<div class="input-group flex-grow-1 me-2" [hidden]="viewMode === 'tree'">
<div class="input-group-text"><i class="bi bi-filter"></i></div>
<input #textInput type="text" class="form-control" [value]="filter | async"
[placeholder]="'PLACEHOLDER_FILTER' | translate" (change)="applyFilter(textInput.value)"
(keydown.enter)="applyFilter(textInput.value)" [disabled]="viewMode === 'tree'">
[placeholder]="'PLACEHOLDER_FILTER' | translate" (change)="setFilter(textInput.value)"
(keydown.enter)="setFilter(textInput.value)" [disabled]="viewMode === 'tree'">
</div>
</ng-template>
11 changes: 8 additions & 3 deletions projects/aas-portal/src/app/start/start.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import * as StartActions from './start.actions';
import { State } from './start.state';
import { UploadFormComponent } from './upload-form/upload-form.component';
import { getEndpointType } from '../configuration';
import { selectFilter, selectViewMode } from './start.selectors';
import { selectFilter, selectViewMode, selectLimit } from './start.selectors';
import { ToolbarService } from '../toolbar.service';

@Component({
Expand Down Expand Up @@ -51,6 +51,7 @@ export class StartComponent implements OnInit, OnDestroy, AfterViewInit {
) {
this.store = store as Store<State>;
this.filter = this.store.select(selectFilter);
this.limit = this.store.select(selectLimit);
}

@ViewChild('aasTable')
Expand All @@ -63,7 +64,7 @@ export class StartComponent implements OnInit, OnDestroy, AfterViewInit {

public readonly filter: Observable<string>;

public readonly limit = of(5);
public readonly limit: Observable<number>;

public workspaces: AASWorkspace[] = [];

Expand Down Expand Up @@ -278,10 +279,14 @@ export class StartComponent implements OnInit, OnDestroy, AfterViewInit {
}
}

public applyFilter(filter: string): void {
public setFilter(filter: string): void {
this.store.dispatch(StartActions.setFilter({ filter }));
}

public setLimit(limit: number): void {
this.store.dispatch(StartActions.setLimit({ limit }));
}

private selectSubmodels(document: AASDocument, semanticId: string): aas.Submodel[] {
return document.content?.submodels.filter(submodel => lib.resolveSemanticId(submodel) === semanticId) ?? [];
}
Expand Down
9 changes: 9 additions & 0 deletions projects/aas-portal/src/app/start/start.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { StartState } from './start.state';

const initialState: StartState = {
viewMode: ViewMode.List,
limit: 10,
filter: ''
};

Expand All @@ -22,6 +23,10 @@ export const startReducer = createReducer(
StartActions.setFilter,
(state, { filter }) => setFilter(state, filter)
),
on(
StartActions.setLimit,
(state, { limit }) => setLimit(state, limit)
),
on(
StartActions.setViewMode,
(state, { viewMode }) => setViewMode(state, viewMode)
Expand All @@ -34,4 +39,8 @@ function setViewMode(state: StartState, viewMode: ViewMode): StartState {

function setFilter(state: StartState, filter: string): StartState {
return { ...state, filter };
}

function setLimit(state: StartState, limit: number): StartState {
return { ...state, limit };
}
5 changes: 4 additions & 1 deletion projects/aas-portal/src/app/start/start.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import { ViewMode } from 'projects/aas-lib/src/public-api';

const getFilter = (state: State) => state.start.filter;
const getViewMode = (state: State) => state.start.viewMode;
const getLimit = (state: State) => state.start.limit;

export const selectFilter = createSelector(getFilter, filter => filter);

export const selectViewMode = createSelector(getViewMode, viewMode => viewMode);

export const selectIsViewModeList = createSelector(getViewMode, viewMode => viewMode === ViewMode.List);

export const selectIsViewModeTree = createSelector(getViewMode, viewMode => viewMode === ViewMode.Tree);
export const selectIsViewModeTree = createSelector(getViewMode, viewMode => viewMode === ViewMode.Tree);

export const selectLimit = createSelector(getLimit, limit => limit);
1 change: 1 addition & 0 deletions projects/aas-portal/src/app/start/start.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ViewMode } from 'projects/aas-lib/src/public-api';
export interface StartState {
viewMode: ViewMode;
filter: string;
limit: number;
}

export interface State {
Expand Down
49 changes: 23 additions & 26 deletions projects/aas-server/src/app/aas-provider/low-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ export class LowIndex extends AASIndex {
return { isFirst: true, documents, isLast: true };
}

let min = this.getId(this.db.data.documents[0]);
let max = min;
let min: AASDocumentId | undefined;
let max: AASDocumentId | undefined;
for (const document of this.db.data.documents) {
if (!filter || await filter.do(document)) {
const id = this.getId(document);
let isGreater: boolean;
if (this.compare(id, min) < 0) {
if (!min || this.compare(id, min) < 0) {
min = id;
isGreater = true;
} else {
Expand All @@ -133,7 +133,7 @@ export class LowIndex extends AASIndex {
}
}

if (this.compare(id, max) > 0) {
if (!max || this.compare(id, max) > 0) {
max = id;
}
}
Expand All @@ -142,14 +142,14 @@ export class LowIndex extends AASIndex {
return { isFirst: true, documents, isLast: this.isLastPage(documents, max) };
}

private async getNextPage(previous: AASDocumentId, limit: number, filter?: AASFilter): Promise<AASPage> {
private async getNextPage(previous: AASDocumentId, limit: number, filter?: AASFilter): Promise<AASPage> {
const documents: AASDocument[] = [];
if (this.db.data.documents.length === 0) {
return { isFirst: true, documents, isLast: true };
}

let min = this.getId(this.db.data.documents[0]);
let max = min;
let min: AASDocumentId | undefined;
let max: AASDocumentId | undefined;
for (const document of this.db.data.documents) {
if (!filter || await filter.do(document)) {
const id = this.getId(document);
Expand All @@ -165,11 +165,9 @@ export class LowIndex extends AASIndex {
}
}

if (this.compare(id, min) < 0) {
if (!min || this.compare(id, min) < 0) {
min = id;
}

if (this.compare(id, max) > 0) {
} else if (!max || this.compare(id, max) > 0) {
max = id;
}
}
Expand All @@ -178,14 +176,14 @@ export class LowIndex extends AASIndex {
return { isFirst: this.isFirstPage(documents, min), documents, isLast: this.isLastPage(documents, max) };
}

private async getPreviousPage(next: AASDocumentId, limit: number, filter?: AASFilter): Promise<AASPage> {
private async getPreviousPage(next: AASDocumentId, limit: number, filter?: AASFilter): Promise<AASPage> {
let documents: AASDocument[] = [];
if (this.db.data.documents.length === 0) {
return { isFirst: true, documents, isLast: true };
}

let min = this.getId(this.db.data.documents[0]);
let max = min;
let min: AASDocumentId | undefined;
let max: AASDocumentId | undefined;
for (const document of this.db.data.documents) {
if (!filter || await filter.do(document)) {
const id = this.getId(document);
Expand All @@ -201,11 +199,9 @@ export class LowIndex extends AASIndex {
}
}

if (this.compare(id, min) < 0) {
if (!min || this.compare(id, min) < 0) {
min = id;
}

if (this.compare(id, max) > 0) {
} else if (!max || this.compare(id, max) > 0) {
max = id;
}
}
Expand All @@ -222,13 +218,13 @@ export class LowIndex extends AASIndex {
return { isFirst: true, documents, isLast: true };
}

let max = this.getId(this.db.data.documents[0]);
let min = max;
let min: AASDocumentId | undefined;
let max: AASDocumentId | undefined;
for (const document of this.db.data.documents) {
if (!filter || await filter.do(document)) {
const id = this.getId(document);
let isLower: boolean;
if (this.compare(id, max) > 0) {
if (!max || this.compare(id, max) > 0) {
max = id;
isLower = true;
} else {
Expand All @@ -247,7 +243,7 @@ export class LowIndex extends AASIndex {
}
}

if (this.compare(id, min) < 0) {
if (!min || this.compare(id, min) < 0) {
min = id;
}
}
Expand All @@ -258,12 +254,13 @@ export class LowIndex extends AASIndex {
return { isFirst: this.isFirstPage(documents, min), documents, isLast: true };
}

private isFirstPage(documents: AASDocument[], min: AASDocumentId): boolean {
return documents.length === 0 || this.compare(min, this.getId(documents[0])) === 0;
private isFirstPage(documents: AASDocument[], min: AASDocumentId | undefined): boolean {
return documents.length === 0 || min == null || this.compare(min, this.getId(documents[0])) === 0;
}

private isLastPage(documents: AASDocument[], max: AASDocumentId): boolean {
return documents.length === 0 || this.compare(max, this.getId(documents[documents.length - 1])) === 0;
private isLastPage(documents: AASDocument[], max: AASDocumentId | undefined): boolean {
return documents.length === 0 || max == null ||
this.compare(max, this.getId(documents[documents.length - 1])) === 0;
}

private getKey(url: string): string {
Expand Down

0 comments on commit 86d537b

Please sign in to comment.