Skip to content

Commit

Permalink
fix tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
heyitsmebev committed Jan 15, 2025
1 parent 88c3da5 commit 880237f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
38 changes: 35 additions & 3 deletions app/dao/services_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,15 +515,44 @@ def dao_fetch_stats_for_service_from_days_for_user(
start_date = get_midnight_in_utc(start_date)
end_date = get_midnight_in_utc(end_date + timedelta(days=1))

total_substmt = (
select(
func.date_trunc("day", NotificationAllTimeView.created_at).label("day"),
Job.notification_count.label("notification_count"),
)
.join(Job, NotificationAllTimeView.job_id == Job.id)
.where(
NotificationAllTimeView.service_id == service_id,
NotificationAllTimeView.key_type != KeyType.TEST,
NotificationAllTimeView.created_at >= start_date,
NotificationAllTimeView.created_at < end_date,
NotificationAllTimeView.created_by_id == user_id,
)
.group_by(
Job.id,
Job.notification_count,
func.date_trunc("day", NotificationAllTimeView.created_at),
)
.subquery()
)

total_stmt = select(
total_substmt.c.day,
func.sum(total_substmt.c.notification_count).label("total_notifications"),
).group_by(total_substmt.c.day)

total_notifications = {
row.day: row.total_notifications for row in db.session.execute(total_stmt).all()
}

stmt = (
select(
NotificationAllTimeView.notification_type,
NotificationAllTimeView.status,
func.date_trunc("day", NotificationAllTimeView.created_at).label("day"),
func.count(NotificationAllTimeView.id).label("count"),
)
.select_from(NotificationAllTimeView)
.filter(
.where(
NotificationAllTimeView.service_id == service_id,
NotificationAllTimeView.key_type != KeyType.TEST,
NotificationAllTimeView.created_at >= start_date,
Expand All @@ -536,7 +565,10 @@ def dao_fetch_stats_for_service_from_days_for_user(
func.date_trunc("day", NotificationAllTimeView.created_at),
)
)
return db.session.execute(stmt).scalars().all()

data = db.session.execute(stmt).all()

return total_notifications, data


def dao_fetch_todays_stats_for_all_services(
Expand Down
14 changes: 9 additions & 5 deletions app/service/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,16 @@ def get_service_statistics_for_specific_days_by_user(
end_date = datetime.strptime(start, "%Y-%m-%d")
start_date = end_date - timedelta(days=days - 1)

results = dao_fetch_stats_for_service_from_days_for_user(
total_notifications, results = dao_fetch_stats_for_service_from_days_for_user(
service_id, start_date, end_date, user_id
)

stats = get_specific_days_stats(results, start_date, days=days)

stats = get_specific_days_stats(
results,
start_date,
days=days,
total_notifications=total_notifications,
)
return stats


Expand Down Expand Up @@ -663,11 +667,11 @@ def get_single_month_notification_stats_by_user(service_id, user_id):
month_year = datetime(year, month, 10, 00, 00, 00)
start_date, end_date = get_month_start_and_end_date_in_utc(month_year)

results = dao_fetch_stats_for_service_from_days_for_user(
total_notifications, results = dao_fetch_stats_for_service_from_days_for_user(
service_id, start_date, end_date, user_id
)

stats = get_specific_days_stats(results, start_date, end_date=end_date)
stats = get_specific_days_stats(results, start_date, end_date=end_date, total_notifications=total_notifications,)
return jsonify(stats)


Expand Down

0 comments on commit 880237f

Please sign in to comment.