~allanlesage/+junk/qakit-vis

« back to all changes in this revision

Viewing changes to qakit/generate_practitest_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 datetime
 
20
import sys
 
21
 
 
22
import pymongo
 
23
 
 
24
import config
 
25
import practitest
 
26
from util import logger
 
27
 
 
28
 
 
29
def strptime_instance_last_run(instance):
 
30
    """PractiTest times look like '13-Apr-2015  22:02', return a datetime."""
 
31
    try:
 
32
        return datetime.datetime.strptime(
 
33
            instance['last_run']['value'], '%d-%b-%Y  %H:%M')
 
34
    except ValueError:
 
35
        # not all instances have last_runs
 
36
        return None
 
37
 
 
38
 
 
39
def get_first_instance(instances):
 
40
    instances = list(map(strptime_instance_last_run, instances))
 
41
    instances = [instance for instance in instances if instance is not None]
 
42
    first = None
 
43
    last = None
 
44
    for instance in instances:
 
45
        try:
 
46
            if instance < first:
 
47
                first = instance
 
48
        except TypeError:
 
49
            first = instance
 
50
        try:
 
51
            if instance > last:
 
52
                last = instance
 
53
        except TypeError:
 
54
            last = instance
 
55
    # FIXME: return this instead
 
56
    logger.info(first)
 
57
    logger.info(last)
 
58
    logger.info(last-first)
 
59
 
 
60
 
 
61
def get_practitest_report_for_week(week_number):
 
62
    # FIXME: switch this over to config.ini
 
63
    session = practitest.PractitestSession(
 
64
        config.PROJECT_ID, config.API_KEY, config.API_SECRET_KEY)
 
65
    instances = session.get_instances(week_number)
 
66
    maximum = get_first_instance(instances)
 
67
    logger.info(maximum)
 
68
 
 
69
 
 
70
def _parse_arguments():
 
71
    parser = argparse.ArgumentParser('Compute team totals for a given week.')
 
72
    parser.add_argument('week',
 
73
                        help='week to total',
 
74
                        type=int)
 
75
    return parser.parse_args()
 
76
 
 
77
 
 
78
def main():
 
79
    args = _parse_arguments()
 
80
    get_practitest_report_for_week(args.week)
 
81
 
 
82
 
 
83
if __name__ == '__main__':
 
84
    sys.exit(main())