#!/usr/bin/python
#
#
# PLEASE THINK TWICE BEFORE USING --batch_limit 0
# OTHERWISE THE LOADING IS HEAVY
#
#
# $ ./diffsbm.py -u foo -k foo 93246 93262 --batch_limit 0
#   <TEST ITEM>     <93246 TEST STATUS> <93262 TEST STATUS>
#   mediacard/mmc-insert-after-suspend pass fail
#   networking/internet fail pass
#   suspend/network_after_suspend uninitiated pass
#   mediacard/sd-storage pass uninitiated
#   mediacard/sd-remove-after-suspend fail uninitiated
#   mediacard/mmc-storage-after-suspend pass uninitiated
#   networking/ntp fail pass
#   mediacard/sd-remove fail uninitiated
#   keys/wireless fail untested
#   mediacard/mmc-remove-after-suspend fail uninitiated
#   mediacard/sd-storage-after-suspend pass uninitiated
#   mediacard/sd-insert-after-suspend pass fail
#   optical/cdrom-write_sr0 pass untested
#   ......
#
# TODO:
#   1. handling the Key Error when keys is different in two submissions
#       Key Error for key: networking/info_eth0
#   2. dump to a json file then we only need to access the site at first
#   3. LP: #1208757






#import ipdb

import argparse
import requests
import logging

# This will be replaced by the method:
# getTestStatusByUrl
# to walk through the status by url and then
# the loading of the site could be smaller
#
# LP: #1208757
#
# input:args (args for future different protocal
# return: test report in json
def getTestRpt(args, rpt_no):

    api_uri = args.instance_uri + 'api/v1/testresults/?report__id=' + rpt_no
    api_params = {'username': args.username, 'api_key': args.apikey, 'limit': args.batch_limit}
    result = requests.get(api_uri, params=api_params)

    return result.json()

# input: test report in json
# return: dictionary = {"testing-items" : "status"}
def getTestStatus(rpt_json):

    rtn = {}
    for rpt_entry in rpt_json['objects']:
        rtn[rpt_entry['test']['name']] = rpt_entry['status']

    return rtn

# rprtprev, rptnext are dictionaries = {"testing-items" : "status"}
def diffTestStatus(rptprev, rptnext, mode):

    for entry in rptprev:
        try:
            if rptprev[entry] != rptnext[entry]:
                print  "%s %s %s" % (entry, rptprev[entry], rptnext[entry])
        except KeyError:
            print "Key Error for key: %s" % entry


def main():
    # parsing the arguments and initializing the parameters
    parser = argparse.ArgumentParser()
    parser.add_argument('--batch_limit', type=int, default=1,
                        help='Number of element in a batch result. 0 for as many as possible')
    parser.add_argument('--instance_uri',
                        default="https://certification.canonical.com/",
                        help='Certification site URI. ')
    parser.add_argument('-u', '--username', help='User name', required=True)
    parser.add_argument('-k', '--apikey', help='API key', required=True)
    parser.add_argument('-m', '--mode',
                        default='all',
                        help='pass, fail or skipped. Default is all')
    parser.add_argument('rptprev',
                        help='a benchmark test report. Input the report number.')
    parser.add_argument('rptnext',
                        help='a test report compared to the benchmark test report. Input the report number.')
    parser.add_argument("-d", "--debug", help="Set debug mode",
#                        action='store_const', const=logging.DEBUG,
                        default=logging.WARNING)

    args = parser.parse_args()

    logging.basicConfig(level=args.debug)

    rptprev_json = getTestRpt(args, args.rptprev)
    rptnext_json = getTestRpt(args, args.rptnext)

    rptprev_status = getTestStatus(rptprev_json)
    rptnext_status = getTestStatus(rptnext_json)

    diffTestStatus(rptprev_status, rptnext_status, args.mode)


if __name__ == "__main__":
    main()
