Skip to content

Commit

Permalink
Metadata: Add --force flag to optimize command #1736
Browse files Browse the repository at this point in the history
  • Loading branch information
lastzero committed Nov 18, 2021
1 parent 9da2e92 commit 04cde0f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
15 changes: 11 additions & 4 deletions internal/commands/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/entity/photo_optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions internal/entity/photo_optimize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ 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")
} else if len(merged) > 0 {
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)
Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions internal/workers/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/workers/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/workers/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()
Expand Down

0 comments on commit 04cde0f

Please sign in to comment.