Skip to content

Commit de1c012

Browse files
sdatkodanpawlik
authored andcommitted
[PCP] Add annotations to plots
This commit introduces a feature that explores the Ansible log files from within the ci-framework-data directory and annotates the plots generated from PCP metrics with details, such as when some EDPM stages were executed and at what time there were testing frameworks launched.
1 parent 6ad7ddf commit de1c012

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

hooks/playbooks/pcp-metrics-post.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
- name: Process metrics
2020
hosts: localhost
2121
tasks:
22+
- name: Gather annotations
23+
ansible.builtin.include_role:
24+
name: pcp_metrics
25+
tasks_from: annotations
26+
2227
- name: Generate figures
2328
ansible.builtin.include_role:
2429
name: pcp_metrics

roles/pcp_metrics/files/plot.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import csv
4+
from datetime import datetime
45
from glob import glob
56
from os import getenv
67
import os.path
@@ -19,6 +20,7 @@
1920
#
2021
# Parameters
2122
#
23+
ANNOTATIONS_FILE = getenv('ANNOTATIONS_FILE', 'metrics/annotations.txt')
2224
METRICS_SRC = (sys.argv[1:] if len(sys.argv) > 1
2325
else [getenv('METRICS_SRC', 'metrics/*.csv')])
2426
OUTPUT_DIR = getenv('OUTPUT_DIR', 'metrics/')
@@ -376,6 +378,41 @@ def subplot(ax: plt.Axes,
376378
yscale=yscale)
377379

378380

381+
def annotate(axs: Iterable[plt.Axes]) -> None:
382+
'''Draws vertical annotation lines on the interesting time marks.
383+
384+
Parameters
385+
----------
386+
axs : Iterable[plt.Axes]
387+
A collection of subplot objects in which the data were plotted.
388+
'''
389+
if not os.path.isfile(ANNOTATIONS_FILE):
390+
print('WARNING No annotations dafa found in file:', ANNOTATIONS_FILE)
391+
return
392+
393+
with open(ANNOTATIONS_FILE) as file:
394+
data = file.read().strip().split('\n')
395+
396+
for annotation in data:
397+
time, details = annotation.split(' | ', maxsplit=1)
398+
time = datetime.strptime(time, '%Y-%m-%d %H:%M:%S,%f')
399+
400+
if details.startswith('PLAY'):
401+
color = 'darkred'
402+
403+
elif details.startswith('TASK [kustomize_deploy'):
404+
color = 'navy'
405+
406+
elif details.startswith('TASK [test_operator'):
407+
color = 'darkgreen'
408+
409+
else: # generic
410+
color = 'grey'
411+
412+
for ax in axs:
413+
ax.axvline(time, color=color, ls='--', alpha=0.5)
414+
415+
379416
def plot(df: pd.DataFrame,
380417
output: str,
381418
title: Union[str, None] = None,
@@ -416,6 +453,8 @@ def plot(df: pd.DataFrame,
416453
if title:
417454
fig.suptitle(title, fontsize=16)
418455

456+
annotate(axs)
457+
419458
# NOTE: the argument of Pandas query() & eval() is expected to be a valid
420459
# Python expression, which does not allow any special characters
421460
# (e.g. dots); the backticks can be used to specify column names
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
- name: Extract annotations
3+
ansible.builtin.shell: >-
4+
grep
5+
--no-filename
6+
--regexp 'PLAY \['
7+
--regexp 'TASK \[kustomize_deploy : Apply generated content for'
8+
--regexp 'TASK \[test_operator : Run'
9+
$( find "{{ ansible_user_dir }}" -iname 'ansible*.log' 2>/dev/null )
10+
| sed
11+
--regexp-extended
12+
--expression 's#p=[0-9]+ u=[^ ]+ n=[^ ]+ |##g'
13+
--expression 's# (_raw_params|msg|chdir)=.*#]#g'
14+
--expression 's#[ *]*$##g'
15+
| sort
16+
--numeric-sort
17+
--key=1,2
18+
| uniq
19+
ignore_errors: true
20+
changed_when: false
21+
register: _annotations_shell
22+
23+
- name: Ensure the output directory exist
24+
file:
25+
path: "{{ pcp_metrics_output_dir }}"
26+
state: directory
27+
mode: '0755'
28+
29+
- name: Save annotations
30+
copy:
31+
content: "{{ _annotations_shell.stdout }}"
32+
dest: "{{ pcp_metrics_output_dir }}/annotations.txt"

roles/pcp_metrics/tasks/plot.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
ansible.builtin.shell: >-
2222
. "{{ pcp_metrics_venv_dir }}/bin/activate"
2323
&& export OUTPUT_DIR="{{ pcp_metrics_output_dir }}"
24+
&& export ANNOTATIONS_FILE="{{ pcp_metrics_output_dir }}/annotations.txt"
2425
&& python3 "{{ pcp_metrics_venv_dir }}/plot.py" "{{ pcp_metrics_output_dir }}/*.csv"
2526
register: _pcp_shell
2627
failed_when:

0 commit comments

Comments
 (0)