Skip to content

Commit

Permalink
feat(song): finish view
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Dec 10, 2024
1 parent 418d6b0 commit 66a3fcf
Show file tree
Hide file tree
Showing 20 changed files with 222 additions and 139 deletions.
4 changes: 2 additions & 2 deletions config/development.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server:
host: "localhost"
host: "0.0.0.0"
port: 3000

db:
Expand Down Expand Up @@ -102,7 +102,7 @@ tui:
song:
interval_current_s: 5
interval_history_s: 5
interval_top_s: 3600
interval_top_s: 300

tap:
interval_s: 60
Expand Down
13 changes: 13 additions & 0 deletions db/migrations/20241209222600_song_created_at.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE song_history
ALTER COLUMN created_at
SET DEFAULT (NOW() - INTERVAL '1 second');
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
ALTER TABLE song_history
ALTER COLUMN created_at
SET DEFAULT CURRENT_TIMESTAMP;
-- +goose StatementEnd
8 changes: 4 additions & 4 deletions db/queries/song.sql
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ SELECT s.title
FROM song_history sh
JOIN song s ON sh.song_id = s.id
ORDER BY created_at DESC
LIMIT 5;
LIMIT 10;

-- name: GetTopSongs :many
SELECT s.id AS song_id, s.title, COUNT(sh.id) AS play_count
FROM song_history sh
JOIN song s ON sh.song_id = s.id
GROUP BY s.id, s.title
ORDER BY play_count DESC
LIMIT 5;
LIMIT 10;

-- name: GetTopArtists :many
SELECT sa.id AS artist_id, sa.name AS artist_name, COUNT(sh.id) AS total_plays
Expand All @@ -93,7 +93,7 @@ JOIN song_artist_song sas ON s.id = sas.song_id
JOIN song_artist sa ON sas.artist_id = sa.id
GROUP BY sa.id, sa.name
ORDER BY total_plays DESC
LIMIT 5;
LIMIT 10;

-- name: GetTopGenres :many
SELECT g.genre AS genre_name, COUNT(sh.id) AS total_plays
Expand All @@ -105,4 +105,4 @@ JOIN song_artist_genre sag ON sa.id = sag.artist_id
JOIN song_genre g ON sag.genre_id = g.id
GROUP BY g.genre
ORDER BY total_plays DESC
LIMIT 5;
LIMIT 10;
2 changes: 1 addition & 1 deletion db/queries/tap.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ FROM tap
GROUP BY category;

-- name: GetOrderCountByCategorySinceOrderID :many
SELECT category, COUNT(*), CAST(MAX(order_created_at) AS INTEGER) AS latest_order_created_at
SELECT category, COUNT(*), MAX(order_created_at)::TIMESTAMP AS latest_order_created_at
FROM tap
WHERE order_id >= $1
GROUP BY category;
3 changes: 2 additions & 1 deletion internal/pkg/db/dto/event.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dto

