~allanlesage/+junk/qakit-vis

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
#!/usr/bin/python3
# UEQA KPIs
# Copyright (C) 2015 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import argparse
import datetime
import sys

import pymongo

import config
import practitest
from util import logger


def strptime_instance_last_run(instance):
    """PractiTest times look like '13-Apr-2015  22:02', return a datetime."""
    try:
        return datetime.datetime.strptime(
            instance['last_run']['value'], '%d-%b-%Y  %H:%M')
    except ValueError:
        # not all instances have last_runs
        return None


def get_first_instance(instances):
    instances = list(map(strptime_instance_last_run, instances))
    instances = [instance for instance in instances if instance is not None]
    first = None
    last = None
    for instance in instances:
        try:
            if instance < first:
                first = instance
        except TypeError:
            first = instance
        try:
            if instance > last:
                last = instance
        except TypeError:
            last = instance
    # FIXME: return this instead
    logger.info(first)
    logger.info(last)
    logger.info(last-first)


def get_practitest_report_for_week(week_number):
    # FIXME: switch this over to config.ini
    session = practitest.PractitestSession(
        config.PROJECT_ID, config.API_KEY, config.API_SECRET_KEY)
    instances = session.get_instances(week_number)
    maximum = get_first_instance(instances)
    logger.info(maximum)


def _parse_arguments():
    parser = argparse.ArgumentParser('Compute team totals for a given week.')
    parser.add_argument('week',
                        help='week to total',
                        type=int)
    return parser.parse_args()


def main():
    args = _parse_arguments()
    get_practitest_report_for_week(args.week)


if __name__ == '__main__':
    sys.exit(main())