Files
Faircot_cron_script/faircot.py
2026-02-10 19:12:50 +00:00

94 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pandas as pd
import pyodbc
from sqlalchemy import create_engine, text, inspect
import re
import random
import string
# Connexion SQL Server
sql_server_conn_str = (
"DRIVER={ODBC Driver 18 for SQL Server};"
"SERVER=VPS88.DATACENTER.CSTI;"
"DATABASE=FAIRCOT_RC;"
"UID=SINGA_META;"
"PWD=Start.123;"
"TrustServerCertificate=yes;"
)
sql_conn = pyodbc.connect(sql_server_conn_str)
# Lire toute la vue
query = "SELECT * FROM [SINGA_VW_EXECUTION_FOLLOW_UP_BY_AGENT]"
df = pd.read_sql(query, sql_conn)
print("Colonnes chargées depuis la vue :")
print(df.columns.tolist())
# Connexion PostgreSQL
pg_user = "metabase"
pg_password = "dsproject"
pg_host = "localhost"
pg_port = "5432"
pg_db = "faircot"
pg_conn_str = f"postgresql://{pg_user}:{pg_password}@{pg_host}:{pg_port}/{pg_db}"
pg_engine = create_engine(pg_conn_str)
# Nettoyage des noms
def normalize_name(name):
return re.sub(r'\W+', '_', name.strip().lower())
# Génération d'un mot de passe fort (utile si tu crées les users)
def generate_password(length=12):
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
# Obtenir la liste unique des agents et clients
agents = df['Agent'].dropna().unique()
clients = df['Customer'].dropna().unique()
inspector = inspect(pg_engine)
# Connexion brute pour exécuter DELETE/TRUNCATE
with pg_engine.connect() as conn:
trans = conn.begin() # démarrer transaction
try:
for agent in agents:
norm_name = normalize_name(str(agent))
table_name = f"agent_{norm_name}"
agent_df = df[df['Agent'] == agent].copy()
if table_name in inspector.get_table_names():
# La table existe → on la vide puis on recharge
conn.execute(text(f'DELETE FROM "{table_name}";'))
trans.commit() # valider le DELETE
trans = conn.begin() # redémarrer transaction
agent_df.to_sql(table_name, pg_engine, if_exists="append", index=False)
print(f"♻️ Table mise à jour : {table_name} ({len(agent_df)} lignes)")
else:
# La table nexiste pas → on la crée
agent_df.to_sql(table_name, pg_engine, if_exists="replace", index=False)
print(f"✅ Table créée : {table_name} ({len(agent_df)} lignes)")
for client in clients:
norm_name = normalize_name(str(client))
table_name = f"customer_{norm_name}"
client_df = df[df['Customer'] == client].copy()
if table_name in inspector.get_table_names():
conn.execute(text(f'DELETE FROM "{table_name}";'))
trans.commit() # valider le DELETE
trans = conn.begin()
client_df.to_sql(table_name, pg_engine, if_exists="append", index=False)
print(f"♻️ Table mise à jour : {table_name} ({len(client_df)} lignes)")
else:
client_df.to_sql(table_name, pg_engine, if_exists="replace", index=False)
print(f"✅ Table créée : {table_name} ({len(client_df)} lignes)")
trans.commit()
except Exception as e:
trans.rollback()
print("⚠️ Erreur :", e)
print("\n🎉 Transfert terminé (tables créées ou mises à jour, droits conservés).")