Sindbad~EG File Manager

Current Path : /home/numerotech/hs.aios-scientificcommittee.org/scan_v1/core/model/
Upload File :
Current File : //home/numerotech/hs.aios-scientificcommittee.org/scan_v1/core/model/LocalModel.py

from sqlalchemy import create_engine, MetaData, Table, insert, null, select,update,delete,text
from sqlalchemy.sql import and_, or_
from sqlalchemy import asc, desc
from core import app
import json
from datetime import timedelta,date,datetime,time

#engine = create_engine(app.config['DATABASE_URI'])
engine      = create_engine(app.config['DATABASE_URI'],pool_pre_ping=True,pool_recycle=3600,future=True)
engine_fk   = create_engine(app.config['DATABASE_URI_FK'],pool_pre_ping=True,pool_recycle=3600,future=True)
engine_conf = create_engine(app.config['DATABASE_URI_CONF'],pool_pre_ping=True,pool_recycle=3600,future=True)

class LocalModel():
	def __init__(self):
		try:
			self.meta = MetaData()
		except Exception as e:
			print(e)

	def get_not_sync_hall_scan_data(self,conf_id,conf_key,hall_id,scan_table_name):
		with engine_conf.connect() as conn:
			stmt  = text("select * from "+scan_table_name+" where (new_sync_at is null or updated_sync_at is null)  and entry_at is not null;")
			# stmt  = text("select * from "+scan_table_name+" where  entry_at is not null and exist_at is null;")	
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			return results

	def get_last_sync_at_local(self,scan_table_name):
		with engine_conf.connect() as conn:
			stmt  = text("select GREATEST(max(new_sync_at),max(updated_sync_at)) as last_sync_at_local from "+scan_table_name+";")
			print("geratest ........",stmt)
			# stmt  = text("select * from "+scan_table_name+" where  entry_at is not null and exist_at is null;")	
			result  = conn.execute(stmt).first()
			result = dict(result._mapping) if result else None
			return result

	def get_active_session_local(self,conf_id,hall_id):
		with engine_conf.connect() as conn:
			stmt  = text(f"select asession_id,updated_at from abs_sessions where conf_id = {conf_id} and hall_id = {hall_id} and is_active = 1;")
			result  = conn.execute(stmt).first()
			result = dict(result._mapping) if result else None
			return result

	def get_last_update_session_local(self,conf_id,hall_id):
		with engine_conf.connect() as conn:
			stmt  = text(f"select asession_id,updated_at,is_active from abs_sessions where conf_id = {conf_id} and hall_id = {hall_id} order by updated_at desc limit 1")
			result  = conn.execute(stmt).first()
			result = dict(result._mapping) if result else None
			return result



	def update_last_sync_at_from_live(self,last_sync_at,scan_table_name):
		with engine_conf.connect() as conn:
			try:
				stmt      = text(f"update {scan_table_name} set new_sync_at = (case when new_sync_at is null then '{last_sync_at}' else  new_sync_at  end), updated_sync_at = '{last_sync_at}'  where created_at < '{last_sync_at}' and (new_sync_at is null or updated_sync_at is null) ;")
				restult_1 = conn.execute(stmt)
				conn.commit()
				return "success"
			except Exception as e:
				print("Error: ",e)
				return str(e)


	def update_updated_sync_at(self,conf_id,conf_key,hall_id,updated_sync_at,scan_table_name):
		with engine_conf.connect() as conn:
			try:
				stmt      = text("update "+scan_table_name+" set updated_sync_at = '"+updated_sync_at+"' where exist_at <= '"+updated_sync_at+"';")
				restult_1 = conn.execute(stmt)
				conn.commit()
				return "success"
			except Exception as e:
				return str(e)

	def get_update_hall_scan_table_data(self,conf_id,conf_key,hall_id,scan_table_name):
		with engine_conf.connect() as conn:
			# stmt  = text("select scan_id, conf_id, hall_id, delegate_no, created_at, updated_at, deleted_at, user_id,entry_at,exist_at, exist_by_system, duration,abs_session_id,is_duplicate,sync_at from "+scan_table_name+" where sync_at is null ;")
			stmt  = text("SELECT * FROM "+scan_table_name+" where updated_sync_at is null and exist_at is not null;")
			result  = conn.execute(stmt).all()
			results = [dict(r._mapping) for r in result] if result else None
			return results

	
	def update_active_session(self,active_session_id,is_active,conf_id,hall_id,session_updated_at_local):
		with engine_conf.connect() as conn:
			try:
				stmt_bulk      = text(f"update abs_sessions set is_active = Null where conf_id = {conf_id} and hall_id = {hall_id};")
				restult_1 = conn.execute(stmt_bulk)
				if active_session_id:
					stmt_single      = text(f"update abs_sessions set is_active = {is_active},updated_at='{session_updated_at_local}' where asession_id={active_session_id} and  conf_id = {conf_id} and hall_id = {hall_id};")
				restult_2 = conn.execute(stmt_single)
				conn.commit()
				return "success"
			except Exception as e:
				print("Error: ",e)
				return str(e)


	# Used
	def insert_update_table_data_to_local(self,scan_data,scan_table_name,last_sync_at_local):
		try:
			with engine_conf.connect() as conn:
				columns_to_insert = scan_data[0].keys()
				
				insert_data = []
				stmt_update = ''	
				for data in scan_data:
					new_sync_at = datetime.strptime(data.get('new_sync_at'), "%a, %d %b %Y %H:%M:%S %Z") if data.get('new_sync_at') is not None else None
					updated_sync_at = datetime.strptime(data.get('updated_sync_at'), "%a, %d %b %Y %H:%M:%S %Z") if data.get('updated_sync_at') is not None else None
					
					data['new_sync_at'] = new_sync_at.strftime("%Y-%m-%d %H:%M:%S") if new_sync_at is not None else None
					data['updated_sync_at'] = updated_sync_at.strftime("%Y-%m-%d %H:%M:%S") if updated_sync_at is not None else None
					data['created_at'] = datetime.strptime(data.get('created_at'), "%a, %d %b %Y %H:%M:%S %Z").strftime("%Y-%m-%d %H:%M:%S")  if data.get('created_at') is not None else None
					data['entry_at'] = datetime.strptime(data.get('entry_at'), "%a, %d %b %Y %H:%M:%S %Z").strftime("%Y-%m-%d %H:%M:%S")  if data.get('entry_at') is not None else None
					data['exist_at'] = datetime.strptime(data.get('exist_at'), "%a, %d %b %Y %H:%M:%S %Z").strftime("%Y-%m-%d %H:%M:%S")  if data.get('exist_at') is not None else None
					
					if last_sync_at_local == None:
						data['scan_id']      = None
						insert_data.append(data)
					elif new_sync_at > last_sync_at_local:
						data['scan_id']      = None
						insert_data.append(data)
					elif updated_sync_at > last_sync_at_local:
						delegate_no = data["delegate_no"]
						entry_at    = data["entry_at"]
						set_values = ', '.join([ f"{key} = '{value or 'NULL'}'".replace("'NULL'", "NULL") for key, value in data.items() if key != 'scan_id'])
						stmt_update = stmt_update + f"UPDATE {scan_table_name} SET {set_values} WHERE  new_sync_at IS NOT NULL and delegate_no = '{delegate_no}' and entry_at = '{entry_at}' and exist_at is null;"
				if insert_data and len(insert_data) >0:		
					placeholders = ', '.join([':%s' % col for col in columns_to_insert])
					stmt_1 = f"INSERT INTO {scan_table_name} ({', '.join(columns_to_insert)}) VALUES ({placeholders})"
					print("stmt_1",stmt_1)
					result = conn.execute(text(stmt_1), insert_data)
					conn.commit()
				if stmt_update and stmt_update != '':
					result = conn.execute(text(stmt_update))
					conn.commit()
				# stmt   = text("select MAX(new_sync_at) as last_new_sync_at from "+scan_table_name+" where conf_id ="+str(conf_id)+" and new_sync_at is not null and updated_sync_at is  null limit 1; ")
				# result = conn.execute(stmt)
				# results = result.one_or_none()
				# return dict(results._mapping) if results else None
				return "success"
		except Exception as e:
			print("Error - insert_update_table_data_to_local : ",e)
			return str(e)
		

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists