Enhanced progress bar logic on admin's measurements export view.
LocationBasedServices/mowesta/pipeline/head This commit looks good

This commit is contained in:
Adriano Markovic
2020-08-31 14:41:42 +02:00
parent 746faf92b1
commit 8a564df34f
4 changed files with 111 additions and 43 deletions
@@ -19,12 +19,14 @@ export interface MeasurementMsg {
/** Current status of the fetching progress. */
export enum ProgressStatus {
DONE, // Fetching is done.
FETCHING, // Fetching.
NO_DEVICES, // No devices found.
NO_MEASUREMENTS, // No measurements found.
EXPORTING, // Exporting.
UP_TO_DATE // Data for time period is up-to-date.
DONE, // Fetching is done.
FETCHING, // Fetching.
NO_DEVICES, // No devices found.
NO_MEASUREMENTS, // No measurements found.
EXPORTING, // Exporting.
UP_TO_DATE, // Data for time period is up-to-date.
RELEASE_CACHE_PARTIALLY, // Cache partially release. Takes indeterminant amount of time.
RESET // Data is reseted.
}
/**
@@ -471,8 +473,10 @@ class AdminCachingHandler extends AbstractExportHandler {
private readonly measurementChanges$: BehaviorSubject<MeasurementMsg>;
private cachedMeasurements: Array<Measurement>;
private dmPages: Array<DeviceMeasurements>;
private timeRanges: Array<TimeRange> | null;
private exporting: boolean;
private cacheReleaseCount: number;
constructor(
protected readonly defaultService: DefaultService,
@@ -488,12 +492,19 @@ class AdminCachingHandler extends AbstractExportHandler {
}
start(range: SatDatepickerRangeValue<Date>, filename?: string): void {
const fetch = this.checkIfDataNeedsToBeFetched(range);
const status = this.checkIfDataNeedsToBeFetched(range);
this.init(range);
if (fetch) {
this.peekMeasurementsTotal();
if (status === ProgressStatus.FETCHING) {
// Added to event queue because the fetching process of new data should be started after all
// obsolete parts of the cache have been released, if any were allocated in the first place.
setTimeout(() => {
this._progressFn(ProgressStatus.FETCHING);
this.peekMeasurementsTotal();
}, 10);
} else {
this._progressFn(ProgressStatus.UP_TO_DATE);
if (status === ProgressStatus.UP_TO_DATE) {
this._progressFn(ProgressStatus.UP_TO_DATE);
}
this.fetching = false;
}
}
@@ -519,30 +530,69 @@ class AdminCachingHandler extends AbstractExportHandler {
this.timeRanges = null;
}
private checkIfDataNeedsToBeFetched(satRange: SatDatepickerRangeValue<Date>): boolean {
let fetch: boolean;
private checkIfDataNeedsToBeFetched(satRange: SatDatepickerRangeValue<Date>): ProgressStatus {
let fetch: ProgressStatus;
this.timeRanges = new Array<TimeRange>();
const newRange = this.satDatepickerRangeToTimeRange(satRange);
if (!this.dateRange || newRange.start > this.dateRange.end || newRange.end < this.dateRange.start) {
// No temporal overlap, cache can be released completely.
this.timeRanges.push(newRange);
this.cachedMeasurements = new Array<Measurement>();
fetch = true;
fetch = ProgressStatus.FETCHING;
this._progressFn(ProgressStatus.RESET);
} else {
const start = this.mustFetchDataOrReleaseCachePartially(newRange.start, this.dateRange.start,
m => m.time >= newRange.start);
const end = this.mustFetchDataOrReleaseCachePartially(this.dateRange.end, newRange.end,
m => m.time <= newRange.end);
fetch = start || end;
const start = this.checkIfCacheBorderNeedsTweak(newRange.start, this.dateRange.start);
const end = this.checkIfCacheBorderNeedsTweak(this.dateRange.end, newRange.end);
if (start === ProgressStatus.FETCHING || end === ProgressStatus.FETCHING) {
fetch = ProgressStatus.FETCHING;
}
if (start === ProgressStatus.RELEASE_CACHE_PARTIALLY || end === ProgressStatus.RELEASE_CACHE_PARTIALLY) {
if (fetch !== ProgressStatus.FETCHING) {
fetch = ProgressStatus.RELEASE_CACHE_PARTIALLY;
}
this.optimizeCache(newRange, start, end);
} else if (fetch !== ProgressStatus.FETCHING) {
fetch = ProgressStatus.UP_TO_DATE;
}
}
return fetch;
}
private mustFetchDataOrReleaseCachePartially(range1: number, range2: number,
isWithinRange: (m: Measurement) => boolean): boolean {
private checkIfCacheBorderNeedsTweak(range1: number, range2: number): ProgressStatus {
const diff = range1 - range2;
if (diff > 0) {
// No fetching data, only release cache partially.
return ProgressStatus.RELEASE_CACHE_PARTIALLY;
} else if (diff < 0) {
// Fetching data, expand cache.
this.timeRanges.push({
start: range1,
end: range2,
duration: Math.abs(diff)
});
return ProgressStatus.FETCHING;
} else {
// No changes, time range equal.
return ProgressStatus.UP_TO_DATE;
}
}
private optimizeCache(newRange: TimeRange, startStatus: ProgressStatus, endStatus: ProgressStatus): void {
this.cacheReleaseCount = 0;
this._progressFn(ProgressStatus.RELEASE_CACHE_PARTIALLY);
if (startStatus === ProgressStatus.RELEASE_CACHE_PARTIALLY) {
this.releaseCache(m => m.time >= newRange.start);
}
if (endStatus === ProgressStatus.RELEASE_CACHE_PARTIALLY) {
this.releaseCache(m => m.time <= newRange.end);
}
}
private releaseCache(isWithinRange: (m: Measurement) => boolean): void {
++this.cacheReleaseCount;
// Needs to be put into event queue to enable the renderer to
// initialize ui components to serve as progress feedback.
setTimeout(() => {
const copiedCache = new Array<Measurement>();
this.cachedMeasurements = this.cachedMeasurements.filter(m => {
if (isWithinRange(m)) {
@@ -552,25 +602,16 @@ class AdminCachingHandler extends AbstractExportHandler {
return false;
}
});
console.log(this.cachedMeasurements);
this.measurementChanges$.next({ data: copiedCache });
return false;
} else if (diff < 0) {
// Fetching data, expand cache.
this.timeRanges.push({
start: range1,
end: range2,
duration: Math.abs(diff)
});
return true;
} else {
// No changes, time range equal.
return false;
}
if (--this.cacheReleaseCount === 0) {
this._progressFn(ProgressStatus.DONE);
}
}, 10);
}
private peekMeasurementsTotal(): void {
let peekCount = 0;
this.dmPages = new Array<DeviceMeasurements>();
this.fetchedMeasurements = this.totalMeasurements = 0;
this.timeRanges.forEach(range => {
this.defaultService.getAllMeasurementsByRange(range.start, range.duration,
@@ -582,11 +623,12 @@ class AdminCachingHandler extends AbstractExportHandler {
page.offset += page.elements.length;
this.fetchedMeasurements += page.elements.length;
this.totalMeasurements += page.total;
this.dmPages.push({ id: -1, page });
this.measurementChanges$.next({ data: page.elements, partialData: true });
}
if (peekCount === this.timeRanges.length) {
if (this.fetchedMeasurements > 0) {
this.fetchRemainingMeasurements(0, { id: -1, page });
this.fetchRemainingMeasurements(0, this.dmPages[0]);
} else {
this._progressFn(ProgressStatus.NO_MEASUREMENTS);
this.reset();
@@ -598,7 +640,7 @@ class AdminCachingHandler extends AbstractExportHandler {
private fetchRemainingMeasurements(idx: number, dmBuffer: DeviceMeasurements): void {
this._progressFn(ProgressStatus.FETCHING, this.fetchedMeasurements, this.totalMeasurements);
if (this.isPageIncomplete(dmBuffer.page)) {
if (dmBuffer && this.isPageIncomplete(dmBuffer.page)) {
this.defaultService.getAllMeasurementsByRange(this.timeRanges[idx].start, this.timeRanges[idx].duration,
dmBuffer.page.offset, AbstractExportHandler.BATCH_SIZE)
.pipe(takeUntil(this.abortRequests$))
@@ -609,10 +651,10 @@ class AdminCachingHandler extends AbstractExportHandler {
this.fetchRemainingMeasurements(idx, dmBuffer);
});
} else if (idx < this.timeRanges.length) {
this.fetchRemainingMeasurements(++idx, dmBuffer);
this.cachedMeasurements.push(...dmBuffer.page.elements);
this.fetchRemainingMeasurements(++idx, this.dmPages[idx]);
} else {
this.fetching = false;
this.cachedMeasurements.push(...dmBuffer.page.elements);
this.cachedMeasurements.sort((a, b) => a.device - b.device);
this._progressFn(ProgressStatus.DONE);
}
@@ -243,6 +243,8 @@ export class ResourceService {
'export_measurements_export_failed_title' : 'Export fehlgeschlagen!',
'export_measurements_export_failed_msg' : 'Der Export konnte leider nicht durchgeführt werden.',
'export_measurements_tooltip_title' : 'Messung',
'export_measurements_progress_label' : 'Fortschritt',
'export_measurements_alt_progress_label' : 'Bitte warten. Messdaten werden aktualisiert...',
'reset_confirm_title' : 'Passwort zurückgesetzt',
'reset_confirm_message' : 'Wir haben Dir einen Link an Deine Kontaktemailadresse versandt, '
@@ -708,6 +710,8 @@ export class ResourceService {
'export_measurements_export_failed_title' : 'Export failed!',
'export_measurements_export_failed_msg' : 'Unfortunately the export could not be carried out.',
'export_measurements_tooltip_title' : 'Measurement',
'export_measurements_progress_label' : 'Progress',
'export_measurements_alt_progress_label' : 'Please wait. Measurements are updated...',
'reset_confirm_title' : 'Password reset',
'reset_confirm_message' : 'We just sent an email to your primary email address that contains a password reset link. '
@@ -32,9 +32,15 @@
<app-map #map [bounds]="initialBounds" [maxZoom]="16" draggable="true"></app-map>
<mat-card *ngIf="fetchInProgress || exportInProgress" class="bottom-overflow">
<mat-label class="fx-row fx-center progress-label" matLine>Fortschritt: {{ progress }}%</mat-label>
<mat-label *ngIf="!indeterminate; else altLabel" class="fx-row fx-center progress-label" matLine>{{ res.strings('export_measurements_progress_label') }}: {{ progress }}%</mat-label>
<ng-template #altLabel>
<mat-label class="fx-row fx-center progress-label" matLine>{{ res.strings('export_measurements_alt_progress_label')}}</mat-label>
</ng-template>
<div class="fx-row fx-center mt-sm">
<mat-progress-bar class="mr-md" mode="determinate" [value]="progress" matLine></mat-progress-bar>
<mat-progress-bar *ngIf="!indeterminate; else altProgBar" class="mr-md" mode="determinate" [value]="progress" matLine></mat-progress-bar>
<ng-template #altProgBar >
<mat-progress-bar class="mr-md" mode="indeterminate" matLine></mat-progress-bar>
</ng-template>
<button mat-raised-button color="warn" (click)="stop()">
<mat-icon class="icon-button">cancel_icon</mat-icon>
<span style="padding-right: 20px">{{ res.strings('export_measurements_cancel_btn') }}</span>
@@ -33,8 +33,10 @@ export class ManageMeasurementsMapComponent extends AbstractCanDeactivate implem
fetchedMeasurements: Array<Measurement>;
initialBounds: Array<Coordinate>;
dateRange: SatDatepickerRangeValue<Date>;
indeterminate: boolean;
private readonly markers = new Array<L.Circle>();
private readonly canvas: L.Canvas = L.canvas({ padding: 0.5 });
constructor(
public readonly res: ResourceService,
@@ -54,6 +56,7 @@ export class ManageMeasurementsMapComponent extends AbstractCanDeactivate implem
this.fetchInProgress = false;
this.fetchedMeasurements = new Array<Measurement>();
this.initialBounds = this.res.getLanguage() === 'de' ? MapUtils.DACH_BOUNDS : MapUtils.WORLD_BOUNDS;
this.indeterminate = false;
}
canDeactivate(): boolean {
@@ -112,17 +115,29 @@ export class ManageMeasurementsMapComponent extends AbstractCanDeactivate implem
stop(): void {
if (this.fetchInProgress || this.exportInProgress) {
this.progress = 0;
this.indeterminate = false;
this.fetchInProgress = this.exportInProgress = false;
this.bulkService.stop();
}
}
private getProgressFn(statusOnWhichToModifyProgress: ProgressStatus): ProgressFn {
return (status, fetched, total) => {
return (status, current, total) => {
if (status === statusOnWhichToModifyProgress) {
this.progress = Math.floor((Math.min(fetched, total) / total) * 100);
if (current !== undefined && total !== undefined) {
this.progress = Math.floor((Math.min(current, total) / total) * 100);
} else {
if (status === ProgressStatus.FETCHING) { this.fetchInProgress = true; }
}
} else if (status === ProgressStatus.RELEASE_CACHE_PARTIALLY) {
this.indeterminate = true;
} else if (status === ProgressStatus.RESET) {
this.resetMap();
} else {
this.fetchInProgress = this.exportInProgress = false;
this.progress = 0;
this.indeterminate = false;
this.fetchInProgress = this.exportInProgress = false;
if (status === ProgressStatus.NO_MEASUREMENTS || status == ProgressStatus.NO_DEVICES) {
const pre = statusOnWhichToModifyProgress === ProgressStatus.EXPORTING ? 'export' : 'fetch';
const title = this.res.strings(pre + '_measurements_export_aborted');
@@ -156,6 +171,7 @@ export class ManageMeasurementsMapComponent extends AbstractCanDeactivate implem
className: 'forecast-tooltip',
}).setContent(this.createTooltipContent(m));
const marker = L.circle([m.coordinate?.latitude, m.coordinate?.longitude], {
renderer: this.canvas,
radius: 32,
color: '#00695c',
fillColor: '#f9a825'