ngOnInit()

in web/frontend/src/app/pages/streams/components/filters-panel/filters-panel.component.ts [100:231]


  ngOnInit() {
    this.activatedRoute.params
      .pipe(switchMap((tab) => {
        this.stream = tab.stream;
        return this.schemaService.getSchema(tab.stream, null, true);
      }))
      .pipe(
        map((response) => [...response.types]),
        takeUntil(this.destroy$),
        catchError((e: HttpErrorResponse) => {
          this.streamsService.nonExistentStreamNavigated.next(this.stream);
          return throwError(e);
        }),
      )
      .subscribe((schema) => (this.schema = schema));
    
    const range$ = this.activatedRoute.params.pipe(
      switchMap((params) =>
        params.symbol
          ? this.symbolsService
            .getProps(params.stream, params.symbol, 1000)
            .pipe(map((p) => p?.props.symbolRange))
          : this.streamsService.getProps(params.stream).pipe(map((p) => p?.props.range)),
      ),
      shareReplay(1),
      catchError(e => {
        this.hasError = true;
        this.cdr.detectChanges();
        return of(null);
      }),
      filter(r => !!r),
    );
    
    this.initialDate$ = this.activatedRoute.params.pipe(
      switchMap(() =>
        combineLatest([
          this.appStore.pipe(
            select(getActiveTab),
            filter((t) => !!t),
          ),
          range$,
        ]),
      ),
      map(([tab, range]) => {
        if (tab.live) {
          return new Date(new Date(range.end).getTime() + 1).toISOString();
        }
        return tab.reverse || tab.monitor ? range?.end : range?.start;
      }),
      map((date) => date || this.now.toISOString()),
    );
    
    const filtersAndInitial$ = combineLatest([
      this.appStore.pipe(
        select(getActiveTabFilters),
        filter((f) => !!f),
      ),
      this.initialDate$,
    ]);
    
    this.dateTitle$ = merge(
      filtersAndInitial$.pipe(
        switchMap(([filters, initial]) => this.utcToDatePicker(filters.from || initial)),
      ),
      this.tmpDate$.pipe(map((date) => date.toISOString())),
    );
    
    this.dateTitle$.pipe(takeUntil(this.destroy$)).subscribe(date => this.selectedDate = date);
    
    const filters$ = this.appStore.pipe(select(getActiveTabFilters));
    this.manuallyChanged$ = filters$.pipe(map((filters) => filters?.manuallyChanged));
    
    this.filteredTypesSymbols$ = filters$.pipe(
      map((filters) => !!(filters?.filter_symbols?.length || filters?.filter_types?.length)),
    );
    
    this.activatedRoute.params
      .pipe(
        tap(() => {
          this.hasError = false;
          this.cdr.detectChanges();
        }),
        switchMap((params) =>
          filtersAndInitial$.pipe(
            take(1),
            map((data) => data),
          ),
        ),
        takeUntil(this.destroy$),
      )
      .subscribe(([filter, initial]) => {
        if (!filter?.from) {
          this.updateFilters({from: initial});
        }
      });
    
    this.bsConfig$ = this.globalFiltersService.getBsConfig();
    const activeTab$ = this.appStore.pipe(select(getActiveOrFirstTab));
    
    this.showExportBtn$ = activeTab$.pipe(
      filter((t) => !!t),
      map((tab) => !tab.monitor && !tab.live),
    );
    
    this.streamUpdatesService
      .onUpdates()
      .pipe(
        withLatestFrom(activeTab$),
        debounceTime(100),
        switchMap(([updates, activeTab]) => {
          if (updates.changed.includes(activeTab.stream) && activeTab.filter?.filter_symbols) {
            return this.symbolsService.getSymbols(activeTab.stream, activeTab.space);
          }
          
          return of(null);
        }),
        withLatestFrom(activeTab$),
        takeUntil(this.destroy$),
      )
      .subscribe(([updatedSymbols, activeTab]) => {
        if (!updatedSymbols) {
          return;
        }
        
        const newSymbols = activeTab.filter.filter_symbols.filter((symbol) =>
          updatedSymbols.includes(symbol),
        );
        if (newSymbols.length !== activeTab.filter.filter_symbols.length) {
          this.updateFilterSymbols(newSymbols);
        }
      });
  }