From 3bc8ef36d1ab47ea0dc848d400d5856c9c6a9149 Mon Sep 17 00:00:00 2001 From: Boris Vasilenko Date: Fri, 11 Aug 2023 08:25:44 +0300 Subject: [PATCH] NAS-123274 / 23.10 / Remove usages of CoreService for getting disk temperatures (#8503) * NAS-123274: Remove usages of CoreService for getting disk temperatures * NAS-123274: Fix remarks * NAS-123274: Fix duplicate interval --------- Co-authored-by: Boris Vasilenko --- .../events/disk-events.interface.ts | 7 --- .../enclosure-disks-mini.component.ts | 3 -- .../enclosure-disks.component.ts | 19 +++----- src/app/services/disk-temperature.service.ts | 47 ++++++++++++------- 4 files changed, 35 insertions(+), 41 deletions(-) diff --git a/src/app/interfaces/events/disk-events.interface.ts b/src/app/interfaces/events/disk-events.interface.ts index c90df1810ab..89a48249ba6 100644 --- a/src/app/interfaces/events/disk-events.interface.ts +++ b/src/app/interfaces/events/disk-events.interface.ts @@ -1,12 +1,5 @@ import { EnclosureView } from 'app/interfaces/enclosure.interface'; import { DriveTray } from 'app/pages/system/view-enclosure/classes/drivetray'; -import { Temperature } from 'app/services/disk-temperature.service'; - -export interface DiskTemperaturesEvent { - name: 'DiskTemperatures'; - sender: unknown; - data: Temperature; -} export interface DriveSelectedEvent { name: 'DriveSelected'; diff --git a/src/app/pages/system/view-enclosure/components/enclosure-disks-mini/enclosure-disks-mini.component.ts b/src/app/pages/system/view-enclosure/components/enclosure-disks-mini/enclosure-disks-mini.component.ts index 16e99d025a9..6a5c21f9f5f 100644 --- a/src/app/pages/system/view-enclosure/components/enclosure-disks-mini/enclosure-disks-mini.component.ts +++ b/src/app/pages/system/view-enclosure/components/enclosure-disks-mini/enclosure-disks-mini.component.ts @@ -15,7 +15,6 @@ import { EnclosureDisksComponent, } from 'app/pages/system/view-enclosure/components/enclosure-disks/enclosure-disks.component'; import { EnclosureStore } from 'app/pages/system/view-enclosure/stores/enclosure-store.service'; -import { CoreService } from 'app/services/core-service/core.service'; import { DialogService } from 'app/services/dialog.service'; import { DiskTemperatureService } from 'app/services/disk-temperature.service'; import { ThemeService } from 'app/services/theme/theme.service'; @@ -38,7 +37,6 @@ export class EnclosureDisksMiniComponent extends EnclosureDisksComponent { } constructor( - protected core: CoreService, public cdr: ChangeDetectorRef, public dialogService: DialogService, protected translate: TranslateService, @@ -50,7 +48,6 @@ export class EnclosureDisksMiniComponent extends EnclosureDisksComponent { protected enclosureStore: EnclosureStore, ) { super( - core, cdr, dialogService, translate, diff --git a/src/app/pages/system/view-enclosure/components/enclosure-disks/enclosure-disks.component.ts b/src/app/pages/system/view-enclosure/components/enclosure-disks/enclosure-disks.component.ts index 1696ab9374d..5d1882866b6 100644 --- a/src/app/pages/system/view-enclosure/components/enclosure-disks/enclosure-disks.component.ts +++ b/src/app/pages/system/view-enclosure/components/enclosure-disks/enclosure-disks.component.ts @@ -18,11 +18,7 @@ import { Enclosure, EnclosureElement, EnclosureSlot, EnclosureView, } from 'app/interfaces/enclosure.interface'; import { CoreEvent } from 'app/interfaces/events'; -import { - CanvasExtractEvent, - DiskTemperaturesEvent, - DriveSelectedEvent, -} from 'app/interfaces/events/disk-events.interface'; +import { CanvasExtractEvent, DriveSelectedEvent } from 'app/interfaces/events/disk-events.interface'; import { LabelDrivesEvent } from 'app/interfaces/events/label-drives-event.interface'; import { Pool } from 'app/interfaces/pool.interface'; import { Disk, TopologyDisk } from 'app/interfaces/storage.interface'; @@ -58,7 +54,6 @@ import { import { SystemProfile } from 'app/pages/system/view-enclosure/components/view-enclosure/view-enclosure.component'; import { ViewConfig } from 'app/pages/system/view-enclosure/interfaces/view.config'; import { EnclosureState, EnclosureStore } from 'app/pages/system/view-enclosure/stores/enclosure-store.service'; -import { CoreService } from 'app/services/core-service/core.service'; import { DialogService } from 'app/services/dialog.service'; import { DiskTemperatureService, Temperature } from 'app/services/disk-temperature.service'; import { ThemeService } from 'app/services/theme/theme.service'; @@ -278,7 +273,6 @@ export class EnclosureDisksComponent implements AfterContentInit, OnDestroy { readonly EnclosureLocation = EnclosureLocation; constructor( - protected core: CoreService, public cdr: ChangeDetectorRef, public dialogService: DialogService, protected translate: TranslateService, @@ -292,11 +286,11 @@ export class EnclosureDisksComponent implements AfterContentInit, OnDestroy { this.themeUtils = new ThemeUtils(); this.diskTemperatureService.listenForTemperatureUpdates(); - core.register({ observerClass: this, eventName: 'DiskTemperatures' }).pipe(untilDestroyed(this)).subscribe((evt: DiskTemperaturesEvent) => { + this.diskTemperatureService.temperature$.pipe(untilDestroyed(this)).subscribe((data) => { const chassisView: ChassisView = this.chassisView && this.view === 'rear' ? this.chassis?.rear : this.chassis?.front; if (!this.chassis || !chassisView?.driveTrayObjects) { return; } - const clone: Temperature = { ...evt.data }; + const clone: Temperature = { ...data }; clone.values = {}; clone.keys = []; @@ -309,7 +303,7 @@ export class EnclosureDisksComponent implements AfterContentInit, OnDestroy { if (disk) { clone.keys.push(disk.name); - clone.values[disk.name] = evt.data.values[disk.name]; + clone.values[disk.name] = data.values[disk.name]; } }); } else { @@ -322,7 +316,7 @@ export class EnclosureDisksComponent implements AfterContentInit, OnDestroy { this.temperatures = clone; }); - core.emit({ name: 'DiskTemperaturesSubscribe', sender: this }); + this.diskTemperatureService.diskTemperaturesSubscribe(); this.store$.select(selectTheme).pipe( filter(Boolean), @@ -438,8 +432,7 @@ export class EnclosureDisksComponent implements AfterContentInit, OnDestroy { // Component Cleanup ngOnDestroy(): void { - this.core.emit({ name: 'DiskTemperaturesUnsubscribe', sender: this }); - this.core.unregister({ observerClass: this }); + this.diskTemperatureService.diskTemperaturesUnsubscribe(); this.destroyAllEnclosures(); this.app.stage.destroy(true); this.app.destroy(true); diff --git a/src/app/services/disk-temperature.service.ts b/src/app/services/disk-temperature.service.ts index e4119903dc8..9740d5c6fc2 100644 --- a/src/app/services/disk-temperature.service.ts +++ b/src/app/services/disk-temperature.service.ts @@ -1,10 +1,9 @@ import { Injectable, OnDestroy } from '@angular/core'; import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy'; -import { map, Subject } from 'rxjs'; +import { map, Observable, Subject } from 'rxjs'; import { DiskType } from 'app/enums/disk-type.enum'; import { Disk, DiskTemperatures } from 'app/interfaces/storage.interface'; import { Interval } from 'app/interfaces/timeout.interface'; -import { CoreService } from 'app/services/core-service/core.service'; import { DisksUpdateService } from 'app/services/disks-update.service'; import { WebSocketService } from 'app/services/ws.service'; @@ -26,27 +25,18 @@ export class DiskTemperatureService implements OnDestroy { private disksUpdateSubscriptionId: string; + private _temperature$ = new Subject(); + + get temperature$(): Observable { + return this._temperature$.asObservable(); + } + constructor( - protected core: CoreService, protected websocket: WebSocketService, private disksUpdateService: DisksUpdateService, ) { } listenForTemperatureUpdates(): void { - this.core.register({ observerClass: this, eventName: 'DiskTemperaturesSubscribe' }).subscribe(() => { - this.subscribers++; - if (!this.broadcast) { - this.start(); - } - }); - - this.core.register({ observerClass: this, eventName: 'DiskTemperaturesUnsubscribe' }).subscribe(() => { - this.subscribers--; - if (this.subscribers === 0) { - this.stop(); - } - }); - this.websocket.call('disk.query', [[], { select: ['name', 'type'] }]).subscribe((disks) => { this.disks = disks; if (this.subscribers > 0) this.start(); @@ -63,10 +53,31 @@ export class DiskTemperatureService implements OnDestroy { this.disks = disks; if (this.subscribers > 0) this.start(); }); + if (this.disksUpdateSubscriptionId) { + this.disksUpdateService.removeSubscriber(this.disksUpdateSubscriptionId); + } this.disksUpdateSubscriptionId = this.disksUpdateService.addSubscriber(disksUpdateTrigger$, true); } + diskTemperaturesSubscribe(): void { + this.subscribers++; + if (!this.broadcast) { + this.start(); + } + } + + diskTemperaturesUnsubscribe(): void { + this.subscribers--; + if (this.subscribers === 0) { + this.stop(); + } + } + start(): void { + if (this.broadcast) { + return; + } + this.fetch(this.disks.map((disk) => disk.name)); this.broadcast = setInterval(() => { this.fetch(this.disks.map((disk) => disk.name)); @@ -86,7 +97,7 @@ export class DiskTemperatureService implements OnDestroy { unit: 'Celsius', symbolText: '°', }; - this.core.emit({ name: 'DiskTemperatures', data, sender: this }); + this._temperature$.next(data); }); }