def get_db_session(): """ get a database session""" db = Session() try: return db finally: db.close()
async def dispatch(self, request, call_next): logging.info(f"Creating Database session for {request.url}") request.state.db = Session() response = await call_next(request) logging.info(f"Closing Database session for {request.url}") request.state.db.close() return response
def main(): session = Session() try: lift_service = LiftService() lifts = lift_service.fetch_lifts() updated = [] for lift in lifts: stored_lift = session.query(Lift).filter( Lift.season.like(SEASON), Lift.name.like(lift['name'])).first() if stored_lift and stored_lift.status != lift['status']: stored_lift.status = lift['status'] stored_lift.last_updated = lift['last_updated'] session.commit() updated.append(lift) if any(updated): notification_service = NotificationService(len(updated)) notification_service.send_notifications() finally: session.close()
async def db_session_middleware(request: Request, call_next): response = Response("Internal server error", status_code=500) try: request.state.db = Session() response = await call_next(request) finally: request.state.db.close() return response
def init() -> None: try: # Try to create session to check if DB is awake db = Session() db.execute("SELECT 1") except Exception as e: logger.error(e) raise e
async def db_session_middleware( request: Request, call_next: tp.Callable ) -> Response: """Middleware to add database session object to request state.""" request.state.db = Session() response = await call_next(request) request.state.db.close() return response
async def db_session_middleware(request: Request, call_next ): logger.debug("Entered middleware") request.state.db = Session() logger.debug(f"call_next type: {type(call_next)}, __name__ {call_next.__func__.__name__}") logger.debug("Before calling call_next") response = await call_next(request) # TODO: Проблема с возвращаемыми типами: Приходит ORM модель, которая не dict logger.debug(f"Response: class - {type(response)} - {response}") request.state.db.close() logger.debug(f"Response: class - {type(response)} - {response}") return response
def main() -> None: # for each spot that's gathering data # gather forecast and swell data # format to a forecast object # save in DB session = Session() try: timestamp = datetime.utcnow() spots = session.query(Spot).filter(Spot.gathering_data == True).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 forecast_info = fetch_forecast_info(requests_session, surfline_spot_id, access_token) swell_info = fetch_swell_info(requests_session, surfline_spot_id, access_token) forecast = Forecast( spot_id=spot_id, timestamp=timestamp, am_min_height=forecast_info['am']['minHeight'], am_max_height=forecast_info['am']['maxHeight'], am_rating=forecast_info['am']['rating'], pm_min_height=forecast_info['pm']['minHeight'], pm_max_height=forecast_info['pm']['maxHeight'], pm_rating=forecast_info['pm']['rating'], swell1_height=swell_info['swells'][0]['height'], swell1_period=swell_info['swells'][0]['period'], swell1_direction=swell_info['swells'][0]['direction'], swell2_height=swell_info['swells'][1]['height'], swell2_period=swell_info['swells'][1]['period'], swell2_direction=swell_info['swells'][1]['direction'], swell3_height=swell_info['swells'][2]['height'], swell3_period=swell_info['swells'][2]['period'], swell3_direction=swell_info['swells'][2]['direction'], swell4_height=swell_info['swells'][3]['height'], swell4_period=swell_info['swells'][3]['period'], swell4_direction=swell_info['swells'][3]['direction'], swell5_height=swell_info['swells'][4]['height'], swell5_period=swell_info['swells'][4]['period'], swell5_direction=swell_info['swells'][4]['direction'], swell6_height=swell_info['swells'][5]['height'], swell6_period=swell_info['swells'][5]['period'], swell6_direction=swell_info['swells'][5]['direction']) session.add(forecast) session.commit() finally: session.close()
def main() -> None: # delete all predictions older than 30 days session = Session() try: today = datetime.datetime.today() one_month_ago = today - datetime.timedelta(days=30) session.query(Prediction).filter( Prediction.created_on < one_month_ago).delete() finally: session.close()
async def root(request): session = Session() try: season = request.query_params.get('season', SEASON) lifts = session.query(Lift).filter( Lift.season == season).order_by(Lift.last_updated.desc()).all() lift_dicts = [l._for_html() for l in lifts] return templates.TemplateResponse('lifts/index.html.j2', {'request': request, 'season': season, 'lifts': lift_dicts}) finally: session.close()
async def lifts(request): session = Session() try: season = request.query_params.get('season', SEASON) lifts = session.query(Lift).filter( Lift.season == season).order_by(Lift.last_updated.desc()).all() lift_dicts = [l._for_json() for l in lifts] return JSONResponse({'lifts': lift_dicts}) finally: session.close()
def main(): session = Session() for spot_attributes in NEW_SPOTS: spot = Spot(surfline_id=spot_attributes['surfline_id'], surfline_spot_id=spot_attributes['surfline_spot_id'], name=spot_attributes['name'], favorable_swells=spot_attributes['favorable_swells']) session.add(spot) session.commit() session.close()
def add_static_routes(): from app.db_models.dataset import Dataset as DBDataset db = Session() datasets = db.query(DBDataset).all() for dataset in datasets: app.mount( config.DATASET_STATIC_ORIG_TEMPLATE.format(dataset_id=dataset.id), StaticFiles(directory=dataset.base_dir), name="static") app.mount( config.DATASET_STATIC_THUMB_TEMPLATE.format(dataset_id=dataset.id), StaticFiles(directory=dataset.thumbnail_dir), name="static") db.close()
def main() -> None: session = Session() try: for spot_attributes in SPOTS: spot = Spot(surfline_id=spot_attributes['surfline_id'], surfline_spot_id=spot_attributes['surfline_spot_id'], name=spot_attributes['name'], favorable_swells=spot_attributes['favorable_swells'], gathering_data=spot_attributes['gathering_data']) session.add(spot) session.commit() finally: session.close()
async def read_posts(websocket: WebSocket, ): db = Session() await websocket.accept() while True: message = await websocket.receive_text() try: post_id = int(message) except ValueError: break post = crud.post.get(db, post_id) if not post: await websocket.send_json( NotFoundSchema(message="Post not found").dict()) continue logger.warning(f"post id = {post_id} = {post}") post = Post.from_orm(post) await websocket.send_text( json.dumps(post.dict(), default=lambda a: str(a))) await websocket.close()
def main(): session = Session() try: lift_service = LiftService() lifts = lift_service.fetch_lifts() for lift in lifts: new_lift = Lift( name=lift['name'], status=lift['status'], kind=lift['kind'], season=lift['season'], last_updated=lift['last_updated'] ) session.add(new_lift) session.commit() finally: session.close()
async def predictions(request): session = Session() try: predictions_query = session.query(Prediction) spot_ids_param = request.query_params.get('spot_ids', None) if spot_ids_param: try: spot_ids = spot_ids_param.split(',') except: spot_ids = [] predictions_query = predictions_query.filter( Prediction.spot_id.in_(spot_ids)) created_on_param = request.query_params.get('created_on', None) if created_on_param: try: created_on_date = datetime.fromisoformat( created_on_param).date() except: created_on_date = datetime.utcnow().date() predictions_query = predictions_query.filter( cast(Prediction.created_on, Date) == created_on_date) predictions_query = predictions_query.order_by(Prediction.id.desc()) page_param = request.query_params.get('page', '1') page = int(page_param) predictions_query = predictions_query.limit(PAGE_SIZE).offset( (page - 1) * PAGE_SIZE) predictions = predictions_query.all() prediction_dicts = [p._asdict() for p in predictions] return JSONResponse({'predictions': prediction_dicts}) finally: session.close()
async def forecasts(request): session = Session() try: forecasts_query = session.query(Forecast) spot_ids_param = request.query_params.get('spot_ids', None) if spot_ids_param: try: spot_ids = spot_ids_param.split(',') except: spot_ids = [] forecasts_query = forecasts_query.filter( Forecast.spot_id.in_(spot_ids)) after_param = request.query_params.get('after', None) if after_param: try: after_datetime = datetime.fromisoformat(after_param) except: after_datetime = datetime.utcnow() forecasts_query = forecasts_query.filter( Forecast.timestamp >= after_datetime) forecasts_query = forecasts_query.order_by(Forecast.id.desc()) page_param = request.query_params.get('page', '1') page = int(page_param) forecasts_query = forecasts_query.limit(PAGE_SIZE).offset( (page - 1) * PAGE_SIZE) forecasts = forecasts_query.all() forecast_dicts = [f._asdict() for f in forecasts] return JSONResponse({'forecasts': forecast_dicts}) finally: session.close()
async def spots(request): session = Session() try: spots_query = session.query(Spot) surfline_spot_ids_param = request.query_params.get( 'surfline_spot_ids', None) if surfline_spot_ids_param: try: surfline_spot_ids = surfline_spot_ids_param.split(',') except: surfline_spot_ids = [] spots_query = spots_query.filter( Spot.surfline_spot_id.in_(surfline_spot_ids)) spots = spots_query.all() spot_dicts = [s._asdict() for s in spots] return JSONResponse({'spots': spot_dicts}) finally: session.close()
def db() -> Generator: yield Session()
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 db_session(): with Session() as session: yield session
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 get_db(): try: db = Session() yield db finally: db.close()
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
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()
async def dispatch(self, request, call_next): request.state.db = Session() response = await call_next(request) request.state.db.close() 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)