mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +00:00
feat(photos): a photo keeps its quality signals, measured once off the event loop
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { EntityRepositoryType, type Opt, type Ref, defineEntity, p } from '@mikro-orm/core';
|
||||
import { TrekPhotoSignalsRepository } from '../repositories/TrekPhotoSignals.repository';
|
||||
import { DbTimestampType } from '../types';
|
||||
import { TrekPhotos } from './TrekPhotos.entity';
|
||||
|
||||
export class TrekPhotoSignals {
|
||||
[EntityRepositoryType]?: TrekPhotoSignalsRepository;
|
||||
id!: number & Opt;
|
||||
photo!: Ref<TrekPhotos>;
|
||||
photo_id!: number;
|
||||
algo!: number;
|
||||
phash?: string | null;
|
||||
sharpness?: number | null;
|
||||
luma?: number | null;
|
||||
clipped?: number | null;
|
||||
orig_width?: number | null;
|
||||
orig_height?: number | null;
|
||||
computed_at!: string & Opt;
|
||||
}
|
||||
|
||||
export const TrekPhotoSignalsSchema = defineEntity({
|
||||
class: TrekPhotoSignals,
|
||||
repository: () => TrekPhotoSignalsRepository,
|
||||
uniques: [{ properties: ['photo'] }],
|
||||
properties: {
|
||||
id: p.integer().primary(),
|
||||
photo: () => p.manyToOne(TrekPhotos).ref().deleteRule('cascade').hidden(),
|
||||
photo_id: p.integer().persist(false),
|
||||
algo: p.integer(),
|
||||
phash: p.text().nullable(),
|
||||
sharpness: p.double().nullable(),
|
||||
luma: p.double().nullable(),
|
||||
clipped: p.double().nullable(),
|
||||
orig_width: p.integer().nullable(),
|
||||
orig_height: p.integer().nullable(),
|
||||
computed_at: p.type(DbTimestampType).defaultRaw(`CURRENT_TIMESTAMP`),
|
||||
},
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { Collection, EntityRepositoryType, type Opt, type Ref, defineEntity, p }
|
||||
import { TrekPhotosRepository } from '../repositories/TrekPhotos.repository';
|
||||
import { DbTimestampType } from '../types';
|
||||
import { JourneyPhotos } from './JourneyPhotos.entity';
|
||||
import { TrekPhotoSignals } from './TrekPhotoSignals.entity';
|
||||
import { TripPhotos } from './TripPhotos.entity';
|
||||
import { Users } from './Users.entity';
|
||||
|
||||
@@ -24,6 +25,7 @@ export class TrekPhotos {
|
||||
lat?: number | null;
|
||||
lng?: number | null;
|
||||
journey_photos_collection = new Collection<JourneyPhotos>(this);
|
||||
trek_photo_signals_collection = new Collection<TrekPhotoSignals>(this);
|
||||
trip_photos_collection = new Collection<TripPhotos>(this);
|
||||
}
|
||||
|
||||
@@ -62,6 +64,7 @@ export const TrekPhotosSchema = defineEntity({
|
||||
lat: p.double().nullable(),
|
||||
lng: p.double().nullable(),
|
||||
journey_photos_collection: () => p.oneToMany(JourneyPhotos).mappedBy('photo').hidden(),
|
||||
trek_photo_signals_collection: () => p.oneToMany(TrekPhotoSignals).mappedBy('photo').hidden(),
|
||||
trip_photos_collection: () => p.oneToMany(TripPhotos).mappedBy('photo').hidden(),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -129,6 +129,7 @@ import { TourTypesSchema } from './TourTypes.entity';
|
||||
import { TourWaypointsSchema } from './TourWaypoints.entity';
|
||||
import { ToursSchema } from './Tours.entity';
|
||||
import { TrekPhotoCacheMetaSchema } from './TrekPhotoCacheMeta.entity';
|
||||
import { TrekPhotoSignalsSchema } from './TrekPhotoSignals.entity';
|
||||
import { TrekPhotosSchema } from './TrekPhotos.entity';
|
||||
import { TripAlbumLinksSchema } from './TripAlbumLinks.entity';
|
||||
import { TripDocumentLinksSchema } from './TripDocumentLinks.entity';
|
||||
@@ -289,6 +290,7 @@ export const ALL_ENTITIES: readonly EntitySchema[] = [
|
||||
TourWaypointsSchema,
|
||||
ToursSchema,
|
||||
TrekPhotoCacheMetaSchema,
|
||||
TrekPhotoSignalsSchema,
|
||||
TrekPhotosSchema,
|
||||
TripAlbumLinksSchema,
|
||||
TripDocumentLinksSchema,
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
|
||||
// change the migration, then run `npm run gen:db-types`.
|
||||
import type { InsertOptional } from './columns';
|
||||
|
||||
export interface TrekPhotoSignalsTable {
|
||||
id: InsertOptional<number>;
|
||||
photo_id: number;
|
||||
algo: number;
|
||||
phash: InsertOptional<string | null>;
|
||||
sharpness: InsertOptional<number | null>;
|
||||
luma: InsertOptional<number | null>;
|
||||
clipped: InsertOptional<number | null>;
|
||||
orig_width: InsertOptional<number | null>;
|
||||
orig_height: InsertOptional<number | null>;
|
||||
computed_at: InsertOptional<string>;
|
||||
}
|
||||
@@ -135,6 +135,7 @@ import type { TourTypesTable } from './TourTypes.table';
|
||||
import type { TourWaypointsTable } from './TourWaypoints.table';
|
||||
import type { ToursTable } from './Tours.table';
|
||||
import type { TrekPhotoCacheMetaTable } from './TrekPhotoCacheMeta.table';
|
||||
import type { TrekPhotoSignalsTable } from './TrekPhotoSignals.table';
|
||||
import type { TrekPhotosTable } from './TrekPhotos.table';
|
||||
import type { TripAlbumLinksTable } from './TripAlbumLinks.table';
|
||||
import type { TripDocumentLinksTable } from './TripDocumentLinks.table';
|
||||
@@ -301,6 +302,7 @@ export interface DB {
|
||||
tour_waypoints: TourWaypointsTable;
|
||||
tours: ToursTable;
|
||||
trek_photo_cache_meta: TrekPhotoCacheMetaTable;
|
||||
trek_photo_signals: TrekPhotoSignalsTable;
|
||||
trek_photos: TrekPhotosTable;
|
||||
trip_album_links: TripAlbumLinksTable;
|
||||
trip_document_links: TripDocumentLinksTable;
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
/**
|
||||
* What a photo's pixels say, measured once so the assistant can pick photos
|
||||
* for a TREK Studio book without looking at them: a perceptual hash (`phash`,
|
||||
* 64 bits as 16 hex digits) to find near duplicates, how sharp it is
|
||||
* (`sharpness`, the variance of a Laplacian over the grey image), its mean
|
||||
* brightness (`luma`, 0 to 1) and the share of its pixels that are burnt out
|
||||
* or crushed black (`clipped`, 0 to 1), and the original's size in pixels
|
||||
* (`orig_width`, `orig_height`) for the print check.
|
||||
*
|
||||
* `algo` is the version of the measurement a row was made with, so a better
|
||||
* one later measures again instead of mixing the two. A null signal is one
|
||||
* that could not be measured (a format TREK cannot decode, a photo too large to
|
||||
* read), which is an answer too: the photo is not measured again.
|
||||
*
|
||||
* One row per photo, `photo_id UNIQUE` beside its own id rather than the photo
|
||||
* id as key: a key that is also a foreign key maps to a one-to-one on both
|
||||
* entities (see `trip_journal_links`). The row goes with its photo.
|
||||
*/
|
||||
export class Migration20200101046240_a_photo_keeps_its_quality_signals extends Migration {
|
||||
override name = 'Migration20200101046240_a_photo_keeps_its_quality_signals';
|
||||
|
||||
override up(): void {
|
||||
this.addSql(`
|
||||
CREATE TABLE IF NOT EXISTS trek_photo_signals (
|
||||
id INTEGER PRIMARY KEY,
|
||||
photo_id INTEGER NOT NULL UNIQUE REFERENCES trek_photos(id) ON DELETE CASCADE,
|
||||
algo INTEGER NOT NULL,
|
||||
phash TEXT,
|
||||
sharpness REAL,
|
||||
luma REAL,
|
||||
clipped REAL,
|
||||
orig_width INTEGER,
|
||||
orig_height INTEGER,
|
||||
computed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
import type { TrekPhotoSignals } from '../entities/TrekPhotoSignals.entity';
|
||||
import type { DB } from '../kysely/db';
|
||||
import { dbNow } from '../types';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
|
||||
type SignalsDB = Pick<DB, 'trek_photo_signals' | 'trek_photos' | 'journey_photos'>;
|
||||
|
||||
/** What one measurement of a photo found; a null is a signal that could not be measured. */
|
||||
export interface TrekPhotoSignalWrite {
|
||||
photo_id: number;
|
||||
algo: number;
|
||||
phash: string | null;
|
||||
sharpness: number | null;
|
||||
luma: number | null;
|
||||
clipped: number | null;
|
||||
orig_width: number | null;
|
||||
orig_height: number | null;
|
||||
}
|
||||
|
||||
/** A stored measurement, with when it was made. */
|
||||
export interface TrekPhotoSignalRow extends TrekPhotoSignalWrite {
|
||||
computed_at: string;
|
||||
}
|
||||
|
||||
/** A photo of a journey's gallery as the photo picker reads it: no bytes, no paths. */
|
||||
export interface GalleryPhotoFacts {
|
||||
photo_id: number;
|
||||
caption: string | null;
|
||||
provider: string;
|
||||
media_type: string;
|
||||
width: number | null;
|
||||
height: number | null;
|
||||
taken_at: string | null;
|
||||
lat: number | null;
|
||||
lng: number | null;
|
||||
}
|
||||
|
||||
/** SQLite binds at most so many values per statement; a gallery can hold more photos. */
|
||||
const IN_CHUNK = 400;
|
||||
|
||||
/**
|
||||
* `trek_photo_signals`: what a photo's pixels say, one row per photo, written
|
||||
* by `PhotoSignalsService` once its worker has measured the photo and read
|
||||
* again whenever photos are picked for a book. A new measurement of a photo
|
||||
* replaces the old one (upsert on `photo_id`).
|
||||
*/
|
||||
export class TrekPhotoSignalsRepository extends TrekRepository<TrekPhotoSignals> {
|
||||
private db() {
|
||||
return this.kysely<SignalsDB>();
|
||||
}
|
||||
|
||||
/** The measurements stored for these photos, whatever version made them; a photo without one is left out. */
|
||||
async listForPhotos(photoIds: number[]): Promise<TrekPhotoSignalRow[]> {
|
||||
const out: TrekPhotoSignalRow[] = [];
|
||||
for (let i = 0; i < photoIds.length; i += IN_CHUNK) {
|
||||
const rows = await this.db()
|
||||
.selectFrom('trek_photo_signals')
|
||||
.select([
|
||||
'photo_id',
|
||||
'algo',
|
||||
'phash',
|
||||
'sharpness',
|
||||
'luma',
|
||||
'clipped',
|
||||
'orig_width',
|
||||
'orig_height',
|
||||
'computed_at',
|
||||
])
|
||||
.where('photo_id', 'in', photoIds.slice(i, i + IN_CHUNK))
|
||||
.execute();
|
||||
out.push(...rows);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Stores a measurement, replacing the photo's earlier one. */
|
||||
async saveSignals(row: TrekPhotoSignalWrite): Promise<void> {
|
||||
const computedAt = dbNow();
|
||||
await this.db()
|
||||
.insertInto('trek_photo_signals')
|
||||
.values({ ...row, computed_at: computedAt })
|
||||
.onConflict((oc) =>
|
||||
oc.column('photo_id').doUpdateSet({
|
||||
algo: row.algo,
|
||||
phash: row.phash,
|
||||
sharpness: row.sharpness,
|
||||
luma: row.luma,
|
||||
clipped: row.clipped,
|
||||
orig_width: row.orig_width,
|
||||
orig_height: row.orig_height,
|
||||
computed_at: computedAt,
|
||||
}),
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* The photos of a journey's gallery with what their rows know about them
|
||||
* (capture time and place, size, kind), in gallery order. The size is a
|
||||
* thumbnail's for an uploaded photo and none for a provider's: good for the
|
||||
* shape, never for print.
|
||||
*/
|
||||
async listGalleryFacts(journeyId: number): Promise<GalleryPhotoFacts[]> {
|
||||
return await this.db()
|
||||
.selectFrom('journey_photos as gp')
|
||||
.innerJoin('trek_photos as tp', 'tp.id', 'gp.photo_id')
|
||||
.select([
|
||||
'gp.photo_id',
|
||||
'gp.caption',
|
||||
'tp.provider',
|
||||
'tp.media_type',
|
||||
'tp.width',
|
||||
'tp.height',
|
||||
'tp.taken_at',
|
||||
'tp.lat',
|
||||
'tp.lng',
|
||||
])
|
||||
.where('gp.journey_id', '=', journeyId)
|
||||
.orderBy('gp.sort_order', 'asc')
|
||||
.orderBy('gp.id', 'asc')
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user