work-tracing/app/utils/formatting.py
2025-05-16 17:55:30 +04:00

97 lines
3.0 KiB
Python

"""
Data formatting functions for employee workstation activity tracking.
This module contains functions for formatting database query results into API responses.
"""
from flask import current_app
def format_report_data(results, time_period):
"""
Formats the raw database results into a list of dictionaries for the API.
Assumes the input `results` are already correctly aggregated by user and period from the SQL query.
Args:
results (list): List of database result rows (SQLAlchemy RowMapping objects)
time_period (str): Time period of the report ('daily', 'weekly', or 'monthly')
Returns:
list: List of formatted dictionaries for API response
"""
current_app.logger.debug(
f"Formatting report data for period: {time_period}. Input rows: {len(results)}"
)
period_key_map = {"daily": "day", "weekly": "week_start", "monthly": "month_start"}
period_key = period_key_map.get(time_period, "period_start")
formatted_data = []
for row in results:
period_value = row["period_start"]
if hasattr(period_value, "isoformat"):
period_value = period_value.isoformat()
first_login_time = row["first_login_time"]
if hasattr(first_login_time, "isoformat"):
first_login_time = first_login_time.isoformat()
duration_hours = row["total_hours"]
if duration_hours is None:
duration_hours = 0.0
else:
duration_hours = float(duration_hours)
formatted_data.append(
{
"user": row["user"],
period_key: period_value,
"duration_hours": duration_hours,
"first_login_time": first_login_time,
}
)
current_app.logger.debug(
f"Formatted report data created. Output rows: {len(formatted_data)}"
)
return formatted_data
def format_user_activity(results):
"""
Formats the raw user activity results into a list of dictionaries.
Args:
results (list): List of database result rows (SQLAlchemy RowMapping objects)
Returns:
list: List of formatted user activity dictionaries
"""
formatted_data = []
for row in results:
start_time = row["start_time"]
end_time = row["end_time"]
# Format timestamps for display
if hasattr(start_time, "isoformat"):
start_time = start_time.isoformat()
if hasattr(end_time, "isoformat"):
end_time = end_time.isoformat()
# Format duration as float
duration = (
float(row["session_duration_hours"])
if row["session_duration_hours"] is not None
else 0.0
)
formatted_data.append(
{
"date": row["work_date"].isoformat()
if hasattr(row["work_date"], "isoformat")
else str(row["work_date"]),
"start_time": start_time,
"end_time": end_time,
"duration_hours": round(duration, 2),
}
)
return formatted_data