From cdaccb911ba60d70832f39dd82e574f16c42ed3a Mon Sep 17 00:00:00 2001 From: Ivan Vazhenin Date: Mon, 8 Apr 2024 20:30:34 +0300 Subject: [PATCH] Correcting replication and no_files flag --- db.py | 1 + main.py | 76 +++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/db.py b/db.py index 8f96f55..bc6833a 100644 --- a/db.py +++ b/db.py @@ -77,6 +77,7 @@ class Profile(Base): scheme: Mapped[str] branch: Mapped[str] = mapped_column(String, nullable=True) json: Mapped[str] = mapped_column(String, nullable=True) + no_files: Mapped[bool] user: Mapped['User'] = relationship(back_populates='profiles') diff --git a/main.py b/main.py index 78222b6..d687010 100644 --- a/main.py +++ b/main.py @@ -80,6 +80,15 @@ def get_branch(bndname: str, scheme: str): return None, None +def get_profile(bndname: str, scheme: str): + conn = db.connect_db() + with Session(conn) as session: + item = session.query(db.Profile).filter_by(scheme=scheme).join(db.User).filter_by(bndname=bndname).one_or_none() + if item: + return item + return None + + def run_tasks(): logger.debug('Task thread started.') while True: @@ -256,6 +265,8 @@ def replication(bnd_name: str, commit_id: str, schema: str): rxmls = RequestXmlService() res_id = uuid4().hex + profile = get_profile(bndname=bnd_name, scheme=schema) + res = rxmls.get_request_document(res_id, None) rxmls.set_result(res, 0, '') ET.SubElement(res, 'replication', {'id': commit_id, 'scheme': schema}) @@ -307,22 +318,23 @@ def replication(bnd_name: str, commit_id: str, schema: str): qu.setUids(created) ws.load(qu, uids) exported_files = [] - for feature_uid in uids: - feature = ws.featureByUid(feature_uid) - if not feature: - continue - for attr in feature.attributes('c1000'): - updated_files.append(variantToFileValue(attr.val())) - # updated_files.append(feature_uid) - for x in updated_files: - exported_files.append({ - 'key': x.key, - 'bucket': x.bucket, - 'filename': x.fileName, - }) - fp = os.path.join(z.dirname, x.key) - os.makedirs(os.path.dirname(fp), exist_ok=True) - download_file(x.key, x.bucket, fp) + if not profile or not profile.no_files: + for feature_uid in uids: + feature = ws.featureByUid(feature_uid) + if not feature: + continue + for attr in feature.attributes('c1000'): + updated_files.append(variantToFileValue(attr.val())) + # updated_files.append(feature_uid) + for x in updated_files: + exported_files.append({ + 'key': x.key, + 'bucket': x.bucket, + 'filename': x.fileName, + }) + fp = os.path.join(z.dirname, x.key) + os.makedirs(os.path.dirname(fp), exist_ok=True) + download_file(x.key, x.bucket, fp) with open(os.path.join(z.dirname, 'export_files.json'), 'w') as f: f.write(json.dumps(exported_files)) ws.clearData(True) @@ -898,6 +910,38 @@ async def correction_replication(bnd_name: str, schema: str): logger.error('Error sending') +@app.get("/get_cr") +async def correction_replication(bnd_name: str, schema: str): + + date = datetime.datetime.now() + rxmls = RequestXmlService() + res_id = uuid4().hex + + res = rxmls.get_request_document(res_id, None) + + con = OOConnectionParams(schema, Config.oodb_host, Config.oodb_port, Config.oodb_dbname, + Config.oodb_username, Config.oodb_passwd, schema) + ws = OODBWorkspace.ws(schema) + if not ws.isInit(): + res = ws.init(con) + logger.warning(res) + ET.SubElement(res, 'commit', {'id': ws.currentCommit(), 'schema': schema}) + + params = { + 'from': f'tcp://{Config.self_bnd}', + 'to': f'tcp://{bnd_name}', + 'ts_added': date.timestamp(), + 'user_id': '0', + 'query_type': NEW_COMMIT_RESPONSE, + 'query_data': ET.tostring(res, encoding='unicode', xml_declaration=True), + } + proxy = ServerProxy(Config.enserver) + try: + proxy.send(params, [], Config.ret_path) + except: + logger.error('Error sending') + + def main(): global connection global server