diff --git a/flomo/cli.py b/flomo/cli.py index 632fde9..a7b5bb6 100644 --- a/flomo/cli.py +++ b/flomo/cli.py @@ -1,4 +1,5 @@ import datetime +import sqlite3 import click import click_aliases @@ -15,16 +16,6 @@ def flomo(): pass -@flomo.command(aliases=["i"]) -def init(): - """ - Initialize the database. - """ - db = tracker.Tracker() - db.create_table() - db.conn.close() - - @flomo.command(aliases=["s"]) @click.option("-t", "--tag", default="Default", help="Session tag name.") @click.option("-n", "--name", default="Work", help="Session Name") @@ -39,5 +30,16 @@ def start(tag: str, name: str): ui.main(tag.lower(), name, session_id) +@flomo.command(aliases=["t"]) +def tracking(): + """ + Show the tracking history. + """ + try: + tracker.show_sessions() + except sqlite3.OperationalError: + print("No sessions were found.") + + if __name__ == "__main__": flomo() diff --git a/flomo/helpers.py b/flomo/helpers.py index 6ea4fc3..355704a 100644 --- a/flomo/helpers.py +++ b/flomo/helpers.py @@ -46,7 +46,7 @@ def format_time(seconds: int) -> str: return f"{hours:02}:{mins:02}:{secs:02}" -def end_session(session_id: float): +def update_session(session_id: float): db = tracker.Tracker() - db.end_session(session_id, datetime.datetime.now()) + db.update_session(session_id, datetime.datetime.now()) db.conn.close() diff --git a/flomo/tracker.py b/flomo/tracker.py index 56df02e..43a7633 100644 --- a/flomo/tracker.py +++ b/flomo/tracker.py @@ -1,6 +1,9 @@ import datetime import sqlite3 +import pandas +import tabulate + import flomo.helpers as helpers @@ -13,27 +16,29 @@ def __init__(self): def create_table(self): self.cursor.execute( - "CREATE TABLE IF NOT EXISTS sessions (id FLOAT PRIMARY KEY, tag TEXT, name TEXT, start_time TEXT, end_time TEXT, total_time TEXT)" + "CREATE TABLE IF NOT EXISTS sessions (id FLOAT PRIMARY KEY, date_time TEXT, tag TEXT, name TEXT, total_time TEXT)" ) self.conn.commit() def create_session( self, tag: str, name: str, start_time: datetime.datetime ) -> float: - session_id = start_time.timestamp() + session_id = start_time.timestamp() % 1000000 self.cursor.execute( - "INSERT INTO sessions (id, tag, name, start_time) VALUES (?, ?, ?, ?)", - (session_id, tag, name, start_time.strftime("%Y-%m-%d %H:%M:%S")), + "INSERT INTO sessions (id, date_time, tag, name) VALUES (?, ?, ?, ?)", + (session_id, start_time.strftime("%Y-%m-%d %H:%M:%S"), tag, name), ) self.conn.commit() return session_id - def end_session(self, session_id: float, end_time: datetime.datetime): - total_time = end_time - datetime.datetime.fromtimestamp(session_id) + def update_session(self, session_id: float, end_time: datetime.datetime): + date_time = self.get_session(session_id)[1] + total_time = end_time - datetime.datetime.strptime( + date_time, "%Y-%m-%d %H:%M:%S" + ) self.cursor.execute( - "UPDATE sessions SET end_time = ?, total_time = ? WHERE id = ?", + "UPDATE sessions SET total_time = ? WHERE id = ?", ( - end_time.strftime("%Y-%m-%d %H:%M:%S"), helpers.format_time(round(total_time.total_seconds())), session_id, ), @@ -47,3 +52,18 @@ def get_sessions(self): def get_session(self, session_id: float): self.cursor.execute("SELECT * FROM sessions WHERE id = ?", (session_id,)) return self.cursor.fetchone() + + +def show_sessions(): + db = Tracker() + sessions = db.get_sessions() + db.conn.close() + + print( + tabulate.tabulate( + pandas.DataFrame(sessions), # type: ignore + headers=["ID", "Session Date & Time", "Tag", "Name", "Total Time"], + tablefmt="psql", + showindex=False, + ) + ) diff --git a/flomo/ui.py b/flomo/ui.py index 847cc82..5aa320b 100644 --- a/flomo/ui.py +++ b/flomo/ui.py @@ -11,6 +11,7 @@ import flomo.helpers as helpers + class UI: def __init__( self, status: int, tag: str, name: str, chilling_time: int | None = None @@ -63,6 +64,7 @@ def get_input(self): def main(tag: str, name: str, session_id: float): + # TODO: Do something with the Terminal close issue try: while True: play_sound_thread = threading.Thread(target=helpers.play_sound, daemon=True) @@ -120,7 +122,6 @@ def main(tag: str, name: str, session_id: float): if isinstance(e, Exception): helpers.message_log(f"{datetime.datetime.now()} - Error: {e}") - if isinstance(e, KeyboardInterrupt): - helpers.end_session(session_id) finally: + helpers.update_session(session_id) sys.exit() diff --git a/requirements.txt b/requirements.txt index fa576fc..dcee28a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,26 +1,31 @@ -ansicon==1.89.0 -black==24.4.2 -blessed==1.20.0 -click==8.1.7 -click-aliases==1.0.4 -colorama==0.4.6 -flake8==7.1.0 --e git+https://github.com/Jonak-Adipta-Kalita/flomo.git@8f47f9c3cbde26194daf1dd30334ec7171b3721f#egg=flomodoro -isort==5.13.2 -jinxed==1.2.1 -markdown-it-py==3.0.0 -mccabe==0.7.0 -mdurl==0.1.2 -mypy-extensions==1.0.0 -packaging==24.1 -pathspec==0.12.1 -platformdirs==4.2.2 -playsound==1.2.2 -pycodestyle==2.12.0 -pyflakes==3.2.0 -Pygments==2.18.0 -rich==13.7.1 -setuptools==70.1.0 -six==1.16.0 -wcwidth==0.2.13 -wheel==0.43.0 +ansicon==1.89.0 +black==24.4.2 +blessed==1.20.0 +click==8.1.7 +click-aliases==1.0.4 +colorama==0.4.6 +flake8==7.1.0 +isort==5.13.2 +jinxed==1.2.1 +markdown-it-py==3.0.0 +mccabe==0.7.0 +mdurl==0.1.2 +mypy-extensions==1.0.0 +numpy==2.0.0 +packaging==24.1 +pandas==2.2.2 +pathspec==0.12.1 +platformdirs==4.2.2 +playsound==1.2.2 +pycodestyle==2.12.0 +pyflakes==3.2.0 +Pygments==2.18.0 +python-dateutil==2.9.0.post0 +pytz==2024.1 +rich==13.7.1 +setuptools==70.1.0 +six==1.16.0 +tabulate==0.9.0 +tzdata==2024.1 +wcwidth==0.2.13 +wheel==0.43.0