brick.py 5.11 KB
Newer Older
Wei Shoulin's avatar
brick  
Wei Shoulin committed
1
2
3
4
import grpc

from csst_dfs_commons.models import Result
from csst_dfs_commons.models.common import from_proto_model_list
Wei Shoulin's avatar
Wei Shoulin committed
5
from csst_dfs_commons.models.facility import Brick, BrickObsStatus, BrickLevel1
Wei Shoulin's avatar
brick  
Wei Shoulin committed
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

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())

Wei Shoulin's avatar
Wei Shoulin committed
56
            if resp.record is None or (resp.record.id == 0 and resp.record.ra == 0.0 and resp.record.dec == 0.0):
Wei Shoulin's avatar
brick  
Wei Shoulin committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
                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
        '''  
Wei Shoulin's avatar
Wei Shoulin committed
74
75
        rec = brick_pb2.BrickRecord(
            id = get_parameter(kwargs, "id", -1),
Wei Shoulin's avatar
brick  
Wei Shoulin committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
            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],
Wei Shoulin's avatar
Wei Shoulin committed
96
97
            band = [string],
            limit = [int]
Wei Shoulin's avatar
brick  
Wei Shoulin committed
98
99
100
101
102
103

        :returns: csst_dfs_common.models.Result
        '''
        try:
            resp, _ =  self.stub.FindObsStatus.with_call(brick_pb2.FindObsStatusReq(
                brick_id = get_parameter(kwargs, "brick_id", -1),
Wei Shoulin's avatar
Wei Shoulin committed
104
105
                band = get_parameter(kwargs, "band", ""),
                limit = get_parameter(kwargs, "limit", 0)
Wei Shoulin's avatar
brick  
Wei Shoulin committed
106
107
108
            ),metadata = get_auth_headers())

            if resp.success:
Wei Shoulin's avatar
Wei Shoulin committed
109
                return Result.ok_data(data = from_proto_model_list(BrickObsStatus, resp.records)).append("totalCount", resp.totalCount)
Wei Shoulin's avatar
brick  
Wei Shoulin committed
110
111
112
113
114
115
116
117
118
119
120
121
            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
Wei Shoulin's avatar
Wei Shoulin committed
122
123
            module = [str],
            limit = [int]
Wei Shoulin's avatar
brick  
Wei Shoulin committed
124
125
126
127
128
129
130

        :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),
Wei Shoulin's avatar
Wei Shoulin committed
131
132
                module = get_parameter(kwargs, "limit", ""),
                limit = get_parameter(kwargs, "limit", 0)
Wei Shoulin's avatar
brick  
Wei Shoulin committed
133
134
135
136
137
138
139
140
141
            ),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))