2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
20 import { Component, OnInit, ViewChild } from '@angular/core';
21 import { FormControl, FormGroup } from '@angular/forms';
22 import { MatSort, Sort } from '@angular/material/sort';
23 import { MatTableDataSource } from '@angular/material/table';
24 import { forkJoin, of } from 'rxjs';
25 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
26 import { mergeMap, finalize } from 'rxjs/operators';
27 import { EIService } from '@services/ei/ei.service';
28 import { EIProducer } from '@interfaces/ei.types';
29 import { UiService } from '@services/ui/ui.service';
32 selector: 'nrcp-producers-list',
33 templateUrl: './producers-list.component.html',
34 styleUrls: ['./producers-list.component.scss']
36 export class ProducersListComponent implements OnInit {
38 @ViewChild(MatSort) sort: MatSort;
40 producersDataSource: MatTableDataSource<EIProducer>;
41 producerForm: FormGroup;
44 private loadingSubject = new BehaviorSubject<boolean>(false);
45 private producerSubject = new BehaviorSubject<EIProducer[]>([]);
46 public loading$ = this.loadingSubject.asObservable();
49 private eiSvc: EIService,
50 private ui: UiService) {
52 this.producerForm = new FormGroup({
53 ei_producer_id: new FormControl(''),
54 ei_producer_types: new FormControl(''),
55 status: new FormControl('')
61 this.producerSubject.subscribe((data) => {
62 this.producersDataSource = new MatTableDataSource<EIProducer>(data);
64 this.producersDataSource.filterPredicate = ((data, filter) => {
65 let searchTerms = JSON.parse(filter);
66 return this.isDataIncluding(data.ei_producer_id, searchTerms.ei_producer_id)
67 && this.isDataIncluding(data.ei_producer_types.join(','), searchTerms.ei_producer_types)
68 && this.isDataIncluding(data.status, searchTerms.status);
69 }) as (data: EIProducer, filter: any) => boolean;
72 this.producerForm.valueChanges.subscribe(value => {
73 this.producersDataSource.filter = JSON.stringify(value);
76 this.ui.darkModeState.subscribe((isDark) => {
77 this.darkMode = isDark;
82 if (!this.producerSubject) this.producerSubject.unsubscribe();
83 if (!this.loadingSubject) this.loadingSubject.unsubscribe();
84 if (!this.ui.darkModeState) this.ui.darkModeState.unsubscribe();
87 isDataIncluding(data: string, filter: string): boolean {
88 const transformedFilter = filter.trim().toLowerCase();
89 return data.toLowerCase().includes(transformedFilter);
93 this.producerForm.get('ei_producer_id').setValue('');
94 this.producerForm.get('ei_producer_types').setValue('');
95 this.producerForm.get('status').setValue('');
98 sortProducers(sort: Sort) {
99 const data = this.producersDataSource.data
100 data.sort((a: EIProducer, b: EIProducer) => {
101 const isAsc = sort.direction === 'asc';
102 switch (sort.active) {
103 case 'id': return this.compare(a.ei_producer_id, b.ei_producer_id, isAsc);
104 case 'types': return this.compare(a.ei_producer_types, b.ei_producer_types, isAsc);
105 case 'status': return this.compare(a.status, b.status, isAsc);
109 this.producersDataSource.data = data;
112 compare(a: any, b: any, isAsc: boolean) {
113 return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
116 stopSort(event: any) {
117 event.stopPropagation();
120 getProducerTypes(eiProducer: EIProducer): string[] {
121 if (eiProducer.ei_producer_types) {
122 return eiProducer.ei_producer_types;
124 return ['< No types >'];
127 getProducerStatus(eiProducer: EIProducer): string {
128 if (eiProducer.status) {
129 return eiProducer.status;
131 return '< No status >';
134 public eiProducers(): EIProducer[] {
135 return this.producerSubject.value;
139 this.loadingSubject.next(true);
142 this.eiSvc.getProducerIds().pipe(
144 forkJoin(prodIds.map(id => {
147 this.eiSvc.getProducer(id),
148 this.eiSvc.getProducerStatus(id)
152 finalize(() => this.loadingSubject.next(false)),
153 ).subscribe(result => {
154 producers = result.map(producer => {
155 let eiProducer = <EIProducer>{};
156 eiProducer.ei_producer_id = producer[0];
157 if (producer[1].supported_ei_types) {
158 eiProducer.ei_producer_types = producer[1].supported_ei_types;
160 if (producer[2].operational_state) {
161 eiProducer.status = producer[2].operational_state.toString();
165 this.producerSubject.next(producers);
167 console.error("Subscribe function error:" + err);