brick.py 4.87 KB
Newer Older
Wei Shoulin's avatar
brick  
Wei Shoulin committed
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import grpc

from csst_dfs_commons.models import Result
from csst_dfs_commons.models.common import from_proto_model_list
from csst_dfs_commons.models.facility import Brick, FindObsStatus, BrickLevel1

from csst_dfs_proto.facility.brick import brick_pb2, brick_pb2_grpc

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

class BrickApi(object):
    """
    Brick Operation Class
    """    
    def __init__(self):
        self.stub = brick_pb2_grpc.BrickSrvStub(ServiceProxy().channel())

    def find(self, **kwargs):
        ''' find brick records

        :param kwargs:
            limit: limits returns the number of records,default 0:no-limit

        :returns: csst_dfs_common.models.Result
        '''
        try:
            resp, _ =  self.stub.Find.with_call(brick_pb2.FindBrickReq(
                limit = get_parameter(kwargs, "limit", 0),
                other_conditions = {"test":"cnlab.test"}
            ),metadata = get_auth_headers())

            if resp.success:
                return Result.ok_data(data = from_proto_model_list(Brick, resp.records)).append("totalCount", resp.totalCount)
            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 get(self, **kwargs):
        '''  fetch a record from database

        :param kwargs:
            id : [int]

        :returns: csst_dfs_common.models.Result
        '''
        try:
            brick_id = get_parameter(kwargs, "id", -1)
            resp, _ =  self.stub.Get.with_call(brick_pb2.GetBrickReq(
                id = brick_id
            ),metadata = get_auth_headers())

            if resp.brick is None or resp.brick.id == -1:
                return Result.error(message=f"{brick_id} not found")  

            return Result.ok_data(data=Brick().from_proto_model(resp.record))
           
        except grpc.RpcError as e:
            return Result.error(message="%s:%s" % (e.code().value, e.details))        

    def write(self, **kwargs):
        ''' insert a brickal record into database

        :param kwargs: Parameter dictionary, key items support:
            ra = [float],
            dec = [float],
            boundingbox = [str]
        
        :returns: csst_dfs_common.models.Result
        '''  
        rec = brick_pb2.Brick(
            id = get_parameter(kwargs, "id", 0),
            ra = get_parameter(kwargs, "ra", 0.0),
            dec = get_parameter(kwargs, "dec", 0.0),
            boundingbox = get_parameter(kwargs, "boundingbox", "")
        )
        req = brick_pb2.WriteBrickReq(record = rec)
        try:
            resp,_ = self.stub.Write.with_call(req,metadata = get_auth_headers())
            if resp.success:
                return Result.ok_data(data=Brick().from_proto_model(resp.record))
            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 find_obs_status(self, **kwargs):
        ''' find observation status of bricks

        :param kwargs:
            brick_id = [int],
            band = [string]

        :returns: csst_dfs_common.models.Result
        '''
        try:
            resp, _ =  self.stub.FindObsStatus.with_call(brick_pb2.FindObsStatusReq(
                brick_id = get_parameter(kwargs, "brick_id", -1),
                band = get_parameter(kwargs, "band", "")
            ),metadata = get_auth_headers())

            if resp.success:
                return Result.ok_data(data = from_proto_model_list(FindObsStatus, resp.records)).append("totalCount", resp.totalCount)
            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 find_level1_data(self, **kwargs):
        ''' find level1 data

        :param kwargs: Parameter dictionary, support:
            brick_id = [int]\n
            level1_id = [int]\n
            module = [str]

        :returns: csst_dfs_common.models.Result
        '''
        try:
            resp, _ =  self.stub.FindLevel1.with_call(brick_pb2.FindLevel1Req(
                brick_id = get_parameter(kwargs, "brick_id", -1),
                level1_id = get_parameter(kwargs, "level1_id", 0),
                module = get_parameter(kwargs, "limit", "")
            ),metadata = get_auth_headers())

            if resp.success:
                return Result.ok_data(data = from_proto_model_list(BrickLevel1, resp.records)).append("totalCount", resp.totalCount)
            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))