level1.py 2.96 KB
Newer Older
Wei Shoulin's avatar
c3    
Wei Shoulin committed
1

Wei Shoulin's avatar
C9    
Wei Shoulin committed
2
import datetime
Wei Shoulin's avatar
Wei Shoulin committed
3
from ..common.service import grpc_channel
Wei Shoulin's avatar
c3    
Wei Shoulin committed
4
from ..common.utils import *
Wei Shoulin's avatar
C9    
Wei Shoulin committed
5
from ..common.constants import *
Wei Shoulin's avatar
c3    
Wei Shoulin committed
6
7
class Level1DataApi(object):
    def __init__(self):
Wei Shoulin's avatar
Wei Shoulin committed
8
        self.stub = None
Wei Shoulin's avatar
c3    
Wei Shoulin committed
9

Wei Shoulin's avatar
Wei Shoulin committed
10
    @grpc_channel
Wei Shoulin's avatar
c3    
Wei Shoulin committed
11
    def find(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
12
        return find_req("Level1Servicer.Find", kwargs)
Wei Shoulin's avatar
c3    
Wei Shoulin committed
13

Wei Shoulin's avatar
Wei Shoulin committed
14
    @grpc_channel
Wei Shoulin's avatar
hstdm    
Wei Shoulin committed
15
    def find_by_brick_ids(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
16
17
18
        if not isinstance(get_parameter(kwargs, "brick_ids"), list):
            return Result.error(message="brick_ids is not a list")        
        return find_req("Level1Servicer.FindByBrickIds", kwargs)
Wei Shoulin's avatar
Wei Shoulin committed
19
20
21
22
23
24
25
    
    @grpc_channel
    def find_refs_by_ids(self, **kwargs):
        if not isinstance(get_parameter(kwargs, "ids"), list):
            return Result.error(message="ids is not a list")
        return find_req("Level1Servicer.FindRefsByIds", kwargs)
    
Wei Shoulin's avatar
Wei Shoulin committed
26
    @grpc_channel
Wei Shoulin's avatar
Wei Shoulin committed
27
    def find_by_ids(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
28
29
30
        if not isinstance(get_parameter(kwargs, "ids"), list):
            return Result.error(message="ids is not a list")
        return find_req("Level1Servicer.FindByIds", kwargs)
Wei Shoulin's avatar
Wei Shoulin committed
31
32
    
    @grpc_channel
Wei Shoulin's avatar
Wei Shoulin committed
33
    def sls_find_by_qc1_status(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
34
35
36
        conditions = {"limit": 1}
        conditions.update(kwargs)
        return find_req("Level1Servicer.FindByQc1Status", conditions)
Wei Shoulin's avatar
Wei Shoulin committed
37

Wei Shoulin's avatar
Wei Shoulin committed
38
    @grpc_channel
Wei Shoulin's avatar
c3    
Wei Shoulin committed
39
    def get(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
40
        return get_req("Level1Servicer.Get", kwargs)
Wei Shoulin's avatar
c3    
Wei Shoulin committed
41

Wei Shoulin's avatar
Wei Shoulin committed
42
    @grpc_channel
Wei Shoulin's avatar
c3    
Wei Shoulin committed
43
    def update_proc_status(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
44
        return update_req("Level1Servicer.UpdateProcStatus", kwargs)
Wei Shoulin's avatar
c3    
Wei Shoulin committed
45

Wei Shoulin's avatar
Wei Shoulin committed
46
    @grpc_channel
Wei Shoulin's avatar
c3    
Wei Shoulin committed
47
    def update_qc1_status(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
48
        return update_req("Level1Servicer.UpdateQc1Status", kwargs)
Wei Shoulin's avatar
c3    
Wei Shoulin committed
49

Wei Shoulin's avatar
Wei Shoulin committed
50
    @grpc_channel
Wei Shoulin's avatar
c3    
Wei Shoulin committed
51
52
    def write(self, **kwargs):
        try:
Wei Shoulin's avatar
C9    
Wei Shoulin committed
53
54
55
56
57
            conditions = {}
            conditions.update(kwargs)
            file_path = get_parameter(kwargs, "file_path", "")
            filename = get_parameter(kwargs, "filename", "")            
            if not file_path:
Wei Shoulin's avatar
Wei Shoulin committed
58
                return Result.error(message="file_path is blank")
Wei Shoulin's avatar
C9    
Wei Shoulin committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
            if not os.path.exists(file_path):
                return Result.error(message="the file [%s] not existed" % (file_path, ))
            if not filename:
                filename = os.path.basename(file_path)
                conditions["filename"] = filename
            if not conditions.get("qc1_status", ''):
                conditions["qc1_status"] = "0"
            if not conditions.get("prc_status", ''):
                conditions["prc_status"] = "-1024"
            if not conditions.get("prc_time", ''):
                time_now = datetime.datetime.now()
                conditions["prc_time"] = time_now.strftime('%Y-%m-%d %H:%M:%S')  

            if not conditions.get("build_id", ''):
                conditions["build_id"] = "0"
            with open(file_path, 'rb') as f:
                byte_stream = io.BytesIO(f.read())
                return write_stream_req("Level1Servicer.Write", byte_stream, conditions)
        except Exception as e:
            return Result.error(message="%s" % (e,))