Skip to content

Commit

Permalink
Fixed loading task audit log entries [#468]
Browse files Browse the repository at this point in the history
 * Added a start and stop action to control fetching entries.
 * Return latest = current date when no long entries are found.
  • Loading branch information
mcpierce authored and BRUCELLA2 committed Aug 25, 2020
1 parent ad896f0 commit 56b3ba2
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 23 deletions.
Expand Up @@ -19,25 +19,39 @@
import { createAction, props } from '@ngrx/store';
import { TaskAuditLogEntry } from 'app/backend-status/models/task-audit-log-entry';

/**
* Starts the process of loading task audit log entries.
*/
export const startLoadingTaskAuditLogEntries = createAction(
'[Load Task Audit Log] Start loading entries'
);

/**
* Loads all task audit log entries after the specified date.
*/
export const loadTaskAuditLogEntries = createAction(
'[LoadTaskAuditLog] Load task audit log entries',
'[Load Task Audit Log] Load task audit log entries',
props<{ since: number }>()
);

/**
* Receives additional task audit log entries.
*/
export const taskAuditLogEntriesLoaded = createAction(
'[LoadTaskAuditLog] Task audit log entries loaded',
'[Load Task Audit Log] Task audit log entries loaded',
props<{ entries: TaskAuditLogEntry[]; latest: number }>()
);

/**
* Failed to get task audit log entries.
*/
export const loadTaskAuditLogFailed = createAction(
'[LoadTaskAuditLog] Failed to load the task audit log entries'
'[Load Task Audit Log] Failed to load the task audit log entries'
);

/**
* Stops loading task audit log entries.
*/
export const stopLoadingTaskAuditLogEntries = createAction(
'[Load Task Audit Log] Stop loading task audit log entries'
);
Expand Up @@ -8,7 +8,7 @@ <h2>{{"task-audit-log-page.title"|translate}}</h2>
</div>
</p-toolbar>
<p-table [value]="entries"
[rows]="rows"
[rows]="10"
[paginator]="true"
paginatorPosition="both"
[loading]="clearingAuditLog">
Expand Down
Expand Up @@ -54,7 +54,6 @@ describe('TaskAuditLogPageComponent', () => {
TestBed.configureTestingModule({
imports: [
CoreModule,
LibraryModule,
RouterTestingModule,
HttpClientTestingModule,
TranslateModule.forRoot(),
Expand Down
Expand Up @@ -20,7 +20,6 @@ import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { TaskAuditLogEntry } from 'app/backend-status/models/task-audit-log-entry';
import { LoggerService } from '@angular-ru/logger';
import { LibraryDisplayAdaptor } from 'app/user/adaptors/library-display.adaptor';
import { BreadcrumbAdaptor } from 'app/adaptors/breadcrumb.adaptor';
import { TranslateService } from '@ngx-translate/core';
import { Title } from '@angular/platform-browser';
Expand All @@ -33,7 +32,10 @@ import {
selectLoadTaskAuditLogState,
selectTaskAuditLogEntries
} from 'app/backend-status/selectors/load-task-audit-log.selectors';
import { loadTaskAuditLogEntries } from 'app/backend-status/actions/load-task-audit-log.actions';
import {
loadTaskAuditLogEntries,
startLoadingTaskAuditLogEntries
} from 'app/backend-status/actions/load-task-audit-log.actions';
import { LoadTaskAuditLogState } from 'app/backend-status/reducers/load-task-audit-log.reducer';
import { filter } from 'rxjs/operators';

Expand All @@ -43,40 +45,38 @@ import { filter } from 'rxjs/operators';
styleUrls: ['./task-audit-log-page.component.scss']
})
export class TaskAuditLogPageComponent implements OnInit, OnDestroy {
taskAuditLogStateSubscription: Subscription;
entriesSubscription: Subscription;
entries: TaskAuditLogEntry[] = [];
rowsSubscription: Subscription;
rows = 0;
langChangeSubscription: Subscription;
clearingAuditLogSubscription: Subscription;
clearingAuditLog = false;
langChangeSubscription: Subscription;

constructor(
private logger: LoggerService,
private libraryDisplayAdaptor: LibraryDisplayAdaptor,
private breadcrumbAdaptor: BreadcrumbAdaptor,
private translateService: TranslateService,
private titleService: Title,
private confirmationService: ConfirmationService,
private store: Store<AppState>
) {
this.store
this.taskAuditLogStateSubscription = this.store
.select(selectLoadTaskAuditLogState)
.pipe(filter(state => !!state))
.subscribe((state: LoadTaskAuditLogState) => {
if (!state.loading) {
if (!state.loading && !state.stopped) {
this.logger.trace('Fetching task audit log entries');
this.store.dispatch(loadTaskAuditLogEntries({ since: state.latest }));
}
});
this.store
this.entriesSubscription = this.store
.select(selectTaskAuditLogEntries)
.pipe(filter(state => !!state))
.subscribe(entries => (this.entries = entries));
this.store
this.clearingAuditLogSubscription = this.store
.select(selectClearTaskingAuditLogWorking)
.pipe(filter(state => !!state))
.subscribe(clearing => (this.clearingAuditLog = clearing));
this.rowsSubscription = this.libraryDisplayAdaptor.rows$.subscribe(
rows => (this.rows = rows)
);
this.langChangeSubscription = this.translateService.onLangChange.subscribe(
() => {
this.loadTranslations();
Expand All @@ -85,10 +85,14 @@ export class TaskAuditLogPageComponent implements OnInit, OnDestroy {
this.loadTranslations();
}

ngOnInit() {}
ngOnInit() {
this.store.dispatch(startLoadingTaskAuditLogEntries());
}

ngOnDestroy() {
this.rowsSubscription.unsubscribe();
this.taskAuditLogStateSubscription.unsubscribe();
this.entriesSubscription.unsubscribe();
this.clearingAuditLogSubscription.unsubscribe();
}

private loadTranslations() {
Expand Down
Expand Up @@ -24,6 +24,8 @@ import {
import {
loadTaskAuditLogEntries,
loadTaskAuditLogFailed,
startLoadingTaskAuditLogEntries,
stopLoadingTaskAuditLogEntries,
taskAuditLogEntriesLoaded
} from 'app/backend-status/actions/load-task-audit-log.actions';
import {
Expand Down Expand Up @@ -59,6 +61,10 @@ describe('LoadTaskAuditLog Reducer', () => {
expect(state.loading).toBeFalsy();
});

it('clears the stopped flag', () => {
expect(state.stopped).toBeFalsy();
});

it('has no entries', () => {
expect(state.entries).toEqual([]);
});
Expand All @@ -68,6 +74,32 @@ describe('LoadTaskAuditLog Reducer', () => {
});
});

describe('starting loading entries', () => {
beforeEach(() => {
state = reducer(
{
...state,
stopped: true,
entries: LOG_ENTRIES,
latest: new Date().getTime()
},
startLoadingTaskAuditLogEntries()
);
});

it('clears the stopped flag', () => {
expect(state.stopped).toBeFalsy();
});

it('clears the entries', () => {
expect(state.entries).toEqual([]);
});

it('resets the latest date', () => {
expect(state.latest).toEqual(0);
});
});

describe('when loading the entries', () => {
beforeEach(() => {
state = reducer(
Expand Down Expand Up @@ -118,4 +150,17 @@ describe('LoadTaskAuditLog Reducer', () => {
expect(state.loading).toBeFalsy();
});
});

describe('stopping loading entries', () => {
beforeEach(() => {
state = reducer(
{ ...state, stopped: false },
stopLoadingTaskAuditLogEntries()
);
});

it('sets the stopped flag', () => {
expect(state.stopped).toBeTruthy();
});
});
});
Expand Up @@ -21,26 +21,36 @@ import { TaskAuditLogEntry } from 'app/backend-status/models/task-audit-log-entr
import {
loadTaskAuditLogEntries,
loadTaskAuditLogFailed,
startLoadingTaskAuditLogEntries,
stopLoadingTaskAuditLogEntries,
taskAuditLogEntriesLoaded
} from 'app/backend-status/actions/load-task-audit-log.actions';

export const LOAD_TASK_AUDIT_LOG_FEATURE_KEY = 'load_task_audit_log_state';

export interface LoadTaskAuditLogState {
loading: boolean;
stopped: boolean;
entries: TaskAuditLogEntry[];
latest: number;
}

export const initialState: LoadTaskAuditLogState = {
loading: false,
stopped: false,
entries: [],
latest: 0
};

const loadTaskAuditLogReducer = createReducer(
initialState,

on(startLoadingTaskAuditLogEntries, state => ({
...state,
stopped: false,
entries: [],
latest: 0
})),
on(loadTaskAuditLogEntries, state => ({ ...state, loading: true })),
on(taskAuditLogEntriesLoaded, (state, action) => {
const entries = state.entries.concat(action.entries);
Expand All @@ -51,7 +61,8 @@ const loadTaskAuditLogReducer = createReducer(
latest: action.latest
};
}),
on(loadTaskAuditLogFailed, state => ({ ...state, loading: false }))
on(loadTaskAuditLogFailed, state => ({ ...state, loading: false })),
on(stopLoadingTaskAuditLogEntries, state => ({ ...state, stopped: true }))
);

export function reducer(
Expand Down
Expand Up @@ -73,8 +73,10 @@ public ApiResponse<GetTaskAuditLogResponse> getAllTaskEntriesAfterDate(
final List<TaskAuditLogEntry> entries = this.taskService.getAuditLogEntriesAfter(cutoff);
response.setResult(new GetTaskAuditLogResponse());
response.getResult().setEntries(entries);
if (!entries.isEmpty())
response.getResult().setLatest(entries.get(entries.size() - 1).getStartTime());
response
.getResult()
.setLatest(
entries.isEmpty() ? new Date() : entries.get(entries.size() - 1).getStartTime());
response.setSuccess(true);
} catch (ComiXedServiceException error) {
log.error("Failed to load task audit log entries", error);
Expand Down

0 comments on commit 56b3ba2

Please sign in to comment.