Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions app/crud.py
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions app/database.py
Original file line number Diff line number Diff line change
@@ -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()
104 changes: 104 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -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"}
10 changes: 10 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions app/schemas.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions node_modules/.yarn-integrity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
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
distlib==0.3.9
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
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
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
platformdirs==4.3.6
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
SQLAlchemy==2.0.36
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
virtualenv==20.28.0
wadllib==1.3.6
watchfiles==1.0.3
websockets==14.1
xdg==5
xkit==0.0.0
zipp==1.0.0
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1