1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """General purpose utilities.
17 import multiprocessing
23 from os import walk, makedirs, environ
24 from os.path import join, isdir
25 from shutil import move, Error
28 from errors import PresentationError
29 from jumpavg.BitCountingClassifier import BitCountingClassifier
33 """Calculate mean value from the items.
35 :param items: Mean value is calculated from these items.
41 return float(sum(items)) / len(items)
45 """Calculate stdev from the items.
47 :param items: Stdev is calculated from these items.
54 variance = [(x - avg) ** 2 for x in items]
55 stddev = sqrt(mean(variance))
59 def relative_change(nr1, nr2):
60 """Compute relative change of two values.
62 :param nr1: The first number.
63 :param nr2: The second number.
66 :returns: Relative change of nr1.
70 return float(((nr2 - nr1) / nr1) * 100)
73 def get_files(path, extension=None, full_path=True):
74 """Generates the list of files to process.
76 :param path: Path to files.
77 :param extension: Extension of files to process. If it is the empty string,
78 all files will be processed.
79 :param full_path: If True, the files with full path are generated.
83 :returns: List of files to process.
88 for root, _, files in walk(path):
89 for filename in files:
91 if filename.endswith(extension):
93 file_list.append(join(root, filename))
95 file_list.append(filename)
97 file_list.append(join(root, filename))
102 def get_rst_title_char(level):
103 """Return character used for the given title level in rst files.
105 :param level: Level of the title.
107 :returns: Character used for the given title level in rst files.
110 chars = ('=', '-', '`', "'", '.', '~', '*', '+', '^')
111 if level < len(chars):
117 def execute_command(cmd):
118 """Execute the command in a subprocess and log the stdout and stderr.
120 :param cmd: Command to execute.
122 :returns: Return code of the executed command.
127 proc = subprocess.Popen(
129 stdout=subprocess.PIPE,
130 stderr=subprocess.PIPE,
134 stdout, stderr = proc.communicate()
141 if proc.returncode != 0:
142 logging.error(" Command execution failed.")
143 return proc.returncode, stdout, stderr
146 def get_last_successful_build_number(jenkins_url, job_name):
147 """Get the number of the last successful build of the given job.
149 :param jenkins_url: Jenkins URL.
150 :param job_name: Job name.
151 :type jenkins_url: str
153 :returns: The build number as a string.
157 url = "{}/{}/lastSuccessfulBuild/buildNumber".format(jenkins_url, job_name)
158 cmd = "wget -qO- {url}".format(url=url)
160 return execute_command(cmd)
163 def get_last_completed_build_number(jenkins_url, job_name):
164 """Get the number of the last completed build of the given job.
166 :param jenkins_url: Jenkins URL.
167 :param job_name: Job name.
168 :type jenkins_url: str
170 :returns: The build number as a string.
174 url = "{}/{}/lastCompletedBuild/buildNumber".format(jenkins_url, job_name)
175 cmd = "wget -qO- {url}".format(url=url)
177 return execute_command(cmd)
180 def archive_input_data(spec):
181 """Archive the report.
183 :param spec: Specification read from the specification file.
184 :type spec: Specification
185 :raises PresentationError: If it is not possible to archive the input data.
188 logging.info(" Archiving the input data files ...")
190 extension = spec.input["file-format"]
191 data_files = get_files(spec.environment["paths"]["DIR[WORKING,DATA]"],
193 dst = spec.environment["paths"]["DIR[STATIC,ARCH]"]
194 logging.info(" Destination: {0}".format(dst))
200 for data_file in data_files:
201 logging.info(" Moving the file: {0} ...".format(data_file))
204 except (Error, OSError) as err:
205 raise PresentationError("Not possible to archive the input data.",
208 logging.info(" Done.")
211 def classify_anomalies(data):
212 """Process the data and return anomalies and trending values.
214 Gathers data into groups with common trend value.
215 Decorates first value in the group to be an outlier, regression,
216 normal or progression.
218 :param data: Full data set with unavailable samples replaced by nan.
219 :type data: pandas.Series
220 :returns: Classification and trend values
221 :rtype: 2-tuple, list of strings and list of floats
223 bare_data = [sample for _, sample in data.iteritems()
224 if not np.isnan(sample)]
225 # TODO: Put analogous iterator into jumpavg library.
226 groups = BitCountingClassifier.classify(bare_data)
227 groups.reverse() # Just to use .pop() for FIFO.
233 for _, sample in data.iteritems():
235 classification.append("outlier")
238 if values_left < 1 or active_group is None:
240 while values_left < 1: # To ignore empty groups.
241 active_group = groups.pop()
242 values_left = len(active_group.values)
243 avg = active_group.metadata.avg
244 classification.append(active_group.metadata.classification)
248 classification.append("normal")
251 return classification, avgs
254 class Worker(multiprocessing.Process):
255 """Worker class used to process tasks in separate parallel processes.
258 def __init__(self, work_queue, data_queue, func):
261 :param work_queue: Queue with items to process.
262 :param data_queue: Shared memory between processes. Queue which keeps
263 the result data. This data is then read by the main process and used
264 in further processing.
265 :param func: Function which is executed by the worker.
266 :type work_queue: multiprocessing.JoinableQueue
267 :type data_queue: multiprocessing.Manager().Queue()
268 :type func: Callable object
270 super(Worker, self).__init__()
271 self._work_queue = work_queue
272 self._data_queue = data_queue
276 """Method representing the process's activity.
281 self.process(self._work_queue.get())
283 self._work_queue.task_done()
285 def process(self, item_to_process):
286 """Method executed by the runner.
288 :param item_to_process: Data to be processed by the function.
289 :type item_to_process: tuple
291 self._func(self.pid, self._data_queue, *item_to_process)