level0.py 6.53 KB
Newer Older
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
1
2
3
import grpc

from csst_dfs_commons.models import Result
Wei Shoulin's avatar
Wei Shoulin committed
4
from csst_dfs_commons.models.common import from_proto_model_list
Wei Shoulin's avatar
brick    
Wei Shoulin committed
5
from csst_dfs_commons.models.ifs import Level0Record
Wei Shoulin's avatar
bugs    
Wei Shoulin committed
6
from csst_dfs_commons.models.constants import UPLOAD_CHUNK_SIZE
Wei Shoulin's avatar
Wei Shoulin committed
7

Wei Shoulin's avatar
brick    
Wei Shoulin committed
8
from csst_dfs_proto.ifs.level0 import level0_pb2, level0_pb2_grpc
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
9
10
11
12
13
14
15
16
17
18
19
20

from ..common.service import ServiceProxy
from ..common.utils import *

class Level0DataApi(object):
    def __init__(self):
        self.stub = level0_pb2_grpc.Level0SrvStub(ServiceProxy().channel())

    def find(self, **kwargs):
        ''' retrieve level0 records from database

        parameter kwargs:
Wei Shoulin's avatar
bugs    
Wei Shoulin committed
21
22
23
24
            obs_id: [str],
            detector_no: [str],
            obs_type: [str],
            object_name: [str],
Wei Shoulin's avatar
c3    
Wei Shoulin committed
25
            obs_time : (start, end),
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
26
27
            qc0_status : [int],
            prc_status : [int],
Wei Shoulin's avatar
bugs    
Wei Shoulin committed
28
29
30
31
32
            file_name: [str],
            version: [str],
            ra: [float],
            dec: [float],
            radius: [float],
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
33
34
35
36
37
38
            limit: limits returns the number of records,default 0:no-limit

        return: csst_dfs_common.models.Result
        '''
        try:
            resp, _ =  self.stub.Find.with_call(level0_pb2.FindLevel0DataReq(
Wei Shoulin's avatar
Wei Shoulin committed
39
                obs_id = get_parameter(kwargs, "obs_id"),
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
40
41
                detector_no = get_parameter(kwargs, "detector_no"),
                obs_type = get_parameter(kwargs, "obs_type"),
Wei Shoulin's avatar
c3    
Wei Shoulin committed
42
43
                exp_time_start = get_parameter(kwargs, "obs_time", [None, None])[0],
                exp_time_end = get_parameter(kwargs, "obs_time", [None, None])[1],
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
44
45
46
                qc0_status = get_parameter(kwargs, "qc0_status"),
                prc_status = get_parameter(kwargs, "prc_status"),
                file_name = get_parameter(kwargs, "file_name"),
Wei Shoulin's avatar
bugs    
Wei Shoulin committed
47
48
49
50
51
                object_name = get_parameter(kwargs, "object_name"),
                version = get_parameter(kwargs, "version"),
                ra = get_parameter(kwargs, "ra"),
                dec = get_parameter(kwargs, "dec"),
                radius = get_parameter(kwargs, "radius"),
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
52
53
54
55
56
                limit = get_parameter(kwargs, "limit", 0),
                other_conditions = {"test":"cnlab.test"}
            ),metadata = get_auth_headers())

            if resp.success:
Wei Shoulin's avatar
Wei Shoulin committed
57
                return Result.ok_data(data=from_proto_model_list(Level0Record, resp.records)).append("totalCount", resp.totalCount)
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
58
            else:
Wei Shoulin's avatar
c3    
Wei Shoulin committed
59
                return Result.error(message = str(resp.error.detail))
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
60
61
62
63
64
65
66
67

        except grpc.RpcError as e:
            return Result.error(message="%s:%s" % (e.code().value, e.details))

    def get(self, **kwargs):
        '''  fetch a record from database

        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
68
69
            id : [int],
            level0_id: [str]
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
70
71
72
73

        return csst_dfs_common.models.Result
        '''
        try:
Wei Shoulin's avatar
Wei Shoulin committed
74
75
            id = get_parameter(kwargs, "id")
            level0_id = get_parameter(kwargs, "level0_id")
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
76
            resp, _ =  self.stub.Get.with_call(level0_pb2.GetLevel0DataReq(
Wei Shoulin's avatar
Wei Shoulin committed
77
78
                id = id,
                level0_id = level0_id
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
79
80
            ),metadata = get_auth_headers())

Wei Shoulin's avatar
Wei Shoulin committed
81
            if resp.record is None or resp.record.id == 0:
