Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 1 | import {Injectable} from '@angular/core'; |
| 2 | import {ServiceInfoModel, ServiceInfoUiModel} from '../shared/server/serviceInfo/serviceInfo.model'; |
| 3 | import {isNullOrUndefined} from "util"; |
| 4 | import { Observable } from 'rxjs/Observable'; |
| 5 | import 'rxjs/observable/of'; |
| 6 | |
| 7 | export let PENDING : string = "pending"; |
| 8 | export let INPROGRESS : string = "inprogress"; |
| 9 | export let PAUSE : string = "pause"; |
| 10 | export let X_O : string = "X_o"; |
| 11 | export let SUCCESS_CIRCLE : string = "success+Circle"; |
| 12 | export let STOPED : string = "stoped"; |
| 13 | |
| 14 | |
| 15 | @Injectable() |
| 16 | export class InstantiationStatusComponentService { |
| 17 | generateServiceInfoDataMapping(arr: Array<ServiceInfoModel>) : { [serviceInstanceId: string]: Array<ServiceInfoModel>}{ |
| 18 | let serviceInfoData: { [serviceInstanceId: string]: Array<ServiceInfoModel>; } = {}; |
| 19 | for(let item of arr){ |
| 20 | if(isNullOrUndefined(serviceInfoData[item.templateId])){ |
| 21 | serviceInfoData[item.templateId] = [item]; |
| 22 | }else { |
| 23 | serviceInfoData[item.templateId].push(item); |
| 24 | } |
| 25 | } |
| 26 | return serviceInfoData; |
| 27 | } |
| 28 | |
| 29 | convertObjectToArray(arr: Array<ServiceInfoModel>) : Observable<Array<ServiceInfoUiModel>>{ |
| 30 | const obj = this.generateServiceInfoDataMapping(arr); |
| 31 | let index:number = 0; |
| 32 | let result = []; |
| 33 | for(let item in obj) { |
| 34 | obj[item].map(item => { |
| 35 | item['serviceStatus'] = this.getStatus(item.jobStatus); |
| 36 | item['serviceIndex'] = index; |
| 37 | }); |
| 38 | index++; |
| 39 | result = result.concat(obj[item]); |
| 40 | } |
| 41 | |
| 42 | console.log(result); |
| 43 | return Observable.of(result); |
| 44 | } |
| 45 | |
| 46 | getStatus(status : string) : ServiceStatus { |
| 47 | switch(status.toUpperCase()) { |
| 48 | case 'PENDING' : |
| 49 | return new ServiceStatus(PENDING, '#009FDB', 'Pending: The service will automatically be sent for instantiation as soon as possible.'); |
| 50 | case 'IN_PROGRESS' : |
| 51 | return new ServiceStatus(INPROGRESS, '#009FDB', 'In-progress: the service is in process of instantiation.'); |
| 52 | case 'PAUSED' : |
| 53 | return new ServiceStatus(PAUSE, '#009FDB', 'Paused: Service has paused and waiting for your action.\n Select actions from the menu to the right.'); |
| 54 | case 'FAILED' : |
| 55 | return new ServiceStatus(X_O, '#D02B2B', 'Failed: Service instantiation has failed, load the service to see the error returned.'); |
| 56 | case 'COMPLETED' : |
| 57 | return new ServiceStatus(SUCCESS_CIRCLE, '#53AD15', 'Completed successfully: Service is successfully instantiated.'); |
| 58 | case 'STOPPED' : |
| 59 | return new ServiceStatus(STOPED, '#D02B2B', 'Stopped: Due to previous failure, will not be instantiated.'); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | export class ServiceStatus { |
| 66 | iconClassName : string; |
| 67 | color : string; |
| 68 | tooltip : string; |
| 69 | |
| 70 | constructor(_iconClassName : string, _color : string, _tooltip : string){ |
| 71 | this.iconClassName = _iconClassName; |
| 72 | this.color = _color; |
| 73 | this.tooltip = _tooltip; |
| 74 | } |
| 75 | } |