import { Injectable } from '@angular/core' ; import { BehaviorSubject } from 'rxjs' ; /**
 * A service that parses an oaf product into more consumable bits,
 * the forecast and model objects
 */ @ Injectable ( ) export class OafService { forecast$ : BehaviorSubject < any > = new BehaviorSubject < any > ( null ) ; model$ : BehaviorSubject < any > = new BehaviorSubject < any > ( null ) ; oaf$ : BehaviorSubject < any > = new BehaviorSubject < any > ( null ) ; param product getOaf ( product ) { try { const bytes = product . contents [ '' ] . bytes ; const oaf = this . parseOaf ( bytes ) ; const forecast = this . parseForecast ( oaf . forecast ) ; const model = this . parseModel ( oaf . model ) ; this . oaf$ . next ( oaf ) ; this . forecast$ . next ( forecast ) ; this . model$ . next ( model ) ; } catch ( e ) { this . oaf$ . next ( null ) ; this . forecast$ . next ( null ) ; this . model$ . next ( null ) ; } } param model return { any } parseForecast ( forecasts ) { const columns = [ ] ; const columnIds = [ 'magnitude' ] ; const rows = [ ] ; forecasts . forEach ( ( forecast , fIndex ) => { const id = forecast . label . replace ( ' ' , '_' ) ; columnIds . push ( id ) ; columns . push ( { id , label : forecast . label , timeEnd : forecast . timeEnd , timeStart : forecast . timeStart } ) ; forecast . bins . forEach ( ( bin , bIndex ) => { if ( fIndex === 0 ) { rows [ bIndex ] = { data : { } , magnitude : bin . magnitude } ; } rows [ bIndex ] . data [ id ] = bin ; } ) ; } ) ; return { columnIds , columns , rows } ; } param model parseModel ( model ) { return { name : model . name , parameters : { keys : Object . keys ( model . parameters ) , values : model . parameters } , ref : model . ref } ; } param bytes return { any } parseOaf ( bytes ) { return JSON . parse ( bytes ) ; } }