result0.py 4.21 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
import os
Wei Shoulin's avatar
Wei Shoulin committed
2
import logging
Wei Shoulin's avatar
Wei Shoulin committed
3
4
5
6
import time, datetime
import shutil

from ..common.db import DBClient
Wei Shoulin's avatar
Wei Shoulin committed
7
from ..common.utils import *
Wei Shoulin's avatar
Wei Shoulin committed
8
9

log = logging.getLogger('csst')
Wei Shoulin's avatar
Wei Shoulin committed
10

Wei Shoulin's avatar
Wei Shoulin committed
11
12
13
class Result0Api(object):
    def __init__(self, sub_system = "ifs"):
        self.sub_system = sub_system
Wei Shoulin's avatar
Wei Shoulin committed
14
15
16
17
18
19
20
21
22
23
24
25
26
        self.root_dir = os.getenv("CSST_LOCAL_FILE_ROOT", "/opt/temp/csst")
        self.check_dir()
        self.db = DBClient()

    def check_dir(self):
        if not os.path.exists(self.root_dir):
            os.mkdir(self.root_dir)
            log.info("using [%s] as root directory", self.root_dir)

        if not os.path.exists(os.path.join(self.root_dir, "results0")):
            os.mkdir(os.path.join(self.root_dir, "results0")) 

    def find(self, **kwargs):
Xie Zhou's avatar
update    
Xie Zhou committed
27
28
        '''
        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
29
30
            raw_id = [int],
            file_name = [str],
Wei Shoulin's avatar
Wei Shoulin committed
31
            proc_type = [str]
Xie Zhou's avatar
update    
Xie Zhou committed
32

Wei Shoulin's avatar
Wei Shoulin committed
33
        return list of level 0 record
Xie Zhou's avatar
update    
Xie Zhou committed
34
        '''
Wei Shoulin's avatar
Wei Shoulin committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
        paths = []
        
        raw_id = get_parameter(kwargs, "raw_id", -1)
        file_name = get_parameter(kwargs, "file_name")
        proc_type = get_parameter(kwargs, "proc_type")
        sql = []

        sql.append("select * from ifs_result_0 where raw_id=%d" %(raw_id,))

        if proc_type is not None:
            sql.append(" and proc_type='" + proc_type + "'")

        if file_name:
            sql = ["select * from ifs_result_0 where filename='" + file_name + "'"]

Wei Shoulin's avatar
Wei Shoulin committed
50
        _, recs = self.db.select_many("".join(sql))
Wei Shoulin's avatar
Wei Shoulin committed
51

Wei Shoulin's avatar
Wei Shoulin committed
52
53
54
        for r in recs:
            r['file_path'] = os.path.join(self.root_dir, r['file_path'])
        return recs
Wei Shoulin's avatar
Wei Shoulin committed
55
56
        
    def get(self, **kwargs):
Wei Shoulin's avatar
Wei Shoulin committed
57
58
        ''' query database, return a record as dict

Wei Shoulin's avatar
Wei Shoulin committed
59
60
61
62
63
64
65
66
        parameter kwargs:
            fits_id = [int] 

        return dict or None
        '''
        fits_id = get_parameter(kwargs, "fits_id", -1)
        r = self.db.select_one(
            "select * from ifs_result_0 where id=?", (fits_id,))
Wei Shoulin's avatar
Wei Shoulin committed
67
68
        if r:
            r['file_path'] = os.path.join(self.root_dir, r['file_path'])            
Wei Shoulin's avatar
Wei Shoulin committed
69
        return r
Xie Zhou's avatar
update    
Xie Zhou committed
70
71

    def read(self, **kwargs):
Wei Shoulin's avatar
Wei Shoulin committed
72
73
        ''' yield bytes of fits file

Wei Shoulin's avatar
Wei Shoulin committed
74
        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
75
76
77
            fits_id = [int],
            file_path = [str], 
            chunk_size = [int] default 20480
Wei Shoulin's avatar
Wei Shoulin committed
78
79
80
81
82
83
84
85
86
87
88
89
90

        yield bytes of fits file
        '''
        fits_id = get_parameter(kwargs, "fits_id")
        file_path = get_parameter(kwargs, "file_path")

        if fits_id is None and file_path is None:
            raise Exception("fits_id or file_path need to be defined")

        if fits_id is not None:
            r = self.db.select_one(
                "select * from ifs_result_0 where id=?", (fits_id))
            if r is not None:
Wei Shoulin's avatar
Wei Shoulin committed
91
                file_path = r["file_path"]
Wei Shoulin's avatar
Wei Shoulin committed
92
93

        if file_path is not None:
Wei Shoulin's avatar
Wei Shoulin committed
94
95
            chunk_size = get_parameter(kwargs, "chunk_size", 20480)
            return yield_file_bytes(os.path.join(self.root_dir, file_path), chunk_size)
Wei Shoulin's avatar
Wei Shoulin committed
96
97

    def write(self, **kwargs):
Wei Shoulin's avatar
Wei Shoulin committed
98
99
        ''' copy a local level 0 file to file storage, and insert a record into database

Wei Shoulin's avatar
Wei Shoulin committed
100
        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
101
102
            raw_id = [int],
            file_path = [str],
Wei Shoulin's avatar
Wei Shoulin committed
103
104
105
106
107
108
109
110
111
112
113
            proc_type = [str]
        '''
        raw_id = get_parameter(kwargs, "raw_id")
        file_path = get_parameter(kwargs, "file_path")
        proc_type = get_parameter(kwargs, "proc_type", "default")

        if file_path is None:
            raise Exception("file_path need to be defined")

        new_file_dir = create_dir(os.path.join(self.root_dir, "results0"),
                self.sub_system, 
Wei Shoulin's avatar
Wei Shoulin committed
114
                "/".join([str(datetime.now().year),"%02d"%(datetime.now().month),"%02d"%(datetime.now().day)]))
Wei Shoulin's avatar
Wei Shoulin committed
115
116
117
118
119
120
        

        file_basename = os.path.basename(file_path)
        new_file_path = os.path.join(new_file_dir, file_basename)
        shutil.copyfile(file_path, new_file_path)

Wei Shoulin's avatar
Wei Shoulin committed
121
122
123
124
        file_path = new_file_path.replace(self.root_dir, '')
        if file_path.index("/") == 0:
            file_path = file_path[1:]

Wei Shoulin's avatar
Wei Shoulin committed
125
126
127
        self.db.execute(
            'INSERT INTO ifs_result_0 (filename, file_path, raw_id,  proc_type, create_time) \
                VALUES(?,?,?,?,?)',
Wei Shoulin's avatar
Wei Shoulin committed
128
            (file_basename, file_path, raw_id, proc_type, format_time_ms(time.time()))
Wei Shoulin's avatar
Wei Shoulin committed
129
130
131
        )

        self.db.end()
Xie Zhou's avatar
update    
Xie Zhou committed
132

Wei Shoulin's avatar
Wei Shoulin committed
133
134
        log.info("result0 fits %s imported.", file_path)
        return new_file_path