This commit is contained in:
Cat /dev/Nulo 2024-08-04 14:48:54 -03:00
parent d38b2a8cb0
commit a3bdc59b73
4 changed files with 37 additions and 10 deletions

View file

@ -42,6 +42,9 @@ importers:
drizzle-orm:
specifier: ^0.32.0
version: 0.32.0(@types/better-sqlite3@7.6.9)(better-sqlite3@11.1.2)
ky:
specifier: ^1.5.0
version: 1.5.0
zod:
specifier: ^3.22.4
version: 3.22.4
@ -1224,6 +1227,10 @@ packages:
resolution: {integrity: sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==}
engines: {node: '>=6'}
ky@1.5.0:
resolution: {integrity: sha512-bkQo+UqryW6Zmo/DsixYZE4Z9t2mzvNMhceyIhuMuInb3knm5Q+GNGMKveydJAj+Z6piN1SwI6eR/V0G+Z0BtA==}
engines: {node: '>=18'}
lilconfig@2.1.0:
resolution: {integrity: sha512-utWOt/GHzuUxnLKxB6dk81RoOeoNeHgbrXiuGk4yyF5qlRz+iIVWu56E2fqGHFrXz0QNUhLB/8nKqvRH66JKGQ==}
engines: {node: '>=10'}
@ -2675,6 +2682,8 @@ snapshots:
kleur@4.1.5: {}
ky@1.5.0: {}
lilconfig@2.1.0: {}
lilconfig@3.1.1: {}

View file

@ -214,6 +214,20 @@ order by fetched_at
Json(precios)
}
async fn get_info(State(pool): State<SqlitePool>) -> impl IntoResponse {
#[derive(Serialize)]
struct Info {
count: i64,
}
let count = sqlx::query!("select count(distinct ean) as count from precios")
.fetch_one(&pool)
.await
.unwrap()
.count;
Json(Info { count })
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
@ -252,6 +266,7 @@ async fn main() {
.route("/api/healthcheck", get(healthcheck))
.route("/api/0/best-selling-products", get(get_best_selling))
.route("/api/0/ean/:ean/history", get(get_product_history))
.route("/api/0/info", get(get_info))
.with_state(pool);
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap();

View file

@ -40,6 +40,7 @@
"chartjs-adapter-dayjs-4": "^1.0.4",
"dayjs": "^1.11.10",
"drizzle-orm": "^0.32.0",
"ky": "^1.5.0",
"zod": "^3.22.4"
},
"packageManager": "pnpm@9.5.0+sha512.140036830124618d624a2187b50d04289d5a087f326c9edfc0ccd733d76c4f52c3a313d4fc148794a2a9d81553016004e6742e8cf850670268a7387fc220c903"

View file

@ -1,15 +1,17 @@
import { countDistinct } from "drizzle-orm";
import type { PageServerLoad } from "./$types";
import { getDb, schema } from "$lib/server/db";
const { precios } = schema;
import { z } from "zod";
import ky from "ky";
import { API_HOST } from "$lib";
async function getInfo() {
return z
.object({
count: z.number(),
})
.parse(await ky.get(`${API_HOST}/api/0/info`).json());
}
export const load: PageServerLoad = async () => {
const db = await getDb();
const nProductosR = await db
.select({
count: countDistinct(precios.ean),
})
.from(precios);
const nProductos = nProductosR[0].count;
const nProductos = (await getInfo()).count;
return { nProductos };
};