Streamline camera, simulation sql queries

This commit is contained in:
Sam Becker 2024-03-03 15:59:30 -06:00
parent b1a943d6a0
commit 808691cb5e
11 changed files with 42 additions and 91 deletions

View File

@ -59,8 +59,7 @@ export default async function PhotoFilmSimulationPage({
const [ const [
photos, photos,
count, { count, dateRange },
dateRange,
] = await getPhotosFilmSimulationDataCached({ simulation }); ] = await getPhotosFilmSimulationDataCached({ simulation });
return <> return <>

View File

@ -17,8 +17,7 @@ export async function generateMetadata({
}: FilmSimulationProps): Promise<Metadata> { }: FilmSimulationProps): Promise<Metadata> {
const [ const [
photos, photos,
count, { count, dateRange },
dateRange,
] = await getPhotosFilmSimulationDataCached({ ] = await getPhotosFilmSimulationDataCached({
simulation, simulation,
limit: GRID_THUMBNAILS_TO_SHOW_MAX, limit: GRID_THUMBNAILS_TO_SHOW_MAX,

View File

@ -18,8 +18,7 @@ export async function generateMetadata({
}: FilmSimulationProps): Promise<Metadata> { }: FilmSimulationProps): Promise<Metadata> {
const [ const [
photos, photos,
count, { count, dateRange },
dateRange,
] = await getPhotosFilmSimulationDataCached({ ] = await getPhotosFilmSimulationDataCached({
simulation, simulation,
limit: GRID_THUMBNAILS_TO_SHOW_MAX, limit: GRID_THUMBNAILS_TO_SHOW_MAX,

View File

@ -65,8 +65,7 @@ export default async function PhotoCameraPage({
const [ const [
photos, photos,
count, { count, dateRange },
dateRange,
] = await getPhotosCameraDataCached({ camera }); ] = await getPhotosCameraDataCached({ camera });
return <> return <>

View File

@ -20,8 +20,7 @@ export async function generateMetadata({
const [ const [
photos, photos,
count, { count, dateRange },
dateRange,
] = await getPhotosCameraDataCached({ ] = await getPhotosCameraDataCached({
camera, camera,
limit: GRID_THUMBNAILS_TO_SHOW_MAX, limit: GRID_THUMBNAILS_TO_SHOW_MAX,

View File

@ -21,8 +21,7 @@ export async function generateMetadata({
const [ const [
photos, photos,
count, { count, dateRange },
dateRange,
] = await getPhotosCameraDataCached({ ] = await getPhotosCameraDataCached({
camera, camera,
limit: GRID_THUMBNAILS_TO_SHOW_MAX, limit: GRID_THUMBNAILS_TO_SHOW_MAX,

View File

@ -5,8 +5,7 @@ import {
import { Camera } from '.'; import { Camera } from '.';
import { import {
getPhotosCached, getPhotosCached,
getPhotosCameraCountCached, getPhotosCameraMetaCached,
getPhotosCameraDateRangeCached,
} from '@/photo/cache'; } from '@/photo/cache';
import { pathForCamera } from '@/site/paths'; import { pathForCamera } from '@/site/paths';
@ -19,8 +18,7 @@ export const getPhotosCameraDataCached = ({
}) => }) =>
Promise.all([ Promise.all([
getPhotosCached({ camera, limit }), getPhotosCached({ camera, limit }),
getPhotosCameraCountCached(camera), getPhotosCameraMetaCached(camera),
getPhotosCameraDateRangeCached(camera),
]); ]);
export const getPhotosCameraDataCachedWithPagination = async ({ export const getPhotosCameraDataCachedWithPagination = async ({
@ -34,7 +32,7 @@ export const getPhotosCameraDataCachedWithPagination = async ({
}) => { }) => {
const { offset, limit } = getPaginationFromSearchParams(searchParams); const { offset, limit } = getPaginationFromSearchParams(searchParams);
const [photos, count, dateRange] = const [photos, { count, dateRange }] =
await getPhotosCameraDataCached({ await getPhotosCameraDataCached({
camera, camera,
limit: limitProp ?? limit, limit: limitProp ?? limit,

View File

@ -9,16 +9,14 @@ import {
getPhoto, getPhoto,
getPhotos, getPhotos,
getPhotosCount, getPhotosCount,
getPhotosCameraCount,
getPhotosCountIncludingHidden, getPhotosCountIncludingHidden,
getUniqueCameras, getUniqueCameras,
getUniqueTags, getUniqueTags,
getPhotosTagMeta, getPhotosTagMeta,
getPhotosCameraDateRange, getPhotosCameraMeta,
getUniqueTagsHidden, getUniqueTagsHidden,
getUniqueFilmSimulations, getUniqueFilmSimulations,
getPhotosFilmSimulationDateRange, getPhotosFilmSimulationMeta,
getPhotosFilmSimulationCount,
getPhotosDateRange, getPhotosDateRange,
getPhotosNearId, getPhotosNearId,
} from '@/services/vercel-postgres'; } from '@/services/vercel-postgres';
@ -161,35 +159,21 @@ export const getPhotosCountIncludingHiddenCached =
[KEY_PHOTOS, KEY_COUNT, KEY_HIDDEN], [KEY_PHOTOS, KEY_COUNT, KEY_HIDDEN],
); );
export const getPhotosCameraCountCached = (
...args: Parameters<typeof getPhotosCameraCount>
) =>
unstable_cache(
getPhotosCameraCount,
[KEY_PHOTOS, KEY_COUNT, createCameraKey(...args)],
)(...args);
export const getPhotosFilmSimulationCountCached =
unstable_cache(
getPhotosFilmSimulationCount,
[KEY_PHOTOS, KEY_FILM_SIMULATIONS, KEY_COUNT],
);
export const getPhotosTagMetaCached = export const getPhotosTagMetaCached =
unstable_cache( unstable_cache(
getPhotosTagMeta, getPhotosTagMeta,
[KEY_PHOTOS, KEY_TAGS, KEY_DATE_RANGE], [KEY_PHOTOS, KEY_TAGS, KEY_DATE_RANGE],
); );
export const getPhotosCameraDateRangeCached = export const getPhotosCameraMetaCached =
unstable_cache( unstable_cache(
getPhotosCameraDateRange, getPhotosCameraMeta,
[KEY_PHOTOS, KEY_CAMERAS, KEY_DATE_RANGE], [KEY_PHOTOS, KEY_CAMERAS, KEY_DATE_RANGE],
); );
export const getPhotosFilmSimulationDateRangeCached = export const getPhotosFilmSimulationMetaCached =
unstable_cache( unstable_cache(
getPhotosFilmSimulationDateRange, getPhotosFilmSimulationMeta,
[KEY_PHOTOS, KEY_FILM_SIMULATIONS, KEY_DATE_RANGE], [KEY_PHOTOS, KEY_FILM_SIMULATIONS, KEY_DATE_RANGE],
); );

View File

@ -165,22 +165,6 @@ const sqlGetPhotosCountIncludingHidden = async () => sql`
SELECT COUNT(*) FROM photos SELECT COUNT(*) FROM photos
`.then(({ rows }) => parseInt(rows[0].count, 10)); `.then(({ rows }) => parseInt(rows[0].count, 10));
const sqlGetPhotosCameraCount = async (camera: Camera) => sql`
SELECT COUNT(*) FROM photos
WHERE
LOWER(make)=${parameterize(camera.make, true)} AND
LOWER(REPLACE(model, ' ', '-'))=${parameterize(camera.model, true)} AND
hidden IS NOT TRUE
`.then(({ rows }) => parseInt(rows[0].count, 10));
const sqlGetPhotosFilmSimulationCount = async (
simulation: FilmSimulation,
) => sql`
SELECT COUNT(*) FROM photos
WHERE film_simulation=${simulation} AND
hidden IS NOT TRUE
`.then(({ rows }) => parseInt(rows[0].count, 10));
const sqlGetPhotosDateRange = async () => sql` const sqlGetPhotosDateRange = async () => sql`
SELECT MIN(taken_at_naive) as start, MAX(taken_at_naive) as end SELECT MIN(taken_at_naive) as start, MAX(taken_at_naive) as end
FROM photos FROM photos
@ -201,27 +185,33 @@ const sqlGetPhotosTagMeta = async (tag: string) => sql`
: undefined, : undefined,
})); }));
const sqlGetPhotosCameraDateRange = async (camera: Camera) => sql` const sqlGetPhotosCameraMeta = async (camera: Camera) => sql`
SELECT MIN(taken_at_naive) as start, MAX(taken_at_naive) as end SELECT COUNT(*), MIN(taken_at_naive) as start, MAX(taken_at_naive) as end
FROM photos FROM photos
WHERE WHERE
LOWER(make)=${parameterize(camera.make, true)} AND LOWER(make)=${parameterize(camera.make, true)} AND
LOWER(REPLACE(model, ' ', '-'))=${parameterize(camera.model, true)} AND LOWER(REPLACE(model, ' ', '-'))=${parameterize(camera.model, true)} AND
hidden IS NOT TRUE hidden IS NOT TRUE
`.then(({ rows }) => rows[0]?.start && rows[0]?.end `.then(({ rows }) => ({
? rows[0] as PhotoDateRange count: parseInt(rows[0].count, 10),
: undefined); ...rows[0]?.start && rows[0]?.end
? { dateRange: rows[0] as PhotoDateRange }
: undefined,
}));
const sqlGetPhotosFilmSimulationDateRange = async ( const sqlGetPhotosFilmSimulationMeta = async (
simulation: FilmSimulation, simulation: FilmSimulation,
) => sql` ) => sql`
SELECT MIN(taken_at_naive) as start, MAX(taken_at_naive) as end SELECT COUNT(*), MIN(taken_at_naive) as start, MAX(taken_at_naive) as end
FROM photos FROM photos
WHERE film_simulation=${simulation} AND WHERE film_simulation=${simulation} AND
hidden IS NOT TRUE hidden IS NOT TRUE
`.then(({ rows }) => rows[0]?.start && rows[0]?.end `.then(({ rows }) => ({
? rows[0] as PhotoDateRange count: parseInt(rows[0].count, 10),
: undefined); ...rows[0]?.start && rows[0]?.end
? { dateRange: rows[0] as PhotoDateRange }
: undefined,
}));
const sqlGetUniqueTags = async () => sql` const sqlGetUniqueTags = async () => sql`
SELECT DISTINCT unnest(tags) as tag, COUNT(*) SELECT DISTINCT unnest(tags) as tag, COUNT(*)
@ -467,27 +457,17 @@ export const getPhotosTagMeta = (tag: string) =>
// CAMERAS // CAMERAS
export const getUniqueCameras = () => export const getUniqueCameras = () =>
safelyQueryPhotos(sqlGetUniqueCameras, 'getUniqueCameras'); safelyQueryPhotos(sqlGetUniqueCameras, 'getUniqueCameras');
export const getPhotosCameraDateRange = (camera: Camera) => export const getPhotosCameraMeta = (camera: Camera) =>
safelyQueryPhotos( safelyQueryPhotos(
() => sqlGetPhotosCameraDateRange(camera), () => sqlGetPhotosCameraMeta(camera),
'getPhotosCameraDateRange', 'getPhotosCameraMeta',
);
export const getPhotosCameraCount = (camera: Camera) =>
safelyQueryPhotos(
() => sqlGetPhotosCameraCount(camera),
'getPhotosCameraCount',
); );
// FILM SIMULATIONS // FILM SIMULATIONS
export const getUniqueFilmSimulations = () => export const getUniqueFilmSimulations = () =>
safelyQueryPhotos(sqlGetUniqueFilmSimulations, 'getUniqueFilmSimulations'); safelyQueryPhotos(sqlGetUniqueFilmSimulations, 'getUniqueFilmSimulations');
export const getPhotosFilmSimulationDateRange = export const getPhotosFilmSimulationMeta =
(simulation: FilmSimulation) => safelyQueryPhotos( (simulation: FilmSimulation) => safelyQueryPhotos(
() => sqlGetPhotosFilmSimulationDateRange(simulation), () => sqlGetPhotosFilmSimulationMeta(simulation),
'getPhotosFilmSimulationDateRange', 'getPhotosFilmSimulationMeta',
);
export const getPhotosFilmSimulationCount = (simulation: FilmSimulation) =>
safelyQueryPhotos(
() => sqlGetPhotosFilmSimulationCount(simulation),
'getPhotosFilmSimulationCount',
); );

View File

@ -1,7 +1,6 @@
import { import {
getPhotosCached, getPhotosCached,
getPhotosFilmSimulationCountCached, getPhotosFilmSimulationMetaCached,
getPhotosFilmSimulationDateRangeCached,
} from '@/photo/cache'; } from '@/photo/cache';
import { import {
PaginationSearchParams, PaginationSearchParams,
@ -19,8 +18,7 @@ export const getPhotosFilmSimulationDataCached = ({
}) => }) =>
Promise.all([ Promise.all([
getPhotosCached({ simulation, limit }), getPhotosCached({ simulation, limit }),
getPhotosFilmSimulationCountCached(simulation), getPhotosFilmSimulationMetaCached(simulation),
getPhotosFilmSimulationDateRangeCached(simulation),
]); ]);
export const getPhotosFilmSimulationDataCachedWithPagination = async ({ export const getPhotosFilmSimulationDataCachedWithPagination = async ({
@ -34,7 +32,7 @@ export const getPhotosFilmSimulationDataCachedWithPagination = async ({
}) => { }) => {
const { offset, limit } = getPaginationFromSearchParams(searchParams); const { offset, limit } = getPaginationFromSearchParams(searchParams);
const [photos, count, dateRange] = const [photos, { count, dateRange }] =
await getPhotosFilmSimulationDataCached({ await getPhotosFilmSimulationDataCached({
simulation, simulation,
limit: limitProp ?? limit, limit: limitProp ?? limit,

View File

@ -13,13 +13,10 @@ import { capitalizeWords, convertStringToArray } from '@/utility/string';
export const TAG_FAVS = 'favs'; export const TAG_FAVS = 'favs';
export type TagWithMeta = { export type TagsWithMeta = {
tag: string tag: string
count: number count: number
dataRange?: PhotoDateRange }[]
}
export type TagsWithMeta = Omit<TagWithMeta, 'dateRange'>[]
export const formatTag = (tag?: string) => export const formatTag = (tag?: string) =>
capitalizeWords(tag?.replaceAll('-', ' ')); capitalizeWords(tag?.replaceAll('-', ' '));