Files
deskmeter/dmapp/dmweb/dm.py

129 lines
3.8 KiB
Python

from datetime import datetime, timedelta
from flask import Blueprint, render_template, jsonify
from .get_period_times import get_period_totals, task_or_none, timezone, get_work_period_totals, get_current_task_info, convert_seconds, get_task_time_seconds
dmbp = Blueprint("deskmeter", __name__, url_prefix="/", template_folder="templates")
@dmbp.route("/favicon.ico")
def favicon():
return "", 204 # No Content
@dmbp.route("/")
@dmbp.route("/<string:task>")
def index(task=None):
"""
Show total time used in each desktop for today
"""
task = task_or_none(task)
start = datetime.today().replace(hour=0, minute=0, second=0, tzinfo=timezone)
end = datetime.today().replace(hour=23, minute=59, second=59, tzinfo=timezone)
rows = get_period_totals(start, end, task)
# Get current task info
current_task_id, current_task_path = get_current_task_info()
current_task_time = None
if current_task_id:
total_seconds = get_task_time_seconds(start, end, current_task_id)
if total_seconds > 0:
current_task_time = convert_seconds(total_seconds)
return render_template("main.html", rows=rows, current_task_path=current_task_path, current_task_time=current_task_time, auto_refresh=True)
@dmbp.route("/api/today")
@dmbp.route("/api/today/<string:task>")
def api_today(task=None):
"""
HTML fragment API endpoint for today's data (for AJAX updates)
"""
task = task_or_none(task)
start = datetime.today().replace(hour=0, minute=0, second=0, tzinfo=timezone)
end = datetime.today().replace(hour=23, minute=59, second=59, tzinfo=timezone)
rows = get_period_totals(start, end, task)
# Get current task info
current_task_id, current_task_path = get_current_task_info()
current_task_time = None
if current_task_id:
total_seconds = get_task_time_seconds(start, end, current_task_id)
if total_seconds > 0:
current_task_time = convert_seconds(total_seconds)
return render_template("main_content.html", rows=rows, current_task_path=current_task_path, current_task_time=current_task_time)
@dmbp.route("/day/<int:month>/<int:day>")
@dmbp.route("/day/<string:task>/<int:month>/<int:day>")
def oneday(
month,
day,
task=None,
):
task = task_or_none(task)
start = datetime(2025, month, day).replace(
hour=0, minute=0, second=0, tzinfo=timezone
)
end = datetime(2025, month, day).replace(
hour=23, minute=59, second=59, tzinfo=timezone
)
rows = get_period_totals(start, end)
return render_template("pages.html", rows=rows, auto_refresh=True)
@dmbp.route("/period/<start>/<end>")
def period(start, end):
start = datetime(*map(int, start.split("-"))).replace(
hour=0, minute=0, second=0, tzinfo=timezone
)
end = datetime(*map(int, end.split("-"))).replace(
hour=23, minute=59, second=59, tzinfo=timezone
)
rows = get_period_totals(start, end)
return render_template("pages.html", rows=rows, auto_refresh=True)
@dmbp.route("/work")
def work():
"""
Show total time used per work project for today
"""
start = datetime.today().replace(hour=0, minute=0, second=0, tzinfo=timezone)
end = datetime.today().replace(hour=23, minute=59, second=59, tzinfo=timezone)
rows = get_work_period_totals(start, end)
return render_template("main.html", rows=rows, auto_refresh=False)
@dmbp.route("/totals")
@dmbp.route("/totals/<string:task>")
def totals(task=None):
"""
Show total time used in each desktop for all time
"""
task = task_or_none(task)
start = datetime(2020, 1, 1).replace(hour=0, minute=0, second=0, tzinfo=timezone)
end = datetime(2030, 1, 1).replace(hour=23, minute=59, second=59, tzinfo=timezone)
rows = get_period_totals(start, end)
return render_template("pages.html", rows=rows, auto_refresh=True)