From 925c9b354d4ffad1044bd285ddbf39aa34a4a6ff Mon Sep 17 00:00:00 2001 From: Danielle Mayabb Date: Mon, 22 Jan 2024 09:17:50 -0800 Subject: [PATCH] db: Add function to get list of project work for summary This function breaks out work by project daily and weekly for a user. This will be used to fetch the breakdown for display in the work summary panel. --- .../functions/get_user_project_summaries.py | 74 +++++++++++++++++++ ...2f89_add_function_for_project_summaries.py | 35 +++++++++ 2 files changed, 109 insertions(+) create mode 100644 api/db/functions/get_user_project_summaries.py create mode 100644 api/migrations/versions/cd28ca502f89_add_function_for_project_summaries.py diff --git a/api/db/functions/get_user_project_summaries.py b/api/db/functions/get_user_project_summaries.py new file mode 100644 index 000000000..778218b27 --- /dev/null +++ b/api/db/functions/get_user_project_summaries.py @@ -0,0 +1,74 @@ +from alembic_utils.pg_function import PGFunction + + +get_user_project_summaries = PGFunction( + schema="public", + signature="get_user_project_summaries(user_id integer, current date)", + definition=""" + RETURNS TABLE ( + project_id INT, + project VARCHAR, + today_total INT, + today_text VARCHAR, + week_total INT, + week_text VARCHAR, + is_vacation BOOLEAN + ) + LANGUAGE plpgsql + AS $function$ + DECLARE vacation_project_id INT := (SELECT vacation_project_id from config); + + BEGIN + RETURN QUERY + with today as ( + select project.id as project_id, project.description, + sum(task.task_total_minutes) as total + from public.task + inner join public.project + on public.task.projectid = public.project.id + where public.task._date = current + and public.task.usrid = user_id + group by project.id + ), + week as ( + select project.id as project_id, project.description, + sum(task.task_total_minutes) as total + from public.task + inner join public.project + on public.task.projectid = public.project.id + where public.task._date between date_trunc('week', current) + and (date_trunc('week', current) + interval '6 days') + and public.task.usrid = user_id + group by project.id + ) + select + week.project_id, + week.description as project, + today.total::int as today_total, + case + when today.total is not null then + cast( + concat( + extract(hour from make_interval(mins:=cast(today.total as int))), 'h ', + extract(minutes from make_interval(mins:=cast(today.total as int))), 'm') + as varchar) + end as today_text, + week.total::int as week_total, + case + when week.total is not null then + cast( + concat( + extract(hour from make_interval(mins:=cast(week.total as int))), 'h ', + extract(minutes from make_interval(mins:=cast(week.total as int))), 'm') + as varchar) + end as week_text, + case + when week.project_id = vacation_project_id + then (select true) + else (select false) + end as is_vacation + from week + left join today on week.project_id = today.project_id; + END; $function$ + """, +) diff --git a/api/migrations/versions/cd28ca502f89_add_function_for_project_summaries.py b/api/migrations/versions/cd28ca502f89_add_function_for_project_summaries.py new file mode 100644 index 000000000..acc2be60c --- /dev/null +++ b/api/migrations/versions/cd28ca502f89_add_function_for_project_summaries.py @@ -0,0 +1,35 @@ +"""Add function for project summaries + +Revision ID: cd28ca502f89 +Revises: 2a20fa1bde50 +Create Date: 2024-01-09 11:56:53.889112 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql +from alembic_utils.pg_function import PGFunction +from db.functions.get_user_project_summaries import get_user_project_summaries + +# revision identifiers, used by Alembic. +revision = 'cd28ca502f89' +down_revision = '2a20fa1bde50' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_entity(get_user_project_summaries) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + get_user_project_summaries = PGFunction( + schema="public", + signature="get_user_project_summaries(user_id int, current date)", + definition="# " + ) + op.drop_entity(get_user_project_summaries) + # ### end Alembic commands ###