Skip to content

Commit

Permalink
Merge pull request #54 from Jonak-Adipta-Kalita/main
Browse files Browse the repository at this point in the history
visualizing tracking data - 1
  • Loading branch information
Jonak-Adipta-Kalita authored Jul 14, 2024
2 parents a271897 + bb845eb commit d607c7d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 48 deletions.
22 changes: 12 additions & 10 deletions flomo/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import sqlite3

import click
import click_aliases
Expand All @@ -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")
Expand All @@ -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()
4 changes: 2 additions & 2 deletions flomo/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
36 changes: 28 additions & 8 deletions flomo/tracker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime
import sqlite3

import pandas
import tabulate

import flomo.helpers as helpers


Expand All @@ -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,
),
Expand All @@ -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,
)
)
5 changes: 3 additions & 2 deletions flomo/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
57 changes: 31 additions & 26 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d607c7d

Please sign in to comment.