File size: 2,956 Bytes
9afaf17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import pandas as pd
import os
from datetime import datetime
import streamlit as st

class TaskManager:
    TASKS_FILE = "tasks.csv"
    CATEGORIES = ['Learning', 'Gym', 'Personal', 'Family', 'Work', 'Prayer']

    def __init__(self):
        if 'tasks' not in st.session_state:
            st.session_state.tasks = self.load_tasks()

    def load_tasks(self):
        if os.path.exists(self.TASKS_FILE):
            if os.path.getsize(self.TASKS_FILE) > 0:
                return pd.read_csv(self.TASKS_FILE, parse_dates=['Task Time']).to_dict(orient='records')
            else:
                return []
        else:
            df = pd.DataFrame(columns=["Task Name", "Task Time", "Task Duration (hours)", "Task Duration (minutes)", "Category"])
            df.to_csv(self.TASKS_FILE, index=False)
            return []

    def save_tasks(self, tasks):
        df = pd.DataFrame(tasks)
        df.to_csv(self.TASKS_FILE, index=False)

    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
        }
        st.session_state.tasks.append(task_entry)
        self.save_tasks(st.session_state.tasks)

    def delete_task_by_name(self, task_name):
        for index, task in enumerate(st.session_state.tasks):
            if task['Task Name'] == task_name:
                st.session_state.tasks.pop(index)
                self.save_tasks(st.session_state.tasks)
                return True
        return False

    def generate_report(self, timeframe):
        df = pd.DataFrame(st.session_state.tasks)
        if df.empty:
            return pd.DataFrame()  # Return empty DataFrame if no tasks are present
        
        # Ensure the 'Task Time' is in datetime format
        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()  # Empty DataFrame for unsupported timeframes

        report['Total Duration'] = report['Task Duration (hours)'] + report['Task Duration (minutes)'] / 60.0
        return report