result1.py 4.21 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
2
3
4
5
6
import os
import logging
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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

log = logging.getLogger('csst')
class Result1Api(object):
    def __init__(self, sub_system = "ifs"):
        self.sub_system = sub_system
        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, "results1")):
            os.mkdir(os.path.join(self.root_dir, "results1")) 
    
    def find(self, **kwargs):
        '''
        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
28
            file_name = [str],
Wei Shoulin's avatar
Wei Shoulin committed
29
30
            proc_type = [str]

Wei Shoulin's avatar
Wei Shoulin committed
31
        return list of level 1 record
Wei Shoulin's avatar
Wei Shoulin committed
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
        '''
        paths = []
        
        file_name = get_parameter(kwargs, "file_name")
        proc_type = get_parameter(kwargs, "proc_type", "default")

        sql = ["select * from ifs_result_1  where proc_type='" + proc_type + "'"]

        if file_name:
            sql = ["select * from ifs_result_1 where filename='" + file_name + "'"]
        _, r = self.db.select_many("".join(sql))

        return r

    def get(self, **kwargs):
        '''
        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_1 where id=?", (fits_id,))
            
        _, result0s = self.db.select_many(
            "select result0_id, create_time from ifs_result_0_1 where result1_id=?", (fits_id,))

        return r, result0s

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

Wei Shoulin's avatar
Wei Shoulin committed
65
        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
66
67
68
            fits_id = [int],
            file_path = [str], 
            chunk_size = [int] default 20480
Wei Shoulin's avatar
Wei Shoulin committed
69
70
71
72
73
74
75
76
77
78
79
80
81

        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_1 where id=?", (fits_id))
            if r is not None:
Wei Shoulin's avatar
Wei Shoulin committed
82
                file_path = r["file_path"]
Wei Shoulin's avatar
Wei Shoulin committed
83
84

        if file_path is not None:
Wei Shoulin's avatar
Wei Shoulin committed
85
86
            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
87
88

    def write(self, **kwargs):
Wei Shoulin's avatar
Wei Shoulin committed
89
        ''' copy a local level 1 file to file storage, and insert a record into database
Wei Shoulin's avatar
Wei Shoulin committed
90
        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
91
92
            file_path = [str],
            proc_type = [str],
Wei Shoulin's avatar
Wei Shoulin committed
93
94
95
96
97
98
99
100
101
102
103
104
105
            result0_ids = [list]

            insert into database
        '''
        file_path = get_parameter(kwargs, "file_path")
        proc_type = get_parameter(kwargs, "proc_type", "default")
        result0_ids = get_parameter(kwargs, "result0_ids", [])

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

        new_file_dir = create_dir(os.path.join(self.root_dir, "results1"),
                self.sub_system, 
Wei Shoulin's avatar
Wei Shoulin committed
106
                "/".join([str(datetime.now().year),"%02d"%(datetime.now().month),"%02d"%(datetime.now().day)]))
Wei Shoulin's avatar
Wei Shoulin committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
        
        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)


        self.db.execute(
            'INSERT INTO ifs_result_1 (filename, file_path,  proc_type, create_time) \
                VALUES(?,?,?,?)',
            (file_basename, new_file_path.replace(self.root_dir, '')[1:], proc_type, format_time_ms(time.time()),)
        )
        self.db.end()
        result1_id = 1
        for id0 in result0_ids:
            self.db.execute(
                'INSERT INTO ifs_result_0_1 (result0_id, result1_id, create_time) \
                    VALUES(?,?,?)',
                (id0, result1_id, format_time_ms(time.time()))
            )            

            self.db.end()

        log.info("result1 fits %s imported.", file_path)
        return new_file_path