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()
def get_db_session(): """ get a database session""" db = Session() try: return db finally: db.close()
def get_user(*, db: Session, condition: dict): try: result = db.query(CovidUser).filter_by(**condition).all() return result except Exception as _: db.rollback() raise finally: db.close()
def get_region_list(*, db: Session): try: result = db.query(distinct(Covid19.country_en)).all() return result except Exception as _: db.rollback() raise finally: db.close()
def add_user(*, db: Session, email: str, token: str): try: new_user = CovidUser(email=email, token=token) db.add(new_user) db.commit() except Exception as _: db.rollback() raise finally: db.close()
def get_last_update_date(*, db: Session): try: max_update_date = db.query( func.max(Covid19.update_date).label("max_update_date")).all() return max_update_date[0][0] except Exception as _: db.rollback() raise finally: db.close()
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 update_user(*, db: Session, condition: dict, data: dict): try: result = Session.query(CovidUser).filter_by( **condition).update(data) db.commit() return result except Exception as _: db.rollback() raise finally: db.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()
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 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 add_captcha(*, db: Session, captcha: str, session_id: str, expiration: str): try: new_captcha = Captcha(captcha=captcha, session_id=session_id, expiration=expiration) db.add(new_captcha) db.commit() except Exception as _: db.rollback() raise finally: db.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 get_population(*, db: Session, country: str): try: if country: filters = and_(Population.country_en == country) else: filters = and_(1 == 1) result = db.query(Population).filter(filters).all() return result except Exception as _: db.rollback() raise finally: db.close()
def get_captcha_by_session( *, db: Session, session: str, ): try: result = db.query(Captcha).filter_by(session_id=session).order_by( Captcha.id.desc()).first() return result except Exception as _: db.rollback() raise finally: db.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 get_infection_global_data(*, db: Session): try: result = db.query( Covid19.country_en, func.sum(Covid19.confirmed_add).label("confirmed_add"), func.sum(Covid19.deaths_add).label("deaths_add"), func.sum( Covid19.recovered_add).label("recovered_add")).group_by( Covid19.country_en).all() return result except Exception as _: db.rollback() raise finally: 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()
def get_area_list(*, db: Session, region: str, hmt: bool): if hmt: # 包含港澳台 filters = and_(Covid19.continents_en != "", Covid19.country_en == region) else: # 不包含港澳台 filters = and_(Covid19.continents_en != "", Covid19.province_en.notin_(HMT), Covid19.country_en == region) try: result = db.query(distinct( Covid19.province_en)).filter(filters).all() return result except Exception as _: db.rollback() raise finally: db.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 get_infection_city_data(*, db: Session, city: str, stime: str or None, etime: str or None, country: str): try: if country: # 查询条件中有国家 filters = and_(Covid19.province_en == city, Covid19.country_en == country) else: # 查询条件中无国家 filters = and_(Covid19.province_en == city) if stime and etime: result = db.query( func.sum(Covid19.confirmed_add).label("confirmed_add"), func.sum(Covid19.deaths_add).label("deaths_add"), func.sum(Covid19.recovered_add).label("recovered_add"), ).filter(and_(Covid19.update_date.between(stime, etime)), filters).all() return result else: # 获取最新时间 max_update_date = db.query( func.max( Covid19.update_date).label("max_update_date")).all() result = db.query( func.sum(Covid19.confirmed_add).label("confirmed_add"), func.sum(Covid19.deaths_add).label("deaths_add"), func.sum(Covid19.recovered_add).label("recovered_add"), ).filter( and_(Covid19.update_date == str(max_update_date[0][0])), filters).all() return result except Exception as _: db.rollback() raise finally: db.close()
def get_db(): try: db = Session() yield db finally: db.close()
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_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()