From 3de8271fb1f3923286fa28b7cdfe34c94da65a50 Mon Sep 17 00:00:00 2001 From: Marvin Steadfast Date: Mon, 31 May 2021 08:48:28 +0200 Subject: [PATCH] storing mpd playlist tracks for metrics and not the ones from the config --- assets/web/assets.go | 5 ++- pkg/run/run.go | 73 ++++++++++++++++++++++++++------------------ pkg/sselog/sselog.go | 1 + 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/assets/web/assets.go b/assets/web/assets.go index 1d19fca..b507363 100644 --- a/assets/web/assets.go +++ b/assets/web/assets.go @@ -1,4 +1,4 @@ -//nolint:gochecknoglobals,golint,stylecheck +//nolint:gochecknoglobals package web import ( @@ -12,14 +12,17 @@ import ( //go:embed files var files embed.FS +// Files is the sub directed http.FileSystem for files. var Files = sub(files, "files") +// Templates stores the templates. //go:embed templates var Templates embed.FS //go:embed swagger-ui var swaggerUI embed.FS +// SwaggerUI is the sub directed http.FileSystem for the swagger-ui. var SwaggerUI = sub(swaggerUI, "swagger-ui") func sub(f embed.FS, dir string) http.FileSystem { diff --git a/pkg/run/run.go b/pkg/run/run.go index c54f5f7..19f6921 100644 --- a/pkg/run/run.go +++ b/pkg/run/run.go @@ -65,11 +65,43 @@ func (m *mpc) play(logger zerolog.Logger, rfid string, name string, uris []strin } } - metrics.NewPlay(rfid, name, uris) + // Getting playlist uris from MPD server. + // This is needed to identify the right metric to use. + mpdURIS, err := m.playlistURIS() + if err != nil { + metrics.BoxErrors.Inc() + + return err + } + + metrics.NewPlay(rfid, name, mpdURIS) return m.conn.Play(-1) } +// playlistURIS extracts uris from MPD playlist. +func (m *mpc) playlistURIS() ([]string, error) { + // Check if we can connect to MPD server. + if err := m.conn.Ping(); err != nil { + return nil, fmt.Errorf("could not ping MPD server: %w", err) + } + + attrs, err := m.conn.PlaylistInfo(-1, -1) + if err != nil { + return nil, fmt.Errorf("could not get playlist: %w", err) + } + + // Stores the tracklist it got from the MPD server. + uris := []string{} + + // Builds uri list. + for _, a := range attrs { + uris = append(uris, a["file"]) + } + + return uris, nil +} + func (m *mpc) watch() { log.Debug().Msg("starting watch") @@ -79,31 +111,14 @@ func (m *mpc) watch() { for { <-ticker.C - // Check if we can connect to MPD server. - if err := m.conn.Ping(); err != nil { - log.Error().Err(err).Msg("could not ping MPD server") - metrics.BoxErrors.Inc() - - continue - } - - // Getting playlist info. - attrs, err := m.conn.PlaylistInfo(-1, -1) + uris, err := m.playlistURIS() if err != nil { - log.Error().Err(err).Msg("could not get playlist info") + log.Error().Err(err).Msg("could not get playlist uris") metrics.BoxErrors.Inc() continue } - // Stores the tracklist it got from the MPD server. - uris := []string{} - - // Builds uri list. - for _, a := range attrs { - uris = append(uris, a["file"]) - } - // Gettings MPD state. s, err := m.conn.Status() if err != nil { @@ -129,16 +144,6 @@ func Run(cmd *cobra.Command, args []string) { log.Fatal().Err(err).Msg("could not start RFID reader") } - // Create MPD connection on every received event. - c, err := mpd.Dial("tcp", fmt.Sprintf("%s:%d", config.Cfg.MPD.Hostname, config.Cfg.MPD.Port)) - if err != nil { - log.Fatal().Err(err).Msg("could not connect to MPD server") - } - - m := newMpc(c) - - m.watch() - go func() { var id string @@ -148,6 +153,14 @@ func Run(cmd *cobra.Command, args []string) { logger := log.With().Str("id", id).Logger() logger.Info().Msg("received id") + // Create MPD connection on every received event. + c, err := mpd.Dial("tcp", fmt.Sprintf("%s:%d", config.Cfg.MPD.Hostname, config.Cfg.MPD.Port)) + if err != nil { + log.Fatal().Err(err).Msg("could not connect to MPD server") + } + + m := newMpc(c) + // Check of stop tag was detected. if id == config.Cfg.Meta.Stop { logger.Info().Msg("stopping") diff --git a/pkg/sselog/sselog.go b/pkg/sselog/sselog.go index acebb82..cbb7906 100644 --- a/pkg/sselog/sselog.go +++ b/pkg/sselog/sselog.go @@ -10,6 +10,7 @@ import ( "go.xsfx.dev/logginghandler" ) +// Log is the global sse logger struct. var Log *SSELog type SSELog struct {