level1.py 2.71 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
hstdm    
Wei Shoulin committed
19

Wei Shoulin's avatar
Wei Shoulin committed
20
    @grpc_channel
Wei Shoulin's avatar
Wei Shoulin committed
21
    def find_by_ids(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
22
23
24
        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
25
26
    
    @grpc_channel
Wei Shoulin's avatar
Wei Shoulin committed
27
    def sls_find_by_qc1_status(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
28
29
30
        conditions = {"limit": 1}
        conditions.update(kwargs)
        return find_req("Level1Servicer.FindByQc1Status", conditions)
Wei Shoulin's avatar
Wei Shoulin committed
31

Wei Shoulin's avatar
Wei Shoulin committed
32
    @grpc_channel
Wei Shoulin's avatar
c3    
Wei Shoulin committed
33
    def get(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
34
        return get_req("Level1Servicer.Get", kwargs)
Wei Shoulin's avatar
c3    
Wei Shoulin committed
35

Wei Shoulin's avatar
Wei Shoulin committed
36
    @grpc_channel
Wei Shoulin's avatar
c3    
Wei Shoulin committed
37
    def update_proc_status(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
38
        return update_req("Level1Servicer.UpdateProcStatus", kwargs)
Wei Shoulin's avatar
c3    
Wei Shoulin committed
39

Wei Shoulin's avatar
Wei Shoulin committed
40
    @grpc_channel
Wei Shoulin's avatar
c3    
Wei Shoulin committed
41
    def update_qc1_status(self, **kwargs):
Wei Shoulin's avatar
C9    
Wei Shoulin committed
42
        return update_req("Level1Servicer.UpdateQc1Status", kwargs)
Wei Shoulin's avatar
c3    
Wei Shoulin committed
43

Wei Shoulin's avatar
Wei Shoulin committed
44
    @grpc_channel
Wei Shoulin's avatar
c3    
Wei Shoulin committed
45
46
    def write(self, **kwargs):
        try:
Wei Shoulin's avatar
C9    
Wei Shoulin committed
47
48
49
50
51
            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
52
                return Result.error(message="file_path is blank")
Wei Shoulin's avatar
C9    
Wei Shoulin committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
            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,))