import datetime from ..common.service import grpc_channel from ..common.utils import * from ..common.constants import * class Level1DataApi(object): def __init__(self): self.stub = None @grpc_channel def find(self, **kwargs): return find_req("Level1Servicer.Find", kwargs) @grpc_channel def find_by_brick_ids(self, **kwargs): 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) @grpc_channel def find_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.FindByIds", kwargs) @grpc_channel def sls_find_by_qc1_status(self, **kwargs): conditions = {"limit": 1} conditions.update(kwargs) return find_req("Level1Servicer.FindByQc1Status", conditions) @grpc_channel def get(self, **kwargs): return get_req("Level1Servicer.Get", kwargs) @grpc_channel def update_proc_status(self, **kwargs): return update_req("Level1Servicer.UpdateProcStatus", kwargs) @grpc_channel def update_qc1_status(self, **kwargs): return update_req("Level1Servicer.UpdateQc1Status", kwargs) @grpc_channel def write(self, **kwargs): try: conditions = {} conditions.update(kwargs) file_path = get_parameter(kwargs, "file_path", "") filename = get_parameter(kwargs, "filename", "") if not file_path: return Result.error(message="file_path is blank") 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,))