/
moments.go
328 lines (266 loc) Β· 9.34 KB
/
moments.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package query
import (
"fmt"
"strings"
"time"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/maps"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/txt"
)
// Moment contains photo counts per month and year
type Moment struct {
Label string `json:"Label"`
Year int `json:"Year"`
Month int `json:"Month"`
State string `json:"State"`
Country string `json:"Country"`
PhotoCount int `json:"PhotoCount"`
}
var MomentLabels = map[string]string{
"park": "Parks & Gardens",
"botanical-garden": "Parks & Gardens",
"water-park": "Parks & Gardens",
"alpine": "Outdoor Adventures",
"hiking": "Outdoor Adventures",
"mountain": "Outdoor Adventures",
"mountains": "Outdoor Adventures",
"camping": "Outdoor Adventures",
"camper": "Outdoor Adventures",
"bench": "Outdoor Adventures",
"bunker": "Outdoor Adventures",
"castle": "Outdoor Adventures",
"viewpoint": "Outdoor Adventures",
"nature-reserve": "Nature & Landscape",
"landscape": "Nature & Landscape",
"nature": "Nature & Landscape",
"flower": "Nature & Landscape",
"field": "Nature & Landscape",
"forest": "Nature & Landscape",
"rocks": "Nature & Landscape",
"valley": "Nature & Landscape",
"bay": "Bays, Capes & Beaches",
"beach": "Bays, Capes & Beaches",
"seashore": "Bays, Capes & Beaches",
"cape": "Bays, Capes & Beaches",
"ship": "Water, Ships & Boats",
"water": "Water, Ships & Boats",
"pier": "Water, Ships & Boats",
"boat": "Water, Ships & Boats",
"boathouse": "Water, Ships & Boats",
"lakeside": "Water, Ships & Boats",
"shark": "Water, Ships & Boats",
"fish": "Water, Ships & Boats",
"jellyfish": "Water, Ships & Boats",
"submarine": "Water, Ships & Boats",
"diving": "Water, Ships & Boats",
"festival": "Festivals & Entertainment",
"nightclub": "Festivals & Entertainment",
"microphone": "Festivals & Entertainment",
"stage": "Festivals & Entertainment",
"theater": "Festivals & Entertainment",
"theme park": "Festivals & Entertainment",
"event": "Festivals & Entertainment",
"wine": "Festivals & Entertainment",
"easter": "Holidays",
"thanksgiving": "Holidays",
"christmas": "Holidays",
"passover": "Holidays",
"shavuot": "Holidays",
"hanukkah": "Holidays",
"cat": "Pets",
"dog": "Pets",
"gerbil": "Pets",
"parakeet": "Pets",
"rabbit": "Pets",
"hamster": "Pets",
}
// MomentLabelsFilter returns the smart filter string for a moment based on a matching label.
func MomentLabelsFilter(label string) string {
// TODO: Needs refactoring
label = strings.SplitN(label, txt.Or, 2)[0]
title := MomentLabels[label]
if title == "" {
return ""
}
var l []string
for i := range MomentLabels {
if MomentLabels[i] == title {
l = append(l, i)
}
}
return strings.Join(txt.UniqueWords(l), txt.Or)
}
// CountryName returns the country name if any.
func (m Moment) CountryName() string {
if m.Country == "" {
return ""
}
return maps.CountryName(m.Country)
}
// Location returns the location name for the moment.
func (m Moment) Location() string {
if state := clean.State(m.State, m.Country); state != "" {
return fmt.Sprintf("%s, %s", state, m.CountryName())
}
return m.CountryName()
}
// Slug returns an identifier string for a moment.
func (m Moment) Slug() (s string) {
state := clean.State(m.State, m.Country)
if state == "" {
return m.TitleSlug()
}
country := maps.CountryName(m.Country)
if m.Year > 1900 && m.Month == 0 {
s = fmt.Sprintf("%s-%s-%04d", country, state, m.Year)
} else if m.Year > 1900 && m.Month > 0 && m.Month <= 12 {
s = fmt.Sprintf("%s-%s-%04d-%02d", country, state, m.Year, m.Month)
} else {
s = fmt.Sprintf("%s-%s", country, state)
}
return txt.Slug(s)
}
// TitleSlug returns an identifier string based on the title.
func (m Moment) TitleSlug() string {
return txt.Slug(m.Title())
}
// Title returns an english title for the moment.
func (m Moment) Title() string {
state := clean.State(m.State, m.Country)
if m.Year == 0 && m.Month == 0 {
if m.Label != "" {
return MomentLabels[strings.SplitN(m.Label, txt.Or, 2)[0]]
}
if state != "" {
return state
}
return m.CountryName()
}
if m.Country != "" && m.Year > 1900 && m.Month == 0 {
if state != "" {
return fmt.Sprintf("%s / %d", state, m.Year)
}
return fmt.Sprintf("%s %d", m.CountryName(), m.Year)
}
if m.Year > 1900 && m.Month > 0 && m.Month <= 12 {
date := time.Date(m.Year, time.Month(m.Month), 1, 0, 0, 0, 0, time.UTC)
if state != "" {
return fmt.Sprintf("%s / %s", state, date.Format("January 2006"))
}
if m.Country == "" {
return date.Format("January 2006")
}
return fmt.Sprintf("%s / %s", m.CountryName(), date.Format("January 2006"))
}
if m.Month > 0 && m.Month <= 12 {
return time.Month(m.Month).String()
}
return m.CountryName()
}
// Moments represents a list of moments.
type Moments []Moment
// MomentsTime counts photos by month and year.
func MomentsTime(threshold int, public bool) (results Moments, err error) {
stmt := UnscopedDb().Table("photos").
Select("photos.photo_year AS year, photos.photo_month AS month, COUNT(*) AS photo_count").
Where("photos.photo_quality >= 3 AND deleted_at IS NULL AND photos.photo_year > 0 AND photos.photo_month > 0")
// Ignore private pictures?
if public {
stmt = stmt.Where("photo_private = 0")
}
stmt = stmt.Group("photos.photo_year, photos.photo_month").
Order("photos.photo_year DESC, photos.photo_month DESC").
Having("photo_count >= ?", threshold)
if err = stmt.Scan(&results).Error; err != nil {
return results, err
}
return results, nil
}
// MomentsCountries returns the most popular countries by year.
func MomentsCountries(threshold int, public bool) (results Moments, err error) {
stmt := UnscopedDb().Table("photos").
Select("photo_year AS year, photo_country AS country, COUNT(*) AS photo_count").
Where("photos.photo_quality >= 3 AND deleted_at IS NULL AND photo_country <> 'zz' AND photo_year > 0")
// Ignore private pictures?
if public {
stmt = stmt.Where("photo_private = 0")
}
stmt = stmt.Group("photo_year, photo_country").
Having("photo_count >= ?", threshold)
if err = stmt.Scan(&results).Error; err != nil {
return results, err
}
return results, nil
}
// MomentsStates returns the most popular states and countries by year.
func MomentsStates(threshold int, public bool) (results Moments, err error) {
stmt := UnscopedDb().Table("photos").
Select("p.place_country AS country, p.place_state AS state, COUNT(*) AS photo_count").
Joins("JOIN places p ON p.id = photos.place_id").
Where("photos.photo_quality >= 3 AND photos.deleted_at IS NULL AND p.place_state <> '' AND p.place_country <> 'zz'")
// Ignore private pictures?
if public {
stmt = stmt.Where("photo_private = 0")
}
stmt = stmt.Group("p.place_country, p.place_state").
Having("photo_count >= ?", threshold)
if err = stmt.Scan(&results).Error; err != nil {
return results, err
}
return results, nil
}
// MomentsLabels returns the most popular photo labels.
func MomentsLabels(threshold int, public bool) (results Moments, err error) {
var labelSlugs []string
for labelSlug := range MomentLabels {
labelSlugs = append(labelSlugs, labelSlug)
}
m := Moments{}
stmt := UnscopedDb().Table("photos").
Select("l.label_slug AS label, COUNT(*) AS photo_count").
Joins("JOIN photos_labels pl ON pl.photo_id = photos.id AND pl.uncertainty < 100").
Joins("JOIN labels l ON l.id = pl.label_id").
Where("photos.photo_quality >= 3 AND photos.deleted_at IS NULL AND l.label_slug IN (?)", labelSlugs)
// Ignore private pictures?
if public {
stmt = stmt.Where("photo_private = 0")
}
stmt = stmt.Group("l.label_slug").
Having("photo_count >= ?", threshold)
if err = stmt.Scan(&m).Error; err != nil {
return m, err
}
done := make(map[string]bool)
for i := 0; i < len(m); i++ {
f := MomentLabelsFilter(m[i].Label)
if _, ok := done[f]; ok {
continue
} else {
done[f] = true
}
m[i].Label = f
results = append(results, m[i])
}
return results, nil
}
// RemoveDuplicateMoments deletes generated albums with duplicate slug or filter.
func RemoveDuplicateMoments() (removed int, err error) {
if res := UnscopedDb().Exec(`DELETE FROM links WHERE share_uid
IN (SELECT a.album_uid FROM albums a JOIN albums b ON a.album_type <> ?
AND a.album_type = b.album_type AND a.id > b.id
WHERE (a.album_slug = b.album_slug OR a.album_filter = b.album_filter)
GROUP BY a.album_uid)`, entity.AlbumManual); res.Error != nil {
return removed, res.Error
}
if res := UnscopedDb().Exec(`DELETE FROM albums WHERE id
IN (SELECT a.id FROM albums a JOIN albums b ON a.album_type <> ?
AND a.album_type = b.album_type AND a.id > b.id
WHERE (a.album_slug = b.album_slug OR a.album_filter = b.album_filter)
GROUP BY a.album_uid)`, entity.AlbumManual); res.Error != nil {
return removed, res.Error
} else if res.RowsAffected > 0 {
removed = int(res.RowsAffected)
}
return removed, nil
}