Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * SDC |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 6 | * ================================================================================ |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 21 | /** |
| 22 | * Created by obarda on 7/7/2016. |
| 23 | */ |
| 24 | |
| 25 | 'use strict'; |
| 26 | import IIntervalService = angular.IIntervalService; |
| 27 | |
| 28 | export class ProgressService { |
| 29 | |
| 30 | public progresses:any = {}; |
| 31 | |
| 32 | static '$inject' = ['$interval']; |
| 33 | |
| 34 | constructor(protected $interval:any) { |
| 35 | } |
| 36 | |
| 37 | private totalProgress:number = 90; |
| 38 | private startProgress:number = 10; |
| 39 | private onePercentIntervalSeconds:number = 5; |
| 40 | private createComponentInterval; |
| 41 | |
| 42 | public setProgressValue(name:string, value:number):void { |
| 43 | if (!this.progresses[name]) { |
| 44 | this.progresses[name] = {}; |
| 45 | } |
| 46 | this.progresses[name].value = value; |
| 47 | } |
| 48 | |
| 49 | public getProgressValue(name:string):number { |
| 50 | if (this.progresses[name]) { |
| 51 | return this.progresses[name].value; |
| 52 | } |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | public deleteProgressValue(name:string):void { |
| 57 | this.stopCreateComponentInterval(); |
| 58 | delete this.progresses[name]; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | private stopCreateComponentInterval = ():void => { |
| 63 | this.$interval.cancel(this.createComponentInterval); |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | public initCreateComponentProgress = (componentId:string):void => { |
| 68 | let progressValue:number = this.startProgress; |
| 69 | if (!this.getProgressValue(componentId)) { |
| 70 | this.stopCreateComponentInterval(); |
| 71 | this.setProgressValue(componentId, this.startProgress); |
| 72 | this.createComponentInterval = this.$interval(():void => { |
| 73 | //TODO replace getProgressMockData to real data after BE provide the API |
| 74 | let progressValue = this.getProgressMockData(componentId); |
| 75 | if (progressValue <= this.totalProgress) { |
| 76 | this.setProgressValue(componentId, progressValue); |
| 77 | } else { |
| 78 | /** |
| 79 | * Currently the progress is not really checking against the BE. |
| 80 | * So the progress can pass 100. So the workaround for now, in case we pass 90 (totalProgress) |
| 81 | * stop the interval, so the progress will be kept at 90 until the promise will return value and set |
| 82 | * the progress to 100. |
| 83 | */ |
| 84 | this.deleteProgressValue(componentId); |
| 85 | } |
| 86 | }, this.onePercentIntervalSeconds * 1000); |
| 87 | } |
| 88 | |
| 89 | }; |
| 90 | |
| 91 | |
| 92 | private getProgressMockData = (id:string):number => { |
| 93 | let progressValue = this.getProgressValue(id); |
| 94 | if (progressValue > 0) { |
| 95 | progressValue = progressValue + 1; |
| 96 | } |
| 97 | //if not finish always stay on 90% |
| 98 | if (progressValue > 90) { |
| 99 | progressValue = 90; |
| 100 | } |
| 101 | |
| 102 | return progressValue; |
| 103 | } |
| 104 | |
| 105 | } |