switching to sqlmodel...
This commit is contained in:
0
eveal/__init__.py
Normal file
0
eveal/__init__.py
Normal file
14
eveal/database.py
Normal file
14
eveal/database.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from sqlmodel import create_engine, SQLModel, Session
|
||||
|
||||
from eveal import models_sde
|
||||
|
||||
sqlite_file_name = "eveal.db"
|
||||
sqlite_url = f"sqlite:///{sqlite_file_name}"
|
||||
engine = create_engine(sqlite_url, echo=True, future=True)
|
||||
|
||||
def get_session():
|
||||
db = Session(engine)
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
42
eveal/main.py
Normal file
42
eveal/main.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from collections import defaultdict
|
||||
|
||||
from fastapi import FastAPI, Depends
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from typing import List
|
||||
from sqlmodel import SQLModel, Session, select
|
||||
|
||||
from eveal.schemas import Evepraisal, PriceReprocess
|
||||
from eveal.database import engine, get_session
|
||||
from eveal import models_sde
|
||||
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
app = FastAPI()
|
||||
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Hello World"}
|
||||
|
||||
|
||||
@app.post("/reprocess/")
|
||||
async def reprocess(ep_items: Evepraisal, ep_mat: Evepraisal, efficiency: float = .55, db: Session = Depends(get_session)):
|
||||
matprices = {item.typeID: {'sell': item.prices.sell.min, 'buy': item.prices.buy.max} for item in ep_mat.items}
|
||||
|
||||
item_reprocess: List[PriceReprocess] = []
|
||||
for rawitem in ep_items.items:
|
||||
# item = db.exec(select(models_sde.Type).where(models_sde.Type.id == rawitem.typeID)).one()
|
||||
item = db.get(models_sde.Type, rawitem.typeID)
|
||||
buy_reprocess = sell_reprocess = 0.0
|
||||
for mat in item.materials.all():
|
||||
buy_reprocess += matprices[mat.type.id]['buy'] * mat.quantity * efficiency
|
||||
sell_reprocess += matprices[mat.type.id]['sell'] * mat.quantity * efficiency
|
||||
item_reprocess.append(PriceReprocess(typeID=item.typeID,
|
||||
buy=item.prices.buy.max,
|
||||
sell=item.prices.sell.min,
|
||||
buy_reprocess=buy_reprocess,
|
||||
sell_reprocess=sell_reprocess,
|
||||
name=item.name
|
||||
))
|
||||
return item_reprocess
|
||||
109
eveal/models_sde.py
Normal file
109
eveal/models_sde.py
Normal file
@@ -0,0 +1,109 @@
|
||||
from typing import Optional, List
|
||||
from sqlmodel import SQLModel, Field, Relationship
|
||||
|
||||
|
||||
class Icon(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
|
||||
description: Optional[str] = None
|
||||
iconFile: str
|
||||
|
||||
categories: List['Category'] = Relationship(back_populates="icon")
|
||||
groups: List['Group'] = Relationship(back_populates="icon")
|
||||
marketgroups: List['MarketGroup'] = Relationship(back_populates="icon")
|
||||
types: List['Type'] = Relationship(back_populates="icon")
|
||||
|
||||
|
||||
class Category(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
|
||||
icon_id: Optional[int] = Field(default=None, foreign_key="icon.id")
|
||||
icon: Optional[Icon] = Relationship(back_populates="categories")
|
||||
|
||||
name: str
|
||||
published: bool
|
||||
|
||||
groups: List['Group'] = Relationship(back_populates="category")
|
||||
|
||||
|
||||
class Group(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
|
||||
anchorable: bool
|
||||
anchored: bool
|
||||
|
||||
category_id: Optional[int] = Field(default=None, foreign_key="category.id")
|
||||
category: Optional[Category] = Relationship(back_populates="groups")
|
||||
|
||||
fittableNonSingletion: bool
|
||||
|
||||
icon_id: Optional[int] = Field(default=None, foreign_key="icon.id")
|
||||
icon: Optional[Icon] = Relationship(back_populates="groups")
|
||||
|
||||
name: str
|
||||
published: bool
|
||||
useBasePrice: bool
|
||||
|
||||
types: List['Type'] = Relationship(back_populates="group")
|
||||
|
||||
|
||||
class MarketGroup(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
|
||||
description: Optional[str] = None
|
||||
hasTypes: bool
|
||||
|
||||
icon_id: Optional[int] = Field(default=None, foreign_key="icon.id")
|
||||
icon: Optional[Icon] = Relationship(back_populates="marketgroups")
|
||||
|
||||
name: str
|
||||
|
||||
parent_marketgroup_id: Optional[int] = Field(default=None, foreign_key="marketgroup.id")
|
||||
parent_marketgroup: Optional['MarketGroup'] = Relationship(back_populates="children_marketgroups",
|
||||
sa_relationship_kwargs={"remote_side": 'MarketGroup.id'}) # workaround for self reference: https://github.com/tiangolo/sqlmodel/issues/127#issuecomment-1224135123
|
||||
children_marketgroups: List['MarketGroup'] = Relationship(back_populates="parent_marketgroup")
|
||||
|
||||
types: List['Type'] = Relationship(back_populates="marketgroup")
|
||||
|
||||
|
||||
class Type(SQLModel, table=True):
|
||||
id: int = Field(primary_key=True)
|
||||
|
||||
# id = Column(Integer, primary_key=True, index=True)
|
||||
group_id: Optional[int] = Field(default=None, foreign_key="group.id")
|
||||
group: Optional[Group] = Relationship(back_populates="types")
|
||||
|
||||
marketgroup_id: Optional[int] = Field(default=None, foreign_key="marketgroup.id")
|
||||
marketgroup: Optional[MarketGroup] = Relationship(back_populates="types")
|
||||
|
||||
name: str
|
||||
published: bool = False
|
||||
description: Optional[str] = None
|
||||
basePrice: float
|
||||
|
||||
icon_id: Optional[int] = Field(default=None, foreign_key="icon.id")
|
||||
icon: Optional[Icon] = Relationship(back_populates="types")
|
||||
|
||||
volume: Optional[float] = None
|
||||
portionSize: int
|
||||
|
||||
materials: List['TypeMaterial'] = Relationship(back_populates="type",
|
||||
sa_relationship_kwargs={"foreign_keys": '[TypeMaterial.type_id]'}) # https://github.com/tiangolo/sqlmodel/issues/10#issuecomment-1537445078
|
||||
material_of: List['TypeMaterial'] = Relationship(back_populates="material_type",
|
||||
sa_relationship_kwargs={"foreign_keys": '[TypeMaterial.material_type_id]'}) # https://github.com/tiangolo/sqlmodel/issues/10#issuecomment-1537445078
|
||||
|
||||
|
||||
class TypeMaterial(SQLModel, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
|
||||
type_id: Optional[int] = Field(default=None, foreign_key="type.id")
|
||||
type: Optional[Type] = Relationship(back_populates="materials",
|
||||
sa_relationship_kwargs={"primaryjoin": 'TypeMaterial.type_id==Type.id',
|
||||
'lazy': 'joined'}) # workaround: https://github.com/tiangolo/sqlmodel/issues/10#issuecomment-1002835506
|
||||
|
||||
material_type_id: Optional[int] = Field(default=None, foreign_key="type.id")
|
||||
material_type: Optional[Type] = Relationship(back_populates="material_of",
|
||||
sa_relationship_kwargs={"primaryjoin": 'TypeMaterial.material_type_id==Type.id',
|
||||
'lazy': 'joined'}) # workaround: https://github.com/tiangolo/sqlmodel/issues/10#issuecomment-1002835506
|
||||
|
||||
quantity: int
|
||||
52
eveal/schemas.py
Normal file
52
eveal/schemas.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Price(BaseModel):
|
||||
avg: float
|
||||
max: float
|
||||
median: float
|
||||
min: float
|
||||
percentile: float
|
||||
stddev: float
|
||||
volume: int
|
||||
order_count: int
|
||||
|
||||
class Prices(BaseModel):
|
||||
all: Price
|
||||
buy: Price
|
||||
sell: Price
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
typeID: int
|
||||
typeName: str
|
||||
typeVolume: float
|
||||
quantity: int
|
||||
prices: Prices
|
||||
|
||||
class Total_price(BaseModel):
|
||||
buy: float
|
||||
sell: float
|
||||
volume: float
|
||||
|
||||
class Evepraisal(BaseModel):
|
||||
id: str
|
||||
created: int
|
||||
kind: str
|
||||
market_name: str
|
||||
items: list[Item]
|
||||
totals: Total_price
|
||||
price_percentage: float
|
||||
raw: str
|
||||
|
||||
|
||||
class PriceReprocess(BaseModel):
|
||||
typeID: int
|
||||
buy: float
|
||||
sell: float
|
||||
buy_reprocess: float
|
||||
sell_reprocess: float
|
||||
name: str
|
||||
|
||||
|
||||
Reference in New Issue
Block a user