Metadata: Add --force flag to optimize command #1736

This commit is contained in:
Michael Mayer 2021-11-18 03:47:25 +01:00
parent 9da2e92fb6
commit 04cde0f392
6 changed files with 21 additions and 14 deletions

View file

@ -13,12 +13,18 @@ import (
// OptimizeCommand registers the index cli command.
var OptimizeCommand = cli.Command{
Name: "optimize",
Usage: "Updates estimates, titles, and descriptions",
Name: "optimize",
Usage: "Updates estimates, titles, and other metadata",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "update all, including recently estimated",
},
},
Action: optimizeAction,
}
// optimizeAction updates metadata such as titles and estimate.
// optimizeAction updates estimates, titles, and other metadata.
func optimizeAction(ctx *cli.Context) error {
start := time.Now()
@ -38,9 +44,10 @@ func optimizeAction(ctx *cli.Context) error {
log.Infof("config: read-only mode enabled")
}
force := ctx.Bool("force")
worker := workers.NewMeta(conf)
if err := worker.Start(time.Second * 15); err != nil {
if err := worker.Start(time.Second*15, force); err != nil {
return err
} else {
elapsed := time.Since(start)

View file

@ -9,7 +9,7 @@ import (
)
// Optimize photo data, improve if possible.
func (m *Photo) Optimize(mergeMeta, mergeUuid, estimatePlace bool) (updated bool, merged Photos, err error) {
func (m *Photo) Optimize(mergeMeta, mergeUuid, estimatePlace, force bool) (updated bool, merged Photos, err error) {
if !m.HasID() {
return false, merged, errors.New("photo: can't maintain, id is empty")
}
@ -29,7 +29,7 @@ func (m *Photo) Optimize(mergeMeta, mergeUuid, estimatePlace bool) (updated bool
}
if estimatePlace {
m.EstimatePlace(false)
m.EstimatePlace(force)
}
labels := m.ClassifyLabels()

View file

@ -10,7 +10,7 @@ func TestPhoto_Optimize(t *testing.T) {
t.Run("update", func(t *testing.T) {
photo := PhotoFixtures.Get("Photo19")
if updated, merged, err := photo.Optimize(false, false, true); err != nil {
if updated, merged, err := photo.Optimize(false, false, true, false); err != nil {
t.Fatal(err)
} else if !updated {
t.Error("photo should be updated")
@ -18,7 +18,7 @@ func TestPhoto_Optimize(t *testing.T) {
t.Error("no photos should be merged")
}
if updated, merged, err := photo.Optimize(false, false, true); err != nil {
if updated, merged, err := photo.Optimize(false, false, true, false); err != nil {
t.Fatal(err)
} else if updated {
t.Errorf("photo should NOT be updated, merged: %+v", merged)
@ -28,7 +28,7 @@ func TestPhoto_Optimize(t *testing.T) {
})
t.Run("photo without id", func(t *testing.T) {
photo := Photo{}
result, merged, err := photo.Optimize(false, false, true)
result, merged, err := photo.Optimize(false, false, true, false)
assert.Error(t, err)
assert.False(t, result)

View file

@ -30,7 +30,7 @@ func (m *Meta) originalsPath() string {
}
// Start metadata optimization routine.
func (m *Meta) Start(delay time.Duration) (err error) {
func (m *Meta) Start(delay time.Duration, force bool) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("metadata: %s (panic)\nstack: %s", r, debug.Stack())
@ -87,7 +87,7 @@ func (m *Meta) Start(delay time.Duration) (err error) {
done[photo.PhotoUID] = true
updated, merged, err := photo.Optimize(settings.StackMeta(), settings.StackUUID(), settings.Features.Estimates)
updated, merged, err := photo.Optimize(settings.StackMeta(), settings.StackUUID(), settings.Features.Estimates, force)
if err != nil {
log.Errorf("metadata: %s (optimize photo)", err)

View file

@ -23,13 +23,13 @@ func TestPrism_Start(t *testing.T) {
t.Fatal(err)
}
if err := worker.Start(time.Second); err == nil {
if err := worker.Start(time.Second, true); err == nil {
t.Fatal("error expected")
}
mutex.MetaWorker.Stop()
if err := worker.Start(time.Second); err != nil {
if err := worker.Start(time.Second, true); err != nil {
t.Fatal(err)
}
}

View file

@ -83,7 +83,7 @@ func StartMeta(conf *config.Config) {
if !mutex.WorkersBusy() {
go func() {
worker := NewMeta(conf)
if err := worker.Start(time.Minute); err != nil {
if err := worker.Start(time.Minute, false); err != nil {
log.Warnf("metadata: %s", err)
}
}()