This commit is contained in:
Cat /dev/Nulo 2024-02-05 22:36:00 -03:00 committed by Nulo
parent 9480c44dfe
commit 749bb0beb2
4 changed files with 58 additions and 10 deletions

View file

@ -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 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/db-datos node_modules/db-datos
COPY --from=build /usr/src/app/sitio/build . 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 ENV DB_PATH=/db/db.db
EXPOSE 3000 EXPOSE 3000

View file

@ -16,6 +16,9 @@ export const hosts: { [host: string]: Supermercado } = {
"www.cotodigital3.com.ar": Supermercado.Coto, "www.cotodigital3.com.ar": Supermercado.Coto,
"www.jumbo.com.ar": Supermercado.Jumbo, "www.jumbo.com.ar": Supermercado.Jumbo,
}; };
export const hostBySupermercado = Object.fromEntries(
Object.entries(hosts).map(([a, b]) => [b, a])
) as Record<Supermercado, string>;
export const colorBySupermercado: { [supermercado in Supermercado]: string } = { export const colorBySupermercado: { [supermercado in Supermercado]: string } = {
[Supermercado.Dia]: "#d52b1e", [Supermercado.Dia]: "#d52b1e",
[Supermercado.Carrefour]: "#19549d", [Supermercado.Carrefour]: "#19549d",

View file

@ -1,25 +1,63 @@
import type { PageData, PageServerLoad } from "./$types"; import type { PageData, PageServerLoad } from "./$types";
import { getDb, schema } from "$lib/server/db"; import { getDb, schema } from "$lib/server/db";
const { precios } = schema; 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(); let cache: Promise<{ key: Date; data: { precios: Precios } }> = doQuery();
async function doQuery() { async function doQuery() {
const db = await getDb(); const db = await getDb();
const q = db console.time("ean");
const eans = await db
.select({ .select({
ean: precios.ean, ean: precios.ean,
name: precios.name,
imageUrl: precios.imageUrl,
}) })
.from(precios) .from(precios)
.groupBy(precios.ean) .groupBy(precios.ean)
.having(sql`max(length(name)) and max(parser_version) and in_stock`)
.orderBy(sql`random()`) .orderBy(sql`random()`)
.limit(150); .limit(50);
const res = await q; console.timeEnd("ean");
const data = { precios: res };
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 }; return { key: new Date(), data };
} }

View file

@ -7,6 +7,13 @@
(d): d is { ean: string; name: string; imageUrl: string | null } => (d): d is { ean: string; name: string; imageUrl: string | null } =>
!!d.name, !!d.name,
); );
$: productos = precios.reduce(
(prev, curr) => [
...prev,
...(prev.find((p) => p.ean === curr.ean) ? [] : [curr]),
],
[] as { ean: string; name: string; imageUrl: string | null }[],
);
</script> </script>
<h1 class="text-xl">WIP</h1> <h1 class="text-xl">WIP</h1>
@ -39,7 +46,7 @@
<section> <section>
<h2 class="text-lg font-bold">Random</h2> <h2 class="text-lg font-bold">Random</h2>
<ul class="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3"> <ul class="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3">
{#each precios as product} {#each productos as product}
<li> <li>
<ProductPreview {product} /> <ProductPreview {product} />
</li> </li>