2020-05-29 12:21:17 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gosimple/slug"
|
|
|
|
"github.com/photoprism/photoprism/internal/maps"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Moment contains photo counts per month and year
|
|
|
|
type Moment struct {
|
2020-05-30 01:41:47 +02:00
|
|
|
Label string `json:"Label"`
|
2020-05-29 18:04:30 +02:00
|
|
|
Country string `json:"Country"`
|
|
|
|
State string `json:"State"`
|
|
|
|
Year int `json:"Year"`
|
|
|
|
Month int `json:"Month"`
|
|
|
|
PhotoCount int `json:"PhotoCount"`
|
2020-05-29 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
var MomentLabels = map[string]string{
|
2020-05-29 18:04:30 +02:00
|
|
|
"botanical-garden": "Botanical Gardens",
|
|
|
|
"nature-reserve": "Nature & Landscape",
|
|
|
|
"landscape": "Nature & Landscape",
|
|
|
|
"nature": "Nature & Landscape",
|
|
|
|
"bay": "Bays, Capes & Beaches",
|
|
|
|
"beach": "Bays, Capes & Beaches",
|
2020-05-30 21:11:56 +02:00
|
|
|
"seashore": "Bays, Capes & Beaches",
|
2020-05-29 18:04:30 +02:00
|
|
|
"cape": "Bays, Capes & Beaches",
|
2020-06-05 11:09:08 +02:00
|
|
|
"cat": "Pets",
|
|
|
|
"dog": "Pets",
|
2020-05-29 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Slug returns an identifier string for a moment.
|
|
|
|
func (m Moment) Slug() string {
|
|
|
|
return slug.Make(m.Title())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Title returns an english title for the moment.
|
|
|
|
func (m Moment) Title() string {
|
2020-05-29 18:04:30 +02:00
|
|
|
if m.Year == 0 && m.Month == 0 {
|
2020-05-30 01:41:47 +02:00
|
|
|
if m.Label != "" {
|
|
|
|
return MomentLabels[m.Label]
|
2020-05-29 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
country := maps.CountryName(m.Country)
|
2020-05-29 12:21:17 +02:00
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if strings.Contains(m.State, country) {
|
|
|
|
return m.State
|
2020-05-29 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if m.State == "" {
|
|
|
|
return m.Country
|
2020-05-29 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
return fmt.Sprintf("%s / %s", m.State, country)
|
2020-05-29 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if m.Country != "" && m.Year > 1900 && m.Month == 0 {
|
|
|
|
if m.State != "" {
|
|
|
|
return fmt.Sprintf("%s / %s / %d", m.State, maps.CountryName(m.Country), m.Year)
|
2020-05-29 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
return fmt.Sprintf("%s %d", maps.CountryName(m.Country), m.Year)
|
2020-05-29 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
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)
|
2020-05-29 12:21:17 +02:00
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if m.Country == "" {
|
2020-05-29 12:21:17 +02:00
|
|
|
return date.Format("January 2006")
|
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
return fmt.Sprintf("%s / %s", maps.CountryName(m.Country), date.Format("January 2006"))
|
2020-05-29 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
if m.Month > 0 && m.Month <= 12 {
|
|
|
|
return time.Month(m.Month).String()
|
2020-05-29 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
return maps.CountryName(m.Country)
|
2020-05-29 12:21:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 18:04:30 +02:00
|
|
|
// A list of moments.
|
2020-05-29 12:21:17 +02:00
|
|
|
type Moments []Moment
|
|
|
|
|
|
|
|
// MomentsTime counts photos by month and year.
|
|
|
|
func MomentsTime(threshold int) (results Moments, err error) {
|
|
|
|
db := UnscopedDb().Table("photos").
|
2020-05-29 18:04:30 +02:00
|
|
|
Select("photos.photo_year AS year, photos.photo_month AS month, COUNT(*) AS photo_count").
|
2020-06-05 10:59:59 +02:00
|
|
|
Where("photos.photo_quality >= 3 AND deleted_at IS NULL AND photo_private = 0 AND photos.photo_year > 0 AND photos.photo_month > 0").
|
2020-05-29 12:21:17 +02:00
|
|
|
Group("photos.photo_year, photos.photo_month").
|
|
|
|
Order("photos.photo_year DESC, photos.photo_month DESC").
|
|
|
|
Having("photo_count >= ?", threshold)
|
|
|
|
|
|
|
|
if err := db.Scan(&results).Error; err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MomentsCountries returns the most popular countries by year.
|
|
|
|
func MomentsCountries(threshold int) (results Moments, err error) {
|
|
|
|
db := UnscopedDb().Table("photos").
|
2020-05-29 18:04:30 +02:00
|
|
|
Select("photo_country AS country, photo_year AS year, COUNT(*) AS photo_count ").
|
2020-06-05 10:59:59 +02:00
|
|
|
Where("photos.photo_quality >= 3 AND deleted_at IS NULL AND photo_private = 0 AND photo_country <> 'zz' AND photo_year > 0").
|
2020-05-29 12:21:17 +02:00
|
|
|
Group("photo_country, photo_year").
|
|
|
|
Having("photo_count >= ?", threshold)
|
|
|
|
|
|
|
|
if err := db.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) (results Moments, err error) {
|
|
|
|
db := UnscopedDb().Table("photos").
|
2020-05-29 18:04:30 +02:00
|
|
|
Select("p.loc_country AS country, p.loc_state AS state, COUNT(*) AS photo_count").
|
2020-05-29 12:56:24 +02:00
|
|
|
Joins("JOIN places p ON p.id = photos.place_id").
|
2020-06-05 10:59:59 +02:00
|
|
|
Where("photos.photo_quality >= 3 AND photos.deleted_at IS NULL AND photo_private = 0 AND p.loc_state <> '' AND p.loc_country <> 'zz'").
|
2020-05-29 18:04:30 +02:00
|
|
|
Group("p.loc_country, p.loc_state").
|
2020-05-29 12:21:17 +02:00
|
|
|
Having("photo_count >= ?", threshold)
|
|
|
|
|
|
|
|
if err := db.Scan(&results).Error; err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
// MomentsLabels returns the most popular photo labels.
|
|
|
|
func MomentsLabels(threshold int) (results Moments, err error) {
|
2020-05-29 12:21:17 +02:00
|
|
|
var cats []string
|
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
for cat, _ := range MomentLabels {
|
2020-05-29 12:21:17 +02:00
|
|
|
cats = append(cats, cat)
|
|
|
|
}
|
|
|
|
|
|
|
|
db := UnscopedDb().Table("photos").
|
2020-05-30 01:41:47 +02:00
|
|
|
Select("l.label_slug AS label, COUNT(*) AS photo_count").
|
2020-05-29 18:04:30 +02:00
|
|
|
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").
|
2020-06-05 10:59:59 +02:00
|
|
|
Where("photos.photo_quality >= 3 AND photos.deleted_at IS NULL AND photo_private = 0 AND l.label_slug IN (?)", cats).
|
2020-05-29 18:04:30 +02:00
|
|
|
Group("l.label_slug").
|
2020-05-29 12:21:17 +02:00
|
|
|
Having("photo_count >= ?", threshold)
|
|
|
|
|
|
|
|
if err := db.Scan(&results).Error; err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|