summaryrefslogtreecommitdiff
path: root/lorrycontroller/showjob.py
blob: 297b652a55493358b69f221eff985127d561889c (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
# 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 logging
import time

import bottle

import lorrycontroller


class JobShower(object):

    def get_job_as_json(self, statedb, job_id):
        path = statedb.get_job_path(job_id)
        exit = statedb.get_job_exit(job_id)
        output = statedb.get_job_output(job_id)
        started, ended = statedb.get_job_started_and_ended(job_id)
        disk_usage = statedb.get_job_disk_usage(job_id)
        now = statedb.get_current_time()

        return {
            'job_id': job_id,
            'host': statedb.get_job_minion_host(job_id),
            'pid': statedb.get_job_minion_pid(job_id),
            'path': path,
            'exit': 'no' if exit is None else exit,
            'disk_usage': disk_usage,
            'disk_usage_nice': self.format_bytesize(disk_usage or 0),
            'output': output,
            'job_started': self.format_time(started),
            'job_ended': '' if ended is None else self.format_time(ended),
            'timestamp': self.format_time(now),
            }

    def format_time(self, timestamp):
        return time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime(timestamp))

    def format_bytesize(self, num_bytes):
        if num_bytes is None:
            return 'unknown'
        mebibyte = 2**20
        return '%.1f MiB' % (float(num_bytes) / float(mebibyte))


class ShowJob(lorrycontroller.LorryControllerRoute):

    http_method = 'GET'
    path = '/1.0/job/<job_id:int>'

    def run(self, **kwargs):
        logging.info('%s %s called', self.http_method, self.path)
        job_id = int(kwargs['job_id'])

        statedb = self.open_statedb()
        return JobShower().get_job_as_json(statedb, job_id)


class ShowJobHTML(lorrycontroller.LorryControllerRoute):

    http_method = 'GET'
    path = '/1.0/job-html/<job_id:int>'

    def run(self, **kwargs):
        logging.info('%s %s called', self.http_method, self.path)
        job_id = int(kwargs['job_id'])

        statedb = self.open_statedb()
        variables = JobShower().get_job_as_json(statedb, job_id)
        return bottle.template(self._templates['job'], **variables)