def find_by_user_id(self, user_id: UUID, db: Session) -> List[Transaction]: return db.query( self.model).filter(Transaction.user_id == user_id).all()
async def db_session_middleware(request: Request, call_next): request.state.db = Session() response = await call_next(request) request.state.db.close() return response
def init_bloom_filter(): bloom_filter = BloomFilterUtils() all_user = CovidUser.get_all_user(db=Session()) for _d in all_user: bloom_filter.add(_d.email)
def create_with_items(self, db: Session, transaction: TransactionCreate) -> Transaction: # Adding transaction db_trx = Transaction( type=TransactionTypes(transaction.type), description=transaction.description, amount=transaction.amount, user_id=transaction.user_id, ) db.add(db_trx) db.commit() db.refresh(db_trx) # Adding items for item in transaction.items: obj_in_data = jsonable_encoder(item) db_item = TransactionItem(**obj_in_data) db_item.transaction_id = db_trx.id db_item.product_id = db_item.product_id db.add(db_item) db.commit() db.refresh(db_trx) return db_trx
async def dispatch(self, request, call_next): request.state.db = Session() response = await call_next(request) request.state.db.close() return response
async def http_middleware(request: Request, call_next): request.state.request_tag = gen_random_str(min_length=25, max_length=25) request.state.db = Session() response = await call_next(request) return response
"Startup commands to make it easier to use iPython for dev" # flake8: noqa # pylint: skip-file import sqlalchemy as sa from app.db.loader import load_models from app.db.session import Session load_models() db = Session()
def init() -> None: db = Session() init_db(db)
def get_infection_country_area_data(*, db: Session, country: str, start_date: str or None, end_date: str or None, hmt: bool): try: if hmt: # 包含港澳台 filters = and_(Covid19.continents_en != "") else: # 不包含港澳台 filters = and_(Covid19.continents_en != "", Covid19.province_en.notin_(HMT)) if start_date and not end_date: max_update_date = db.query( func.max( Covid19.update_date).label("max_update_date")).all() end_date = str(max_update_date[0][0]) check_date_filter( DateFilters(**{ 'start_date': start_date, 'end_date': end_date })) if start_date and end_date: result = db.query( Covid19.update_date, Covid19.province_en, Covid19.confirmed_add, Covid19.deaths_add, Covid19.recovered_add, Covid19.confirmed, Covid19.deaths, Covid19.recovered, ).filter( and_(Covid19.country_en == country, Covid19.update_date.between(start_date, end_date)), filters).group_by(Covid19.update_date, Covid19.province_en).all() return result else: # 获取最新时间 max_update_date = db.query( func.max( Covid19.update_date).label("max_update_date")).all() result = db.query( Covid19.update_date, Covid19.province_en, Covid19.confirmed_add, Covid19.deaths_add, Covid19.recovered_add, Covid19.confirmed, Covid19.deaths, Covid19.recovered, ).filter( and_(Covid19.country_en == country, Covid19.update_date == str(max_update_date[0][0])), filters).group_by(Covid19.update_date, Covid19.province_en).all() return result except Exception as _: db.rollback() raise finally: db.close()
def db() -> Generator: yield Session()
def main() -> None: # for each spot # fetch the current swell data # make a prediction for onshore swell height # record prediction from surfline and ml model session = Session() try: created_on = datetime.utcnow() spots = session.query(Spot).all() requests_session = requests.Session() access_token = login(requests_session) for spot in spots: spot_id = spot.id surfline_spot_id = spot.surfline_spot_id forecasts = fetch_forecasts(requests_session, surfline_spot_id, access_token) swells = fetch_swells(requests_session, surfline_spot_id, access_token) predictions = fetch_predictions(swells) for i in range(0, (FORECAST_DAYS - 1)): forecast = forecasts[i] swell = swells[i] prediction = predictions[i] forecasted_for = created_on + timedelta(days=(i + 1)) prediction = Prediction( spot_id=spot_id, created_on=created_on, forecasted_for=forecasted_for, surfline_height=humanized_height_round( average_forecast_height(forecast)), stoke_height=humanized_height_round(prediction), swell1_height=swell['swells'][0]['height'], swell1_period=swell['swells'][0]['period'], swell1_direction=swell['swells'][0]['direction'], swell2_height=swell['swells'][1]['height'], swell2_period=swell['swells'][1]['period'], swell2_direction=swell['swells'][1]['direction'], swell3_height=swell['swells'][2]['height'], swell3_period=swell['swells'][2]['period'], swell3_direction=swell['swells'][2]['direction'], swell4_height=swell['swells'][3]['height'], swell4_period=swell['swells'][3]['period'], swell4_direction=swell['swells'][3]['direction'], swell5_height=swell['swells'][4]['height'], swell5_period=swell['swells'][4]['period'], swell5_direction=swell['swells'][4]['direction'], swell6_height=swell['swells'][5]['height'], swell6_period=swell['swells'][5]['period'], swell6_direction=swell['swells'][5]['direction']) session.add(prediction) session.commit() finally: session.close()
def get_db(): try: db = Session() yield db finally: db.close()
def db_session(): with Session() as session: yield session
def find_by_user_id(self, user_id: UUID, db: Session) -> List[Candidate]: return db.query(self.model).filter(Candidate.user_id == user_id).all()
def find_by_transaction_id(self, transaction_id: UUID, db: Session) -> List[TransactionItem]: return (db.query(self.model).filter( TransactionItem.transaction_id == transaction_id).all())