From 9b94f129c1e5b40ad239eb016da90df8878022ad Mon Sep 17 00:00:00 2001 From: krsonal Date: Sat, 21 Dec 2024 02:39:00 +0530 Subject: [PATCH 1/2] initial commit --- .gitignore | 31 ++++++++++ main.py | 49 +++++++++++++++ node_modules/.yarn-integrity | 10 ++++ requirements.txt | 112 +++++++++++++++++++++++++++++++++++ yarn.lock | 4 ++ 5 files changed, 206 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 node_modules/.yarn-integrity create mode 100644 requirements.txt create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..27f09dc --- /dev/null +++ b/.gitignore @@ -0,0 +1,31 @@ +.idea +.tmp +.ipynb_checkpoints +.mypy_cache +.vscode +__pycache__ +.pytest_cache +htmlcov +dist +site +.coverage* +coverage.xml +.netlify +test.db +log.txt +Pipfile.lock +env3.* +env +docs_build +site_build +venv +docs.zip +archive.zip + +# vim temporary files +*~ +.*.sw? +.cache + +# macOS +.DS_Store \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..0d9e9ca --- /dev/null +++ b/main.py @@ -0,0 +1,49 @@ +from typing import Union + +from fastapi import FastAPI, UploadFile, File +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware +from pathlib import Path + +app = FastAPI() + +origins = [ + "http://localhost:8080", +] + +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + + +@app.get("/") +def read_root(): + return {"Hello": "World"} + + +# @app.get("/items/{item_id}") +# def read_item(item_id: int, q: Union[str, None] = None): +# return {"item_id": item_id, "q": q} + + +BASE_DIR = Path("./.tmp/images") + + +@app.post("/save_file") +async def save_file(file: UploadFile = File(...)): + BASE_DIR.mkdir(parents=True, exist_ok=True) + file_save_path = BASE_DIR / file.filename + + with open(file_save_path, "wb") as f: + f.write(file.file.read()) + + return {"status": "file saved"} + + +@app.post("/process_log_file") +async def process_log_file(logs: str = ""): + return {"OK"} \ No newline at end of file diff --git a/node_modules/.yarn-integrity b/node_modules/.yarn-integrity new file mode 100644 index 0000000..3f7172c --- /dev/null +++ b/node_modules/.yarn-integrity @@ -0,0 +1,10 @@ +{ + "systemParams": "linux-x64-93", + "modulesFolders": [], + "flags": [], + "linkedModules": [], + "topLevelPatterns": [], + "lockfileEntries": {}, + "files": [], + "artifacts": {} +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7eb4d1b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,112 @@ +annotated-types==0.7.0 +anyio==4.7.0 +apturl==0.5.2 +bcrypt==3.2.0 +blinker==1.4 +Brlapi==0.8.3 +certifi==2020.6.20 +chardet==4.0.0 +click==8.1.7 +colorama==0.4.4 +command-not-found==0.3 +contourpy==1.2.0 +cryptography==3.4.8 +cupshelpers==1.0 +cycler==0.12.1 +dbus-python==1.2.18 +defer==1.0.6 +distro==1.7.0 +distro-info==1.1+ubuntu0.2 +dnspython==2.7.0 +duplicity==0.8.21 +email_validator==2.2.0 +exceptiongroup==1.2.2 +fastapi==0.115.6 +fastapi-cli==0.0.7 +fasteners==0.14.1 +fonttools==4.47.2 +future==0.18.2 +gpg==1.16.0 +h11==0.14.0 +httpcore==1.0.7 +httplib2==0.20.2 +httptools==0.6.4 +httpx==0.28.1 +idna==3.3 +importlib-metadata==4.6.4 +jeepney==0.7.1 +Jinja2==3.1.4 +keyring==23.5.0 +kiwisolver==1.4.5 +language-selector==0.1 +launchpadlib==1.10.16 +lazr.restfulclient==0.14.4 +lazr.uri==1.0.6 +libcomps==0.1.15 +lockfile==0.12.2 +louis==3.20.0 +macaroonbakery==1.3.1 +Mako==1.1.3 +markdown-it-py==3.0.0 +MarkupSafe==2.0.1 +matplotlib==3.8.2 +mdurl==0.1.2 +monotonic==1.6 +more-itertools==8.10.0 +netifaces==0.11.0 +numpy==1.26.3 +oauthlib==3.2.0 +olefile==0.46 +packaging==23.2 +paramiko==2.9.3 +pexpect==4.8.0 +Pillow==9.0.1 +protobuf==3.12.4 +ptyprocess==0.7.0 +pycairo==1.20.1 +pycups==2.0.1 +pydantic==2.10.4 +pydantic_core==2.27.2 +Pygments==2.18.0 +PyGObject==3.42.1 +PyJWT==2.3.0 +pymacaroons==0.13.0 +PyNaCl==1.5.0 +pyparsing==2.4.7 +pyRFC3339==1.1 +python-apt==2.4.0+ubuntu4 +python-dateutil==2.8.1 +python-debian==0.1.43+ubuntu1.1 +python-dotenv==1.0.1 +python-multipart==0.0.20 +pytz==2022.1 +pyxdg==0.27 +PyYAML==5.4.1 +reportlab==3.6.8 +requests==2.25.1 +rich==13.9.4 +rich-toolkit==0.12.0 +rpm==4.17.0 +SecretStorage==3.3.1 +shellingham==1.5.4 +six==1.16.0 +sniffio==1.3.1 +ssh-import-id==5.11 +starlette==0.41.3 +systemd-python==234 +typer==0.15.1 +typing_extensions==4.12.2 +ubuntu-drivers-common==0.0.0 +ubuntu-pro-client==8001 +ufw==0.36.1 +unattended-upgrades==0.1 +urllib3==1.26.5 +usb-creator==0.3.7 +uvicorn==0.34.0 +uvloop==0.21.0 +wadllib==1.3.6 +watchfiles==1.0.3 +websockets==14.1 +xdg==5 +xkit==0.0.0 +zipp==1.0.0 diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..fb57ccd --- /dev/null +++ b/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + From cb1c3b1d05b0881e56b2862b1a4565dad3e5ed36 Mon Sep 17 00:00:00 2001 From: krsonal Date: Sat, 21 Dec 2024 04:30:43 +0530 Subject: [PATCH 2/2] chore: add database initial commit --- app/crud.py | 15 +++++++ app/database.py | 25 ++++++++++++ app/main.py | 104 +++++++++++++++++++++++++++++++++++++++++++++++ app/models.py | 10 +++++ app/schemas.py | 12 ++++++ main.py | 49 ---------------------- requirements.txt | 6 +++ 7 files changed, 172 insertions(+), 49 deletions(-) create mode 100644 app/crud.py create mode 100644 app/database.py create mode 100644 app/main.py create mode 100644 app/models.py create mode 100644 app/schemas.py delete mode 100644 main.py diff --git a/app/crud.py b/app/crud.py new file mode 100644 index 0000000..ca7ed47 --- /dev/null +++ b/app/crud.py @@ -0,0 +1,15 @@ +# app/crud.py +from sqlalchemy.orm import Session +from models import DbItem +from schemas import Item + +def create_item(db: Session, item: Item): + db_item = DbItem( + name=item.name, + description="; ".join(item.description), # Combine list into a single string + price=item.price + ) + db.add(db_item) # Add the item to the session + db.commit() # Commit to the database + db.refresh(db_item) # Refresh to get the updated record + return db_item diff --git a/app/database.py b/app/database.py new file mode 100644 index 0000000..7952384 --- /dev/null +++ b/app/database.py @@ -0,0 +1,25 @@ +# app/database.py +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +# Database URL - Replace with your actual database connection string +DATABASE_URL = "postgresql://user:password@localhost/dbname" + +# Create the SQLAlchemy engine +engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) + +# Create a session factory +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +# Create a base class for SQLAlchemy models +Base = declarative_base() + + +# Dependency to get the database session +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() diff --git a/app/main.py b/app/main.py new file mode 100644 index 0000000..d850fa8 --- /dev/null +++ b/app/main.py @@ -0,0 +1,104 @@ +from typing import Union + +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware +from pydantic import BaseModel +from typing import List +from sqlalchemy import create_engine, Column, Integer, String +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +from fastapi import FastAPI, Depends, HTTPException +from sqlalchemy.orm import Session +from app.database import get_db +from app.schemas import Item +from app import crud + + +app = FastAPI() + +origins = [ + "http://localhost:8080", +] + +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +# SQLAlchemy Setup +DATABASE_URL = "postgresql://user:password@localhost/dbname" + +# Create the SQLAlchemy engine and session +engine = create_engine(DATABASE_URL) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +Base = declarative_base() + + +# Define the SQLAlchemy model that will represent the 'items' table +class DbItem(Base): + __tablename__ = "items" + id = Column(Integer, primary_key=True, index=True) + name = Column(String, index=True) + description = Column(String) + price = Column(Integer) + +# Create the database tables if they do not exist +Base.metadata.create_all(bind=engine) + +class Item(BaseModel): + action_type: str + url: str + console_error: List[str] + network_error: List[str] + + +# Dependency to get the SQLAlchemy session +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() + +@app.get("/") +def read_root(): + return {"Hello": "World"} + + +# @app.post("/process_logs") +# async def process_logs(items: List[Item], db: Session = Depends(get_db)): +# try: +# # Iterate through each item and save it to the database +# for item in items: +# # Create a DbItem object from the Pydantic Item +# db_item = DbItem( +# name=item.name, +# description="; ".join(item.description), # Joining list of descriptions into a single string +# price=item.price +# ) +# db.add(db_item) # Add item to the session +# db.commit() # Commit changes to the database +# return {"status": "file saved"} +# except Exception as e: +# db.rollback() # Rollback in case of an error +# raise HTTPException(status_code=500, detail=f"Error saving to database: {str(e)}") + + +@app.post("/process_logs") +async def process_logs(items: List[Item], db: Session = Depends(get_db)): + try: + for item in items: + crud.create_item(db, item) # Save each item to the database + return {"status": "file saved"} + except Exception as e: + raise HTTPException(status_code=500, detail=f"Error: {str(e)}") + + +@app.post("/process_log_file") +async def process_log_file(logs: str = ""): + return {"OK"} \ No newline at end of file diff --git a/app/models.py b/app/models.py new file mode 100644 index 0000000..0c879c7 --- /dev/null +++ b/app/models.py @@ -0,0 +1,10 @@ +# app/models.py +from sqlalchemy import Column, Integer, String +from database import Base + +class DbItem(Base): + __tablename__ = "items" + id = Column(Integer, primary_key=True, index=True) + name = Column(String, index=True) + description = Column(String) + price = Column(Integer) diff --git a/app/schemas.py b/app/schemas.py new file mode 100644 index 0000000..04c7b91 --- /dev/null +++ b/app/schemas.py @@ -0,0 +1,12 @@ +# app/schemas.py +from pydantic import BaseModel +from typing import List + + +class Item(BaseModel): + name: str + description: List[str] # Description as an array of strings + price: float + + class Config: + orm_mode = True # Tells Pydantic to work with SQLAlchemy models diff --git a/main.py b/main.py deleted file mode 100644 index 0d9e9ca..0000000 --- a/main.py +++ /dev/null @@ -1,49 +0,0 @@ -from typing import Union - -from fastapi import FastAPI, UploadFile, File -from fastapi import FastAPI -from fastapi.middleware.cors import CORSMiddleware -from pathlib import Path - -app = FastAPI() - -origins = [ - "http://localhost:8080", -] - -app.add_middleware( - CORSMiddleware, - allow_origins=origins, - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) - - -@app.get("/") -def read_root(): - return {"Hello": "World"} - - -# @app.get("/items/{item_id}") -# def read_item(item_id: int, q: Union[str, None] = None): -# return {"item_id": item_id, "q": q} - - -BASE_DIR = Path("./.tmp/images") - - -@app.post("/save_file") -async def save_file(file: UploadFile = File(...)): - BASE_DIR.mkdir(parents=True, exist_ok=True) - file_save_path = BASE_DIR / file.filename - - with open(file_save_path, "wb") as f: - f.write(file.file.read()) - - return {"status": "file saved"} - - -@app.post("/process_log_file") -async def process_log_file(logs: str = ""): - return {"OK"} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 7eb4d1b..aa7cbfd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,6 +15,7 @@ cupshelpers==1.0 cycler==0.12.1 dbus-python==1.2.18 defer==1.0.6 +distlib==0.3.9 distro==1.7.0 distro-info==1.1+ubuntu0.2 dnspython==2.7.0 @@ -24,9 +25,11 @@ exceptiongroup==1.2.2 fastapi==0.115.6 fastapi-cli==0.0.7 fasteners==0.14.1 +filelock==3.16.1 fonttools==4.47.2 future==0.18.2 gpg==1.16.0 +greenlet==3.1.1 h11==0.14.0 httpcore==1.0.7 httplib2==0.20.2 @@ -61,6 +64,7 @@ packaging==23.2 paramiko==2.9.3 pexpect==4.8.0 Pillow==9.0.1 +platformdirs==4.3.6 protobuf==3.12.4 ptyprocess==0.7.0 pycairo==1.20.1 @@ -91,6 +95,7 @@ SecretStorage==3.3.1 shellingham==1.5.4 six==1.16.0 sniffio==1.3.1 +SQLAlchemy==2.0.36 ssh-import-id==5.11 starlette==0.41.3 systemd-python==234 @@ -104,6 +109,7 @@ urllib3==1.26.5 usb-creator==0.3.7 uvicorn==0.34.0 uvloop==0.21.0 +virtualenv==20.28.0 wadllib==1.3.6 watchfiles==1.0.3 websockets==14.1