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
25 from os import walk, makedirs, environ
26 from os.path import join, isdir
27 from shutil import move, Error
28 from datetime import datetime
29 from pandas import Series
31 from errors import PresentationError
32 from jumpavg.BitCountingClassifier import BitCountingClassifier
36 """Calculate mean value from the items.
38 :param items: Mean value is calculated from these items.
44 return float(sum(items)) / len(items)
48 """Calculate stdev from the items.
50 :param items: Stdev is calculated from these items.
55 return Series.std(Series(items))
58 def relative_change(nr1, nr2):
59 """Compute relative change of two values.
61 :param nr1: The first number.
62 :param nr2: The second number.
65 :returns: Relative change of nr1.
69 return float(((nr2 - nr1) / nr1) * 100)
72 def relative_change_stdev(mean1, mean2, std1, std2):
73 """Compute relative standard deviation of change of two values.
75 The "1" values are the base for comparison.
76 Results are returned as percentage (and percentual points for stdev).
77 Linearized theory is used, so results are wrong for relatively large stdev.
79 :param mean1: Mean of the first number.
80 :param mean2: Mean of the second number.
81 :param std1: Standard deviation estimate of the first number.
82 :param std2: Standard deviation estimate of the second number.
87 :returns: Relative change and its stdev.
90 mean1, mean2 = float(mean1), float(mean2)
91 quotient = mean2 / mean1
94 std = quotient * math.sqrt(first * first + second * second)
95 return (quotient - 1) * 100, std * 100
98 def get_files(path, extension=None, full_path=True):
99 """Generates the list of files to process.
101 :param path: Path to files.
102 :param extension: Extension of files to process. If it is the empty string,
103 all files will be processed.
104 :param full_path: If True, the files with full path are generated.
107 :type full_path: bool
108 :returns: List of files to process.
113 for root, _, files in walk(path):
114 for filename in files:
116 if filename.endswith(extension):
118 file_list.append(join(root, filename))
120 file_list.append(filename)
122 file_list.append(join(root, filename))
127 def get_rst_title_char(level):
128 """Return character used for the given title level in rst files.
130 :param level: Level of the title.
132 :returns: Character used for the given title level in rst files.
135 chars = ('=', '-', '`', "'", '.', '~', '*', '+', '^')
136 if level < len(chars):
142 def execute_command(cmd):
143 """Execute the command in a subprocess and log the stdout and stderr.
145 :param cmd: Command to execute.
147 :returns: Return code of the executed command, stdout and stderr.
148 :rtype: tuple(int, str, str)
152 proc = subprocess.Popen(
154 stdout=subprocess.PIPE,
155 stderr=subprocess.PIPE,
159 stdout, stderr = proc.communicate()
166 if proc.returncode != 0:
167 logging.error(" Command execution failed.")
168 return proc.returncode, stdout, stderr
171 def get_last_successful_build_number(jenkins_url, job_name):
172 """Get the number of the last successful build of the given job.
174 :param jenkins_url: Jenkins URL.
175 :param job_name: Job name.
176 :type jenkins_url: str
178 :returns: The build number as a string.
182 url = "{}/{}/lastSuccessfulBuild/buildNumber".format(jenkins_url, job_name)
183 cmd = "wget -qO- {url}".format(url=url)
185 return execute_command(cmd)
188 def get_last_completed_build_number(jenkins_url, job_name):
189 """Get the number of the last completed build of the given job.
191 :param jenkins_url: Jenkins URL.
192 :param job_name: Job name.
193 :type jenkins_url: str
195 :returns: The build number as a string.
199 url = "{}/{}/lastCompletedBuild/buildNumber".format(jenkins_url, job_name)
200 cmd = "wget -qO- {url}".format(url=url)
202 return execute_command(cmd)
205 def get_build_timestamp(jenkins_url, job_name, build_nr):
206 """Get the timestamp of the build of the given job.
208 :param jenkins_url: Jenkins URL.
209 :param job_name: Job name.
210 :param build_nr: Build number.
211 :type jenkins_url: str
214 :returns: The timestamp.
215 :rtype: datetime.datetime
218 url = "{jenkins_url}/{job_name}/{build_nr}".format(jenkins_url=jenkins_url,
221 cmd = "wget -qO- {url}".format(url=url)
223 timestamp = execute_command(cmd)
225 return datetime.fromtimestamp(timestamp/1000)
228 def archive_input_data(spec):
229 """Archive the report.
231 :param spec: Specification read from the specification file.
232 :type spec: Specification
233 :raises PresentationError: If it is not possible to archive the input data.
236 logging.info(" Archiving the input data files ...")
238 extension = spec.input["arch-file-format"]
240 for ext in extension:
241 data_files.extend(get_files(
242 spec.environment["paths"]["DIR[WORKING,DATA]"], extension=ext))
243 dst = spec.environment["paths"]["DIR[STATIC,ARCH]"]
244 logging.info(" Destination: {0}".format(dst))
250 for data_file in data_files:
251 logging.info(" Moving the file: {0} ...".format(data_file))
254 except (Error, OSError) as err:
255 raise PresentationError("Not possible to archive the input data.",
258 logging.info(" Done.")
261 def classify_anomalies(data):
262 """Process the data and return anomalies and trending values.
264 Gather data into groups with average as trend value.
265 Decorate values within groups to be normal,
266 the first value of changed average as a regression, or a progression.
268 :param data: Full data set with unavailable samples replaced by nan.
269 :type data: OrderedDict
270 :returns: Classification and trend values
271 :rtype: 2-tuple, list of strings and list of floats
273 # Nan mean something went wrong.
274 # Use 0.0 to cause that being reported as a severe regression.
275 bare_data = [0.0 if np.isnan(sample.avg) else sample
276 for _, sample in data.iteritems()]
277 # TODO: Put analogous iterator into jumpavg library.
278 groups = BitCountingClassifier().classify(bare_data)
279 groups.reverse() # Just to use .pop() for FIFO.
285 for _, sample in data.iteritems():
286 if np.isnan(sample.avg):
287 classification.append("outlier")
288 avgs.append(sample.avg)
290 if values_left < 1 or active_group is None:
292 while values_left < 1: # Ignore empty groups (should not happen).
293 active_group = groups.pop()
294 values_left = len(active_group.values)
295 avg = active_group.metadata.avg
296 classification.append(active_group.metadata.classification)
300 classification.append("normal")
303 return classification, avgs
306 def convert_csv_to_pretty_txt(csv_file, txt_file):
307 """Convert the given csv table to pretty text table.
309 :param csv_file: The path to the input csv file.
310 :param txt_file: The path to the output pretty text file.
316 with open(csv_file, 'rb') as csv_file:
317 csv_content = csv.reader(csv_file, delimiter=',', quotechar='"')
318 for row in csv_content:
319 if txt_table is None:
320 txt_table = prettytable.PrettyTable(row)
322 txt_table.add_row(row)
323 txt_table.align["Test case"] = "l"
325 with open(txt_file, "w") as txt_file:
326 txt_file.write(str(txt_table))
329 class Worker(multiprocessing.Process):
330 """Worker class used to process tasks in separate parallel processes.
333 def __init__(self, work_queue, data_queue, func):
336 :param work_queue: Queue with items to process.
337 :param data_queue: Shared memory between processes. Queue which keeps
338 the result data. This data is then read by the main process and used
339 in further processing.
340 :param func: Function which is executed by the worker.
341 :type work_queue: multiprocessing.JoinableQueue
342 :type data_queue: multiprocessing.Manager().Queue()
343 :type func: Callable object
345 super(Worker, self).__init__()
346 self._work_queue = work_queue
347 self._data_queue = data_queue
351 """Method representing the process's activity.
356 self.process(self._work_queue.get())
358 self._work_queue.task_done()
360 def process(self, item_to_process):
361 """Method executed by the runner.
363 :param item_to_process: Data to be processed by the function.
364 :type item_to_process: tuple
366 self._func(self.pid, self._data_queue, *item_to_process)