summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/api/directory-store.service.ts
blob: cdc5337ac121921d58b7801cb194f312937e2f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Injectable } from '@angular/core';
import { CephfsService } from './cephfs.service';
import { BehaviorSubject, Observable, Subject, timer } from 'rxjs';
import { CephfsDir } from '../models/cephfs-directory-models';
import { filter, map, retry, share, switchMap, takeUntil, tap } from 'rxjs/operators';

type DirectoryStore = Record<number, CephfsDir[]>;

const POLLING_INTERVAL = 600 * 1000;

@Injectable({
  providedIn: 'root'
})
export class DirectoryStoreService {
  private _directoryStoreSubject = new BehaviorSubject<DirectoryStore>({});

  readonly directoryStore$: Observable<DirectoryStore> = this._directoryStoreSubject.asObservable();

  stopDirectoryPolling = new Subject();

  isLoading = true;

  constructor(private cephFsService: CephfsService) {}

  loadDirectories(id: number, path = '/', depth = 3) {
    this.directoryStore$
      .pipe(
        filter((store: DirectoryStore) => !Boolean(store[id])),
        switchMap(() =>
          timer(0, POLLING_INTERVAL).pipe(
            switchMap(() =>
              this.cephFsService.lsDir(id, path, depth).pipe(
                tap((response) => {
                  this.isLoading = false;
                  this._directoryStoreSubject.next({ [id]: response });
                })
              )
            ),
            retry(),
            share(),
            takeUntil(this.stopDirectoryPolling)
          )
        )
      )
      .subscribe();
  }

  search(term: string, id: number, limit = 5) {
    return this.directoryStore$.pipe(
      map((store: DirectoryStore) => {
        const regEx = new RegExp(term, 'gi');
        const results = store[id]
          .filter((x) => regEx.test(x.path))
          .map((x) => x.path)
          .slice(0, limit);
        return results;
      })
    );
  }

  stopPollingDictories() {
    this.stopDirectoryPolling.next();
  }
}