Esempio n. 1
0
def general(service):

    global audit

    #get dates and the name of the function within 'service' in order to get the daitailed data for the detail graph default values for both is ''
    date = request.args.get('date', '')
    name = request.args.get('name', '')

    #gather data with the date and name variables
    DetailData = audit.getDetail(date, service, name )

    #if the default values for the date and name are apparent, i will not return json response, because then that would be the only thing that would be rendered on the site
    # however, if the values are not the default, then the page has been loaded before and someone has requested new data. in that case it is ok to only send the
    # json response since in that case you are answering a specific client request that wants a json response
    if date != '':

        #format data for Json with the data from DetailData
        data = {'number' : DetailData[1], 'hour': DetailData[0], 'date': date}
        return jsonify(data)

    #if the page gets a request like a button press etc... it will go into this if statement
    if request.method == 'POST':

        #get dates from the calendar 
        dags1 = request.form['datedate']
        dags2 = request.form['date']
        if not isinstance(dags1, datetime.date) or not isinstance(dags2, datetime.date):
            now = datetime.datetime.now().date()
            then = now - datetime.timedelta(days = 30.0)

            dates =[str(now),str(then)]
        else:
            #refactor dates to get the right format for the SQL query returns [date1,date2]
            dates = SQLclient.refactorDates(dags1,dags2)

        #get the span from site
        span = request.form['span']
        if  not span.isdigit():
            span = 1

        #functions is a data array that looks like this: [datapacket for speed of service, datapacket for the number of function calls, name of the functions for the service]
        # the data gathered is found through SQL with the parameters given
        functions = audit.getInfo(service,dates[0],dates[1],span)

        #get the names in a seperate array
        names = functions[-1]

        # then remove it from the functions array, leaving the functions array to look like this: [datapacket for speed of service, datapacket for the number of function calls]
        del functions[-1]

        #a little something in order to get the YYYY - MM - DD format into the site to be used in numerous places
        date1 = datetime.date(int(dates[0][:4]), int(dates[0][5:7]), int(dates[0][8:10])) - datetime.timedelta(int(span)*365/12)
        date2 = datetime.date(int(dates[1][:4]), int(dates[1][5:7]), int(dates[1][8:10])) - datetime.timedelta(int(span)*365/12)

        dags1y = date1.year
        dags2y = date2.year

        dags1m = date1.month
        dags2m = date2.month

        dags1d = date1.day
        dags2d = date2.day

        #render the page with all the gathered data
        return render_template('speedLogger.html', functions = names, name=service,dags1Y = dags1y, dags1M=dags1m, dags1D = dags1d, dags2Y=dags2y, dags2M = dags2m, dags2D = dags2d, data= functions, DD = DetailData[0])
    
    #get default dates for the page when it is being loaded for the first time
    now = datetime.datetime.now().date()
    then = now - datetime.timedelta(days = 30.0)

    #format the dates like in the if statement above to get the YYYY - MM - DD format into the site
    dags11 = str(now)
    dags22 = str(then)

    date1 = datetime.date(int(dags11[:4]), int(dags11[5:7]), int(dags11[8:10])) - datetime.timedelta(1*365/12)
    date2 = datetime.date(int(dags22[:4]), int(dags22[5:7]), int(dags22[8:10])) - datetime.timedelta(1*365/12)

    dags1y = date1.year
    dags2y = date2.year

    dags1m = date1.month
    dags2m = date2.month
    
    dags1d = date1.day
    dags2d = date2.day

    #get the data from the SQL connection with the default parameters -- same as above
    functions = audit.getInfo(service,dags11,dags22,1)
    names = functions[-1]
    del functions[-1]
    return render_template('speedLogger.html', functions = names, name=service,dags1Y = dags1y, dags1M=dags1m, dags1D = dags1d, dags2Y=dags2y, dags2M = dags2m, dags2D = dags2d, data= functions, DD = DetailData[0])