/
places.go
170 lines (134 loc) Β· 3.95 KB
/
places.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package photoprism
import (
"fmt"
"runtime/debug"
"time"
"github.com/dustin/go-humanize/english"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/internal/query"
)
// Places represents a geo data worker.
type Places struct {
conf *config.Config
}
// NewPlaces returns a new Places worker.
func NewPlaces(conf *config.Config) *Places {
instance := &Places{
conf: conf,
}
return instance
}
// Start runs the Places worker.
func (w *Places) Start() (updated []string, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("index: %s (update locations)\nstack: %s", r, debug.Stack())
log.Error(err)
}
}()
// Check if a worker is already running.
if err = mutex.MainWorker.Start(); err != nil {
log.Warnf("index: %s (update locations)", err.Error())
return []string{}, err
}
defer mutex.MainWorker.Stop()
// Fetch cell IDs from index.
cells, err := query.CellIDs()
// Error?
if err != nil {
return []string{}, err
} else if len(cells) == 0 {
log.Warnf("index: found no locations to update")
return []string{}, nil
}
// List of updated cells.
updated = make([]string, 0, len(cells))
log.Infof("index: retrieving location details")
// Update known locations.
for i, cell := range cells {
if i%10 == 0 {
log.Infof("index: updated %s, %s remaining",
english.Plural(i, "location", "locations"),
english.Plural(len(cells)-i, "location", "locations"))
}
if w.Canceled() {
return updated, nil
} else if cell.ID == "" || cell.ID == entity.UnknownID {
// Skip unknown places.
continue
}
// Create cell from location and place ID.
c := entity.Cell{ID: cell.ID, PlaceID: cell.PlaceID}
// Fetch updated cell data from backend API.
if err = c.Refresh(entity.GeoApi); err != nil {
log.Warnf("index: %s", err)
} else {
// Append if successful.
updated = append(updated, cell.ID)
}
// Short break.
time.Sleep(33 * time.Millisecond)
}
// Remove unused entries from the places table.
if err := query.PurgePlaces(); err != nil {
log.Errorf("index: %s (purge places)", err)
}
// Update location-related photo metadata in the index.
if _, err := w.UpdatePhotos(); err != nil {
log.Errorf("index: %s (update photos)", err)
}
// Update photo counts in places.
if err := entity.UpdatePlacesCounts(); err != nil {
log.Errorf("index: %s (update counts)", err)
}
return updated, err
}
// UpdatePhotos updates all location-related photo metadata in the index.
func (w *Places) UpdatePhotos() (affected int, err error) {
start := time.Now()
var u []string
// Find photos without location.
if err = query.UnscopedDb().
Raw(`SELECT photo_uid FROM photos WHERE place_id <> 'zz' OR photo_lat <> 0 OR photo_lng <> 0 ORDER BY id`).
Pluck("photo_uid", &u).Error; err != nil {
return affected, err
}
n := len(u)
if n == 0 {
log.Debugf("index: found no photos with location [%s]", time.Since(start))
return affected, err
}
log.Infof("index: updating references, titles, and keywords")
for i := 0; i < n; i++ {
if i%10 == 0 {
log.Infof("index: updated %s, %s remaining",
english.Plural(i, "photo", "photos"),
english.Plural(n-i, "photo", "photos"))
}
var model entity.Photo
model, err = query.PhotoByUID(u[i])
if err != nil {
log.Errorf("index: %s while loading %s", err, model.String())
continue
} else if model.NoLatLng() {
log.Debugf("index: photo %s has no location", model.String())
continue
}
if err = model.SaveLocation(); err != nil {
log.Errorf("index: %s while updating %s", err, model.String())
} else {
affected++
}
}
return affected, err
}
// Canceled tests if the worker should be stopped.
func (w *Places) Canceled() bool {
return mutex.MainWorker.Canceled() || mutex.MetaWorker.Canceled()
}
// Cancel stops the current operation.
func (w *Places) Cancel() {
mutex.MainWorker.Cancel()
}