#!/usr/bin/python
#-*-coding:utf-8-*-

import os
import sys
import shutil
import subprocess
import json
import pprint
import time
import datetime
import base64
import MySQLdb
import MySQLdb.cursors


#class MySQLCursorDict(mysql.connector.cursor.MySQLCursor):
    #def _row_to_python(self, rowdata, desc=None):
        #row = super(MySQLCursorDict, self)._row_to_python(rowdata, desc)
        #if row:
            #return dict(zip(self.column_names, row))
        #return None


def unicode_to_str(obj):
    """convert unicode object to str"""

    if isinstance(obj, list):
        return [unicode_to_str(element) for element in obj]
    elif isinstance(obj, dict):
        return {unicode_to_str(key) : unicode_to_str(value) for key, value \
                in obj.iteritems()}
    elif isinstance(obj, unicode):
        return obj.encode('utf-8')
    else:
        return obj


if __name__ == "__main__":


    ITEM = {
        'sr'           : '',
        'num'          : '',
        'config_mysql' : 'config_mysql2.json' }


    RESULT = { 'raw'   : {} }


    print '=====[BuildMonitor DB connecting...]=====\n'

    # load config data
    db_ip = os.getenv("BUILDMONITOR_IP")
    db_user = os.getenv("BUILDMONITOR_USER")
    db_pass = os.getenv("BUILDMONITOR_PASS")
    db_name = os.getenv("BUILDMONITOR_NAME")
    print '%s %s %s' % (db_ip, db_user, db_name)
    #if not os.path.isfile( ITEM['config_mysql'] ):
            #ITEM['config_mysql'] = "jenkins-scripts/" + ITEM['config_mysql']
            #if not os.path.isfile( ITEM['config_mysql'] ):
                #sys.exit("ERROR: %s was not found!" % ITEM['config_mysql'])
