level1prc.py 3.72 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
Wei Shoulin committed
5
from csst_dfs_commons.models.ifs import Level1PrcRecord
Wei Shoulin's avatar
Wei Shoulin committed
6

Wei Shoulin's avatar
Wei Shoulin committed
7
from csst_dfs_proto.ifs.level1prc import level1prc_pb2, level1prc_pb2_grpc
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
8
9
10
11

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

Wei Shoulin's avatar
Wei Shoulin committed
12
class Level1PrcApi(object):
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
13
    def __init__(self):
Wei Shoulin's avatar
Wei Shoulin committed
14
        self.stub = level1prc_pb2_grpc.Level1PrcSrvStub(ServiceProxy().channel())
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
15
16

    def find(self, **kwargs):
Wei Shoulin's avatar
Wei Shoulin committed
17
        ''' retrieve level1 procedure records from database
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
18
19

        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
20
            level1_id: [str]
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
21
22
23
24
25
26
27
            pipeline_id: [str]
            prc_module: [str]
            prc_status : [int]

        return: csst_dfs_common.models.Result
        '''
        try:
Wei Shoulin's avatar
Wei Shoulin committed
28
29
            resp, _ =  self.stub.Find.with_call(level1prc_pb2.FindLevel1PrcReq(
                level1_id = get_parameter(kwargs, "level1_id"),
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
30
31
32
33
34
35
36
                pipeline_id = get_parameter(kwargs, "pipeline_id"),
                prc_module = get_parameter(kwargs, "prc_module"),
                prc_status = get_parameter(kwargs, "prc_status"),
                other_conditions = {"test":"cnlab.test"}
            ),metadata = get_auth_headers())

            if resp.success:
Wei Shoulin's avatar
Wei Shoulin committed
37
                return Result.ok_data(data = from_proto_model_list(Level1PrcRecord, resp.records)).append("totalCount", resp.totalCount)
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
38
            else:
Wei Shoulin's avatar
c3    
Wei Shoulin committed
39
                return Result.error(message = str(resp.error.detail))
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
40
41

        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
42
            return Result.error(message="%s:%s" % (e.code().value, e.details()))
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
43
44
45
46
47

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

        parameter kwargs:
Wei Shoulin's avatar
c3    
Wei Shoulin committed
48
            id : [int],
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
49
50
51
52
53
54
55
56
57
            status : [int]

        return csst_dfs_common.models.Result
        '''
        id = get_parameter(kwargs, "id")
        status = get_parameter(kwargs, "status")

        try:
            resp,_ = self.stub.UpdateProcStatus.with_call(
Wei Shoulin's avatar
Wei Shoulin committed
58
                level1prc_pb2.UpdateProcStatusReq(id=id, status=status),
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
59
60
61
62
63
64
65
                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:
Wei Shoulin's avatar
Wei Shoulin committed
66
            return Result.error(message="%s:%s" % (e.code().value, e.details()))
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
67
68

    def write(self, **kwargs):
Wei Shoulin's avatar
Wei Shoulin committed
69
        ''' insert a level1 procedure record into database
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
70
71
 
        parameter kwargs:
Wei Shoulin's avatar
Wei Shoulin committed
72
            level1_id : [int]
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
73
74
            pipeline_id : [str]
            prc_module : [str]
Wei Shoulin's avatar
Wei Shoulin committed
75
            params_file_path : [str]
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
76
77
            prc_status : [int]
            prc_time : [str]
Wei Shoulin's avatar
Wei Shoulin committed
78
            result_file_path : [str]
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
79
80
81
        return csst_dfs_common.models.Result
        '''   

Wei Shoulin's avatar
Wei Shoulin committed
82
        rec = level1prc_pb2.Level1PrcRecord(
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
83
            id = 0,
Wei Shoulin's avatar
Wei Shoulin committed
84
            level1_id = get_parameter(kwargs, "level1_id"),
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
85
86
            pipeline_id = get_parameter(kwargs, "pipeline_id"),
            prc_module = get_parameter(kwargs, "prc_module"),
Wei Shoulin's avatar
Wei Shoulin committed
87
            params_file_path = get_parameter(kwargs, "params_file_path"),
Wei Shoulin's avatar
Wei Shoulin committed
88
            prc_status = get_parameter(kwargs, "prc_status", -1),
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
89
            prc_time = get_parameter(kwargs, "prc_time"),
Wei Shoulin's avatar
Wei Shoulin committed
90
            result_file_path = get_parameter(kwargs, "result_file_path")
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
91
        )
Wei Shoulin's avatar
Wei Shoulin committed
92
        req = level1prc_pb2.WriteLevel1PrcReq(record = rec)
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
93
94
95
        try:
            resp,_ = self.stub.Write.with_call(req,metadata = get_auth_headers())
            if resp.success:
Wei Shoulin's avatar
Wei Shoulin committed
96
                return Result.ok_data(data = Level1PrcRecord().from_proto_model(resp.record))
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
97
98
99
            else:
                return Result.error(message = str(resp.error.detail))
        except grpc.RpcError as e:
Wei Shoulin's avatar
Wei Shoulin committed
100
            return Result.error(message="%s:%s" % (e.code().value, e.details()))
Wei Shoulin's avatar
leve0  
Wei Shoulin committed
101
102
103