admin.py 3.49 KB
Newer Older
Marc Duez's avatar
Marc Duez committed
1
# coding: utf8
Marc Duez's avatar
Marc Duez committed
2
import gluon.contrib.simplejson, re
3
import os.path, subprocess
4
import vidjil_utils
5
from collections import defaultdict
6

Marc Duez's avatar
Marc Duez committed
7 8 9 10 11 12 13 14
if request.env.http_origin:
    response.headers['Access-Control-Allow-Origin'] = request.env.http_origin  
    response.headers['Access-Control-Allow-Credentials'] = 'true'
    response.headers['Access-Control-Max-Age'] = 86400
    

## return admin_panel
def index():
15
    if auth.has_membership("admin"):
16 17 18 19 20 21
        p = subprocess.Popen(["uptime"], stdout=subprocess.PIPE)
        uptime, err = p.communicate()
        
        p = subprocess.Popen(["df", "-h"], stdout=subprocess.PIPE)
        disk_use, err = p.communicate()
        
22 23
        d = monitor()
        return dict(d,
24 25 26
                    uptime=uptime,
                    disk_use=disk_use
                    )
27 28 29


def monitor():
Marc Duez's avatar
Marc Duez committed
30 31 32 33 34
    """
    >>> monitor().has_key('worker')
    True
    
    """
35
    # External monitor
36 37 38 39 40 41 42

    last_results = ''
    for res in db(db.scheduler_task.id == db.results_file.scheduler_task_id).select(orderby=~db.results_file.id,
                                                                                    limitby=(0,10)):
        last_results += res.scheduler_task.status[0]


43
    return dict (worker = len(db().select(db.scheduler_worker.ALL)),
44 45
                 queued = len(db(db.scheduler_task.status=='QUEUED').select()),
                 assigned = len(db(db.scheduler_task.status=='ASSIGNED').select()),
46 47
                 running = len(db(db.scheduler_task.status=='RUNNING').select()),
                 last_results = last_results)
Marc Duez's avatar
Marc Duez committed
48
    
Marc Duez's avatar
Marc Duez committed
49 50 51 52
    
def log():
    if auth.has_membership("admin"):
        
53
        
Marc Duez's avatar
Marc Duez committed
54
        lines = []
55
        file = open(defs.DIR_LOG+request.vars["file"])
Marc Duez's avatar
Marc Duez committed
56
        
57 58
        if "filter" not in request.vars :
            request.vars["filter"] = ""
Marc Duez's avatar
Marc Duez committed
59
            
60 61 62
        for row in reversed(file.readlines()) :

            if vidjil_utils.filter(row, request.vars["filter"]) :
Marc Duez's avatar
Marc Duez committed
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
                line = {}

                tmp = re.split('\t+| +', row) 

                line["date"] = tmp[1]
                line["date2"] = tmp[2].split(',')[0]
                line["type"] = tmp[3]
                line["file"] = tmp[5]

                if tmp[6] != "Creating":
                    line["user"] = tmp[7]
                    line["mes"] = ""
                    for i in range(8,len(tmp)):
                        line["mes"] += tmp[i] + " "
                else:
                    line["user"] = ""
                    line["mes"] = ""
                    for i in range(6,len(tmp)):
                        line["mes"] += tmp[i] + " "

                lines.append(line)

                if len(lines) >= 100 :
                    return dict(lines = lines)
            
Marc Duez's avatar
Marc Duez committed
88
        return dict(lines = lines)
Marc Duez's avatar
Marc Duez committed
89 90 91 92 93 94 95 96 97 98 99 100 101

## to use after change in the upload folder
def repair_missing_files():
    if auth.has_membership("admin"):
        
        flist = ""
        for row in db(db.sequence_file.id>0 and db.sequence_file.data_file != None).select() : 
            seq_file = defs.DIR_SEQUENCES+row.data_file
            
            if not os.path.exists(seq_file) :
                db.sequence_file[row.id] = dict(data_file = None)
                flist += " : " + row.filename
            else :
102
                size = os.path.getsize(seq_file)
Marc Duez's avatar
Marc Duez committed
103 104
                db.sequence_file[row.id] = dict(size_file = size)
                
Mathieu Giraud's avatar
Mathieu Giraud committed
105
        res = {"success" : "true", "message" : "references to missing files have been removed from the database"+flist}
Marc Duez's avatar
Marc Duez committed
106
        return gluon.contrib.simplejson.dumps(res, separators=(',',':'))