Skip to content

Commit

Permalink
chore: Schedules database clean up (M2-8495) (#1723)
Browse files Browse the repository at this point in the history
This PR implements the following cleanup operations:
- Adds four new columns to the `events` table:
    - `activity_id`: taken from the `activity_events` table
    - `activity_flow_id`: taken from the `flow_events` table
    - `user_id`: taken from the `user_events` table
    - `event_type`: Enum with value `activity` or `flow`
- Removes the `periodicity_id` column from the `events` table
- Migrates all the data from the `activity_events`, `flow_events`, and `user_events` tables into the `events` table
- Drops the `activity_events`, `flow_events`, `user_events`, and `periodicity` tables
- Updates the API code to account for these changes, which mostly included rewriting queries to select only from the `events` table where multiple joins (and sometimes multiple queries) were previously being done

These changes are intended to be 100% backwards compatible and should introduce no new features from the client perspective
  • Loading branch information
sultanofcardio authored Feb 20, 2025
1 parent 734bd1f commit 11571c7
Show file tree
Hide file tree
Showing 10 changed files with 514 additions and 906 deletions.
7 changes: 4 additions & 3 deletions src/apps/activities/services/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
from apps.activity_assignments.service import ActivityAssignmentService
from apps.activity_flows.crud import FlowsCRUD
from apps.applets.crud import AppletsCRUD, UserAppletAccessCRUD
from apps.schedule.crud.events import ActivityEventsCRUD, EventCRUD
from apps.schedule.crud.events import EventCRUD
from apps.schedule.domain.constants import EventType
from apps.schedule.service.schedule import ScheduleService
from apps.workspaces.domain.constants import Role
from infrastructure.logger import logger
Expand Down Expand Up @@ -115,9 +116,9 @@ async def update_create(self, applet_id: uuid.UUID, activities_create: list[Acti
activity_id_key_map: dict[uuid.UUID, uuid.UUID] = dict()
prepared_activity_items = list()

all_activities = await ActivityEventsCRUD(self.session).get_by_applet_id(applet_id)
activity_events = await EventCRUD(self.session).get_by_type_and_applet_id(applet_id, EventType.ACTIVITY)

all_activity_ids = [activity.activity_id for activity in all_activities]
all_activity_ids = [activity.activity_id for activity in activity_events if activity.activity_id is not None]

# Save new activity ids
new_activities = []
Expand Down
7 changes: 5 additions & 2 deletions src/apps/activity_flows/service/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from apps.activity_flows.service.flow_item import FlowItemService
from apps.applets.crud import UserAppletAccessCRUD
from apps.applets.domain.applet_history import Version
from apps.schedule.crud.events import EventCRUD, FlowEventsCRUD
from apps.schedule.crud.events import EventCRUD
from apps.schedule.domain.constants import EventType
from apps.schedule.service.schedule import ScheduleService
from apps.workspaces.domain.constants import Role

Expand Down Expand Up @@ -89,7 +90,9 @@ async def update_create(
schemas = list()
prepared_flow_items = list()

all_flows = [flow.flow_id for flow in await FlowEventsCRUD(self.session).get_by_applet_id(applet_id)]
flow_events = await EventCRUD(self.session).get_by_type_and_applet_id(applet_id, EventType.FLOW)

all_flows = [flow_event.activity_flow_id for flow_event in flow_events if flow_event.activity_flow_id]

# Save new flow ids
new_flows = []
Expand Down
78 changes: 28 additions & 50 deletions src/apps/applets/commands/applet_ema.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@
from apps.job.constants import JobStatus
from apps.job.errors import JobStatusError
from apps.job.service import JobService
from apps.schedule.db.schemas import (
ActivityEventsSchema,
EventSchema,
FlowEventsSchema,
PeriodicitySchema,
UserEventsSchema,
)
from apps.schedule.db.schemas import EventSchema
from apps.schedule.domain.constants import PeriodicityType
from apps.shared.domain.base import PublicModel
from apps.subjects.db.schemas import SubjectSchema
Expand Down Expand Up @@ -239,27 +233,24 @@ async def get_user_flow_events(
select(
EventSchema.applet_id,
EventSchema.id.label("event_id"),
UserEventsSchema.user_id,
FlowEventsSchema.flow_id,
PeriodicitySchema.type.label("event_type"),
EventSchema.user_id,
EventSchema.activity_flow_id.label("flow_id"),
EventSchema.periodicity.label("event_type"),
case(
(
PeriodicitySchema.type.in_(("WEEKDAYS", "DAILY")),
EventSchema.periodicity.in_(("WEEKDAYS", "DAILY")),
scheduled_date,
),
(PeriodicitySchema.type.in_(("WEEKLY", "MONTHLY")), PeriodicitySchema.start_date),
else_=PeriodicitySchema.selected_date,
(EventSchema.periodicity.in_(("WEEKLY", "MONTHLY")), EventSchema.start_date),
else_=EventSchema.selected_date,
).label("selected_date"),
PeriodicitySchema.start_date,
PeriodicitySchema.end_date,
EventSchema.start_date,
EventSchema.end_date,
EventSchema.start_time,
EventSchema.end_time,
)
.select_from(EventSchema)
.join(UserEventsSchema, UserEventsSchema.event_id == EventSchema.id)
.join(PeriodicitySchema, PeriodicitySchema.id == EventSchema.periodicity_id)
.join(FlowEventsSchema, FlowEventsSchema.event_id == EventSchema.id)
.where(EventSchema.is_deleted == false(), PeriodicitySchema.type != PeriodicityType.ALWAYS)
.where(EventSchema.is_deleted == false(), EventSchema.periodicity != PeriodicityType.ALWAYS)
).cte("user_flow_events")

query = (
Expand Down Expand Up @@ -327,26 +318,22 @@ def filter_events(raw_events_rows: list[TRawRow], schedule_date: datetime.date)
case PeriodicityType.DAILY:
if row.is_crossday_event:
row.end_date += datetime.timedelta(days=1)
if schedule_date >= row.start_date and schedule_date <= row.end_date:
if row.start_date <= schedule_date <= row.end_date:
filtered.append(row)
case PeriodicityType.ONCE:
schedule_start_date = row.selected_date
row.end_date = row.selected_date
if row.is_crossday_event:
row.end_date += datetime.timedelta(days=1)
if schedule_date >= schedule_start_date and schedule_date <= row.end_date:
if schedule_start_date <= schedule_date <= row.end_date:
filtered.append(row)
case PeriodicityType.WEEKDAYS:
last_weekday = FRIDAY_WEEKDAY
if row.is_crossday_event:
last_weekday = SATURDAY_WEEKDAY
if row.end_date.weekday() == FRIDAY_WEEKDAY:
row.end_date += datetime.timedelta(days=1)
if (
schedule_date.weekday() <= last_weekday
and schedule_date >= row.start_date
and schedule_date <= row.end_date
):
if schedule_date.weekday() <= last_weekday and row.start_date <= schedule_date <= row.end_date:
filtered.append(row)
case PeriodicityType.WEEKLY:
scheduled_weekday = row.start_date.weekday()
Expand All @@ -362,10 +349,8 @@ def filter_events(raw_events_rows: list[TRawRow], schedule_date: datetime.date)
if row.start_date.weekday() == row.end_date.weekday():
row.end_date += datetime.timedelta(days=1)
if (
(schedule_date.weekday() == scheduled_weekday or schedule_date.weekday() == following_weekday)
and schedule_date >= row.start_date
and schedule_date <= row.end_date
):
schedule_date.weekday() == scheduled_weekday or schedule_date.weekday() == following_weekday
) and row.start_date <= schedule_date <= row.end_date:
filtered.append(row)
case PeriodicityType.MONTHLY:
scheduled_monthday = row.start_date.day
Expand All @@ -382,14 +367,10 @@ def filter_events(raw_events_rows: list[TRawRow], schedule_date: datetime.date)
):
row.end_date += datetime.timedelta(days=1)
if (
(
schedule_date.day == scheduled_monthday
or schedule_date.day == following_monthday
or (is_last_day_of_month(schedule_date) and row.start_date)
)
and schedule_date >= row.start_date
and schedule_date <= row.end_date
):
schedule_date.day == scheduled_monthday
or schedule_date.day == following_monthday
or (is_last_day_of_month(schedule_date) and row.start_date)
) and row.start_date <= schedule_date <= row.end_date:
filtered.append(row)
return filtered

Expand Down Expand Up @@ -505,27 +486,24 @@ async def get_user_activity_events(
select(
EventSchema.applet_id,
EventSchema.id.label("event_id"),
UserEventsSchema.user_id,
ActivityEventsSchema.activity_id,
PeriodicitySchema.type.label("event_type"),
EventSchema.user_id,
EventSchema.activity_id,
EventSchema.periodicity.label("event_type"),
case(
(
PeriodicitySchema.type.in_(("WEEKDAYS", "DAILY")),
EventSchema.periodicity.in_(("WEEKDAYS", "DAILY")),
scheduled_date,
),
(PeriodicitySchema.type.in_(("WEEKLY", "MONTHLY")), PeriodicitySchema.start_date),
else_=PeriodicitySchema.selected_date,
(EventSchema.periodicity.in_(("WEEKLY", "MONTHLY")), EventSchema.start_date),
else_=EventSchema.selected_date,
).label("selected_date"),
PeriodicitySchema.start_date,
PeriodicitySchema.end_date,
EventSchema.start_date,
EventSchema.end_date,
EventSchema.start_time,
EventSchema.end_time,
)
.select_from(EventSchema)
.join(UserEventsSchema, UserEventsSchema.event_id == EventSchema.id)
.join(PeriodicitySchema, PeriodicitySchema.id == EventSchema.periodicity_id)
.join(ActivityEventsSchema, ActivityEventsSchema.event_id == EventSchema.id)
.where(EventSchema.is_deleted == false(), PeriodicitySchema.type != PeriodicityType.ALWAYS)
.where(EventSchema.is_deleted == false(), EventSchema.periodicity != PeriodicityType.ALWAYS)
).cte("user_activity_events")

query = (
Expand Down
Loading

0 comments on commit 11571c7

Please sign in to comment.