People: Improve subject and marker entity logs #22

This commit is contained in:
Michael Mayer 2021-10-06 12:16:52 +02:00
parent 07ae9b83f4
commit 717ea83553
5 changed files with 30 additions and 21 deletions

View file

@ -22,12 +22,22 @@ import (
var log = event.Log
var GeoApi = "places"
// logError logs the message if the argument is an error.
func logError(result *gorm.DB) {
if result.Error != nil {
log.Error(result.Error.Error())
}
}
// TypeString returns an entity type string for logging.
func TypeString(entityType string) string {
if entityType == "" {
return "unknown"
}
return entityType
}
type Types map[string]interface{}
// Entities contains database entities and their table names.

View file

@ -4,6 +4,8 @@ import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/sirupsen/logrus"
)
@ -22,3 +24,8 @@ func TestMain(m *testing.M) {
os.Exit(code)
}
func TestTypeString(t *testing.T) {
assert.Equal(t, "unknown", TypeString(""))
assert.Equal(t, "foo", TypeString("foo"))
}

View file

@ -372,13 +372,7 @@ func (m *Marker) InvalidArea() error {
return nil
}
msg := fmt.Sprintf("invalid %s area x=%d%% y=%d%% w=%d%% h=%d%%", txt.Quote(m.MarkerType), int(m.X*100), int(m.Y*100), int(m.W*100), int(m.H*100))
if m.MarkerUID == "" {
return fmt.Errorf("markers: %s", msg)
} else {
return fmt.Errorf("marker %s: %s", m.MarkerUID, msg)
}
return fmt.Errorf("invalid %s crop area x=%d%% y=%d%% w=%d%% h=%d%%", TypeString(m.MarkerType), int(m.X*100), int(m.Y*100), int(m.W*100), int(m.H*100))
}
// Save updates the existing or inserts a new row.
@ -712,15 +706,13 @@ func CreateMarkerIfNotExists(m *Marker) (*Marker, error) {
if m.MarkerUID != "" {
return m, nil
} else if err := Db().Where(`file_uid = ? AND thumb = ? AND marker_type = ?`,
m.FileUID, m.Thumb, m.MarkerType).First(&result).Error; err == nil {
log.Infof("markers: %s marker %s already exists for %s", txt.Quote(m.MarkerType), txt.Quote(result.MarkerUID), txt.Quote(result.FileUID))
return &result, err
} else if Db().Where(`file_uid = ? AND marker_type = ? AND thumb = ?`, m.FileUID, m.MarkerType, m.Thumb).
First(&result).Error == nil {
return &result, nil
} else if err := m.Create(); err != nil {
log.Warnf("markers: %s while adding %s", err, txt.Quote(m.MarkerType))
return m, err
} else {
log.Debugf("markers: added %s marker %s for %s", txt.Quote(m.MarkerType), txt.Quote(m.MarkerUID), txt.Quote(m.FileUID))
log.Debugf("markers: added %s marker %s for %s", TypeString(m.MarkerType), txt.Quote(m.MarkerUID), txt.Quote(m.FileUID))
}
return m, nil

View file

@ -225,9 +225,9 @@ func TestMarker_InvalidArea(t *testing.T) {
})
t.Run("InvalidArea1", func(t *testing.T) {
m := NewMarker(FileFixtures.Get("exampleFileName.jpg"), invalidArea1, "lt9k3pw1wowuy3c4", SrcImage, MarkerFace, 100, 65)
assert.EqualError(t, m.InvalidArea(), "markers: invalid face area x=-100% y=20% w=35% h=35%")
assert.EqualError(t, m.InvalidArea(), "invalid face crop area x=-100% y=20% w=35% h=35%")
m.MarkerUID = "m345634636"
assert.EqualError(t, m.InvalidArea(), "marker m345634636: invalid face area x=-100% y=20% w=35% h=35%")
assert.EqualError(t, m.InvalidArea(), "invalid face crop area x=-100% y=20% w=35% h=35%")
m.MarkerType = MarkerUnknown
assert.Nil(t, m.InvalidArea())
})
@ -287,7 +287,7 @@ func TestMarker_Save(t *testing.T) {
if err := m.Save(); err == nil {
t.Fatal("error expected")
} else {
assert.Equal(t, "markers: invalid face area x=-100% y=0% w=20% h=13%", err.Error())
assert.Equal(t, "invalid face crop area x=-100% y=0% w=20% h=13%", err.Error())
}
})
@ -420,7 +420,7 @@ func TestMarker_Create(t *testing.T) {
if err == nil {
t.Fatal("error expected")
} else {
assert.Equal(t, "markers: invalid face area x=0% y=0% w=0% h=0%", err.Error())
assert.Equal(t, "invalid face crop area x=0% y=0% w=0% h=0%", err.Error())
}
})
}

View file

@ -101,7 +101,7 @@ func (m *Subject) Delete() error {
subjectMutex.Lock()
defer subjectMutex.Unlock()
log.Infof("subject: deleting %s %s", txt.Quote(m.SubjType), txt.Quote(m.SubjName))
log.Infof("subject: deleting %s %s", TypeString(m.SubjType), txt.Quote(m.SubjName))
event.EntitiesDeleted("subjects", []string{m.SubjUID})
@ -138,7 +138,7 @@ func (m *Subject) Restore() error {
if m.Deleted() {
m.DeletedAt = nil
log.Infof("subject: restoring %s %s", txt.Quote(m.SubjType), txt.Quote(m.SubjName))
log.Infof("subject: restoring %s %s", TypeString(m.SubjType), txt.Quote(m.SubjName))
event.EntitiesCreated("subjects", []*Subject{m})
@ -176,7 +176,7 @@ func FirstOrCreateSubject(m *Subject) *Subject {
if found := FindSubjectByName(m.SubjName); found != nil {
return found
} else if createErr := m.Create(); createErr == nil {
log.Infof("subject: added %s %s", txt.Quote(m.SubjType), txt.Quote(m.SubjName))
log.Infof("subject: added %s %s", TypeString(m.SubjType), txt.Quote(m.SubjName))
event.EntitiesCreated("subjects", []*Subject{m})
@ -266,7 +266,7 @@ func (m *Subject) UpdateName(name string) (*Subject, error) {
if err := m.SetName(name); err != nil {
return m, err
} else if err := m.Updates(Values{"SubjName": m.SubjName, "SubjSlug": m.SubjSlug}); err == nil {
log.Infof("subject: renamed %s %s", txt.Quote(m.SubjType), txt.Quote(m.SubjName))
log.Infof("subject: renamed %s %s", TypeString(m.SubjType), txt.Quote(m.SubjName))
event.EntitiesUpdated("subjects", []*Subject{m})