summaryrefslogtreecommitdiff
path: root/lorrycontroller/showlorry.py
blob: fc336a504ea32ab324a7d558b9dcf09ea11061be (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
# Copyright (C) 2014  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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import json
import logging
import time
import urlparse

import bottle

import lorrycontroller


class ShowLorry(lorrycontroller.LorryControllerRoute):

    http_method = 'GET'
    path = '/1.0/lorry/<path:path>'

    def run(self, **kwargs):
        logging.info('%s %s called', self.http_method, self.path)
        statedb = self.open_statedb()
        try:
            return statedb.get_lorry_info(kwargs['path'])
        except lorrycontroller.LorryNotFoundError as e:
            bottle.abort(404, str(e))


class ShowLorryHTML(lorrycontroller.LorryControllerRoute):

    http_method = 'GET'
    path = '/1.0/lorry-html/<path:path>'

    def run(self, **kwargs):
        logging.info('%s %s called', self.http_method, self.path)
        statedb = self.open_statedb()
        try:
            lorry_info = statedb.get_lorry_info(kwargs['path'])
        except lorrycontroller.LorryNotFoundError as e:
            bottle.abort(404, str(e))

        renderer = lorrycontroller.StatusRenderer()
        shower = lorrycontroller.JobShower()

        lorry_obj = json.loads(lorry_info['text']).values()[0]
        lorry_info['url'] = lorry_obj['url']

        lorry_info['interval_nice'] = renderer.format_secs_nicely(
            lorry_info['interval'])

        lorry_info['last_run_nice'] = time.strftime(
            '%Y-%m-%d %H:%M:%S UTC',
            time.gmtime(lorry_info['last_run']))
        
        lorry_info['disk_usage_nice'] = shower.format_bytesize(
            lorry_info['disk_usage'])

        now = statedb.get_current_time()

        due = lorry_info['last_run'] + lorry_info['interval']
        lorry_info['due_nice'] = renderer.format_due_nicely(due, now)
        
        timestamp = time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime(now))

        parts = urlparse.urlparse(bottle.request.url)
        host, port = parts.netloc.split(':', 1)
        http_server_root = urlparse.urlunparse(
            (parts.scheme, host, '', '', '', ''))

        return bottle.template(
            self._templates['lorry'],
            http_server_root=http_server_root,
            lorry=lorry_info,
            timestamp=timestamp)