diff --git a/.gitignore b/.gitignore index 613a8f5..97db62e 100644 --- a/.gitignore +++ b/.gitignore @@ -131,4 +131,5 @@ dist .pnp.* chowkidaar -./bot/__pycache__ \ No newline at end of file +./bot/__pycache__ +list.csv \ No newline at end of file diff --git a/__pycache__/config.cpython-310.pyc b/__pycache__/config.cpython-310.pyc index 09fafaf..bcde95f 100644 Binary files a/__pycache__/config.cpython-310.pyc and b/__pycache__/config.cpython-310.pyc differ diff --git a/__pycache__/db.cpython-310.pyc b/__pycache__/db.cpython-310.pyc index 34c2100..7764fa1 100644 Binary files a/__pycache__/db.cpython-310.pyc and b/__pycache__/db.cpython-310.pyc differ diff --git a/__pycache__/dfa.cpython-310.pyc b/__pycache__/dfa.cpython-310.pyc index 76c097d..325714f 100644 Binary files a/__pycache__/dfa.cpython-310.pyc and b/__pycache__/dfa.cpython-310.pyc differ diff --git a/__pycache__/parse_message.cpython-310.pyc b/__pycache__/parse_message.cpython-310.pyc index 5ca64a2..262d9a0 100644 Binary files a/__pycache__/parse_message.cpython-310.pyc and b/__pycache__/parse_message.cpython-310.pyc differ diff --git a/__pycache__/time_check.cpython-310.pyc b/__pycache__/time_check.cpython-310.pyc index 8685fec..019a845 100644 Binary files a/__pycache__/time_check.cpython-310.pyc and b/__pycache__/time_check.cpython-310.pyc differ diff --git a/main.py b/main.py index ecc67b1..5a90c75 100644 --- a/main.py +++ b/main.py @@ -7,7 +7,7 @@ from db import connect_to_database, save_log, check_intext_validity, update_log, delete_log import os from time_check import can_send_message, is_in_time_bracket - +from prometheus_client import Counter , start_http_server intents = discord.Intents.default() intents.messages = True # Ensure the bot can read messages @@ -17,11 +17,16 @@ bot.activity = discord.Activity(type=discord.ActivityType.watching, name="for message updates") +messages_sent_total = Counter('discord_messages_sent_total', 'Total number of messages sent') +messages_edited_total = Counter('discord_messages_edited_total', 'Total number of messages edited') +messages_deleted_total = Counter('discord_messages_deleted_total', 'Total number of messages deleted') + +start_http_server(8000) + @bot.event async def on_ready(): print(f"Bot is ready. Logged in as {bot.user}") - @bot.event async def on_message(message): if message.author == bot.user: @@ -53,6 +58,7 @@ async def on_message(message): except Exception as e: print(f"Error saving message to database: {e}") + messages_sent_total.inc() await bot.process_commands(message) @bot.event @@ -78,6 +84,7 @@ async def on_message_edit(old_message, new_message): await new_message.add_reaction("👀") except Exception as e: print(f"Error updating message in database: {e}") + messages_edited_total.inc() await bot.process_commands(new_message) @bot.event @@ -92,7 +99,8 @@ async def on_message_delete(message): print(f"Message with ID {discord_message_id} was marked deleted.") except Exception as e: print(f"Error deleting message from database: {e}") - + + messages_deleted_total.inc() await bot.process_commands(message) diff --git a/shifter_bulk.py b/shifter_bulk.py new file mode 100644 index 0000000..4ff2d3b --- /dev/null +++ b/shifter_bulk.py @@ -0,0 +1,66 @@ +import pandas as pd +import psycopg2 +from psycopg2 import Error +from config import DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST +from db import connect_to_database + +def upload_csv_to_pg(csv_file, table_name): + try: + # Connect to the PostgreSQL database + conn = connect_to_database() + + # Create a cursor object using the connection + cursor = conn.cursor() + + create_table_query = """ + CREATE TABLE IF NOT EXISTS student_list_2023 ( + sl INT PRIMARY KEY, + student_id VARCHAR(10) NOT NULL, + name VARCHAR(255) NOT NULL, + gender VARCHAR(10) NOT NULL, + branch VARCHAR(50) NOT NULL + ); + """ + + try: + cursor.execute(create_table_query) + conn.commit() + print("Table created successfully!") + except Exception as e: + print("Error:", e) + conn.rollback() + + # Read the CSV file into a pandas DataFrame + df = pd.read_csv(csv_file) + + # Convert DataFrame to list of tuples + data = [tuple(row) for row in df.to_numpy()] + + # Generate the SQL INSERT statement + cols = ', '.join(df.columns) + placeholders = ', '.join(['%s'] * len(df.columns)) + sql = f"INSERT INTO {table_name} ({cols}) VALUES ({placeholders})" + + # Execute the SQL statement to insert data into the table + cursor.executemany(sql, data) + + # Commit the transaction + conn.commit() + + print("Data uploaded successfully") + + except (Exception, Error) as e: + print(f"Error uploading data to PostgreSQL: {e}") + + finally: + # Close the cursor and connection + if conn: + cursor.close() + conn.close() + + +csv_file = "./list.csv" + +table_name = "student_list_2023" + +upload_csv_to_pg(csv_file, table_name)