3
# Copyright (C) 2015 Canonical
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.
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.
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/>.
23
from util import logger
26
def get_team_total_hours_by_week(team_name, week_number):
27
conn = pymongo.MongoClient()
31
'testing_time': {'$gt': 0},
34
{'$group': {'_id': None, 'total': {'$sum': '$testing_time'}}}
36
total_time = db.kpi_cards.aggregate(query)
38
return float(total_time['result'][0]['total'])/60.0/60.0
43
def get_team_number_of_cards_by_week(team_name, week_number):
44
conn = pymongo.MongoClient()
46
number_of_cards = db.kpi_cards.aggregate([
48
'testing_time': {'$gt': 0},
51
{'$group': {'_id': None, 'total': {'$sum': 1}}}])
53
return number_of_cards['result'][0]['total']
58
def get_team_number_of_passed_cards_by_week(team_name, week_number):
59
conn = pymongo.MongoClient()
61
number_of_passed_cards = db.kpi_cards.aggregate([
63
'testing_time': {'$gt': 0},
67
{'$group': {'_id': None, 'total': {'$sum': 1}}}])
69
return int(number_of_passed_cards['result'][0]['total'])
74
def get_team_number_of_failed_cards_by_week(team_name, week_number):
75
conn = pymongo.MongoClient()
77
failed_cards = db.kpi_cards.aggregate([
79
'testing_time': {'$gt': 0},
83
{'$group': {'_id': None, 'total': {'$sum': 1}}}])
85
return int(failed_cards['result'][0]['total'])
90
def get_team_failed_hours_by_week(team_name, week_number):
91
conn = pymongo.MongoClient()
93
failed_time = db.kpi_cards.aggregate([
95
'testing_time': {'$gt': 0},
99
{'$group': {'_id': None, 'total': {'$sum': '$testing_time'}}}])
101
return float(failed_time['result'][0]['total'])/60.0/60.0
106
def get_team_totals_by_week(team_name, week_number):
107
if team_name is None:
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)
112
average = hours/float(cards)
113
except ZeroDivisionError:
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),
123
'failed_hours': get_team_failed_hours_by_week(
124
team_name, week_number),
128
def print_out_totals(totals):
129
template_list = ['{team_name}',
136
totals_template = '\t'.join(template_list)
137
logger.info(totals_template.format(**totals))
140
def get_team_totals(week_number):
141
conn = pymongo.MongoClient()
143
teams = db.kpi_cards.find({'week': week_number}).distinct('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)
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',
159
return parser.parse_args()
163
args = _parse_arguments()
164
return get_team_totals(args.week)
167
if __name__ == '__main__':