~allanlesage/+junk/qakit-vis

« back to all changes in this revision

Viewing changes to qakit/generate_silo_team_report.py

  • Committer: Allan LeSage
  • Date: 2015-05-22 17:48:57 UTC
  • Revision ID: allan.lesage@canonical.com-20150522174857-oxnv16b3ordgx7ez
Add KPI-relevant scripts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
# UEQA KPIs
 
3
# Copyright (C) 2015 Canonical
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation, either version 3 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
import argparse
 
19
import sys
 
20
 
 
21
import pymongo
 
22
 
 
23
from util import logger
 
24
 
 
25
 
 
26
def get_team_total_hours_by_week(team_name, week_number):
 
27
    conn = pymongo.MongoClient()
 
28
    db = conn.kpi
 
29
    query = [
 
30
        {'$match': {
 
31
            'testing_time': {'$gt': 0},
 
32
            'week': week_number,
 
33
            'teams': team_name}},
 
34
        {'$group': {'_id': None, 'total': {'$sum': '$testing_time'}}}
 
35
    ]
 
36
    total_time = db.kpi_cards.aggregate(query)
 
37
    try:
 
38
        return float(total_time['result'][0]['total'])/60.0/60.0
 
39
    except IndexError:
 
40
        return 0.0
 
41
 
 
42
 
 
43
def get_team_number_of_cards_by_week(team_name, week_number):
 
44
    conn = pymongo.MongoClient()
 
45
    db = conn.kpi
 
46
    number_of_cards = db.kpi_cards.aggregate([
 
47
        {'$match': {
 
48
            'testing_time': {'$gt': 0},
 
49
            'week': week_number,
 
50
            'teams': team_name}},
 
51
        {'$group': {'_id': None, 'total': {'$sum': 1}}}])
 
52
    try:
 
53
        return number_of_cards['result'][0]['total']
 
54
    except IndexError:
 
55
        return 0
 
56
 
 
57
 
 
58
def get_team_number_of_passed_cards_by_week(team_name, week_number):
 
59
    conn = pymongo.MongoClient()
 
60
    db = conn.kpi
 
61
    number_of_passed_cards = db.kpi_cards.aggregate([
 
62
        {'$match': {
 
63
            'testing_time': {'$gt': 0},
 
64
            'week': week_number,
 
65
            'teams': team_name,
 
66
            'result': 'Passed'}},
 
67
        {'$group': {'_id': None, 'total': {'$sum': 1}}}])
 
68
    try:
 
69
        return int(number_of_passed_cards['result'][0]['total'])
 
70
    except IndexError:
 
71
        return 0
 
72
 
 
73
 
 
74
def get_team_number_of_failed_cards_by_week(team_name, week_number):
 
75
    conn = pymongo.MongoClient()
 
76
    db = conn.kpi
 
77
    failed_cards = db.kpi_cards.aggregate([
 
78
        {'$match': {
 
79
            'testing_time': {'$gt': 0},
 
80
            'week': week_number,
 
81
            'teams': team_name,
 
82
            'result': 'Failed'}},
 
83
        {'$group': {'_id': None, 'total': {'$sum': 1}}}])
 
84
    try:
 
85
        return int(failed_cards['result'][0]['total'])
 
86
    except IndexError:
 
87
        return 0
 
88
 
 
89
 
 
90
def get_team_failed_hours_by_week(team_name, week_number):
 
91
    conn = pymongo.MongoClient()
 
92
    db = conn.kpi
 
93
    failed_time = db.kpi_cards.aggregate([
 
94
        {'$match': {
 
95
            'testing_time': {'$gt': 0},
 
96
            'week': week_number,
 
97
            'teams': team_name,
 
98
            'result': 'Failed'}},
 
99
        {'$group': {'_id': None, 'total': {'$sum': '$testing_time'}}}])
 
100
    try:
 
101
        return float(failed_time['result'][0]['total'])/60.0/60.0
 
102
    except IndexError:
 
103
        return 0.0
 
104
 
 
105
 
 
106
def get_team_totals_by_week(team_name, week_number):
 
107
    if team_name is None:
 
108
        team_name = []
 
109
    hours = get_team_total_hours_by_week(team_name, week_number)
 
110
    cards = get_team_number_of_cards_by_week(team_name, week_number)
 
111
    try:
 
112
        average = hours/float(cards)
 
113
    except ZeroDivisionError:
 
114
        average = 0.0
 
115
    return {
 
116
        'hours': hours,
 
117
        'cards': cards,
 
118
        'passed_cards': get_team_number_of_passed_cards_by_week(
 
119
            team_name, week_number),
 
120
        'failed_cards': get_team_number_of_failed_cards_by_week(
 
121
            team_name, week_number),
 
122
        'average': average,
 
123
        'failed_hours': get_team_failed_hours_by_week(
 
124
            team_name, week_number),
 
125
    }
 
126
 
 
127
 
 
128
def print_out_totals(totals):
 
129
    template_list = ['{team_name}',
 
130
                     '{cards}',
 
131
                     '{failed_cards}',
 
132
                     '{passed_cards}',
 
133
                     '{average}',
 
134
                     '{hours}',
 
135
                     '{failed_hours}']
 
136
    totals_template = '\t'.join(template_list)
 
137
    logger.info(totals_template.format(**totals))
 
138
 
 
139
 
 
140
def get_team_totals(week_number):
 
141
    conn = pymongo.MongoClient()
 
142
    db = conn.kpi
 
143
    teams = db.kpi_cards.find({'week': week_number}).distinct('teams')
 
144
    logger.debug(teams)
 
145
    for team_name in teams:
 
146
        totals = get_team_totals_by_week(team_name, week_number)
 
147
        totals['team_name'] = team_name
 
148
        print_out_totals(totals)
 
149
    other_totals = get_team_totals_by_week(None, week_number)
 
150
    other_totals['team_name'] = 'other'
 
151
    print_out_totals(other_totals)
 
152
 
 
153
 
 
154
def _parse_arguments():
 
155
    parser = argparse.ArgumentParser('Compute team totals for a given week.')
 
156
    parser.add_argument('week',
 
157
                        help='week to total',
 
158
                        type=int)
 
159
    return parser.parse_args()
 
160
 
 
161
 
 
162
def main():
 
163
    args = _parse_arguments()
 
164
    return get_team_totals(args.week)
 
165
 
 
166
 
 
167
if __name__ == '__main__':
 
168
    sys.exit(main())