Skip to content

Commit

Permalink
feat(stats): add seen this year by month
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Dec 23, 2023
1 parent d9b1d4c commit 4fe1fd5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
20 changes: 20 additions & 0 deletions db/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,23 @@ GROUP BY
ORDER BY
label;

-- name: stats-watched-this-year-by-month
WITH months (
month
) AS (
SELECT
generate_series(DATE_TRUNC('year', CURRENT_DATE), DATE_TRUNC('year', CURRENT_DATE) + INTERVAL '1 year' - INTERVAL '1 day', INTERVAL '1 month'))
SELECT
EXTRACT(MONTH FROM months.month)::integer AS label,
COALESCE(count(seen.id), 0) AS value
FROM
months
LEFT JOIN seen ON DATE_TRUNC('month', seen.date) = months.month
WHERE
EXTRACT(YEAR FROM seen.date) = EXTRACT(YEAR FROM CURRENT_DATE)
OR seen.date IS NULL
GROUP BY
months.month
ORDER BY
months.month;

7 changes: 7 additions & 0 deletions handlers/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,20 @@ func HandleGetStats(c *fiber.Ctx) error {
return err
}

seenThisYearByMonth, err := getGraphWithQuery("stats-watched-this-year-by-month")

if err != nil {
return err
}

return utils.TemplRender(c, views.Stats(
stats,
utils.FormatRuntime(stats.TotalRuntime),
cast,
watchedByYear,
ratings,
movies,
seenThisYearByMonth,
))
}

Expand Down
7 changes: 4 additions & 3 deletions views/stats.templ
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
)

templ Stats(stats types.Stats, formattedTotalRuntime string, mostWatchedCast []components.ListItem, watchedByYear []types.Bar, ratings []types.Bar, mostWatchedMovies []components.ListItem) {
templ Stats(stats types.Stats, formattedTotalRuntime string, mostWatchedCast []components.ListItem, watchedByYear []types.Bar, ratings []types.Bar, mostWatchedMovies []components.ListItem, seenThisYear []types.Bar) {
@Layout("Stats") {
<div class="mx-auto flex max-w-xl flex-col gap-8 px-5 py-8">
<nav class="flex items-center gap-5">
Expand All @@ -17,8 +17,6 @@ templ Stats(stats types.Stats, formattedTotalRuntime string, mostWatchedCast []c
}
</div>
</nav>
@components.Graph(watchedByYear, "Watched by year")
@components.Graph(ratings, "Ratings")
@components.Section("Stats", 0) {
@components.DescriptionList() {
@components.DescriptionListItem("Unique movies seen", true) {
Expand All @@ -43,6 +41,9 @@ templ Stats(stats types.Stats, formattedTotalRuntime string, mostWatchedCast []c
}
}
}
@components.Graph(watchedByYear, "Watched by year")
@components.Graph(ratings, "Ratings")
@components.Graph(seenThisYear, "Seen this year by month")
@components.Section("Most watched movies", 0) {
@components.OrderedList(mostWatchedMovies, "movie")
}
Expand Down
22 changes: 13 additions & 9 deletions views/stats_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4fe1fd5

Please sign in to comment.