Skip to content

Commit

Permalink
radio: use Listeners type more consistantly
Browse files Browse the repository at this point in the history
website/api/v1: send no-buffering header for the SSE endpoint

website/api/v1: add listener sending, this is currently every value
	returned by the stream of data. That is currently updated by
	the non-working loadbalancer sending 0s every 10 seconds.

mocks: regenerate for Listeners type usage

rpc: fix for Listeners type usage

ircbot: use StreamValue for CurrentTrack

website: use New functions for the public and admin States, this is to
	avoid forgetting to set a new field when one is added.

website/public: use StreamValue on the home page rendering

website/api/php: use StreamValue for most of the api endpoints
  • Loading branch information
Wessie committed Feb 25, 2024
1 parent ae0e1c9 commit 119b86c
Show file tree
Hide file tree
Showing 18 changed files with 247 additions and 80 deletions.
9 changes: 8 additions & 1 deletion assets/js/radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ function now() {
htmx.createEventSource = function (url) {
console.log(url);
es = new EventSource(url);
es.addEventListener("streamer", (event) => {
console.log(event.data);
});
es.addEventListener("listeners", (event) => {
console.log(event.data, Date.now());
});
es.addEventListener("metadata", (event) => {
console.log(event.data);
});
Expand Down Expand Up @@ -61,9 +67,10 @@ htmx.on('htmx:load', (event) => {
console.log("creating stream player");
stream = new Stream(initStream.getElementsByTagName("source")[0].src);
}
if (stream && stream.button()) {
if (stream && stream.button() && !stream.button().dataset.hasclick) {
console.log("registering stream play/stop button handler");
stream.button().onclick = stream.playStop;
stream.button().dataset.hasclick = true;
if (stream.audio && !stream.audio.paused) {
stream.setButton("Stop Stream");
}
Expand Down
6 changes: 1 addition & 5 deletions ircbot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/errors"
"github.com/R-a-dio/valkyrie/util"
"github.com/lrstanley/girc"
"github.com/rs/zerolog"
)
Expand Down Expand Up @@ -231,10 +230,7 @@ func (e Event) ArgumentTrack(key string) (*radio.Song, error) {
func (e Event) CurrentTrack() (*radio.Song, error) {
const op errors.Op = "irc/Event.CurrentTrack"

status, err := util.OneOff(e.Ctx, e.Bot.Manager.CurrentStatus)
if err != nil {
return nil, errors.E(op, err)
}
status := e.Bot.StatusValue.Latest()

song, err := e.Storage.Song(e.Ctx).FromMetadata(status.Song.Metadata)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions manager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (m *Manager) UpdateSong(ctx context.Context, update *radio.SongUpdate) erro

var prev radio.Song
var prevInfo radio.SongInfo
var listenerCountDiff *int
var listenerCountDiff *radio.Listeners

// critical section to swap our new song with the previous one
m.mu.Lock()
Expand All @@ -162,7 +162,7 @@ func (m *Manager) UpdateSong(ctx context.Context, update *radio.SongUpdate) erro
// record listener count and calculate the difference between start/end of song
currentListenerCount := m.status.Listeners
// update and retrieve listener count of start of song
var startListenerCount int
var startListenerCount radio.Listeners
startListenerCount, m.songStartListenerCount = m.songStartListenerCount, currentListenerCount

m.mu.Unlock()
Expand Down Expand Up @@ -239,7 +239,7 @@ func (m *Manager) UpdateListeners(ctx context.Context, listeners radio.Listeners
m.listenerStream.Send(listeners)

m.mu.Lock()
m.status.Listeners = int(listeners)
m.status.Listeners = listeners
m.mu.Unlock()
return nil
}
2 changes: 1 addition & 1 deletion manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type Manager struct {
status radio.Status
autoStreamerTimer *time.Timer
// listener count at the start of a song
songStartListenerCount int
songStartListenerCount radio.Listeners

// streaming support
userStream *eventstream.EventStream[radio.User]
Expand Down
14 changes: 7 additions & 7 deletions mocks/radio.gen.go

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

96 changes: 96 additions & 0 deletions mocks/templates.gen.go

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

6 changes: 3 additions & 3 deletions radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Status struct {
// StreamerName is the name given to us by the user that is streaming
StreamerName string
// Listeners is the current amount of stream listeners
Listeners int
Listeners Listeners
// Thread is an URL to a third-party platform related to the current stream
Thread string
// RequestsEnabled tells you if requests to the automated streamer are enabled
Expand Down Expand Up @@ -646,7 +646,7 @@ type SongStorage interface {
PlayedCount(Song) (int64, error)
// AddPlay adds a play to the song. If present, ldiff is the difference in amount
// of listeners between song-start and song-end
AddPlay(song Song, ldiff *int) error
AddPlay(song Song, ldiff *Listeners) error

// FavoriteCount returns the amount of users that have added this song to
// their favorite list
Expand Down Expand Up @@ -747,7 +747,7 @@ type UserStorage interface {
// Permissions returns all available permissions
Permissions() ([]UserPermission, error)
// RecordListeners records a history of listener count
RecordListeners(int, User) error
RecordListeners(Listeners, User) error
}

// StatusStorageService is a service able to supply a StatusStorage
Expand Down
2 changes: 1 addition & 1 deletion rpc/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func fromProtoStatus(s *StatusResponse) radio.Status {
User: fromProtoUser(s.User),
Song: fromProtoSong(s.Song),
SongInfo: fromProtoSongInfo(s.Info),
Listeners: int(s.ListenerInfo.Listeners),
Listeners: s.ListenerInfo.Listeners,
Thread: s.Thread,
RequestsEnabled: s.StreamerConfig.RequestsEnabled,
StreamerName: s.StreamerName,
Expand Down
2 changes: 1 addition & 1 deletion rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (as AnnouncerShim) AnnounceSong(ctx context.Context, a *SongAnnouncement) (
err := as.announcer.AnnounceSong(ctx, radio.Status{
Song: fromProtoSong(a.Song),
SongInfo: fromProtoSongInfo(a.Info),
Listeners: int(a.ListenerInfo.Listeners),
Listeners: a.ListenerInfo.Listeners,
})
return new(emptypb.Empty), err
}
Expand Down
2 changes: 1 addition & 1 deletion storage/mariadb/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (ss SongStorage) PlayedCount(song radio.Song) (int64, error) {
}

// AddPlay implements radio.SongStorage
func (ss SongStorage) AddPlay(song radio.Song, ldiff *int) error {
func (ss SongStorage) AddPlay(song radio.Song, ldiff *radio.Listeners) error {
const op errors.Op = "mariadb/SongStorage.AddPlay"

var query = `INSERT INTO eplay (isong, ldiff) VALUES (?, ?);`
Expand Down
2 changes: 1 addition & 1 deletion storage/mariadb/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func (us UserStorage) Permissions() ([]radio.UserPermission, error) {
}

// RecordListeners implements radio.UserStorage
func (us UserStorage) RecordListeners(listeners int, user radio.User) error {
func (us UserStorage) RecordListeners(listeners radio.Listeners, user radio.User) error {
const op errors.Op = "mariadb/UserStorage.RecordListeners"

var query = `INSERT INTO listenlog (listeners, dj) VALUES (?, ?);`
Expand Down
7 changes: 4 additions & 3 deletions templates/default/home.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
</div>
<div sse-swap="metadata">
{{template "nowplaying" .Status}}


</div>
</div>
<div class="column is-3" sse-swap="streamer">
Expand Down Expand Up @@ -84,13 +82,16 @@ Home {{printjson .}}
<progress id="current-song-progress" class="progress is-large mb-0" value="0" max="{{.Song.Length.Seconds}}"></progress>
<div class="columns">
<div class="column is-6">
<p class="has-text-centered">Listeners: <span id="listener-count">{{.Listeners}}</span></p>
<p class="has-text-centered" sse-swap="listeners">{{template "listeners" .Listeners}}</p>
</div>
<div class="column is-6">
<p class="has-text-centered"><span id="progress-current" data-start="{{.SongInfo.Start.UnixMilli}}">00:00</span> / <span id="progress-max">{{.Song.Length | MediaDuration}}<span></p>
</div>
</div>
{{end}}
{{define "listeners"}}
Listeners: <span id="listener-count">{{.}}</span>
{{end}}
{{define "lastplayed"}}
<p class="title is-4 has-text-centered">Last Played</p>
<div class="notification">
Expand Down
23 changes: 23 additions & 0 deletions website/admin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ import (
"github.com/go-chi/chi/v5"
)

func NewState(
_ context.Context,
cfg config.Config,
dp *daypass.Daypass,
storage radio.StorageService,
siteTmpl *templates.Site,
exec templates.Executor,
sessionManager *scs.SessionManager,
auth vmiddleware.Authentication,
fs afero.Fs,
) State {
return State{
Config: cfg,
Daypass: dp,
Storage: storage,
Templates: siteTmpl,
TemplateExecutor: exec,
SessionManager: sessionManager,
Authentication: auth,
FS: fs,
}
}

type State struct {
config.Config

Expand Down
Loading

0 comments on commit 119b86c

Please sign in to comment.