#!/usr/bin/env python3 import sys import os sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) import cx_Oracle from datetime import datetime ORACLE_LOCAL_USER = "local123" ORACLE_LOCAL_PASSWORD = "local123" ORACLE_LOCAL_DSN = "127.0.0.1:1521/XEPDB1" ORACLE_REMOTE_USER = "FREDERICOAC" ORACLE_REMOTE_PASSWORD = "FREDEricoac" ORACLE_REMOTE_DSN = "oracledhtsrv02.hom.capes.gov.br:1521/hom_dr" def calcular_componente_b(coordenacoes): if not coordenacoes: return 0 base = 70 anos_totais = 0 for c in coordenacoes: inicio = c[7] fim = c[8] if fim: anos = (fim - inicio).days // 365 else: anos = (datetime.now() - inicio).days // 365 anos_totais += anos tempo = min(int(anos_totais * 5), 50) programas_distintos = len(set(c[1] for c in coordenacoes)) extras = min((programas_distintos - 1) * 20, 40) if programas_distintos > 1 else 0 maior_nota = 0 for c in coordenacoes: nota_str = str(c[4]).strip() if c[4] else "" if nota_str == '7': maior_nota = max(maior_nota, 7) elif nota_str == '6': maior_nota = max(maior_nota, 6) elif nota_str == '5': maior_nota = max(maior_nota, 5) elif nota_str == '4': maior_nota = max(maior_nota, 4) elif nota_str == '3': maior_nota = max(maior_nota, 3) mapa_nota = {7: 20, 6: 15, 5: 10, 4: 5, 3: 0} bonus = mapa_nota.get(maior_nota, 0) return base + tempo + extras + bonus print("Conectando Oracle LOCAL...") conn_local = cx_Oracle.connect(ORACLE_LOCAL_USER, ORACLE_LOCAL_PASSWORD, ORACLE_LOCAL_DSN) print("Conectando Oracle REMOTO...") conn_remote = cx_Oracle.connect(ORACLE_REMOTE_USER, ORACLE_REMOTE_PASSWORD, ORACLE_REMOTE_DSN) cursor_local = conn_local.cursor() cursor_remote = conn_remote.cursor() print("Buscando consultores...") cursor_local.execute("SELECT ID_PESSOA FROM TB_RANKING_CONSULTOR") ids_pessoas = [row[0] for row in cursor_local.fetchall()] print(f"Total: {len(ids_pessoas)} consultores") processados = 0 com_ppg = 0 batch_updates = [] for id_pessoa in ids_pessoas: try: cursor_remote.execute(""" SELECT c.ID_PESSOA, c.ID_PROGRAMA_SNPG, p.NM_PROGRAMA, p.CD_PROGRAMA_PPG, p.DS_CONCEITO, p.NM_PROGRAMA_MODALIDADE, aa.NM_AREA_AVALIACAO, c.DT_INICIO_VIGENCIA, c.DT_FIM_VIGENCIA FROM SUCUPIRA_PAINEL.VM_COORDENADOR c INNER JOIN SUCUPIRA_PAINEL.VM_PROGRAMA_SUCUPIRA p ON c.ID_PROGRAMA_SNPG = p.ID_PROGRAMA LEFT JOIN SUCUPIRA_PAINEL.VM_AREA_CONHECIMENTO ac ON p.ID_AREA_CONHECIMENTO_ATUAL = ac.ID_AREA_CONHECIMENTO LEFT JOIN SUCUPIRA_PAINEL.VM_AREA_AVALIACAO aa ON ac.ID_AREA_AVALIACAO = aa.ID_AREA_AVALIACAO WHERE c.ID_PESSOA = :id_pessoa """, {"id_pessoa": id_pessoa}) coordenacoes = cursor_remote.fetchall() if coordenacoes: comp_b = calcular_componente_b(coordenacoes) batch_updates.append((comp_b, id_pessoa)) com_ppg += 1 processados += 1 if len(batch_updates) >= 1000: cursor_local.executemany( "UPDATE TB_RANKING_CONSULTOR SET COMPONENTE_B = :comp_b, PONTUACAO_TOTAL = COMPONENTE_A + :comp_b + COMPONENTE_C + COMPONENTE_D WHERE ID_PESSOA = :id_pessoa", batch_updates ) conn_local.commit() print(f"Processados: {processados}/{len(ids_pessoas)} | Com PPG: {com_ppg}") batch_updates = [] except Exception as e: print(f"Erro no consultor {id_pessoa}: {e}") if batch_updates: cursor_local.executemany( "UPDATE TB_RANKING_CONSULTOR SET COMPONENTE_B = :comp_b, PONTUACAO_TOTAL = COMPONENTE_A + :comp_b + COMPONENTE_C + COMPONENTE_D WHERE ID_PESSOA = :id_pessoa", batch_updates ) conn_local.commit() print(f"\nFinalizado!") print(f"Total processados: {processados}") print(f"Com PPG: {com_ppg}") print("\nAtualizando posições...") cursor_local.callproc("SP_ATUALIZAR_POSICOES") conn_local.commit() cursor_local.close() cursor_remote.close() conn_local.close() conn_remote.close() print("Concluído!")