File

src/app/modules/daily-processing/processing.service.ts

Index

Properties
Methods

Constructor

constructor(httpClient: HttpClient, datePipe: DatePipe)
Parameters :
Name Type Optional
httpClient HttpClient No
datePipe DatePipe No

Methods

getCutOff
getCutOff(cutOff: string)
Parameters :
Name Type Optional
cutOff string No
Returns : any
getProcessingInfo
getProcessingInfo(date: Date | string)
Parameters :
Name Type Optional
date Date | string No
Returns : void
loadProcessingInfo
loadProcessingInfo()
Returns : void

Properties

Public entry$
entry$:
Default value : this.entrySubject.asObservable()
Private entrySubject
entrySubject:
Default value : new BehaviorSubject<InfoEntry>(null)
Public info$
info$:
Default value : this.infoSubject.asObservable()
Private infoSubject
infoSubject:
Default value : new BehaviorSubject<ProcessingInfo>(null)
import { DatePipe } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { filter, map, tap } from 'rxjs/operators';
import { API_ROOT } from 'src/app/app.constants';
import { InfoEntry, ProcessingInfo } from './models/i-processing-info';

export const colors = {
  RED: 'red',
  GREEN: 'green',
  YELLOW: 'yellow',
  BLUE: 'blue'
};

@Injectable({ providedIn: 'root' })
export class ProcessingService {
  private infoSubject = new BehaviorSubject<ProcessingInfo>(null);
  public info$ = this.infoSubject.asObservable();

  private entrySubject = new BehaviorSubject<InfoEntry>(null);
  public entry$ = this.entrySubject.asObservable();

  constructor(private httpClient: HttpClient, private datePipe: DatePipe) {}

  loadProcessingInfo() {
    try {
      const json = localStorage.getItem('info');
      const info = JSON.parse(json) as ProcessingInfo;
      if (!info) {
        return;
      }
      this.infoSubject.next(info);
    } catch (error) {
      console.log(error);
    }
    return;
  }

  getProcessingInfo(date: Date | string) {
    // if date is a string use it directly otherwise convert it to short date string
    const shortDate = typeof date === 'string' ? date : this.datePipe.transform(date, 'shortDate');
    this.httpClient
      .get<ProcessingInfo>(`${API_ROOT}/processing/info?date=${shortDate}`)
      .pipe(
        map(allInfo => {
          console.log({ allInfo });
          const keys = Object.keys(allInfo);
          const entries = keys.map(key => {
            const entry = allInfo[key] as InfoEntry;
            const healthCheckColor = entry.ProcessingRunInfo.map(run => {
              return run.HealthCheckSummary.HealthCheckStatus;
            })
              .filter(status => status.toLowerCase() !== colors.GREEN)
              .reduce((previousValue, currentValue) => {
                const p = previousValue.toLowerCase();
                const c = currentValue.toLowerCase();
                if (p === c) {
                  return c;
                }

                if (p === colors.BLUE && c !== colors.BLUE) {
                  return c;
                }

                if (p === colors.GREEN && (c === colors.YELLOW || c === colors.RED)) {
                  return c;
                }

                if (p === colors.YELLOW && c === colors.RED) {
                  return c;
                }
                return p;
              }, colors.GREEN)
              .toLowerCase();
            return {
              color: healthCheckColor,
              cutOff: key,
              AchSummary: entry.AchSummary,
              ProcessingRunInfo: entry.ProcessingRunInfo,
              ProcessingRunSummary: entry.ProcessingRunSummary
            } as InfoEntry;
          });
          return { entries } as ProcessingInfo;
        }),
        tap(info => {
          this.infoSubject.next(info);
          localStorage.setItem('info', JSON.stringify(info));
          console.log('info retrieved and stored');
        })
      )
      .subscribe();
  }

  getCutOff(cutOff: string) {
    return this.info$
      .pipe(
        filter(
          info => info !== null && info !== undefined && info.entries !== null && info.entries !== undefined
        ),
        map(info => {
          // get cutoff time from entries list if not found return null
          return info.entries.filter(e => e.cutOff === cutOff).reduce((p, c) => c, null);
        })
      )
      .subscribe(entry => {
        this.entrySubject.next(entry);
        return entry;
      });
  }
}

result-matching ""

    No results matching ""