Repository navigation
Expand file tree
/
Copy pathcube.py
More file actions
89 lines (70 loc) · 2.4 KB
/
Copy pathcube.py
File metadata and controls
89 lines (70 loc) · 2.4 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from bottle import Bottle
from bottle.ext.mongo import MongoPlugin
import datetime
from redis import Redis
from rq import Queue
from mining.utils import conf, parse_dumps
from .base import get, post, put, delete
collection = 'cube'
cube_app = Bottle()
mongo = MongoPlugin(
uri=conf("mongodb")["uri"],
db=conf("mongodb")["db"],
json_mongo=True)
cube_app.install(mongo)
@cube_app.route('/', method='GET')
@cube_app.route('/<slug>', method='GET')
def cube_get(mongodb, slug=None):
return get(mongodb, collection, slug)
@cube_app.route('/runing-cubes', method='GET')
def cube_get_runing(mongodb, slug=None):
cubes = list(mongodb[collection].find(
{'run': 'run'}, {'_id': False}).sort([('last_update', -1)]))
return json.dumps(cubes, default=parse_dumps)
@cube_app.route('/late-cubes', method='GET')
def cube_get_late(mongodb, slug=None):
_cubes = list(mongodb[collection].find({}, {'_id': False}))
cubes = []
for cb in _cubes:
is_late = False
if cb.get('scheduler_type', '') == 'minutes':
lu = cb.get('lastupdate')
if isinstance(lu, datetime.datetime):
n_lu = lu + datetime.timedelta(
minutes=int(cb['scheduler_interval']))
if (datetime.datetime.now() - n_lu).total_seconds() > 0:
cb['late'] = n_lu
is_late = True
elif cb.get('scheduler_type', '') == 'hour':
pass
elif cb.get('scheduler_type', '') == 'day':
pass
if is_late:
cubes.append(cb)
return json.dumps(cubes, default=parse_dumps)
@cube_app.route('/', method='POST')
def cube_post(mongodb, slug=None):
ret = post(mongodb, collection, opt={'status': False})
cube = json.loads(ret)
Queue(connection=Redis()).enqueue_call(
func='mining.bin.cube.run',
args=(cube['slug'],),
timeout=1200
)
return ret
@cube_app.route('/<slug>', method='PUT')
def cube_put(mongodb, slug=None):
ret = put(mongodb, collection, slug, opt={'status': False})
cube = json.loads(ret)
Queue(connection=Redis()).enqueue_call(
func='mining.bin.cube.run',
args=(cube['slug'],),
timeout=1200
)
return ret
@cube_app.route('/<slug>', method='DELETE')
def cube_delete(mongodb, slug=None):
return delete(mongodb, collection, slug)