diff --git a/web-angular/src/app/services/bulk-measurement.service.ts b/web-angular/src/app/services/bulk-measurement.service.ts index ea665b6f..d7b91904 100644 --- a/web-angular/src/app/services/bulk-measurement.service.ts +++ b/web-angular/src/app/services/bulk-measurement.service.ts @@ -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; private cachedMeasurements: Array; + private dmPages: Array; private timeRanges: Array | null; private exporting: boolean; + private cacheReleaseCount: number; constructor( protected readonly defaultService: DefaultService, @@ -488,12 +492,19 @@ class AdminCachingHandler extends AbstractExportHandler { } start(range: SatDatepickerRangeValue, 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): boolean { - let fetch: boolean; + private checkIfDataNeedsToBeFetched(satRange: SatDatepickerRangeValue): ProgressStatus { + let fetch: ProgressStatus; this.timeRanges = new Array(); 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(); - 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(); 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(); 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); } diff --git a/web-angular/src/app/services/resource.service.ts b/web-angular/src/app/services/resource.service.ts index 469da4bd..f4d6678a 100644 --- a/web-angular/src/app/services/resource.service.ts +++ b/web-angular/src/app/services/resource.service.ts @@ -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. ' diff --git a/web-angular/src/app/views-manage/manage-measurements-map.component.html b/web-angular/src/app/views-manage/manage-measurements-map.component.html index bf355d15..115efd18 100644 --- a/web-angular/src/app/views-manage/manage-measurements-map.component.html +++ b/web-angular/src/app/views-manage/manage-measurements-map.component.html @@ -32,9 +32,15 @@ - Fortschritt: {{ progress }}% + {{ res.strings('export_measurements_progress_label') }}: {{ progress }}% + + {{ res.strings('export_measurements_alt_progress_label')}} +
- + + + +