Better log and less noise in prod mode
This commit is contained in:
+11
-2
@@ -1,10 +1,12 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
"github.com/deluan/gosonic/api/responses"
|
"github.com/deluan/gosonic/api/responses"
|
||||||
|
"github.com/deluan/gosonic/domain"
|
||||||
"github.com/deluan/gosonic/itunesbridge"
|
"github.com/deluan/gosonic/itunesbridge"
|
||||||
"github.com/deluan/gosonic/utils"
|
"github.com/deluan/gosonic/utils"
|
||||||
)
|
)
|
||||||
@@ -12,10 +14,11 @@ import (
|
|||||||
type MediaAnnotationController struct {
|
type MediaAnnotationController struct {
|
||||||
BaseAPIController
|
BaseAPIController
|
||||||
itunes itunesbridge.ItunesControl
|
itunes itunesbridge.ItunesControl
|
||||||
|
mfRepo domain.MediaFileRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MediaAnnotationController) Prepare() {
|
func (c *MediaAnnotationController) Prepare() {
|
||||||
utils.ResolveDependencies(&c.itunes)
|
utils.ResolveDependencies(&c.itunes, &c.mfRepo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MediaAnnotationController) Scrobble() {
|
func (c *MediaAnnotationController) Scrobble() {
|
||||||
@@ -24,7 +27,13 @@ func (c *MediaAnnotationController) Scrobble() {
|
|||||||
submission := c.ParamBool("submission", true)
|
submission := c.ParamBool("submission", true)
|
||||||
|
|
||||||
if submission {
|
if submission {
|
||||||
beego.Debug("Scrobbling", id, "at", time)
|
mf, err := c.mfRepo.Get(id)
|
||||||
|
if err != nil || mf == nil {
|
||||||
|
beego.Error("Id", id, "not found!")
|
||||||
|
c.SendError(responses.ERROR_DATA_NOT_FOUND, "Id not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
beego.Info(fmt.Sprintf(`Scrobbling (%s) "%s" at %v`, id, mf.Title, time))
|
||||||
if err := c.itunes.Scrobble(id, time); err != nil {
|
if err := c.itunes.Scrobble(id, time); err != nil {
|
||||||
beego.Error("Error scrobbling:", err)
|
beego.Error("Error scrobbling:", err)
|
||||||
c.SendError(responses.ERROR_GENERIC, "Internal error")
|
c.SendError(responses.ERROR_GENERIC, "Internal error")
|
||||||
|
|||||||
+12
-6
@@ -4,6 +4,8 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
"github.com/deluan/gosonic/api/responses"
|
"github.com/deluan/gosonic/api/responses"
|
||||||
)
|
)
|
||||||
@@ -14,7 +16,7 @@ type ControllerInterface interface {
|
|||||||
SendError(errorCode int, message ...interface{})
|
SendError(errorCode int, message ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Validate(controller ControllerInterface) {
|
func Validate(controller BaseAPIController) {
|
||||||
if beego.AppConfig.String("disableValidation") != "true" {
|
if beego.AppConfig.String("disableValidation") != "true" {
|
||||||
checkParameters(controller)
|
checkParameters(controller)
|
||||||
authenticate(controller)
|
authenticate(controller)
|
||||||
@@ -22,18 +24,18 @@ func Validate(controller ControllerInterface) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkParameters(c ControllerInterface) {
|
func checkParameters(c BaseAPIController) {
|
||||||
requiredParameters := []string{"u", "p", "v", "c"}
|
requiredParameters := []string{"u", "p", "v", "c"}
|
||||||
|
|
||||||
for _, p := range requiredParameters {
|
for _, p := range requiredParameters {
|
||||||
if c.GetString(p) == "" {
|
if c.GetString(p) == "" {
|
||||||
beego.Warn("Missing required parameter:", p)
|
logWarn(c, fmt.Sprintf(`Missing required parameter "%s"`, p))
|
||||||
abortRequest(c, responses.ERROR_MISSING_PARAMETER)
|
abortRequest(c, responses.ERROR_MISSING_PARAMETER)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func authenticate(c ControllerInterface) {
|
func authenticate(c BaseAPIController) {
|
||||||
user := c.GetString("u")
|
user := c.GetString("u")
|
||||||
pass := c.GetString("p")
|
pass := c.GetString("p")
|
||||||
if strings.HasPrefix(pass, "enc:") {
|
if strings.HasPrefix(pass, "enc:") {
|
||||||
@@ -43,11 +45,15 @@ func authenticate(c ControllerInterface) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if user != beego.AppConfig.String("user") || pass != beego.AppConfig.String("password") {
|
if user != beego.AppConfig.String("user") || pass != beego.AppConfig.String("password") {
|
||||||
beego.Warn("Invalid login:", user)
|
logWarn(c, fmt.Sprintf(`Invalid login for user "%s"`, user))
|
||||||
abortRequest(c, responses.ERROR_AUTHENTICATION_FAIL)
|
abortRequest(c, responses.ERROR_AUTHENTICATION_FAIL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func abortRequest(c ControllerInterface, code int) {
|
func abortRequest(c BaseAPIController, code int) {
|
||||||
c.SendError(code)
|
c.SendError(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logWarn(c BaseAPIController, msg string) {
|
||||||
|
beego.Warn(fmt.Sprintf("%s?%s: %s", c.Ctx.Request.URL.Path, c.Ctx.Request.URL.RawQuery, msg))
|
||||||
|
}
|
||||||
|
|||||||
+1
-1
@@ -52,7 +52,7 @@ func mapControllers() {
|
|||||||
|
|
||||||
func mapFilters() {
|
func mapFilters() {
|
||||||
var ValidateRequest = func(ctx *context.Context) {
|
var ValidateRequest = func(ctx *context.Context) {
|
||||||
c := &api.BaseAPIController{}
|
c := api.BaseAPIController{}
|
||||||
c.Ctx = ctx
|
c.Ctx = ctx
|
||||||
api.Validate(c)
|
api.Validate(c)
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-10
@@ -102,7 +102,7 @@ func (i *Importer) Run() {
|
|||||||
if err := i.importLibrary(); err != nil {
|
if err := i.importLibrary(); err != nil {
|
||||||
beego.Error("Error persisting data:", err)
|
beego.Error("Error persisting data:", err)
|
||||||
}
|
}
|
||||||
beego.Info("Finished importing tracks from iTunes Library")
|
beego.Debug("Finished importing tracks from iTunes Library")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Importer) lastModifiedSince() time.Time {
|
func (i *Importer) lastModifiedSince() time.Time {
|
||||||
@@ -126,6 +126,12 @@ func (i *Importer) importLibrary() (err error) {
|
|||||||
als := make(domain.Albums, len(i.scanner.Albums()))
|
als := make(domain.Albums, len(i.scanner.Albums()))
|
||||||
ars := make(domain.Artists, len(i.scanner.Artists()))
|
ars := make(domain.Artists, len(i.scanner.Artists()))
|
||||||
pls := make(domain.Playlists, len(i.scanner.Playlists()))
|
pls := make(domain.Playlists, len(i.scanner.Playlists()))
|
||||||
|
arc, _ := i.artistRepo.CountAll()
|
||||||
|
alc, _ := i.albumRepo.CountAll()
|
||||||
|
mfc, _ := i.mfRepo.CountAll()
|
||||||
|
plc, _ := i.plsRepo.CountAll()
|
||||||
|
alu := 0
|
||||||
|
mfu := 0
|
||||||
|
|
||||||
i.search.ClearAll()
|
i.search.ClearAll()
|
||||||
|
|
||||||
@@ -143,6 +149,7 @@ func (i *Importer) importLibrary() (err error) {
|
|||||||
if err := i.search.IndexMediaFile(mf); err != nil {
|
if err := i.search.IndexMediaFile(mf); err != nil {
|
||||||
beego.Error("Error indexing artist:", err)
|
beego.Error("Error indexing artist:", err)
|
||||||
}
|
}
|
||||||
|
mfu++
|
||||||
if !i.lastScan.IsZero() {
|
if !i.lastScan.IsZero() {
|
||||||
beego.Debug("-- Updated Track:", mf.Title)
|
beego.Debug("-- Updated Track:", mf.Title)
|
||||||
}
|
}
|
||||||
@@ -161,6 +168,7 @@ func (i *Importer) importLibrary() (err error) {
|
|||||||
if err := i.search.IndexAlbum(al); err != nil {
|
if err := i.search.IndexAlbum(al); err != nil {
|
||||||
beego.Error("Error indexing artist:", err)
|
beego.Error("Error indexing artist:", err)
|
||||||
}
|
}
|
||||||
|
alu++
|
||||||
if !i.lastScan.IsZero() {
|
if !i.lastScan.IsZero() {
|
||||||
beego.Debug(fmt.Sprintf(`-- Updated Album:"%s" from "%s"`, al.Name, al.Artist))
|
beego.Debug(fmt.Sprintf(`-- Updated Album:"%s" from "%s"`, al.Name, al.Artist))
|
||||||
}
|
}
|
||||||
@@ -206,19 +214,22 @@ func (i *Importer) importLibrary() (err error) {
|
|||||||
beego.Error(err)
|
beego.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c, _ := i.artistRepo.CountAll()
|
arc2, _ := i.artistRepo.CountAll()
|
||||||
beego.Info("Total Artists in database:", c)
|
alc2, _ := i.albumRepo.CountAll()
|
||||||
c, _ = i.albumRepo.CountAll()
|
mfc2, _ := i.mfRepo.CountAll()
|
||||||
beego.Info("Total Albums in database:", c)
|
plc2, _ := i.plsRepo.CountAll()
|
||||||
c, _ = i.mfRepo.CountAll()
|
|
||||||
beego.Info("Total MediaFiles in database:", c)
|
if arc != arc2 || alc != alc2 || mfc != mfc2 || plc != plc2 {
|
||||||
c, _ = i.plsRepo.CountAll()
|
beego.Info(fmt.Sprintf("Updated library totals: %d(%+d) artists, %d(%+d) albums, %d(%+d) songs, %d(%+d) playlists", arc2, arc2-arc, alc2, alc2-alc, mfc2, mfc2-mfc, plc2, plc2-plc))
|
||||||
beego.Info("Total Playlists in database:", c)
|
}
|
||||||
|
if alu > 0 || mfu > 0 {
|
||||||
|
beego.Info(fmt.Sprintf("Updated items: %d album(s), %d song(s)", alu, mfu))
|
||||||
|
}
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
millis := time.Now().UnixNano() / int64(time.Millisecond)
|
millis := time.Now().UnixNano() / int64(time.Millisecond)
|
||||||
i.propertyRepo.Put(consts.LastScan, fmt.Sprint(millis))
|
i.propertyRepo.Put(consts.LastScan, fmt.Sprint(millis))
|
||||||
beego.Info("LastScan timestamp:", millis)
|
beego.Debug("LastScan timestamp:", millis)
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ type plsRelation struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *ItunesScanner) ScanLibrary(lastModifiedSince time.Time, path string) (int, error) {
|
func (s *ItunesScanner) ScanLibrary(lastModifiedSince time.Time, path string) (int, error) {
|
||||||
beego.Info("Checking for updates since", lastModifiedSince.String(), "- Library:", path)
|
beego.Debug("Checking for updates since", lastModifiedSince.String(), "- Library:", path)
|
||||||
xml, _ := os.Open(path)
|
xml, _ := os.Open(path)
|
||||||
l, err := itl.ReadFromXML(xml)
|
l, err := itl.ReadFromXML(xml)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user