Skip to content

Commit

Permalink
db: Add function to get list of project work for summary
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dmtrek14 committed Jan 23, 2024
1 parent c7a261f commit 925c9b3
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
74 changes: 74 additions & 0 deletions api/db/functions/get_user_project_summaries.py
Original file line number Diff line number Diff line change
@@ -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$
""",
)
Original file line number Diff line number Diff line change
@@ -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 ###

0 comments on commit 925c9b3

Please sign in to comment.