Completed
Push — master ( 1919c8...11a876 )
by Roy
01:49
created

task_in_json()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# -*- encoding: utf-8 -*-
3
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
4
# Author: Binux<[email protected]>
5
#         http://binux.me
6
# Created on 2014-07-16 15:30:57
7
8
import socket
9
from flask import abort, render_template, request, json
10
11
from pyspider.libs import utils
12
from .app import app
13
14
15
@app.route('/task/<taskid>')
16
def task(taskid):
17
    if ':' not in taskid:
18
        abort(400)
19
    project, taskid = taskid.split(':', 1)
20
21
    taskdb = app.config['taskdb']
22
    task = taskdb.get_task(project, taskid)
23
24
    if not task:
25
        abort(404)
26
    resultdb = app.config['resultdb']
27
    if resultdb:
28
        result = resultdb.get(project, taskid)
29
30
    return render_template("task.html", task=task, json=json, result=result,
31
                           status_to_string=app.config['taskdb'].status_to_string)
32
33
34
@app.route('/task/<taskid>.json')
35
def task_in_json(taskid):
36
    if ':' not in taskid:
37
        return json.jsonify({'code': 400, 'error': 'bad project:task_id format'})
38
    project, taskid = taskid.split(':', 1)
39
40
    taskdb = app.config['taskdb']
41
    task = taskdb.get_task(project, taskid)
42
43
    if not task:
44
        return json.jsonify({'code': 404, 'error': 'not found'})
45
    task['status_string'] = app.config['taskdb'].status_to_string(task['status'])
46
    return json.jsonify(task)
47
48
49
@app.route('/tasks')
50
def tasks():
51
    rpc = app.config['scheduler_rpc']
52
    taskdb = app.config['taskdb']
53
    project = request.args.get('project', "")
54
    limit = int(request.args.get('limit', 100))
55
56
    try:
57
        updatetime_tasks = rpc.get_active_tasks(project, limit)
58
    except socket.error as e:
59
        app.logger.warning('connect to scheduler rpc error: %r', e)
60
        return 'connect to scheduler error', 502
61
62
    tasks = {}
63
    result = []
64
    for updatetime, task in sorted(updatetime_tasks, key=lambda x: x[0]):
65
        key = '%(project)s:%(taskid)s' % task
66
        task['updatetime'] = updatetime
67
        if key in tasks and tasks[key].get('status', None) != taskdb.ACTIVE:
68
            result.append(tasks[key])
69
        tasks[key] = task
70
    result.extend(tasks.values())
71
72
    return render_template(
73
        "tasks.html",
74
        tasks=result,
75
        status_to_string=taskdb.status_to_string
76
    )
77
78
79
@app.route('/active_tasks')
80
def active_tasks():
81
    rpc = app.config['scheduler_rpc']
82
    taskdb = app.config['taskdb']
83
    project = request.args.get('project', "")
84
    limit = int(request.args.get('limit', 100))
85
86
    try:
87
        tasks = rpc.get_active_tasks(project, limit)
88
    except socket.error as e:
89
        app.logger.warning('connect to scheduler rpc error: %r', e)
90
        return '{}', 502, {'Content-Type': 'application/json'}
91
92
    result = []
93
    for updatetime, task in tasks:
94
        task['updatetime'] = updatetime
95
        task['updatetime_text'] = utils.format_date(updatetime)
96
        if 'status' in task:
97
            task['status_text'] = taskdb.status_to_string(task['status'])
98
        result.append(task)
99
100
    return json.dumps(result), 200, {'Content-Type': 'application/json'}
101
102
app.template_filter('format_date')(utils.format_date)
103