import (
"bytes"
"time"

"github.com/jackc/pgx/v5/pgtype"
Expand Down Expand Up @@ -31,7 +32,7 @@ func EventDTO(e sqlc.Event) *Event {

// Equal compares 2 events
func (e *Event) Equal(e2 Event) bool {
return e.Name == e2.Name && e.Date.Equal(e2.Date) && e.AcademicYear == e2.AcademicYear && e.Location == e2.Location
return e.Name == e2.Name && e.Date.Equal(e2.Date) && e.AcademicYear == e2.AcademicYear && e.Location == e2.Location && bytes.Equal(e.Poster, e2.Poster)
}

// CreateParams converts a Event DTO to a sqlc CreateEventParams object
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/db/sqlc/song.sql.go

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

4 changes: 2 additions & 2 deletions internal/pkg/db/sqlc/tap.sql.go

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

41 changes: 23 additions & 18 deletions internal/pkg/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/internal/pkg/db/dto"
"github.com/zeusWPI/scc/internal/pkg/db/sqlc"
"github.com/zeusWPI/scc/pkg/config"
)

Expand Down Expand Up @@ -38,15 +39,35 @@ func (e *Event) Update() error {
return nil
}

eventsDB, err := e.db.Queries.GetEventByAcademicYear(context.Background(), events[0].AcademicYear)
eventsDBSQL, err := e.db.Queries.GetEventByAcademicYear(context.Background(), events[0].AcademicYear)
if err != nil {
return err
}

eventsDB := make([]*dto.Event, 0, len(eventsDBSQL))

var wg sync.WaitGroup
var errs []error
for _, event := range eventsDBSQL {
wg.Add(1)

go func(event sqlc.Event) {
defer wg.Done()

ev := dto.EventDTO(event)
eventsDB = append(eventsDB, ev)
err := e.getPoster(ev)
if err != nil {
errs = append(errs, err)
}
}(event)
}
wg.Wait()

// Check if there are any new events
equal := true
for _, event := range eventsDB {
found := slices.ContainsFunc(events, func(ev dto.Event) bool { return ev.Equal(*dto.EventDTO(event)) })
found := slices.ContainsFunc(events, func(ev dto.Event) bool { return ev.Equal(*event) })
if !found {
equal = false
break
Expand All @@ -67,22 +88,6 @@ func (e *Event) Update() error {
if err != nil {
return err
}
var errs []error

var wg sync.WaitGroup
for _, event := range events {
wg.Add(1)

go func(event *dto.Event) {
defer wg.Done()

err := e.getPoster(event)
if err != nil {
errs = append(errs, err)
}
}(&event)
}
wg.Wait()

for _, event := range events {
err = e.getPoster(&event)
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/song/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type artistResponse struct {
}

func (s *Song) getArtist(artist *dto.SongArtist) error {
zap.S().Info("Song: Getting artists info for ", artist.ID)
zap.S().Info("Song: Getting artists info for ", artist.SpotifyID)

req := fiber.Get(fmt.Sprintf("%s/%s/%s", s.api, "artists", artist.SpotifyID)).
Set("Authorization", fmt.Sprintf("Bearer %s", s.AccessToken))
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/song/song.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (s *Song) Track(track *dto.Song) error {
if (a != sqlc.SongArtist{}) {
// Artist already exists
// Add it as an artist for this track
track.Artists[i].ID = a.ID
if _, err := s.db.Queries.CreateSongArtistSong(context.Background(), *track.CreateSongArtistSongParams(i)); err != nil {
errs = append(errs, err)
}
Expand Down
14 changes: 7 additions & 7 deletions tui/components/stopwatch/stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (

var lastID int64

func nextID() int {
return int(atomic.AddInt64(&lastID, 1))
func nextID() int64 {
return atomic.AddInt64(&lastID, 1)
}

// TickMsg is a message that is sent on every stopwatch tick
type TickMsg struct {
id int
id int64
}

// StartStopMsg is a message that controls if the stopwatch is running or not
Expand All @@ -34,12 +34,12 @@ type ResetMsg struct {

// Model for the stopwatch component
type Model struct {
id int
id int64
duration time.Duration
running bool
}

// New created a new stopwatch with a given interval
// New creates a new stopwatch with a given interval
func New() Model {
return Model{
id: nextID(),
Expand All @@ -48,7 +48,7 @@ func New() Model {
}
}

// Init initiates the stopwatch component
// Init initializes the stopwatch component
func (m Model) Init() tea.Cmd {
return nil
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func (m Model) View() string {
return fmt.Sprintf("%02d:%02d", min, sec)
}

func tick(id int) tea.Cmd {
func tick(id int64) tea.Cmd {
return tea.Tick(time.Second, func(_ time.Time) tea.Msg {
return TickMsg{id: id}
})
Expand Down
2 changes: 1 addition & 1 deletion tui/screen/cammie/cammie.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c *Cammie) Update(msg tea.Msg) (screen.Screen, tea.Cmd) {
c.width = msg.Width
c.height = msg.Height

sMsg = sMsg.Width(c.width/2 - sMsg.GetHorizontalFrameSize() - sMsg.GetHorizontalPadding()).Height(c.height - sMsg.GetVerticalFrameSize() - sMsg.GetVerticalPadding())
sMsg = sMsg.Width(c.width/2 - view.GetOuterWidth(sMsg)).Height(c.height - sMsg.GetVerticalFrameSize() - sMsg.GetVerticalPadding())
sTop = sTop.Width(c.width/2 - sTop.GetHorizontalFrameSize()).Height(c.height/2 - sTop.GetVerticalFrameSize())
sBottom = sBottom.Width(c.width/2 - sBottom.GetHorizontalFrameSize()).Height(c.height/2 - sBottom.GetVerticalFrameSize())

Expand Down
4 changes: 2 additions & 2 deletions tui/screen/song/song.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Song struct {

// New creates a new song screen
func New(db *db.DB) screen.Screen {
return &Song{db: db, song: song.NewModel(db), width: 0, height: 0}
return &Song{db: db, song: song.New(db), width: 0, height: 0}
}

// Init initializes the song screen
Expand All @@ -34,7 +34,7 @@ func (s *Song) Update(msg tea.Msg) (screen.Screen, tea.Cmd) {
s.width = msg.Width
s.height = msg.Height

sSong = sSong.Width(s.width - sSong.GetHorizontalFrameSize() - sSong.GetHorizontalPadding()).Height(s.height - sSong.GetVerticalFrameSize() - sSong.GetVerticalPadding())
sSong = sSong.Width(s.width - view.GetOuterWidth(sSong)).Height(s.height - sSong.GetVerticalFrameSize() - sSong.GetVerticalPadding())

return s, s.GetSizeMsg
}
Expand Down
Loading

0 comments on commit 66a3fcf

Please sign in to comment.