Skip to content

Commit

Permalink
feat(stats): display total number of people in period
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Sep 16, 2024
1 parent 8467c02 commit 79e14f3
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 32 deletions.
9 changes: 8 additions & 1 deletion components/mostWatchedPerson.templ
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package components

import "fmt"
import (
"fmt"
"strconv"
)

type MostWatchedPersonProps struct {
Data []ListItem
Job string
Title string
Total int
Year string
Years []string
}
Expand All @@ -16,6 +20,9 @@ templ MostWatchedPerson(props MostWatchedPersonProps) {
@Dropdown(DropdownProps{Route: fmt.Sprintf("/stats/most-watched-person/%s", props.Job), Options: props.Years, Value: props.Year})
}
@OrderedList(props.Data, "person")
<div class="text-xs text-right text-neutral-500 dark:text-neutral-400">
{ strconv.Itoa( props.Total ) } people total
</div>
}
}

Expand Down
41 changes: 34 additions & 7 deletions components/mostWatchedPerson_templ.go

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

12 changes: 12 additions & 0 deletions db/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ ORDER BY
count DESC
LIMIT 10;

-- name: total-watched-by-job-and-year
SELECT
COUNT(*) AS count
FROM
seen s
INNER JOIN movie_person mp ON mp.movie_id = s.movie_id
WHERE
user_id = $1
AND mp.job = $2
AND ($3 = 'All'
OR EXTRACT(YEAR FROM date) = $3::int);

-- name: stats-watched-by-year
SELECT
EXTRACT(YEAR FROM date) AS label,
Expand Down
45 changes: 36 additions & 9 deletions handlers/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ func HandleGetStats(c *fiber.Ctx) error {
var movies []components.ListItem
var shortestAndLongest types.Movies
var wilhelms []int
var totals []components.ListItem

userId := c.Locals("UserId").(string)
now := time.Now()
year := now.Format("2006")
currentYear := now.Format("2006-01-02 15:04:05")

err := db.Dot.Select(db.Client, &movies, "stats-most-watched-movies", userId)
Expand All @@ -56,6 +58,12 @@ func HandleGetStats(c *fiber.Ctx) error {
return err
}

err = db.Dot.Select(db.Client, &totals, "total-watched-by-job-and-year", userId, "cast", year)

if err != nil {
return err
}

err = db.Dot.Get(db.Client, &stats, "stats-data", userId)

if err != nil {
Expand Down Expand Up @@ -128,7 +136,11 @@ func HandleGetStats(c *fiber.Ctx) error {
}
}

year := now.Format("2006")
totalCast := 0

if len(totals) > 0 {
totalCast = totals[0].Count
}

return utils.TemplRender(c, views.Stats(
views.StatsProps{
Expand All @@ -141,6 +153,7 @@ func HandleGetStats(c *fiber.Ctx) error {
Ratings: ratings,
SeenThisYear: seenThisYearByMonth,
Stats: stats,
TotalCast: totalCast,
WatchedByYear: watchedByYear,
Year: year,
YearRatings: yearRatings,
Expand All @@ -152,6 +165,7 @@ func HandleGetStats(c *fiber.Ctx) error {

func HandleGetMostWatchedByJob(c *fiber.Ctx) error {
var persons []components.ListItem
var totals []components.ListItem

job := c.Params("job")
year := c.Query("year", "All")
Expand All @@ -165,14 +179,27 @@ func HandleGetMostWatchedByJob(c *fiber.Ctx) error {
return err
}

return utils.TemplRender(c, components.MostWatchedPerson(components.MostWatchedPersonProps{
Data: persons,
Job: job,
Title: cases.Title(language.English).String(job),
Year: year,
Years: years,
},
))
err = db.Dot.Select(db.Client, &totals, "total-watched-by-job-and-year", userId, job, year)

if err != nil {
return err
}

totalJob := 0

if len(totals) > 0 {
totalJob = totals[0].Count
}

return utils.TemplRender(c, components.MostWatchedPerson(
components.MostWatchedPersonProps{
Data: persons,
Job: job,
Title: cases.Title(language.English).String(job),
Total: totalJob,
Year: year,
Years: years,
}))
}

func getGraphWithQuery(query string, userId string) ([]types.Bar, error) {
Expand Down
4 changes: 4 additions & 0 deletions public/styles.323df8.css → public/styles.5709e2.css
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,10 @@ video {
text-align: center;
}

.text-right {
text-align: right;
}

.text-base {
font-size: 1rem;
line-height: 1.5rem;
Expand Down
4 changes: 4 additions & 0 deletions public/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,10 @@ video {
text-align: center;
}

.text-right {
text-align: right;
}

.text-base {
font-size: 1rem;
line-height: 1.5rem;
Expand Down
2 changes: 2 additions & 0 deletions views/stats.templ
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type StatsProps struct {
SeenThisYear []types.Bar
ShortestAndLongestMovie types.Movies
Stats types.Stats
TotalCast int
WatchedByYear []types.Bar
WilhelmScreams int
Year string
Expand Down Expand Up @@ -174,6 +175,7 @@ templ Stats(props StatsProps) {
Job: "cast",
Title: "Cast",
Data: props.MostWatchedCast,
Total: props.TotalCast,
Year: "All",
Years: append([]string{"All"}, props.Years...),
})
Expand Down
Loading

0 comments on commit 79e14f3

Please sign in to comment.