-
Notifications
You must be signed in to change notification settings - Fork 1
/
distribution.go
329 lines (298 loc) · 9.95 KB
/
distribution.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
329
package luminosity
import (
"database/sql"
"fmt"
"sort"
"strconv"
"gopkg.in/guregu/null.v3"
)
const (
DayFormat = "2006-01-02"
)
// ----------------------------------------------------------------------
// Distibution Types & Utils
// ----------------------------------------------------------------------
type DistributionEntry struct {
Id int64 `json:"id"`
Label string `json:"label"`
Count int64 `json:"count"`
}
type DistributionList []*DistributionEntry
type DistributionMap map[string]*DistributionEntry
func (l DistributionList) ToMap() (m DistributionMap) {
for _, d := range l {
m[d.Label] = copyDistributionEntry(d)
}
return m
}
func (m DistributionMap) ToList() (d DistributionList) {
for _, e := range m {
d = append(d, e)
}
return d
}
func copyDistributionEntry(d *DistributionEntry) *DistributionEntry {
return &DistributionEntry{
Id: d.Id,
Count: d.Count,
Label: d.Label,
}
}
func MergeDistributions(dists ...DistributionList) DistributionList {
merged := DistributionMap{}
for _, dist := range dists {
for _, entry := range dist {
if target, ok := merged[entry.Label]; ok {
target.Count = target.Count + entry.Count
} else {
merged[entry.Label] = copyDistributionEntry(entry)
}
}
}
list := merged.ToList()
sort.Sort(list)
return list
}
func (l DistributionList) Merge(dists ...DistributionList) DistributionList {
return MergeDistributions(append(dists, l)...)
}
func (l DistributionList) Len() int { return len(l) }
func (l DistributionList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l DistributionList) Less(i, j int) bool { return l[i].Label < l[j].Label }
type distributionConvertor func(*sql.Rows) (*DistributionEntry, error)
func defaultDistributionConvertor(rows *sql.Rows) (*DistributionEntry, error) {
var label null.String
var id, count int64
if err := rows.Scan(&id, &label, &count); err != nil {
return nil, err
}
return &DistributionEntry{
Id: id,
Label: label.String,
Count: count,
}, nil
}
func (c *Catalog) queryDistribution(sql string, fn distributionConvertor) (DistributionList, error) {
rows, err := c.db.query("query_distribution", sql)
if err != nil {
return nil, err
}
defer rows.Close()
return convertDistribution(rows, fn)
}
func convertDistribution(rows *sql.Rows, fn distributionConvertor) (DistributionList, error) {
var entries DistributionList
for rows.Next() {
if entry, err := fn(rows); err != nil {
return nil, err
} else {
entries = append(entries, entry)
}
}
return entries, nil
}
// ----------------------------------------------------------------------
// Distribution Queries
// ----------------------------------------------------------------------
// GetPhotoCountsByDate returns a distribution list of the number of
// photos shot by calendar date for every date present in the
// catalog. Empty dates are NOT represented in the returned list.
func (c *Catalog) GetPhotoCountsByDate() (DistributionList, error) {
const query = `
SELECT 0,
date(captureTime),
count(*)
FROM Adobe_images
GROUP BY date(captureTime)
ORDER BY date(captureTime)
`
return c.queryDistribution(query, defaultDistributionConvertor)
}
type ByDate DistributionList
func (a ByDate) Len() int { return len(a) }
func (a ByDate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDate) Less(i, j int) bool {
return a[i].Label < a[j].Label
}
// GetLensDistribution returns a distribution list indicating the
// number of photos shot with each different lens present in the EXIF
// metadata.
func (c *Catalog) GetLensDistribution() (DistributionList, error) {
const query = `
SELECT LensRef.id_local as id,
LensRef.value as name,
count(LensRef.value) as count
FROM Adobe_images image
JOIN AgharvestedExifMetadata metadata ON image.id_local = metadata.image
LEFT JOIN AgInternedExifLens LensRef ON LensRef.id_local = metadata.lensRef
WHERE id is not null
GROUP BY id
ORDER BY count desc
`
return c.queryDistribution(query, defaultDistributionConvertor)
}
// GetFocalLengthDistribution returns a distribution list indicating
// the number of photos shot at each different local length present in
// the EXIF metadata.
func (c *Catalog) GetFocalLengthDistribution() (DistributionList, error) {
const query = `
SELECT id_local as id,
focalLength as name,
count(id_local) as count
FROM AgHarvestedExifMetadata
WHERE focalLength is not null
GROUP BY focalLength
ORDER BY count DESC
`
return c.queryDistribution(query, defaultDistributionConvertor)
}
// GetCameraDistribution returns a distribution list indicating the
// number of photos shot with each different camera present in the
// EXIF metadata.
func (c *Catalog) GetCameraDistribution() (DistributionList, error) {
const query = `
SELECT Camera.id_local as id,
Camera.value as name,
count(Camera.value) as count
FROM Adobe_images image
JOIN AgharvestedExifMetadata metadata ON image.id_local = metadata.image
LEFT JOIN AgInternedExifCameraModel Camera ON Camera.id_local = metadata.cameraModelRef
WHERE id is not null
GROUP BY id
ORDER BY count desc
`
return c.queryDistribution(query, defaultDistributionConvertor)
}
// GetApertureDistribution returns a distribution list indicating the
// number of photos shot with each aperture setting present in the
// EXIF metadata.
func (c *Catalog) GetApertureDistribution() (DistributionList, error) {
const query = `
SELECT aperture,
count(aperture)
FROM AgHarvestedExifMetadata
WHERE aperture is not null
GROUP BY aperture
ORDER BY aperture
`
return c.queryDistribution(query, func(row *sql.Rows) (*DistributionEntry, error) {
var aperture float64
var count int64
if err := row.Scan(&aperture, &count); err != nil {
return nil, err
}
return &DistributionEntry{
Label: fmt.Sprintf("%.1f", ApertureToFNumber(aperture)),
Count: count,
}, nil
})
}
// GetExposureTimeDistribution returns a distribution list indicating
// the number of photos shot with each different exposure time
// (shutter speed) setting present in the EXIF metadata.
func (c *Catalog) GetExposureTimeDistribution() (DistributionList, error) {
const query = `
SELECT shutterSpeed,
count(shutterSpeed)
FROM AgHarvestedExifMetadata
WHERE shutterSpeed is not null
GROUP BY shutterSpeed
ORDER BY shutterSpeed
`
return c.queryDistribution(query, func(row *sql.Rows) (*DistributionEntry, error) {
var shutter float64
var count int64
if err := row.Scan(&shutter, &count); err != nil {
return nil, err
}
return &DistributionEntry{
Label: ShutterSpeedToExposureTime(shutter),
Count: count,
}, nil
})
}
// GetEditCountDistribution returns a distribution list grouping
// counts of photos according to the number of edits that have been
// made to them (e.g. N photos have 1 edit, M photos have 2 edits, NN
// photos have 12 edits, etc....)
func (c *Catalog) GetEditCountDistribution() (DistributionList, error) {
const query = `
SELECT edit_count as id,
edit_count as label,
count(*) as count
FROM (
SELECT count(*) as edit_count,
image
FROM Adobe_libraryImageDevelopHistoryStep
GROUP BY image
ORDER BY edit_count DESC
)
WHERE edit_count > 1
GROUP BY edit_count
`
return c.queryDistribution(query, defaultDistributionConvertor)
}
// GetKeywordDistribution returns a distribution list indicating the
// number of photos tagged with each keyword present in the catalog.
func (c *Catalog) GetKeywordDistribution() (DistributionList, error) {
const query = `
SELECT k.id_local as id,
k.name as label,
p.occurrences as count
FROM AgLibraryKeywordPopularity p
INNER JOIN AgLibraryKeyword k
ON p.tag = k.id_local
ORDER BY p.occurrences desc
`
return c.queryDistribution(query, defaultDistributionConvertor)
}
// GetSunburstStats returns a list of rows of the number of photos
// shot grouped by multiple criteria, suitable for transforming into a
// tree structure capable of feeding a sunburst graph
// representation. The data is not re-organized into a tree here in
// order to allow one set of data to be repartitioned at runtime in a
// web UI (see the accompaning luminosity.js Javascript code).
func (c *Catalog) GetSunburstStats() ([]map[string]string, error) {
const query = `
SELECT count(*) as count,
image.id_local as id,
Camera.Value as camera,
Lens.value as lens,
exif.aperture as aperture,
exif.focalLength as focal_length,
exif.shutterSpeed as exposure
FROM Adobe_images image
JOIN AgharvestedExifMetadata exif ON image.id_local = exif.image
LEFT JOIN AgInternedExifLens Lens ON Lens.id_Local = exif.lensRef
LEFT JOIN AgInternedExifCameraModel Camera ON Camera.id_local = exif.cameraModelRef
WHERE camera is not null and lens is not null
GROUP BY camera, lens, aperture, focal_length, exposure
ORDER BY camera, lens, aperture, focal_length, exposure, count
`
if data, err := c.db.queryStringMap("sunburst_stats", query); err != nil {
return data, err
} else {
for _, record := range data {
// Need to convert the APEX aperture values to f-numbers
// and the exposure time to shutter speed
if apertureStr, ok := record["aperture"]; ok && apertureStr != "" {
aperture, err := strconv.ParseFloat(apertureStr, 64)
if err != nil {
return data, err
}
record["aperture"] = fmt.Sprintf("f/%.1f", ApertureToFNumber(aperture))
}
if exposureStr, ok := record["exposure"]; ok && exposureStr != "" {
exposure, err := strconv.ParseFloat(exposureStr, 64)
if err != nil {
return data, err
}
record["exposure"] = ShutterSpeedToExposureTime(exposure)
}
if focalLength, ok := record["focal_length"]; ok && focalLength != "" {
record["focal_length"] = focalLength + "mm"
}
}
return data, nil
}
}