mirror of
https://github.com/catdevnull/preciazo.git
synced 2024-11-22 14:16:19 +00:00
WIP
This commit is contained in:
parent
18d916bfcd
commit
a4869d068e
4 changed files with 58 additions and 10 deletions
|
@ -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
|
||||
|
|
|
@ -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<Supermercado, string>;
|
||||
export const colorBySupermercado: { [supermercado in Supermercado]: string } = {
|
||||
[Supermercado.Dia]: "#d52b1e",
|
||||
[Supermercado.Carrefour]: "#19549d",
|
||||
|
|
|
@ -1,12 +1,43 @@
|
|||
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();
|
||||
console.time("ean");
|
||||
const eans = await db
|
||||
.select({
|
||||
ean: precios.ean,
|
||||
})
|
||||
.from(precios)
|
||||
.groupBy(precios.ean)
|
||||
.orderBy(sql`random()`)
|
||||
.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,
|
||||
|
@ -15,11 +46,18 @@ async function doQuery() {
|
|||
})
|
||||
.from(precios)
|
||||
.groupBy(precios.ean)
|
||||
.having(sql`max(length(name)) and max(parser_version) and in_stock`)
|
||||
.orderBy(sql`random()`)
|
||||
.limit(150);
|
||||
.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;
|
||||
const data = { precios: res };
|
||||
console.timeEnd(supermercado);
|
||||
return [supermercado, res];
|
||||
},
|
||||
),
|
||||
);
|
||||
const data = { precios: precioss.flatMap(([_, r]) => r) };
|
||||
return { key: new Date(), data };
|
||||
}
|
||||
|
||||
|
|
|
@ -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 }[],
|
||||
);
|
||||
</script>
|
||||
|
||||
<h1 class="text-xl">WIP</h1>
|
||||
|
@ -39,7 +46,7 @@
|
|||
<section>
|
||||
<h2 class="text-lg font-bold">Random</h2>
|
||||
<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>
|
||||
<ProductPreview {product} />
|
||||
</li>
|
||||
|
|
Loading…
Reference in a new issue