-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from openclimatefix/issue/53-show-user-details
Issue/53 show user details
- Loading branch information
Showing
6 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |