Logs_extractor/tytocare_logs_extractor.py
2025-03-30 13:22:49 +08:00

116 lines
5.6 KiB
Python

import os
import pandas as pd
import pyodbc
import shutil
import time
from datetime import datetime
import glob
import warnings
# Constants
SERVER = '192.168.32.10'
DATABASE = 'MES2'
USERNAME = 'mcats'
PASSWORD = 'mdogs'
SOURCE_DIR = 'C:/Users/marcielo.abalos/Desktop/source/'
DESTINATION_DIR = 'C:/Users/marcielo.abalos/Desktop/destination/'
# Global cursor and connection variables
cursor = None
conn = None
# Database connection
def connect_to_database():
global cursor, conn
try:
conn = pyodbc.connect(f'DRIVER=SQL Server;SERVER={SERVER};DATABASE={DATABASE};UID={USERNAME};PWD={PASSWORD}')
cursor = conn.cursor()
except pyodbc.Error as e:
print(f"Database connection error: {e}")
def extract_data_from_file(file_path):
try:
http_lines = []
enddate = []
# Capture and print lines that start with "http://"
with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
for line in file:
if line.startswith('http://192.168.201.59/TytocareWS/TytocareWS.asmx/InsertTestResult'):
http_lines.append(line.strip())
elif line.startswith('END TIME'):
enddate.append(line.strip())
return http_lines, enddate
except Exception as e:
print(f"Error occurred while reading file {file_path}: {e}")
return None, None
def main_loop():
try:
global cursor
if cursor:
# Get all files in the source directory
files = glob.glob(SOURCE_DIR + '*.txt')
if files:
for file in files:
http_lines, enddate = extract_data_from_file(file)
if http_lines is not None:
for line in http_lines:
logfile_data = line.split('?')[1]
split_logfile_data = logfile_data.split('&')
cardno = split_logfile_data[0].split('=')[1]
stage = split_logfile_data[3].split('=')[1]
machine = split_logfile_data[4].split('=')[1]
lastupdatedby = split_logfile_data[5].split('=')[1]
status = split_logfile_data[6].split('=')[1]
symptoms = split_logfile_data[7].split('=')[1]
if status == 'PASSED':
database_status = 'GOOD'
else:
database_status = 'REJECT'
if len(enddate) > 0:
enddate_str = enddate[0].split(': ')[1]
test_date_end = datetime.strptime(enddate_str, "%m-%d-%Y %H:%M:%S:%f")
new_date_str_end = test_date_end.strftime('%Y-%m-%d %H:%M:%S.%f')
log_pass_lastupdate = str(new_date_str_end[:-3])
else:
log_pass_lastupdate = None
# Insert data into the database
insert_logpass = "INSERT INTO dbo.log_pass (account, cardno, line, sequence, station, machine, status, lastupdate, lastupdatedby) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
cursor.execute(insert_logpass, ('G5', cardno + '_1', '', '', stage, machine, database_status, log_pass_lastupdate, 'cats'))
conn.commit()
print('History inserted successfully')
# Insert data into log_repair if failed
if status == 'FAILED':
check_defect = pd.read_sql_query(f"SELECT defectCode FROM dbo.defectProd2 WHERE description = '{symptoms}'", conn)
defect_code = check_defect.iloc[0]['defectCode']
insert_log_repair = "INSERT INTO dbo.log_repair (account, cardno, stage, machine, category, defect, location, details, status, addinfo, serialAffected, component, remarks, rejectDate, rejectUser, repairDate, repairUser, lastupdate, lastupdatedby) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
cursor.execute(insert_log_repair, ('G5', cardno + "_1", stage, machine, "Testing Defects", defect_code, "", symptoms, "0", "", "", "", "", log_pass_lastupdate, lastupdatedby, "", "", log_pass_lastupdate, lastupdatedby))
conn.commit()
print('Reject Details inserted successfully')
shutil.move(file, DESTINATION_DIR + os.path.basename(file))
print('File Moved Successfully')
else:
print('No .txt files in the directory found!')
else:
print('connection failed')
except Exception as e:
print(f"An error occurred: {e}")
# Run the loop continuously
if __name__ == "__main__":
while True:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
connect_to_database()
main_loop()
time.sleep(10)