diff --git a/Dockerfile b/Dockerfile index 73b4dcb..ee1deb9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,7 +23,7 @@ COPY --from=build /usr/src/app/sitio/package.json package.real.json RUN sh -c 'echo {\"name\":\"sitio\",\"type\":\"module\",\"dependencies\":$(jq .dependencies < package.real.json)} > package.json' && npm install COPY --from=build /usr/src/app/db-datos node_modules/db-datos COPY --from=build /usr/src/app/sitio/build . -COPY --from=build /usr/src/app/db-datos/drizzle . +COPY --from=build /usr/src/app/db-datos/drizzle drizzle ENV DB_PATH=/db/db.db EXPOSE 3000 diff --git a/db-datos/supermercado.ts b/db-datos/supermercado.ts index 1e167a0..ec4c8c3 100644 --- a/db-datos/supermercado.ts +++ b/db-datos/supermercado.ts @@ -16,6 +16,9 @@ export const hosts: { [host: string]: Supermercado } = { "www.cotodigital3.com.ar": Supermercado.Coto, "www.jumbo.com.ar": Supermercado.Jumbo, }; +export const hostBySupermercado = Object.fromEntries( + Object.entries(hosts).map(([a, b]) => [b, a]) +) as Record; export const colorBySupermercado: { [supermercado in Supermercado]: string } = { [Supermercado.Dia]: "#d52b1e", [Supermercado.Carrefour]: "#19549d", diff --git a/sitio/src/routes/+page.server.ts b/sitio/src/routes/+page.server.ts index dbb1534..ad906c7 100644 --- a/sitio/src/routes/+page.server.ts +++ b/sitio/src/routes/+page.server.ts @@ -1,25 +1,63 @@ import type { PageData, PageServerLoad } from "./$types"; import { getDb, schema } from "$lib/server/db"; const { precios } = schema; -import { sql } from "drizzle-orm"; +import { desc, sql } from "drizzle-orm"; +import { + Supermercado, + hostBySupermercado, + supermercados, +} from "db-datos/supermercado"; let cache: Promise<{ key: Date; data: { precios: Precios } }> = doQuery(); + async function doQuery() { const db = await getDb(); - const q = db + console.time("ean"); + const eans = await db .select({ ean: precios.ean, - name: precios.name, - imageUrl: precios.imageUrl, }) .from(precios) .groupBy(precios.ean) - .having(sql`max(length(name)) and max(parser_version) and in_stock`) .orderBy(sql`random()`) - .limit(150); - const res = await q; - const data = { precios: res }; + .limit(50); + console.timeEnd("ean"); + + return; + + const precioss = await Promise.all( + supermercados.map( + async ( + supermercado, + ): Promise< + [ + Supermercado, + { ean: string; name: string | null; imageUrl: string | null }[], + ] + > => { + const host = hostBySupermercado[supermercado]; + console.time(supermercado); + const q = db + .select({ + ean: precios.ean, + name: precios.name, + imageUrl: precios.imageUrl, + }) + .from(precios) + .groupBy(precios.ean) + .having(sql`max(fetched_at)`) + .where( + sql`ean in ${eans.map((x) => x.ean)} and in_stock and url like ${`%${host}%`}`, + ); + // console.debug(q.toSQL()); + const res = await q; + console.timeEnd(supermercado); + return [supermercado, res]; + }, + ), + ); + const data = { precios: precioss.flatMap(([_, r]) => r) }; return { key: new Date(), data }; } diff --git a/sitio/src/routes/+page.svelte b/sitio/src/routes/+page.svelte index d41f231..6ce7518 100644 --- a/sitio/src/routes/+page.svelte +++ b/sitio/src/routes/+page.svelte @@ -7,6 +7,13 @@ (d): d is { ean: string; name: string; imageUrl: string | null } => !!d.name, ); + $: productos = precios.reduce( + (prev, curr) => [ + ...prev, + ...(prev.find((p) => p.ean === curr.ean) ? [] : [curr]), + ], + [] as { ean: string; name: string; imageUrl: string | null }[], + );

WIP

@@ -39,7 +46,7 @@

Random