fix rte_crypto_ipsec_mb dependency
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / dpi / progress.py
1 #!/bin/env python
2
3 ##
4 ## Copyright (c) 2010-2017 Intel Corporation
5 ##
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
9 ##
10 ##     http://www.apache.org/licenses/LICENSE-2.0
11 ##
12 ## Unless required by applicable law or agreed to in writing, software
13 ## distributed under the License is distributed on an "AS IS" BASIS,
14 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ## See the License for the specific language governing permissions and
16 ## limitations under the License.
17 ##
18
19 from decimal import *
20 from time import time
21
22 class Progress:
23     def __init__(self, limit, fieldNames = [], overallETA = True):
24         self._fieldNames = fieldNames;
25         self._limit = limit;
26         self._progress = 0;
27         self._prevProgress = 0;
28         self._prevTime = 0;
29         self._progressSetCount = 0;
30         self._time = 0;
31         self._overallETA = overallETA;
32
33     def setProgress(self, progress, fieldValues = []):
34         self._fieldValues = fieldValues;
35         if (self._overallETA == True):
36             self._progress = progress
37             self._time = time();
38             if (self._progressSetCount == 0):
39                 self._prevProgress = self._progress;
40                 self._prevTime = self._time;
41         else:
42             self._prevProgress = self._progress;
43             self._prevTime = self._time;
44             self._progress = progress;
45             self._time = time();
46         self._progressSetCount += 1
47
48     def incrProgress(self):
49         self.setProgress(self._progress + 1);
50
51     def toString(self):
52         ret = ""
53         ret += str(self._getETA()) + " seconds left"
54         for f,v in zip(self._fieldNames, self._fieldValues):
55             ret += ", %s=%s" % (str(f),str(v))
56         return ret;
57
58     def _getETA(self):
59         if (self._progressSetCount < 2):
60             return "N/A"
61         diff = self._progress - self._prevProgress;
62         t_diff = Decimal(self._time - self._prevTime);
63         if (t_diff < 0.001 or diff <= 0):
64             return "N/A"
65         rate = Decimal(diff)/t_diff
66         remaining = Decimal(self._limit - self._progress);
67         return round(remaining/rate, 2);