Backend: Improve places logging #399

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-07-16 14:00:22 +02:00
parent 4656ba42a0
commit 6c13b75562
2 changed files with 25 additions and 20 deletions

View file

@ -50,7 +50,7 @@ func (m *Cell) Find(api string) error {
db := Db()
if err := db.Preload("Place").First(m, "id = ?", m.ID).Error; err == nil {
log.Debugf("places: found cell id %s (%+v)", m.ID, m)
log.Debugf("places: found cell id %s", m.ID)
return nil
}
@ -59,7 +59,7 @@ func (m *Cell) Find(api string) error {
}
if err := l.QueryApi(api); err != nil {
log.Errorf("places: %s in cell id %s", err, m.ID)
log.Errorf("places: %s (query cell id %s)", err, m.ID)
return err
}
@ -76,7 +76,7 @@ func (m *Cell) Find(api string) error {
PhotoCount: 1,
}
if err := place.Create(); err == nil {
if createErr := place.Create(); createErr == nil {
event.Publish("count.places", event.Data{
"count": 1,
})
@ -87,7 +87,7 @@ func (m *Cell) Find(api string) error {
} else if found := FindPlace(l.PrefixedToken(), l.Label()); found != nil {
m.Place = found
} else {
log.Errorf("places: %s in place id %s", err, place.ID)
log.Errorf("places: %s (create place id %s)", createErr, place.ID)
m.Place = &UnknownPlace
}
}
@ -96,12 +96,13 @@ func (m *Cell) Find(api string) error {
m.CellName = l.Name()
m.CellCategory = l.Category()
if err := db.Create(m).Error; err == nil {
if createErr := db.Create(m).Error; createErr == nil {
log.Debugf("places: added cell id %s [%s]", m.ID, time.Since(start))
return nil
} else if err := db.Preload("Place").First(m, "id = ?", m.ID).Error; err != nil {
log.Errorf("places: %s in cell id %s [%s]", err, m.ID, time.Since(start))
return err
} else if findErr := db.Preload("Place").First(m, "id = ?", m.ID).Error; findErr != nil {
log.Errorf("places: %s (create cell id %s)", createErr, m.ID)
log.Errorf("places: %s (find cell id %s)", findErr, m.ID)
return createErr
} else {
log.Debugf("places: found cell id %s after trying again [%s]", m.ID, time.Since(start))
}
@ -122,20 +123,20 @@ func FirstOrCreateCell(m *Cell) *Cell {
}
if m.PlaceID == "" {
log.Errorf("places: place id must not be empty in %s (first or create)", m.ID)
log.Errorf("places: place id must not be empty (first or create cell id %s)", m.ID)
return nil
}
result := Cell{}
if findErr := Db().Where("id = ?", m.ID).First(&result).Error; findErr == nil {
if findErr := Db().Where("id = ?", m.ID).Preload("Place").First(&result).Error; findErr == nil {
return &result
} else if createErr := m.Create(); createErr == nil {
return m
} else if err := Db().Where("id = ?", m.ID).First(&result).Error; err == nil {
} else if err := Db().Where("id = ?", m.ID).Preload("Place").First(&result).Error; err == nil {
return &result
} else {
log.Errorf("places: %s in cell id %s (first or create)", createErr, m.ID)
log.Errorf("places: %s (first or create cell id %s)", createErr, m.ID)
}
return nil

View file

@ -41,19 +41,23 @@ func CreateUnknownPlace() {
// FindPlace finds a matching place or returns nil.
func FindPlace(id string, label string) *Place {
place := &Place{}
place := Place{}
if label == "" {
if err := Db().First(place, "id = ?", id).Error; err != nil {
if err := Db().Where("id = ?", id).First(&place).Error; err != nil {
log.Debugf("places: %s (find place id %s)", err.Error(), id)
return nil
} else {
return &place
}
} else if err := Db().First(place, "id = ? OR place_label = ?", id, label).Error; err != nil {
log.Debugf("places: %s (find place id %s, label %s)", err.Error(), id, txt.Quote(label))
return nil
}
return place
if err := Db().Where("id = ? OR place_label = ?", id, label).First(&place).Error; err != nil {
log.Debugf("places: %s (find place id %s, label %s)", err.Error(), id, txt.Quote(label))
return nil
} else {
return &place
}
}
// Find fetches entity values from the database the primary key.
@ -78,7 +82,7 @@ func FirstOrCreatePlace(m *Place) *Place {
}
if m.PlaceLabel == "" {
log.Errorf("places: label must not be empty in %s (first or create)", m.ID)
log.Errorf("places: label must not be empty (first or create place id %s)", m.ID)
return nil
}
@ -91,7 +95,7 @@ func FirstOrCreatePlace(m *Place) *Place {
} else if err := Db().Where("id = ? OR place_label = ?", m.ID, m.PlaceLabel).First(&result).Error; err == nil {
return &result
} else {
log.Errorf("places: %s in %s (first or create)", createErr, m.ID)
log.Errorf("places: %s (create place id %s)", createErr, m.ID)
}
return nil