__init__.py 2.73 KB
Newer Older
Zixian Cai's avatar
Zixian Cai committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/usr/bin/env python3
# Copyright 2017 The Australian National University
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Zixian Cai's avatar
Zixian Cai committed
16 17
from flask import Flask, render_template, request, flash, redirect, url_for, \
    Blueprint
Zixian Cai's avatar
Zixian Cai committed
18
from mubench.conf import settings
Zixian Cai's avatar
Zixian Cai committed
19
from mubench.frontend.utils import group_stats_result
Zixian Cai's avatar
Zixian Cai committed
20
from mubench.frontend.web import changes, timeline
21
from mubench.frontend.web.storage import LocalStorage as storage
Zixian Cai's avatar
Zixian Cai committed
22
import click
Zixian Cai's avatar
Zixian Cai committed
23
import json
Zixian Cai's avatar
Zixian Cai committed
24

Zixian Cai's avatar
Zixian Cai committed
25 26 27 28
root = Blueprint('root', __name__,
                 template_folder='templates',
                 static_folder="static",
                 url_prefix=settings.WEB_PREFIX)
Zixian Cai's avatar
Zixian Cai committed
29 30


Zixian Cai's avatar
Zixian Cai committed
31 32 33
@root.route("/visualize_commit", methods=["POST"])
def visualize_commit():
    repo, host = settings.WEB_POI_REPO, settings.WEB_POI_HOST
Zixian Cai's avatar
Zixian Cai committed
34
    result = storage.load(repo, host, request.form.get("commit")[:7])
Zixian Cai's avatar
Zixian Cai committed
35 36 37 38
    group_stats_result(result)
    return render_template("visualize.html", result=result)


Zixian Cai's avatar
Zixian Cai committed
39
@root.route("/visualize_file", methods=["POST"])
40
def visualize_file():
Zixian Cai's avatar
Zixian Cai committed
41 42 43 44 45
    if "logfile" not in request.files:
        flash("No log file uploaded")
        return redirect(url_for("index"))
    logfile = request.files["logfile"]
    result = json.loads(logfile.read())
Zixian Cai's avatar
Zixian Cai committed
46
    group_stats_result(result)
Zixian Cai's avatar
Zixian Cai committed
47
    return render_template("visualize.html", result=result)
Zixian Cai's avatar
Zixian Cai committed
48 49


Zixian Cai's avatar
Zixian Cai committed
50
@root.route("/tools")
Zixian Cai's avatar
Zixian Cai committed
51 52 53 54
def tools():
    return render_template("tools.html")


Zixian Cai's avatar
Zixian Cai committed
55
@root.route("/")
Zixian Cai's avatar
Zixian Cai committed
56
def index():
Zixian Cai's avatar
Zixian Cai committed
57 58 59
    repo, host = settings.WEB_POI_REPO, settings.WEB_POI_HOST
    result = storage.load_relative(repo, host, 0)
    result1 = storage.load_relative(repo, host, 1)
Zixian Cai's avatar
Zixian Cai committed
60
    result2 = storage.load_all(repo, host)
Zixian Cai's avatar
Zixian Cai committed
61
    tl, tl_revisions = timeline.make_timeline(result2)
Zixian Cai's avatar
Zixian Cai committed
62 63
    return render_template("index.html",
                           visualzing=result,
64
                           changes=changes.compare(result, result1),
Zixian Cai's avatar
Zixian Cai committed
65 66
                           timeline=tl,
                           timeline_revisions=tl_revisions)
Zixian Cai's avatar
Zixian Cai committed
67 68 69


@click.command()
Zixian Cai's avatar
Zixian Cai committed
70 71 72
@click.option("--host", default=settings.WEB_HOST)
@click.option("--port", default=settings.WEB_PORT, type=int)
@click.option("--debug", default=settings.WEB_DEBUG, is_flag=True)
Zixian Cai's avatar
Zixian Cai committed
73
def web(host, port, debug):
Zixian Cai's avatar
Zixian Cai committed
74 75 76
    app = Flask(__name__)
    app.config['SECRET_KEY'] = settings.WEB_SECRET
    app.register_blueprint(root)
Zixian Cai's avatar
Zixian Cai committed
77
    app.run(host=host, port=port, debug=debug)