|
| 1 | +from fastapi import APIRouter, status |
| 2 | +import os, csv, time |
| 3 | +from app.api.db import get_db_connection |
| 4 | +from app import __version__ |
| 5 | + |
| 6 | +router = APIRouter() |
| 7 | + |
| 8 | +@router.get("/products/update", status_code=status.HTTP_202_ACCEPTED) |
| 9 | +def update_products() -> dict: |
| 10 | + """Process the large big_data.csv file to update products.""" |
| 11 | + csv_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data/big_data.csv')) |
| 12 | + process_csv(csv_path) |
| 13 | + base_url = os.getenv("BASE_URL", "http://localhost:8000") |
| 14 | + epoch = int(time.time() * 1000) |
| 15 | + meta = { |
| 16 | + "severity": "info", |
| 17 | + "title": "Product update from big_data.csv started", |
| 18 | + "version": __version__, |
| 19 | + "base_url": base_url, |
| 20 | + "time": epoch, |
| 21 | + } |
| 22 | + return {"meta": meta, "data": {"filename": "big_data.csv"}} |
| 23 | + |
| 24 | +def process_csv(csv_path: str): |
| 25 | + conn_gen = get_db_connection() |
| 26 | + conn = next(conn_gen) |
| 27 | + cur = conn.cursor() |
| 28 | + with open(csv_path, newline='') as csvfile: |
| 29 | + reader = csv.DictReader(csvfile) |
| 30 | + for row in reader: |
| 31 | + cur.execute( |
| 32 | + """ |
| 33 | + UPDATE product SET |
| 34 | + Params=%(Params)s, |
| 35 | + title=%(desc)s, |
| 36 | + UOS=%(UOS)s, |
| 37 | + Pack_Description=%(Pack_Description)s, |
| 38 | + Hierarchy1=%(Hierarchy1)s, |
| 39 | + Hierarchy2=%(Hierarchy2)s, |
| 40 | + Hierarchy3=%(Hierarchy3)s, |
| 41 | + UOP=%(UOP)s, |
| 42 | + sSell1=%(sSell1)s, |
| 43 | + sSell2=%(sSell2)s, |
| 44 | + sSell3=%(sSell3)s, |
| 45 | + sSell4=%(sSell4)s, |
| 46 | + sSell5=%(sSell5)s, |
| 47 | + pack1=%(pack1)s, |
| 48 | + pack2=%(pack2)s, |
| 49 | + pack3=%(pack3)s, |
| 50 | + pack4=%(pack4)s, |
| 51 | + pack5=%(pack5)s, |
| 52 | + EAN=%(EAN)s |
| 53 | + WHERE item=%(item)s OR EAN=%(EAN)s |
| 54 | + """, |
| 55 | + row |
| 56 | + ) |
| 57 | + conn.commit() |
| 58 | + cur.close() |
| 59 | + conn.close() |
0 commit comments