# components/task_operations.py import sqlite3 import pandas as pd from datetime import datetime import streamlit as st class TaskManager: DB_FILE = "tasks.db" CATEGORIES = ['Learning', 'Gym', 'Personal', 'Family', 'Work', 'Prayer'] def __init__(self): self.conn = sqlite3.connect(self.DB_FILE) self.create_table() def create_table(self): # Create tasks table with unique id query = """ CREATE TABLE IF NOT EXISTS tasks ( id INTEGER PRIMARY KEY AUTOINCREMENT, task_name TEXT, task_time TEXT, task_duration_hours INTEGER, task_duration_minutes INTEGER, category TEXT ); """ self.conn.execute(query) self.conn.commit() def load_tasks(self): # Load tasks from the database query = "SELECT * FROM tasks;" df = pd.read_sql_query(query, self.conn) if not df.empty: # Ensure 'Task Time' is in datetime format and id is an integer df['Task Time'] = pd.to_datetime(df['task_time']) # Convert task_time to datetime df['id'] = df['id'].astype(int) # Ensure id is an integer df = df.rename(columns={ 'task_name': 'Task Name', 'task_time': 'Task Time', 'task_duration_hours': 'Task Duration (hours)', 'task_duration_minutes': 'Task Duration (minutes)', 'category': 'Category', 'id': 'Task ID' # Rename id to 'Task ID' }) return df.to_dict(orient='records') else: return [] def save_task(self, task): # Save task into the database query = """ INSERT INTO tasks (task_name, task_time, task_duration_hours, task_duration_minutes, category) VALUES (?, ?, ?, ?, ?); """ self.conn.execute(query, (task['Task Name'], task['Task Time'], task['Task Duration (hours)'], task['Task Duration (minutes)'], task['Category'])) self.conn.commit() def add_task(self, task_name, task_time, task_duration_hours, task_duration_minutes, task_category): task_time_full = datetime.combine(datetime.today(), task_time) task_entry = { "Task Name": task_name, "Task Time": task_time_full, "Task Duration (hours)": int(task_duration_hours), "Task Duration (minutes)": int(task_duration_minutes), "Category": task_category } self.save_task(task_entry) st.session_state.tasks.append(task_entry) def delete_task_by_id(self, task_id): # Delete task by its ID query = "DELETE FROM tasks WHERE id = ?;" self.conn.execute(query, (task_id,)) self.conn.commit() return True def generate_report(self, timeframe): df = pd.DataFrame(self.load_tasks()) if df.empty: return pd.DataFrame() df['Task Time'] = pd.to_datetime(df['Task Time']) if timeframe == 'daily': report = df[df['Task Time'].dt.date == pd.Timestamp.today().date()] elif timeframe == 'weekly': week_start = pd.Timestamp.today() - pd.DateOffset(days=pd.Timestamp.today().dayofweek) report = df[(df['Task Time'] >= week_start) & (df['Task Time'] < pd.Timestamp.today() + pd.DateOffset(days=1))] elif timeframe == 'monthly': report = df[df['Task Time'].dt.month == pd.Timestamp.today().month] elif timeframe == 'yearly': report = df[df['Task Time'].dt.year == pd.Timestamp.today().year] else: report = pd.DataFrame() report['Total Duration'] = report['Task Duration (hours)'] + report['Task Duration (minutes)'] / 60.0 return report