11import { DataSource } from '@angular/cdk/collections' ;
22import { MatPaginator , MatSort } from '@angular/material' ;
33import { Observable , BehaviorSubject , Subscription } from 'rxjs' ;
4- import { SocketService } from 'src/app/api/api/socket.service' ;
54import { Instance } from 'src/app/model/models/instance' ;
6- import { DatePipe } from '@angular/common' ;
7- import { EventTypeEnum } from 'src/app/model/models/socketMessage' ;
85import { StoreService } from 'src/app/model/store.service' ;
6+ import { EventService } from 'src/app/model/event.service' ;
97
108export interface InfoCenterItem {
119 instanceId : number ;
@@ -23,16 +21,15 @@ export interface InfoCenterItem {
2321export class InfoCenterDataSource extends DataSource < InfoCenterItem > {
2422 private infoCenterSubject : BehaviorSubject < InfoCenterItem [ ] > ;
2523
26- private instanceAddedSubscription : Subscription ;
27- private instanceChangedSubscription : Subscription ;
28- private instanceRemovedSubscription : Subscription ;
24+ private eventSubscription : Subscription ;
2925 private data : InfoCenterItem [ ] ;
3026 public numberEvents = 0 ;
3127 private instance : Instance ;
3228
33- constructor ( private storeService : StoreService , private socketService : SocketService ,
34- private paginator : MatPaginator , private sort : MatSort , private compType : string , private instanceId : string ) {
29+ constructor ( private storeService : StoreService , private paginator : MatPaginator ,
30+ private sort : MatSort , private compType : string , private instanceId : string , private eventService : EventService ) {
3531 super ( ) ;
32+
3633 this . data = [ ] ;
3734 if ( this . instanceId ) {
3835 this . instance = this . storeService . getState ( ) . instances [ this . instanceId ] ;
@@ -44,36 +41,17 @@ export class InfoCenterDataSource extends DataSource<InfoCenterItem> {
4441
4542 this . paginator . page . subscribe ( ( ) => { this . infoCenterSubject . next ( this . getPagedData ( ) ) ; } ) ;
4643 this . sort . sortChange . subscribe ( ( ) => { this . data = this . getSortedData ( this . data ) ; this . infoCenterSubject . next ( this . getPagedData ( ) ) ; } ) ;
47-
48- this . instanceAddedSubscription = this . socketService . subscribeForEvent < Instance > ( EventTypeEnum . InstanceAddedEvent ) .
49- subscribe ( ( newInstance : Instance ) => {
50- if ( this . applyFilter ( newInstance ) ) {
51- const newEntry = this . transformEventToNotificaton ( newInstance , 'new Instance added' , 'add_circle' ) ;
52- this . applyUpdate ( newEntry ) ;
53- }
54- } ) ;
55-
56- this . instanceRemovedSubscription = this . socketService . subscribeForEvent < Instance > ( EventTypeEnum . InstanceRemovedEvent ) .
57- subscribe ( ( removeInstance : Instance ) => {
58- if ( this . applyFilter ( removeInstance ) ) {
59- const newEntry = this . transformEventToNotificaton ( removeInstance , 'Instance removed' , 'delete_sweep' ) ;
60- this . applyUpdate ( newEntry ) ;
61- }
62- } ) ;
63-
64- this . instanceChangedSubscription = this . socketService . subscribeForEvent < Instance > ( EventTypeEnum . StateChangedEvent ) .
65- subscribe ( ( changeInstance : Instance ) => {
66- if ( this . applyFilter ( changeInstance ) ) {
67- const newEntry = this . transformEventToNotificaton ( changeInstance , 'Instance changed' , 'link' ) ;
68- this . applyUpdate ( newEntry ) ;
69- }
70- } ) ;
44+ this . paginator . initialized . subscribe ( ( ) => { this . infoCenterSubject . next ( this . getPagedData ( ) ) ; } ) ;
45+ this . eventSubscription = this . eventService . getEventObservable ( ) . subscribe ( ( newNotifs : InfoCenterItem [ ] ) => {
46+ this . applyUpdate ( newNotifs ) ;
47+ } ) ;
7148 }
7249
73- applyFilter ( instance : Instance ) : boolean {
50+ applyFilter ( notifItem : InfoCenterItem ) : boolean {
7451 if ( ! this . instanceId && ! this . compType ) {
7552 return true ;
7653 } else {
54+ const instance = this . storeService . getState ( ) . instances [ notifItem . instanceId ] ;
7755 if ( this . instanceId ) {
7856 return instance . id === this . instance . id ;
7957 } else {
@@ -91,8 +69,11 @@ export class InfoCenterDataSource extends DataSource<InfoCenterItem> {
9169 return this . infoCenterSubject . asObservable ( ) ;
9270 }
9371
94- private applyUpdate ( newEntry : InfoCenterItem ) {
95- this . data = this . getSortedData ( [ newEntry , ...this . data ] ) ;
72+ private applyUpdate ( newEntry : InfoCenterItem [ ] ) {
73+
74+ const filteredEntries = newEntry . filter ( ( entry => this . applyFilter ( entry ) ) ) ;
75+
76+ this . data = this . getSortedData ( [ ...filteredEntries ] ) ;
9677 this . numberEvents = this . data . length ;
9778 this . infoCenterSubject . next ( this . getPagedData ( ) ) ;
9879 }
@@ -102,9 +83,9 @@ export class InfoCenterDataSource extends DataSource<InfoCenterItem> {
10283 * any open connections or free any held resources that were set up during connect.
10384 */
10485 disconnect ( ) {
105- this . instanceAddedSubscription . unsubscribe ( ) ;
106- this . instanceChangedSubscription . unsubscribe ( ) ;
107- this . instanceRemovedSubscription . unsubscribe ( ) ;
86+ this . eventSubscription . unsubscribe ( ) ;
87+ this . paginator . page . unsubscribe ( ) ;
88+ this . sort . sortChange . unsubscribe ( ) ;
10889 this . infoCenterSubject . complete ( ) ;
10990 }
11091
@@ -113,8 +94,12 @@ export class InfoCenterDataSource extends DataSource<InfoCenterItem> {
11394 * this would be replaced by requesting the appropriate data from the server.
11495 */
11596 private getPagedData ( ) {
116- const startIndex = this . paginator . pageIndex * this . paginator . pageSize ;
117- return [ ...this . data ] . splice ( startIndex , this . paginator . pageSize ) ;
97+
98+ if ( this . paginator && this . paginator . pageIndex !== undefined && this . paginator . pageSize !== undefined ) {
99+ const startIndex = this . paginator . pageIndex * this . paginator . pageSize ;
100+ return [ ...this . data ] . splice ( startIndex , this . paginator . pageSize ) ;
101+ }
102+ return [ ...this . data ] ;
118103 }
119104
120105 /**
@@ -137,13 +122,6 @@ export class InfoCenterDataSource extends DataSource<InfoCenterItem> {
137122 } ) ;
138123 }
139124
140- private transformEventToNotificaton ( instance : Instance , notifName : string , type : string ) : InfoCenterItem {
141- const datePipe = new DatePipe ( 'en-US' ) ;
142- const actualDate = datePipe . transform ( Date . now ( ) , 'dd/MM/yyyy hh:mm:ss:SSS' ) ;
143- return { instanceId : instance . id , type : type ,
144- notifName : notifName , dateTime : actualDate , details : instance . name } ;
145- }
146-
147125}
148126
149127/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
0 commit comments