71 lines
2.6 KiB
Go
71 lines
2.6 KiB
Go
package main
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/config"
|
||
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
||
|
|
)
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
config.LoadDotEnv()
|
||
|
|
url := os.Getenv("DATABASE_URL")
|
||
|
|
if url == "" {
|
||
|
|
fmt.Println("NO_DATABASE_URL")
|
||
|
|
os.Exit(1)
|
||
|
|
}
|
||
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||
|
|
defer cancel()
|
||
|
|
pg, err := pgxpool.New(ctx, url)
|
||
|
|
if err != nil {
|
||
|
|
fmt.Println("pg_err", err)
|
||
|
|
os.Exit(1)
|
||
|
|
}
|
||
|
|
defer pg.Close()
|
||
|
|
company := "2b3159b0-fc08-415b-b248-35ed02a6baab"
|
||
|
|
var rawTotal, mappedWith, mappedEmpty, processedWith, llmSource, taxonomy int
|
||
|
|
_ = pg.QueryRow(ctx, `
|
||
|
|
SELECT COUNT(*),
|
||
|
|
COUNT(*) FILTER (WHERE COALESCE(NULLIF(trim(mapped_data->>'category'), ''), '') <> '' AND lower(trim(mapped_data->>'category')) <> 'none'),
|
||
|
|
COUNT(*) FILTER (WHERE COALESCE(NULLIF(trim(mapped_data->>'category'), ''), '') = '' OR lower(trim(COALESCE(mapped_data->>'category', ''))) = 'none')
|
||
|
|
FROM raw_products WHERE company_id=$1::uuid`, company).Scan(&rawTotal, &mappedWith, &mappedEmpty)
|
||
|
|
_ = pg.QueryRow(ctx, `
|
||
|
|
SELECT COUNT(*) FILTER (WHERE COALESCE(NULLIF(trim(category), ''), '') <> '' AND lower(trim(category)) <> 'none'),
|
||
|
|
COUNT(*) FILTER (WHERE field_sources->>'category' = 'llm')
|
||
|
|
FROM processed_products WHERE company_id=$1::uuid`, company).Scan(&processedWith, &llmSource)
|
||
|
|
_ = pg.QueryRow(ctx, `SELECT COUNT(*) FROM categories WHERE company_id=$1::uuid AND COALESCE(NULLIF(trim(unique_id), ''), '') <> ''`, company).Scan(&taxonomy)
|
||
|
|
fmt.Printf("company=Platform Demo\nraw_total=%d\nmapped_with=%d\nmapped_empty=%d\nprocessed_with=%d\nllm_source=%d\ntaxonomy=%d\n",
|
||
|
|
rawTotal, mappedWith, mappedEmpty, processedWith, llmSource, taxonomy)
|
||
|
|
|
||
|
|
rows, err := pg.Query(ctx, `
|
||
|
|
SELECT COALESCE(rp.gtin,''), COALESCE(trim(pp.category),''), COALESCE(c.name,''),
|
||
|
|
COALESCE(NULLIF(trim(pp.processed_name),''), NULLIF(trim(rp.mapped_data->>'name'),''), '')
|
||
|
|
FROM processed_products pp
|
||
|
|
JOIN raw_products rp ON rp.id = pp.raw_product_id AND rp.company_id = pp.company_id
|
||
|
|
LEFT JOIN categories c ON c.company_id = pp.company_id AND c.unique_id = NULLIF(trim(pp.category), '')
|
||
|
|
WHERE pp.company_id=$1::uuid AND pp.field_sources->>'category'='llm'
|
||
|
|
ORDER BY pp.updated_at DESC
|
||
|
|
LIMIT 15`, company)
|
||
|
|
if err != nil {
|
||
|
|
fmt.Println("sample_err", err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
defer rows.Close()
|
||
|
|
fmt.Println("sample_llm:")
|
||
|
|
for rows.Next() {
|
||
|
|
var gtin, uid, name, pname string
|
||
|
|
_ = rows.Scan(>in, &uid, &name, &pname)
|
||
|
|
fmt.Printf(" gtin=%s uid=%s name=%s product=%s\n", gtin, uid, name, truncate(pname, 60))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func truncate(s string, n int) string {
|
||
|
|
if len(s) <= n {
|
||
|
|
return s
|
||
|
|
}
|
||
|
|
return s[:n] + "…"
|
||
|
|
}
|