Use new meta query for cameras, simulations

This commit is contained in:
Sam Becker 2024-05-20 11:13:44 -05:00
parent 14ee9b30c9
commit 305bf21915
6 changed files with 9 additions and 66 deletions

View File

@ -14,7 +14,7 @@ import PhotoDetailPage from '@/photo/PhotoDetailPage';
import { ReactNode, cache } from 'react';
import { FilmSimulation } from '@/simulation';
import {
getPhotosFilmSimulationMetaCached,
getPhotosMetaCached,
getPhotosNearIdCached,
} from '@/photo/cache';
@ -70,8 +70,7 @@ export default async function PhotoFilmSimulationPage({
if (!photo) { redirect(PATH_ROOT); }
const { count, dateRange } =
await getPhotosFilmSimulationMetaCached(simulation);
const { count, dateRange } = await getPhotosMetaCached({ simulation });
return <>
{children}

View File

@ -12,7 +12,7 @@ import {
} from '@/site/paths';
import PhotoDetailPage from '@/photo/PhotoDetailPage';
import {
getPhotosCameraMetaCached,
getPhotosMetaCached,
getPhotosNearIdCached,
} from '@/photo/cache';
import {
@ -79,7 +79,7 @@ export default async function PhotoCameraPage({
const camera = cameraFromPhoto(photo, { make, model });
const { count, dateRange } = await getPhotosCameraMetaCached(camera);
const { count, dateRange } = await getPhotosMetaCached({ camera });
return <>
{children}

View File

@ -1,7 +1,7 @@
import { cameraFromPhoto, getCameraFromParams } from '.';
import {
getPhotosCached,
getPhotosCameraMetaCached,
getPhotosMetaCached,
} from '@/photo/cache';
export const getPhotosCameraDataCached = async (
@ -12,7 +12,7 @@ export const getPhotosCameraDataCached = async (
const camera = getCameraFromParams({ make, model });
return Promise.all([
getPhotosCached({ camera, limit }),
getPhotosCameraMetaCached(camera),
getPhotosMetaCached({ camera }),
])
.then(([photos, meta]) => [
photos,

View File

@ -12,10 +12,8 @@ import {
getPhotosCountIncludingHidden,
getUniqueCameras,
getUniqueTags,
getPhotosCameraMeta,
getUniqueTagsHidden,
getUniqueFilmSimulations,
getPhotosFilmSimulationMeta,
getPhotosDateRange,
getPhotosNearId,
getPhotosMostRecentUpdate,
@ -189,18 +187,6 @@ export const getPhotosMostRecentUpdateCached =
[KEY_PHOTOS, KEY_COUNT, KEY_DATE_RANGE],
);
export const getPhotosCameraMetaCached =
unstable_cache(
getPhotosCameraMeta,
[KEY_PHOTOS, KEY_CAMERAS, KEY_DATE_RANGE],
);
export const getPhotosFilmSimulationMetaCached =
unstable_cache(
getPhotosFilmSimulationMeta,
[KEY_PHOTOS, KEY_FILM_SIMULATIONS, KEY_DATE_RANGE],
);
export const getPhotoCached = (...args: Parameters<typeof getPhoto>) =>
unstable_cache(
getPhoto,

View File

@ -199,34 +199,6 @@ const sqlGetPhotosDateRange = async () => sql`
? rows[0] as PhotoDateRange
: undefined);
const sqlGetPhotosCameraMeta = async (camera: Camera) => sql`
SELECT COUNT(*), MIN(taken_at_naive) as start, MAX(taken_at_naive) as end
FROM photos
WHERE
LOWER(REPLACE(make, ' ', '-'))=${parameterize(camera.make, true)} AND
LOWER(REPLACE(model, ' ', '-'))=${parameterize(camera.model, true)} AND
hidden IS NOT TRUE
`.then(({ rows }) => ({
count: parseInt(rows[0].count, 10),
...rows[0]?.start && rows[0]?.end
? { dateRange: rows[0] as PhotoDateRange }
: undefined,
}));
const sqlGetPhotosFilmSimulationMeta = async (
simulation: FilmSimulation,
) => sql`
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 }) => ({
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(*)
FROM photos
@ -553,26 +525,12 @@ export const getPhotosMostRecentUpdate = () =>
'getPhotosMostRecentUpdate',
);
// TAGS
// UNIQUE META
export const getUniqueTags = () =>
safelyQueryPhotos(sqlGetUniqueTags, 'getUniqueTags');
export const getUniqueTagsHidden = () =>
safelyQueryPhotos(sqlGetUniqueTagsHidden, 'getUniqueTagsHidden');
// CAMERAS
export const getUniqueCameras = () =>
safelyQueryPhotos(sqlGetUniqueCameras, 'getUniqueCameras');
export const getPhotosCameraMeta = (camera: Camera) =>
safelyQueryPhotos(
() => sqlGetPhotosCameraMeta(camera),
'getPhotosCameraMeta',
);
// FILM SIMULATIONS
export const getUniqueFilmSimulations = () =>
safelyQueryPhotos(sqlGetUniqueFilmSimulations, 'getUniqueFilmSimulations');
export const getPhotosFilmSimulationMeta =
(simulation: FilmSimulation) => safelyQueryPhotos(
() => sqlGetPhotosFilmSimulationMeta(simulation),
'getPhotosFilmSimulationMeta',
);

View File

@ -1,6 +1,6 @@
import {
getPhotosCached,
getPhotosFilmSimulationMetaCached,
getPhotosMetaCached,
} from '@/photo/cache';
import { FilmSimulation } from '.';
@ -13,5 +13,5 @@ export const getPhotosFilmSimulationDataCached = ({
}) =>
Promise.all([
getPhotosCached({ simulation, limit }),
getPhotosFilmSimulationMetaCached(simulation),
getPhotosMetaCached({ simulation }),
]);