Skip to content

Commit

Permalink
Merge pull request #60 from openclimatefix/issue/53-show-user-details
Browse files Browse the repository at this point in the history
Issue/53 show user details
  • Loading branch information
rachel-labri-tipton authored Sep 11, 2023
2 parents f7fa497 + eb3e4ab commit 3e38b35
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from status import status_page
from forecast import forecast_page
from pvsite_forecast import pvsite_forecast_page
from sites_toolbox import sites_toolbox_page

st.get_option("theme.primaryColor")

Expand Down Expand Up @@ -488,6 +489,7 @@ def metric_page():
"Status": status_page,
"Forecast": forecast_page,
"PV Site Forecast": pvsite_forecast_page,
"Sites Toolbox": sites_toolbox_page,
}

demo_name = st.sidebar.selectbox("Choose a page", page_names_to_funcs.keys())
Expand Down
78 changes: 78 additions & 0 deletions src/sites_toolbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""This module contains the sites toolbox for the OCF dashboard"""
import os
import streamlit as st
from datetime import datetime, timedelta, time, timezone
from pvsite_datamodel.connection import DatabaseConnection
from pvsite_datamodel.read import (
get_all_sites,
get_user_by_email,
)
from get_data import (
get_all_users,
get_all_site_groups,
update_user_site_group,
)


import plotly.graph_objects as go


def get_user_details(session, email):
"""Get the user details from the database"""
user_details = get_user_by_email(session=session, email=email)
user_site_group = user_details.site_group.site_group_name
user_site_count = len(user_details.site_group.sites)
user_sites = [
{"site_uuid": str(site.site_uuid), "client_site_id": str(site.client_site_id)}
for site in user_details.site_group.sites
]
return user_sites, user_site_group, user_site_count


def sites_toolbox_page():
st.markdown(
f'<h1 style="color:#FFD053;font-size:48px;">{"OCF Dashboard"}</h1>',
unsafe_allow_html=True,
)
st.markdown(
f'<h1 style="color:#63BCAF;font-size:48px;">{"Sites Toolbox"}</h1>',
unsafe_allow_html=True,
)

url = os.environ["SITES_DB_URL"]

connection = DatabaseConnection(
url=url,
echo=True,
)
with connection.get_session() as session:
# get the user details
users = get_all_users(session=session)
user_list = [user.email for user in users]
sites = get_all_sites(session=session)
# site_uuids = [str(site.site_uuid) for site in sites]
site_groups = get_all_site_groups(session=session)
# site_group_name = [site_groups.site_group_name for site_groups in site_groups]

st.markdown(
f'<h1 style="color:#63BCAF;font-size:32px;">{"Get User Details"}</h1>',
unsafe_allow_html=True,
)
email = st.selectbox("Enter email of user you want to know about.", user_list)

if st.button("Get user details"):
user_sites, user_site_group, user_site_count = get_user_details(
session=session, email=email
)
st.write(
"This user is part of the",
user_site_group,
"site group, which contains",
user_site_count,
"sites.",
)
st.write(
"Here are the site_uuids and client_site_ids for this group:", user_sites
)
if st.button("Close user details"):
st.empty()
43 changes: 43 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import datetime as dt
import uuid
from typing import List

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from testcontainers.postgres import PostgresContainer

from pvsite_datamodel import GenerationSQL, SiteSQL, StatusSQL
from pvsite_datamodel.sqlmodels import Base

@pytest.fixture(scope="session")
def engine():
"""Database engine fixture."""
with PostgresContainer("postgres:14.5") as postgres:
# TODO need to setup postgres database with docker
url = postgres.get_connection_url()
engine = create_engine(url)
Base.metadata.create_all(engine)

yield engine


@pytest.fixture()
def db_session(engine):
"""Return a sqlalchemy session, which tears down everything properly post-test."""
connection = engine.connect()
# begin the nested transaction
transaction = connection.begin()
# use the connection with the already started transaction

with Session(bind=connection) as session:
yield session

session.close()
# roll back the broader transaction
transaction.rollback()
# put back the connection to the connection pool
connection.close()
session.flush()

engine.dispose()
14 changes: 14 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""tests for get_data.py"""
from get_data import get_all_users, get_all_site_groups, attach_site_group_to_user, attach_site_to_site_group

#get all users
def test_get_all_users(db_session):
users = get_all_users(session=db_session)
# assert
assert len(users) == 0

# get all site groups

# get all sites

# update user site group
19 changes: 19 additions & 0 deletions tests/test_sites_toolbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Test the toolbox functions"""
from sites_toolbox import get_user_details
from pvsite_datamodel.write.user_and_site import make_site, make_site_group, make_user

def test_get_user_details(db_session):
"""Test the get user details function"""
site_group = make_site_group(db_session=db_session)
site_1 = make_site(db_session=db_session, ml_id=1)
site_2 = make_site(db_session=db_session, ml_id=2)
site_group.sites.append(site_1)
site_group.sites.append(site_2)

user = make_user(db_session=db_session, email="[email protected]", site_group=site_group)
user_sites, user_site_group, user_site_count = get_user_details(session=db_session,
email="[email protected]")

assert user_sites == [{"site_uuid": str(site.site_uuid), "client_site_id": str(site.client_site_id)}for site in user.site_group.sites]
assert user_site_group == "test_site_group"
assert user_site_count == 2
17 changes: 17 additions & 0 deletions tests/test_toolbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Test the toolbox functions"""
from sites_toolbox import get_user_details
from pvsite_datamodel.write.user_and_site import make_site, make_site_group, make_user

def test_get_user_details(db_session):
"""Test the get user details function"""
site_group = make_site_group(db_session=db_session)
site_1 = make_site(db_session=db_session, ml_id=1)
site_2 = make_site(db_session=db_session, ml_id=2)
site_group.sites.append(site_1)
site_group.sites.append(site_2)

user = make_user(db_session=db_session, email="[email protected]", site_group=site_group)
user_sites, user_site_group = get_user_details(session=db_session, email="[email protected]")

assert user_sites == [str(site_1.site_uuid), str(site_2.site_uuid)]
assert user_site_group == "test_site_group"

0 comments on commit 3e38b35

Please sign in to comment.