-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjenkins_statistics.py
128 lines (103 loc) · 3.8 KB
/
jenkins_statistics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# coding=utf-8
"""
Jenkins statistics related functions
"""
from itertools import groupby
def get_summary_jobs_by_month(jobs_info):
"""
Returns jobs per month summary report
:param jobs_info: Jobs info dictionary, ``dict``
:return: list with jobs per month summary, ``list``
"""
meses = get_available_months(jobs_info)
retorno = [(mes, len(get_jobs_by_month_year(mes, jobs_info)))
for mes in sorted(meses)]
return retorno
def get_summary_builds_by_month(jobs_info):
"""
Return builds per month summary report
:param jobs_info: Jobs info dictionary, ``dict``
:return: list with builds per month summary, ``list``
"""
meses = get_available_months(jobs_info)
retorno = [(mes, get_builds_per_result(mes, jobs_info))
for mes in sorted(meses)]
return retorno
def format_month_year(job_info):
"""
Returns month/year formatted like MM/YYYY.
e.g.: 09/2015
:param job_info: Job info, ``dict``
:return: Formatted month/year (e.g. 09/2015), ``str``
"""
return get_formatted_month_year(job_info['build_date_month'],
job_info['build_date_year'])
def get_formatted_month_year(month, year):
"""
Returns month/year formatted like MM/YYYY.
e.g.: 09/2015
:param year: Year
:param month: Month
:return: Formatted month/year (e.g. 09/2015), ``str``
"""
return "{0:=02}/{1}".format(month,
year)
def get_available_months(jobs_info):
"""
Return list with tuple with months available.
e.g:
('09/2015', month, year)
:param jobs_info: Jobs info, ``list``
:return: list with months, ``list``
"""
meses = [(format_month_year(item),
item['build_date_month'],
item['build_date_year'])
for item in jobs_info]
return list(set(meses))
def get_jobs_by_month_year(mes_ano, jobs_info):
"""
Returns jobs for the informed month/year
:param mes_ano: Formatted month/year (e.g: '09/2015'), ``str``
:param jobs_info: Jobs info, ``dict``
:return: list with jobs, ``list``
"""
return list(set([job_info['job_name']
for job_info in jobs_info
if format_month_year(job_info) == mes_ano[0]]))
def get_builds_per_result(mes_ano, jobs_info):
"""
Returns builds per result for the month/year
:param mes_ano: Month/year formatted (e.g.: '09/2015'), ``str``
:param jobs_info: Jobs info, ``dict``
:return: Builds per result summary, ``list``
"""
jobs_info_mes_ano = [job_info for job_info in jobs_info
if format_month_year(job_info) == mes_ano[0]]
results = list(set([job_info['build_result']
for job_info in jobs_info_mes_ano]))
builds_por_resultado = []
for result in results:
builds_por_resultado.append((
result,
sum([1 for item in jobs_info_mes_ano
if item['build_result'] == result])))
return builds_por_resultado
def top_jobs_by_builds(jobs_details, year, month):
"""
Return top jobs by the number of builds in the informed month/year
:param jobs_details: Jobs
:param year: Year
:param month: Month
:return: [(builds, 'job_name'),(builds, 'job_name')]
"""
formatted_month_year = get_formatted_month_year(month, year)
builds_por_mes = [job_info['job_name']
for job_info in jobs_details
if format_month_year(job_info) == formatted_month_year]
groupby_jobs = []
data = sorted(builds_por_mes)
for key, group in groupby(data):
groupby_jobs.append((len([item for item in group]), key))
return [item for counter, item in enumerate(reversed(sorted(groupby_jobs)))
if counter < 5 and item[0] > 1]