From d006d9978cd11759d9eb340a0b41a2c9f88a0d91 Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Fri, 5 Jan 2024 14:41:59 +0000 Subject: [PATCH] update to use function in pvsite-datamodel --- requirements.txt | 4 +- src/get_data.py | 210 ------------------------------------------- src/sites_toolbox.py | 15 ++-- 3 files changed, 9 insertions(+), 220 deletions(-) diff --git a/requirements.txt b/requirements.txt index d4c7373..c00d4c4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ altair==4.2.2 nowcasting_datamodel==1.5.25 -pvsite-datamodel==1.0.1 +pvsite-datamodel==1.0.7 numpy==1.24.1 pandas==1.5.3 plotly==5.10.0 @@ -9,6 +9,6 @@ SQLAlchemy==1.4.46 streamlit==1.27.2 testcontainers==3.2.0 uvicorn==0.23.2 -geopandas==0.11.1 +geopandas==0.14.2 garrett-streamlit-auth0==0.9 diff --git a/src/get_data.py b/src/get_data.py index 4b171aa..fe96600 100644 --- a/src/get_data.py +++ b/src/get_data.py @@ -173,213 +173,3 @@ def get_site_by_client_site_id(session: Session, client_site_id: str) -> List[Si site = query.first() return site - - -# add site to site group; sites can belong to many groups -def add_site_to_site_group( - session: Session, site_uuid: str, site_group_name: str -) -> SiteGroupSQL: - """Add a site to a site group. - :param session: database session - :param site_uuid: uuid of site - :param site_group_name: name of site group - """ - site_group = ( - session.query(SiteGroupSQL) - .filter(SiteGroupSQL.site_group_name == site_group_name) - .first() - ) - - site = session.query(SiteSQL).filter(SiteSQL.site_uuid == site_uuid).one() - - if site not in site_group.sites: - site_group.sites.append(site) - - session.commit() - - return site_group.sites - - -# make site -def create_new_site( - session: Session, - client_site_id: int, - client_site_name: str, - latitude: float, - longitude: float, - capacity_kw: float, - dno: Optional[str] = None, - gsp: Optional[str] = None, - region: Optional[str] = None, - orientation: Optional[float] = None, - tilt: Optional[float] = None, - inverter_capacity_kw: Optional[float] = None, - module_capacity_kw: Optional[float] = None, -) -> SiteSQL: - """Creates a site and adds it to the database. - :param session: database session - :param client_site_id: id the client uses for the site - :param client_site_name: name the client uses for the site - :param latitude: latitude of site as an integer - :param longitude: longitude of site as an integer - :param capacity_kw: capacity of site in kw - :param dno: dno of site - :param gsp: gsp of site - :param region: region of site, deafut is uk - :param orientation: orientation of site, default is 180 - :param tilt: tilt of site, default is 35 - :param inverter_capacity_kw: inverter capacity of site in kw - :param module_capacity_kw: module capacity of site in kw - - """ - max_ml_id = session.query(func.max(SiteSQL.ml_id)).scalar() - - if max_ml_id is None: - max_ml_id = 0 - - if region in [None, ""]: - region = "uk" - - if orientation in [None, ""]: - orientation = 180 - - if tilt in [None, ""]: - tilt = 35 - - if inverter_capacity_kw in [None, ""]: - inverter_capacity_kw = capacity_kw - - if module_capacity_kw in [None, ""]: - module_capacity_kw = capacity_kw - - if gsp is None: - gsp = get_gsp(latitude=latitude, longitude=longitude) - gsp = json.dumps(gsp) - - if dno is None: - dno = get_dno(latitude=latitude, longitude=longitude) - dno = json.dumps(dno) - - site = SiteSQL( - ml_id=max_ml_id + 1, - client_site_id=client_site_id, - client_site_name=client_site_name, - latitude=latitude, - longitude=longitude, - capacity_kw=capacity_kw, - dno=dno, - gsp=gsp, - region=region, - orientation=orientation, - tilt=tilt, - inverter_capacity_kw=inverter_capacity_kw, - module_capacity_kw=module_capacity_kw, - ) - - session.add(site) - - session.commit() - - message = f"Site with client site id {site.client_site_id} and site uuid {site.site_uuid} created successfully" - - return site, message - - -# create user -def create_user( - session: Session, - email: str, - site_group_name: str, -) -> UserSQL: - """Create a user. - :param session: database session - :param email: email of user being created - :param site_group: name of the site group this user will be part of - """ - - site_group = ( - session.query(SiteGroupSQL) - .filter(SiteGroupSQL.site_group_name == site_group_name) - .first() - ) - - user = UserSQL(email=email, site_group_uuid=site_group.site_group_uuid) - - session.add(user) - site_group.users.append(user) - session.commit() - - return user - - -# delete a site -def delete_site(session: Session, site_uuid: str) -> SiteGroupSQL: - """Delete a site group. - :param session: database session - :param site_uuid: unique identifier for site - """ - site = session.query(SiteSQL).filter(SiteSQL.site_uuid == site_uuid).first() - - forecast_uuids = session.query(ForecastSQL.forecast_uuid).filter(ForecastSQL.site_uuid == site_uuid).all() - - forecast_uuids = [str(forecast_uuid[0]) for forecast_uuid in forecast_uuids] - - # get and delete all forecast values for site - stmt = sa.delete(ForecastValueSQL).where(ForecastValueSQL.forecast_uuid.in_(forecast_uuids)) - session.execute(stmt) - - stmt_2 = sa.delete(ForecastSQL).where(ForecastSQL.forecast_uuid.in_(forecast_uuids)) - session.execute(stmt_2) - - # we decided not to delete generation data for the site because it seems sensible to keep it for now - - session.delete(site) - - message = f"Site with site uuid {site.site_uuid} deleted successfully" - - session.commit() - - return message - -# delete user -def delete_user(session: Session, email: str) -> UserSQL: - """Delete a user. - :param session: database session - :param email: email of user being deleted - """ - user = session.query(UserSQL).filter(UserSQL.email == email).first() - - session.delete(user) - - message = f"User with email {user.email} and site_group_uuid {user.site_group_uuid} deleted successfully" - - session.commit() - - return message - -#delete site group -def delete_site_group(session: Session, site_group_name: str) -> SiteGroupSQL: - """Delete a site group. - :param session: database session - :param site_group_name: name of site group being deleted - """ - site_group = ( - session.query(SiteGroupSQL) - .filter(SiteGroupSQL.site_group_name == site_group_name) - .first() - ) - - site_group_users = site_group.users - - if len(site_group_users) > 0: - message = f"Site group with name {site_group.site_group_name} and site group uuid {site_group.site_group_uuid} has users and cannot be deleted." - return message - - session.delete(site_group) - - message = f"Site group with name {site_group.site_group_name} and site group uuid {site_group.site_group_uuid} deleted successfully." - - session.commit() - - return message - diff --git a/src/sites_toolbox.py b/src/sites_toolbox.py index 8be42b5..47ef272 100644 --- a/src/sites_toolbox.py +++ b/src/sites_toolbox.py @@ -7,7 +7,7 @@ from sqlalchemy import func from pvsite_datamodel.connection import DatabaseConnection from pvsite_datamodel.sqlmodels import SiteSQL -from pvsite_datamodel.write.user_and_site import make_site_group +# from pvsite_datamodel.write.user_and_site import create_site_group from pvsite_datamodel.read import ( get_all_sites, get_user_by_email, @@ -15,17 +15,16 @@ get_site_group_by_name, ) -from get_data import ( - create_new_site, +from get_data import get_all_users, get_all_site_groups, get_site_by_client_site_id +from pvsite_datamodel.write.user_and_site import ( + create_site, create_user, delete_site, delete_user, delete_site_group, - get_all_users, - get_all_site_groups, - get_site_by_client_site_id, add_site_to_site_group, update_user_site_group, + create_site_group ) # colors 7bcdf3 @@ -367,7 +366,7 @@ def sites_toolbox_page(): ) st.write(error) else: # create new - site, message = create_new_site( + site, message = create_site( session=session, client_site_id=client_site_id, client_site_name=client_site_name, @@ -480,7 +479,7 @@ def sites_toolbox_page(): unsafe_allow_html=True, ) else: - new_site_group = make_site_group( + new_site_group = create_site_group( db_session=session, site_group_name=new_site_group_name, )