# -*- coding: utf-8 -*-

from flask import Flask, render_template, url_for, request
import pandas as pd
import numpy as np
import json
#import datetime
#import cx_Oracle
from calculate_pha import plot_pha

app = Flask(__name__)
dbDetails = "rqmmetuser/rqmme1user@KICDT06.KCDC.ATT.COM:1525/RQMPROD"

@app.route('/', methods=['GET', 'POST'])
def hello():
    return render_template('welcome.html')

@app.route('/tat', methods=['GET'])
def tat():
    release= request.args.get('release')
    with open('tat_data.json', 'r') as f:
        data = json.load(f)
#    now=datetime.datetime.now()
#    previous=['2017.10','2017.11','2017.12','2018.01','2018.02',]
    apps =['BGW', 'CADM','EUAM', 'UB', 'EDF_Family' , 'ADOPT', 'IGLOO', 'Pricer-D',
           'SSDF', 'ROME', 'GIOM',  'USRP', 'NC3','AOTS-CM' , 'AOTS-M', 'AOTS-TM']
#    apps =['GIOM']
    dataTable=''
#    for release in previous:
    for app in apps:
#            dataTable=dataTable+repr(data[release][app]['openTATcount'])+'TAT'
        dataTable=dataTable+'<tr><td>'\
        +app+'</td><td bgcolor='\
        +repr(data[release][app]['openTATcolor'])+'><a href='+url_for('appTat', appName=app, release=release, state='Open')+'>'\
        +repr(data[release][app]['openTATvalue'])+'</td><td>'\
        +repr(data[release][app]['openTATcount'])\
        +'</td><td bgcolor='+repr(data[release][app]['closedTATcolor'])\
        +'><a href='+url_for('appTat', appName=app, release=release, state='Closed')+'>'\
        +repr(data[release][app]['closedTATvalue'])+'</a></td><td>'\
        +repr(data[release][app]['closedTATcount'])+'</td><td bgcolor='\
        +repr(data[release][app]['totalTATcolor'])+'><a href='+url_for('appTat', appName=app, release=release, state='Total')+'>'\
        +repr(data[release][app]['totalTATvalue'])+'</a></td>'
#        data[year]
#    year=datetime.date.today().strftime('%Y')
#    month=datetime.date.today().strftime('%m')
#    releaseData=data[2018.02]

    return render_template("Tat.html", data=dataTable, release=release)
#    return render_template('Tat.html', name='Defect Details', data=df.to_html())
    
@app.route('/appTat/<appName>/<release>/<state>', methods=['GET'])
def appTat(appName, release, state):
#    release= request.args.get('release')
#    state=request.args.get('state')
    jfile='defectDetail.json'
    with open(jfile, 'r') as f:
        data = json.load(f)
    header='Application: <b>'+appName + '</b>     Release: ' + release +'<br>'+state+' Defects'
    temp=''
    for i in range(len(data[release][appName][state]['DEFECT_ID'])):
        temp=temp+"<tr><td>"+repr(data[release][appName][state]['DEFECT_ID'][i])+"</td><td>"\
            +repr(data[release][appName][state]['TURNAROUND_HOURS'][i])+"</td><td>"\
            +repr(data[release][appName][state]['STATUS'][i])+"</td><td>"\
            +repr(data[release][appName][state]['TEST_PHASE'][i])+"</td><td></tr>"
        
    return render_template('appTat.html', data= temp, header=header)

@app.route('/pha', methods=['GET', 'POST'])
def pha():
    plot_url=plot_pha()
    data= '<img src="data:image/png;base64,{}" >'.format(plot_url)
#    with open('pha_data.json', 'r') as f:
#        data = json.load(f)
#        
    return render_template('pha.html', data=data)
  
port = os.getenv('VCAP_APP_PORT', '5000')
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=int(port))
