-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$ | ||
""", | ||
) |
35 changes: 35 additions & 0 deletions
35
api/migrations/versions/cd28ca502f89_add_function_for_project_summaries.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ### |