#
#
    #f = open( ITEM['config_mysql'], 'r')
    #mconfig = json.loads(f.read())
    #f.close()




    trbs_data = os.getenv('TRBS_DATA').replace(' ', '+')

    if trbs_data:
        RESULT['raw']['trbs_data'] = unicode_to_str(json.loads(base64.b64decode(trbs_data)))
    else:
        sys.exit( "Error: trbs_data is empty!" )

    print RESULT['raw']['trbs_data']


    ITEM['sr']  = RESULT['raw']['trbs_data']['info']['sr']
    ITEM['num'] = RESULT['raw']['trbs_data']['info']['snapshot_num']



    #dbcon = mysql.connector.connect( user=mconfig['user'], password=mconfig['password'], host=mconfig['host'], database=mconfig['database'] )
    #dbcon = mysql.connector.connect( user=db_user, password=db_pass, host=db_ip, database=db_name )
    dbcon = MySQLdb.connect(host=db_ip, user=db_user, passwd=db_pass, db=db_name, cursorclass=MySQLdb.cursors.DictCursor)
    #query = dbcon.cursor(cursor_class=MySQLCursorDict)
    #query = dbcon.cursor(dictionary=True)
    query = dbcon.cursor()




    # SR의 ID값 확인
    select_sr = "SELECT * FROM sr_status WHERE sr = %s"
    query.execute( select_sr, [ITEM['sr']] )
    RESULT['raw']['sr_status'] = query.fetchall()

    print RESULT['raw']['sr_status']

    if len( RESULT['raw']['sr_status'] ) != 1:
        sys.exit("ERROR: %s was not found in database(sr_status)!" % ITEM['sr'])

    RESULT['sr_id'] = RESULT['raw']['sr_status'][0]['id']
    print "sr_id is", RESULT['sr_id']




    # build_project_id 값 확인
    select_sr_detail = ( "SELECT id, sr_status_id, trbs_build_project_id, post_build_project_id, obs_request_no, obs_request_comment, obs_request_date, obs_request_status "
                         "FROM sr_status_detail "
                         "WHERE sr_status_id = %s "
                         "ORDER BY id DESC" )
    print select_sr_detail

    query.execute( select_sr_detail, [RESULT['sr_id']] )
    RESULT['raw']['sr_detail'] = query.fetchall()
    print RESULT['raw']['sr_detail']



    if len( RESULT['raw']['sr_detail'] ) < 1:
        sys.exit("ERROR: %s was not found in database(sr_status_detail)!" % ITEM['sr'])

    RESULT['build_project_id'] = RESULT['raw']['sr_detail'][0]['trbs_build_project_id']
    print "build_project_id is", RESULT['build_project_id']





    # snapshot 정보 확인
    select_snapshot = ( "SELECT build_snapshot.id, snapshot_name, snapshot_num, snapshot_url, start_time, end_time, build_snapshot.status, info_project_id "
                        "FROM build_snapshot "
                        "LEFT JOIN build_project ON build_project.id = build_snapshot.build_project_id "
                        "WHERE build_project_id = %s ORDER BY snapshot_num DESC" )


    query.execute( select_snapshot, [RESULT['build_project_id']] )
    temp = query.fetchall()
    print temp

    if len(temp) < 1:
        sys.exit("ERROR: snapshot_num is empty!")

    RESULT['raw']['snapshot'] = temp

    RESULT['snapshot_num'] = RESULT['raw']['snapshot'][0]['snapshot_num']
    print "snapshot_num is", RESULT['snapshot_num']



    if ITEM['num'] != RESULT['snapshot_num']:
        sys.exit("Error: snapshot_num different!!")






    for key, value in RESULT['raw']['trbs_data'].items():

        if key == 'trbs_tct':

            insert_tct = ( "INSERT INTO trbs_tct "
                           "( id, sr_status_id, ref_id, sr_name, snapshot_num, profile, plan_name, total, pass, fail, "
                             "block, na, pcnt, time_start, time_end, build_id, device_id, device_model, device_name, "
                             "device_host, manufacturer, tct_profile, tct_version, url) "
                           "VALUES ( %(id)s, %(sr_status_id)s, %(ref_id)s, %(sr_name)s, %(snapshot_num)s, %(profile)s, %(plan_name)s, %(total)s, %(pass)s, %(fail)s, "
                                    "%(block)s, %(na)s, %(pcnt)s, %(time_start)s, %(time_end)s, %(build_id)s, %(device_id)s, %(device_model)s, %(device_name)s, "
                                    "%(device_host)s, %(manufacturer)s, %(tct_profile)s, %(tct_version)s, %(url)s )" )

            try:
                value['sr_status_id'] = RESULT['sr_id']

                query.execute( insert_tct, value )
                dbcon.commit()
                print "[Write: trbs_tct] id =", value['id']
            except MySQLdb.IntegrityError:
                print "[Error] Duplicate entry (" + key + ")-", value

        elif key == 'trbs_tct_suite':

            insert_tct_suite = ( "INSERT INTO trbs_tct_suite "
                                 "( id, trbs_tct_id, name, total, pass, pass_rate, fail, fail_rate, block, block_rate, na, na_rate ) "
                                 "VALUES ( %(id)s, %(trbs_tct_id)s, %(name)s, %(total)s, %(pass)s, %(pass_rate)s, %(fail)s, %(fail_rate)s, %(block)s, %(block_rate)s, %(na)s, %(na_rate)s )" )

            for temp in value:

                try:
                    query.execute( insert_tct_suite, temp )
                    dbcon.commit()
                    print "[Write: trbs_tct_suite] id =", temp['id']
                except MySQLdb.IntegrityError:
                    print "[Error] Duplicate entry (" + key + ")-", temp


        elif key == 'trbs_ref':

            insert_ref = ( "INSERT INTO trbs_ref "
                           "( id, plan_name, total, pass, fail, block, na, pcnt, time_start, time_end, build_id, device_id, "
                             "device_model, device_name, device_host, manufacturer, tct_profile, tct_version, url ) "
                           "VALUES( %(id)s, %(plan_name)s, %(total)s, %(pass)s, %(fail)s, %(block)s, %(na)s, %(pcnt)s, %(time_start)s, %(time_end)s, %(build_id)s, %(device_id)s, "
                                   "%(device_model)s, %(device_name)s, %(device_host)s, %(manufacturer)s, %(tct_profile)s, %(tct_version)s, %(url)s )" )

            try:
                query.execute( insert_ref, value )
                dbcon.commit()
                print "[Write: trbs_ref] id =", value['id']
            except MySQLdb.IntegrityError:
                print "[Error] Duplicate entry (" + key + ")-", value


        elif key == 'trbs_ref_suite':


            insert_ref_suite = ( "INSERT INTO trbs_ref_suite "
                                 "( id, trbs_ref_id, name, total, pass, pass_rate, fail, fail_rate, block, block_rate, na, na_rate ) "
                                 "VALUES ( %(id)s, %(trbs_ref_id)s, %(name)s, %(total)s, %(pass)s, %(pass_rate)s, %(fail)s, %(fail_rate)s, %(block)s, %(block_rate)s, %(na)s, %(na_rate)s )" )

            for temp in value:
                try:
                    query.execute( insert_ref_suite, temp )
                    dbcon.commit()
                    print "[Write: trbs_ref_suite] id =", temp['id']
                except MySQLdb.IntegrityError:
                    print "[Error] Duplicate entry (" + key + ")-", temp


        elif key == 'trbs_smoke':

            insert_smoke = ( "INSERT INTO trbs_smoke "
                             "( id, sr_status_id, sr_name, snapshot_num, profile, pass, fail, total, pcnt, url, time_start, time_end ) "
                             "VALUES ( %(id)s, %(sr_status_id)s, %(sr_name)s, %(snapshot_num)s, %(profile)s, %(pass)s, %(fail)s, %(total)s, %(pcnt)s, %(url)s, %(time_start)s, %(time_end)s ) " )

            try:
                value['sr_status_id'] = RESULT['sr_id']

                query.execute( insert_smoke, value )
                dbcon.commit()
                print "[Write: trbs_smoke] id =", value['id']
            except MySQLdb.IntegrityError:
                print "[Error] Duplicate entry (" + key + ")-", value


        elif key == 'trbs_smoke_suite':

            insert_smoke_suite = ( "INSERT INTO trbs_smoke_suite "
                                   "( id, trbs_smoke_id, name, pass, fail ) "
                                   "VALUES( %(id)s, %(trbs_smoke_id)s, %(name)s, %(pass)s, %(fail)s )" )

            for temp in value:
                try:
                    query.execute( insert_smoke_suite, temp )
                    dbcon.commit()
                    print "[Write: trbs_smoke_suite] id =", temp['id']
                except MySQLdb.IntegrityError:
                    print "[Error] Duplicate entry (" + key + ")-", temp


        elif key == 'trbs_smoke_suite_test':

            insert_smoke_suite_test = ( "INSERT INTO trbs_smoke_suite_test (id, trbs_smoke_suite_id, name, result) "
                                        "VALUES ( %(id)s, %(trbs_smoke_suite_id)s, %(name)s, %(result)s )" )

            for temp in value:
                try:

                    query.execute( insert_smoke_suite_test, temp )
                    dbcon.commit()
                    print "[Write: trbs_smoke_suite_test] id =", temp['id']

                except MySQLdb.IntegrityError:
                    print "[Error] Duplicate entry (" + key + ")-", temp


        elif key == 'trbs_perf':

            insert_perf = ( "INSERT INTO trbs_perf "
                            "( id, sr_status_id, sr_name, snapshot_num, profile, device_core, device_dvfs, device_freq, device_kernel, "
                              "device_platform, total, pass, fail, pcnt, time_start, time_end, url ) "
                            "VALUES ( %(id)s, %(sr_status_id)s, %(sr_name)s, %(snapshot_num)s, %(profile)s, %(device_core)s, %(device_dvfs)s, %(device_freq)s, %(device_kernel)s, "
                                     "%(device_platform)s, %(total)s, %(pass)s, %(fail)s, %(pcnt)s, %(time_start)s, %(time_end)s, %(url)s )" )

            try:
                value['sr_status_id'] = RESULT['sr_id']

                query.execute( insert_perf, value )
                dbcon.commit()
                print "[Write: trbs_perf] id =", value['id']
            except MySQLdb.IntegrityError:
                print "[Error] Duplicate entry (" + key + ")-", value


        elif key == 'trbs_perf_suite':

            insert_perf_suite = ( "INSERT INTO trbs_perf_suite "
                                  "( id, trbs_perf_id, name, total, pass, fail ) "
                                  "VALUES ( %(id)s, %(trbs_perf_id)s, %(name)s, %(total)s, %(pass)s, %(fail)s )" )

            for temp in value:
                try:
                    query.execute( insert_perf_suite, temp )
                    dbcon.commit()
                    print "[Write: trbs_perf_suite] id =", temp['id']
                except MySQLdb.IntegrityError:
                    print "[Error] Duplicate entry (" + key + ")-", temp


        elif key == 'trbs_perf_suite_test':

            insert_perf_suite_test = ( "INSERT INTO trbs_perf_suite_test "
                                       "( `id`, `trbs_perf_suite_id`, `name`, `actualperf`, `expectedperf`, `range`, `result`, `criteria`, `unit` ) "
                                       "VALUES ( %(id)s, %(trbs_perf_suite_id)s, %(name)s, %(actualperf)s, %(expectedperf)s, %(range)s, %(result)s, %(criteria)s, %(unit)s )" )

            for temp in value:
                try:
                    query.execute( insert_perf_suite_test, temp )
                    dbcon.commit()
                    print "[Write: trbs_perf_suite_test] id =", temp['id']
                except MySQLdb.IntegrityError:
                    print "[Error] Duplicate entry (" + key + ")-", temp


        else:

            if key == 'info':
                pass
            else:
                print "[Error] wrong data (", key, ") -", value


    trbs_test_stage_id = 101
    cur_time = datetime.datetime.now()
    insert_sr_stage_sql = "INSERT INTO sr_stage (sr_status_id, info_stage_id, stage_start_time, stage_end_time, stage_status, build_project_id) VALUES ( %s, %s, %s, %s, %s, %s)";
    insert_sr_stage_data = (RESULT['sr_id'], trbs_test_stage_id, cur_time, cur_time, 'S', RESULT['build_project_id'] )
    try:
        query.execute(insert_sr_stage_sql, insert_sr_stage_data)
        dbcon.commit();
        print "[Insert: sr_stage]" + str(insert_sr_stage_sql) + "\n" + str(insert_sr_stage_data)
    except MySQLdb.IntegrityError:
        print "[Error] " + str(insert_sr_stage_sql) + "\n" + str(insert_sr_stage_data)

    # update sr_stage status.
    query.close()
    dbcon.close()


    exit()
