All files / src/app/linqpad-review-pages/referentials/services referentials-data.service.ts

100% Statements 58/58
100% Branches 2/2
100% Functions 20/20
100% Lines 49/49
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 891x 1x 1x         1x 1x 1x 1x 1x 1x 1x     1x   1x 1x   5x         5x 5x 5x 5x 5x 5x 5x   5x     1x 1x 1x 1x 1x 1x 1x 1x           2x 2x     2x 2x 2x       1x 3x   2x 2x     2x 2x 2x 12x   2x 2x                 1x 12x 12x     1x  
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { NgRedux, select } from '@angular-redux/store';
 
import { IFileInfo } from '../../../model/fileInfo.model';
import { IAppState } from '../../../store/state/AppState';
import { IMeasureUpdate } from '../../../model/measure.model';
import { DataService } from '../../../services/data-service/data.service';
import { NameParsingService } from '../../../services/data-service/name-parsing.service';
import { ListFormatterService } from '../../../services/list-formatter.service/list-formatter.service';
import { FileService } from '../../../services/file-service/file.service';
import { ReferentialsFormatService } from './referentials-format.service';
import { ReferentialsActions } from './referentials.actions';
import { Logger } from '../../../common/mw.common.module';
 
@Injectable()
export class ReferentialsDataService extends DataService {
 
  @select(['pages', 'referentials', 'files']) files$: Observable<IFileInfo[]>;
  @select(['config', 'referentialsConfig']) config$: Observable<any>;
 
  protected PAGE = 'referentials';
  private daysToInitialize;
  private daysToDisplay;
 
  constructor (
    protected formatService: ReferentialsFormatService,
    protected nameParsingService: NameParsingService,
    protected listFormatterService: ListFormatterService,
    protected fileService: FileService,
    protected logger: Logger,
    protected actions: ReferentialsActions,
    private ngRedux: NgRedux<IAppState>,
  ) {
    super(formatService, nameParsingService, listFormatterService, fileService, logger, actions);
  }
 
  public initializeList() {
    this.getConfig$().subscribe(_ =>
      this.getBaseDataUrl$().subscribe(__ => {
        this.fileService.getFileList(this.baseUrl);
        this.parsed$(this.filesOfType$).subscribe(files => {
          const filesToDisplay = this.getCountFilesForLastNDays(this.daysToDisplay, files);
          const filesToInit = this.getCountFilesForLastNDays(this.daysToInitialize, files);
          super.initializeList(filesToInit, filesToDisplay);
        });
      })
    );
  }
 
  private getConfig$(): Observable<any> {
    return this.config$
      .waitFor$()
      .do(config => {
        this.filePrefixes = config.filePrefixes;
        this.daysToInitialize = config.daysToInitialize;
        this.daysToDisplay = config.daysToDisplay;
      });
  }
 
  private getCountFilesForLastNDays(numDays: number, files): number {
    return this.filesByDate(files)
      .slice(0, numDays)
      .map(group => group.files.length)
      .reduce((total, num) => total += num, 0 );
  }
 
  public getMeasure(): Observable<IMeasureUpdate> {
    return this.files$
      .waitFor$(data => data.length)
      .filter(files => files.filter(file => file.content))
      .map(files => {
        const history = this.calcHistory(files);
        return {
            id: 'referentials',
            metric: history[0],
            color: this.formatService.getBadgeColor(history[0]),
            history: history.reverse()
          };
      });
  }
 
  private calcHistory(files: Array<IFileInfo>): number[] {
    return this.filesByDate(files.filter(f => !!f.metric))
      .map(x => x.files.reduce((total, file) => total + file.metric, 0));
  }
 
}