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 [
photos,
count,
dateRange,
{ count, dateRange },
] = await getPhotosFilmSimulationDataCached({ simulation });
return <>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -9,16 +9,14 @@ import {
getPhoto,
getPhotos,
getPhotosCount,
getPhotosCameraCount,
getPhotosCountIncludingHidden,
getUniqueCameras,
getUniqueTags,
getPhotosTagMeta,
getPhotosCameraDateRange,
getPhotosCameraMeta,
getUniqueTagsHidden,
getUniqueFilmSimulations,
getPhotosFilmSimulationDateRange,
getPhotosFilmSimulationCount,
getPhotosFilmSimulationMeta,
getPhotosDateRange,
getPhotosNearId,
} from '@/services/vercel-postgres';
@ -161,35 +159,21 @@ export const getPhotosCountIncludingHiddenCached =
[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 =
unstable_cache(
getPhotosTagMeta,
[KEY_PHOTOS, KEY_TAGS, KEY_DATE_RANGE],
);
export const getPhotosCameraDateRangeCached =
export const getPhotosCameraMetaCached =
unstable_cache(
getPhotosCameraDateRange,
getPhotosCameraMeta,
[KEY_PHOTOS, KEY_CAMERAS, KEY_DATE_RANGE],
);
export const getPhotosFilmSimulationDateRangeCached =
export const getPhotosFilmSimulationMetaCached =
unstable_cache(
getPhotosFilmSimulationDateRange,
getPhotosFilmSimulationMeta,
[KEY_PHOTOS, KEY_FILM_SIMULATIONS, KEY_DATE_RANGE],
);

View File

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

View File

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

View File

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