summaryrefslogtreecommitdiffstats
path: root/docker/storperf-reporting/src/app.py
blob: 79baa33f9d97aea68a056b24e9f56b16a6bd39c0 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
##############################################################################
# Copyright (c) 2017 Dell EMC and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import json
import urllib

from flask import Flask, redirect, url_for, request, render_template, session
from flask import send_from_directory, flash

import validators


class ReverseProxied(object):
    '''Wrap the application in this middleware and configure the
    front-end server to add these headers, to let you quietly bind
    this to a URL other than / and to an HTTP scheme that is
    different than what is used locally.

    In nginx:
    location /storperf/ {
        proxy_pass http://localhost:8085/;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header X-Script-Name /storperf;
    }

    :param app: the WSGI application
    '''
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        script_name = environ.get('HTTP_X_SCRIPT_NAME', '')

        if script_name:
            environ['SCRIPT_NAME'] = script_name
            path_info = environ['PATH_INFO']
            if path_info.startswith(script_name):
                environ['PATH_INFO'] = path_info[len(script_name):]

        scheme = environ.get('HTTP_X_SCHEME', '')
        if scheme:
            environ['wsgi.url_scheme'] = scheme

        return self.app(environ, start_response)


app = Flask(__name__)
app.wsgi_app = ReverseProxied(app.wsgi_app)
app.secret_key = 'storperf_graphing_module'


def get_data(data):
    metrics = {}
    report_data = {}
    temp = data.get("results") or data.get("report") or data.get("details")
    if type(temp) is list:
        length = len(temp)
        if length == 1:
            details = temp[0].get('details')
            metrics = details.get('metrics')
            report_data = details.get('report_data')
            return "single", metrics, report_data, temp
        else:
            return "multi", temp
    else:
        metrics = temp.get('metrics')
        report_data = temp.get('report_data')
        return "single", metrics, report_data, temp


@app.route('/reporting/success/')
def success():
    try:
        URL = session["url"]
        if URL.find("jobs") is not -1 and URL.find("metadata") is -1:
            data = urllib.urlopen(URL).read()
            data = json.loads(data)
            temp = data["job_ids"]
            if temp:
                info = {}
                for ID in temp:
                    url = URL + "?id=" + ID + "&type=metadata"
                    data_temp = urllib.urlopen(url).read()
                    data_temp = json.loads(data_temp)
                    report_data = get_data(data_temp)[-1]
                    info[ID] = report_data
                return render_template('plot_jobs.html', results=info)
        if validators.url(URL):
            data = urllib.urlopen(URL).read()
        else:
            data = open("./static/testdata/" + URL).read()
        data = json.loads(data)
        response = get_data(data)
        if response[0] == "single":
            metrics, report_data = response[1], response[2]
            results = response[3]
            return render_template('plot_tables.html',
                                   metrics=metrics, report_data=report_data,
                                   results=results)
        else:
            return render_template('plot_multi_data.html',
                                   results=response[1])
    except Exception as e:
        session['server_error'] = e.message + ' ' + repr(e.args)
        return redirect(url_for('file_not_found'))


@app.route('/reporting/url', methods=['POST', 'GET'])
def url():
    if request.method == 'POST':
        url = request.form['url']
        session["url"] = url
        return redirect(url_for('success'))


@app.route('/reporting/file_not_found/')
def file_not_found():
    error = session.get('server_error')
    flash("Server Error: " + error)
    return redirect(url_for('index'))


@app.route('/reporting/js/<path:path>')
def js(path):
    return send_from_directory('static/js/', path)


@app.route('/reporting/css/<path:path>')
def css(path):
    return send_from_directory('static/css/', path)


@app.route('/reporting/images/<path:path>')
def images(path):
    return send_from_directory('static/images/', path)


@app.route('/reporting/')
def index():
    return render_template('index.html')


if __name__ == '__main__':
    app.run(host="0.0.0.0", debug=True, threaded=True)