summaryrefslogtreecommitdiff
path: root/morphcacheserver/frontend.py
blob: 6eb09175ff644905b751b833eec63bce4d89a3c2 (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
# Copyright (C) 2015 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.


from bottle import Bottle, request, static_file, template

import math


def make_table(data, heading_ids, heading_titles, row_class_cb=None):
    '''Render 'data' as a HTML table.'''

    table_template = """
    <table>
    <tr class="headings">
    %for title in heading_titles:
        <td>{{title}}</td>
    %end
    </tr>
    %for row in rows:
    <tr class="{{ row_class_cb(row) }}" >
    %for col in heading_ids:
        <td>{{row[col]}}</td>
    %end
    </tr>
    %end
    </table>
    """
    return template(table_template, heading_ids=heading_ids,
                    heading_titles=heading_titles, rows=data,
                    row_class_cb=row_class_cb)


def web_frontend(db):
    '''Some pretty stuff for users to interact with.'''
    app = Bottle()

    @app.get('/artifacts')
    def artifacts():
        '''View artifacts in the cache.'''

        # This causes a 500 error if the parameter is not an integer.
        page = int(request.query.get('page', 1))
        page_size = 50

        content = '<p>Artifacts:</p>'

        def row_style_class_cb(row):
            if row['n_different_builds'] > 0:
                return 'fail'
            elif row['n_builds'] <= 1:
                return 'noinfo'
            else:
                return 'pass'

        artifacts = db.view_artifact_statistics(
            start=(page-1)*page_size, page_size=page_size)
        content += make_table(
            artifacts,
            ['cache_name', 'n_builds', 'n_different_builds'],
            ['Artifact', 'Total builds', 'Mismatching builds'],
            row_class_cb=row_style_class_cb)

        n_artifacts = db.count_artifacts()
        n_pages = int(math.ceil(n_artifacts / float(page_size)))

        content += '<div id="footer">'
        content += '<p>Page %i of %i: ' % (page, n_pages)
        if page > 1:
            content += '<a href="?page=%i">prev</a>' % (page-1)
            if page < n_pages:
                content += ' -- '
        if page < n_pages:
            content += '<a href="?page=%i">next</a>' % (page+1)
        content += '</p></div>'

        return template('morphcacheserver/templates/base', base=content)

    @app.get('/')
    def frontpage():
        '''A nice frontpage.'''

        return template('morphcacheserver/templates/index')

    @app.get('/style.css')
    def stylesheet():
        # For heavily-used deployments you should serve static files like a
        # .css using a faster web server like Apache, Cherokee, etc.
        return static_file('style.css', root='morphcacheserver')

    return app