Wei Shoulin's avatar
Wei Shoulin committed
82
                return Result.error(message=f"not found")  
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
83

Wei Shoulin's avatar
Wei Shoulin committed
84
            return Result.ok_data(data = Level0Record().from_proto_model(resp.record))
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
85
86
87
88
89
90
91
92
           
        except grpc.RpcError as e:
            return Result.error(message="%s:%s" % (e.code().value, e.details))   

    def update_proc_status(self, **kwargs):
        ''' update the status of reduction

        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
93
94
            id : [int],
            level0_id: [str],
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
95
96
97
98
            status : [int]

        return csst_dfs_common.models.Result
        '''
Wei Shoulin's avatar
Wei Shoulin committed
99
100
        id = get_parameter(kwargs, "id")
        level0_id = get_parameter(kwargs, "level0_id")
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
101
102
103
        status = get_parameter(kwargs, "status")
        try:
            resp,_ = self.stub.UpdateProcStatus.with_call(
Wei Shoulin's avatar
Wei Shoulin committed
104
105
106
107
                level0_pb2.UpdateProcStatusReq(
                    id=id,
                    level0_id = level0_id,
                    status=status),
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
108
109
110
111
112
113
114
115
116
117
118
119
120
                metadata = get_auth_headers()
            )
            if resp.success:
                return Result.ok_data()
            else:
                return Result.error(message = str(resp.error.detail))
        except grpc.RpcError as e:
            return Result.error(message="%s:%s" % (e.code().value, e.details))

    def update_qc0_status(self, **kwargs):
        ''' update the status of QC0
        
        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
121
122
            id : [int],
            level0_id: [str],
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
123
124
            status : [int]
        '''        
Wei Shoulin's avatar
Wei Shoulin committed
125
126
        id = get_parameter(kwargs, "id")
        level0_id = get_parameter(kwargs, "level0_id")
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
127
128
129
        status = get_parameter(kwargs, "status")
        try:
            resp,_ = self.stub.UpdateQc0Status.with_call(
Wei Shoulin's avatar
Wei Shoulin committed
130
131
132
133
                level0_pb2.UpdateQc0StatusReq( 
                    id=id,
                    level0_id = level0_id,
                    status=status),
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
134
135
136
137
138
139
140
141
142
                metadata = get_auth_headers()
            )
            if resp.success:
                return Result.ok_data()
            else:
                return Result.error(message = str(resp.error.detail))
        except grpc.RpcError as e:
            return Result.error(message="%s:%s" % (e.code().value, e.details))

Wei Shoulin's avatar
c3    
Wei Shoulin committed
143
144
145
146
147
148
149
150
151
152
    def write(self, **kwargs):
        ''' insert a level0 data record into database
 
        parameter kwargs:
            file_path = [str]
        return: csst_dfs_common.models.Result
        '''          
        rec = level0_pb2.Level0Record(
            file_path = get_parameter(kwargs, "file_path")
        )
Wei Shoulin's avatar
bugs    
Wei Shoulin committed
153
154
155
156
157
158
159
160
161

        def stream(rec):
            with open(rec.file_path, 'rb') as f:
                while True:
                    data = f.read(UPLOAD_CHUNK_SIZE)
                    if not data:
                        break
                    yield level0_pb2.WriteLevel0Req(record = rec, data = data)

Wei Shoulin's avatar
c3    
Wei Shoulin committed
162
        try:
Wei Shoulin's avatar
bugs    
Wei Shoulin committed
163
164
165
166
167
168
169
170
            if not rec.file_path:
                return Result.error(message="file_path is blank")
            if not os.path.exists(rec.file_path):
                return Result.error(message="the file [%s] not existed" % (rec.file_path, ))
            if not rec.filename:
                rec.filename = os.path.basename(rec.file_path)

            resp,_ = self.stub.Write.with_call(stream(rec),metadata = get_auth_headers())
Wei Shoulin's avatar
c3    
Wei Shoulin committed
171
            if resp.success:
Wei Shoulin's avatar
bugs    
Wei Shoulin committed
172
                return Result.ok_data(data=Level0Record().from_proto_model(resp.record))
Wei Shoulin's avatar
c3    
Wei Shoulin committed
173
174
175
            else:
                return Result.error(message = str(resp.error.detail))
        except grpc.RpcError as e:
Wei Shoulin's avatar
bugs    
Wei Shoulin committed
176
177
            return Result.error(message="%s:%s" % (e.code().value, e.details))        
     
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
178