Skip to content

Commit

Permalink
add user api page
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdudfield committed Nov 28, 2023
1 parent 0753d63 commit 0e543f4
Show file tree
Hide file tree
Showing 4 changed files with 133 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 @@ -28,6 +28,7 @@
from status import status_page
from tables.raw import make_raw_table
from tables.summary import make_recent_summary_stats, make_forecast_horizon_table
from users import user_page

Check warning on line 31 in src/main.py

View check run for this annotation

Codecov / codecov/patch

src/main.py#L31

Added line #L31 was not covered by tests

st.get_option("theme.primaryColor")
st.set_page_config(layout="centered", page_title="OCF Dashboard")
Expand Down Expand Up @@ -266,6 +267,7 @@ def metric_page():
"Forecast": forecast_page,
"PV Site Forecast": pvsite_forecast_page,
"Sites Toolbox": sites_toolbox_page,
"API Users": user_page,
}

demo_name = st.sidebar.selectbox("Choose a page", page_names_to_funcs.keys())
Expand Down
41 changes: 41 additions & 0 deletions src/plots/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pandas as pd
from plotly import graph_objects as go

from datetime import datetime


def make_api_requests_plot(
api_requests: pd.DataFrame, email_selected: str, end_time: datetime, start_time: datetime
):
"""
Make plot of API requests
Parameters
----------
api_requests : pd.DataFrame, need to have columns "created_utc" and "url"
email_selected : str, email of user
end_time : datetime, end time of plot
start_time : datetime, start time of plot
"""

fig = go.Figure(
layout=go.Layout(
title=go.layout.Title(
text=f"Api requests for {email_selected} between {start_time} and {end_time}"
),
xaxis=go.layout.XAxis(title=go.layout.xaxis.Title(text="Date")),
yaxis=go.layout.YAxis(title=go.layout.yaxis.Title(text="API request")),
legend=go.layout.Legend(title=go.layout.legend.Title(text="Chart Legend")),
)
)
fig.add_trace(
go.Scatter(
x=api_requests["created_utc"],
y=api_requests["url"],
mode="markers",
name="API requests",
)
)
fig.update_yaxes(visible=False)
return fig
77 changes: 77 additions & 0 deletions src/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import pandas as pd
import streamlit as st
from datetime import datetime, timedelta
import os
from nowcasting_datamodel.connection import DatabaseConnection
from nowcasting_datamodel.models.api import UserSQL, APIRequestSQL

Check warning on line 6 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L1-L6

Added lines #L1 - L6 were not covered by tests

from plots.users import make_api_requests_plot

Check warning on line 8 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L8

Added line #L8 was not covered by tests


def user_page():

Check warning on line 11 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L11

Added line #L11 was not covered by tests

st.markdown(

Check warning on line 13 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L13

Added line #L13 was not covered by tests
f'<h1 style="color:#63BCAF;font-size:48px;">{"API Users Page"}</h1>',
unsafe_allow_html=True,
)

st.text("See which users have been using the API")

Check warning on line 18 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L18

Added line #L18 was not covered by tests

start_time = st.sidebar.date_input(

Check warning on line 20 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L20

Added line #L20 was not covered by tests
"Start Date",
min_value=datetime.today() - timedelta(days=365),
max_value=datetime.today(),
value=datetime.today() - timedelta(days=31),
)
end_time = st.sidebar.date_input(

Check warning on line 26 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L26

Added line #L26 was not covered by tests
"End Date", min_value=datetime.today() - timedelta(days=365), max_value=datetime.today()
)

# get last call from the database
url = os.environ["DB_URL"]
connection = DatabaseConnection(url=url, echo=True)
with connection.get_session() as session:

Check warning on line 33 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L31-L33

Added lines #L31 - L33 were not covered by tests

last_requests_sql = (

Check warning on line 35 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L35

Added line #L35 was not covered by tests
session.query(APIRequestSQL)
.distinct(APIRequestSQL.user_uuid)
.join(UserSQL)
.order_by(APIRequestSQL.user_uuid, APIRequestSQL.created_utc.desc())
.all()
)

last_request = [

Check warning on line 43 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L43

Added line #L43 was not covered by tests
(last_request_sql.user.email, last_request_sql.created_utc)
for last_request_sql in last_requests_sql
]

last_request = pd.DataFrame(last_request, columns=["email", "last API reuqest"])
last_request = last_request.sort_values(by="last API reuqest", ascending=False)
last_request.set_index("email", inplace=True)

Check warning on line 50 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L48-L50

Added lines #L48 - L50 were not covered by tests

st.write(last_request)

Check warning on line 52 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L52

Added line #L52 was not covered by tests

# add selectbox for users
email_selected = st.sidebar.selectbox("Select", last_request.index.tolist(), index=0)

Check warning on line 55 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L55

Added line #L55 was not covered by tests

# get all calls for selected user
with connection.get_session() as session:
api_requests_sql = (

Check warning on line 59 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L58-L59

Added lines #L58 - L59 were not covered by tests
session.query(APIRequestSQL)
.join(UserSQL)
.where(UserSQL.email == email_selected)
.where(APIRequestSQL.created_utc >= start_time)
.where(APIRequestSQL.created_utc <= end_time)
.all()
)

api_requests = [

Check warning on line 68 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L68

Added line #L68 was not covered by tests
(api_request_sql.created_utc, api_request_sql.url)
for api_request_sql in api_requests_sql
]
api_requests = pd.DataFrame(api_requests, columns=["created_utc", "url"])

Check warning on line 72 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L72

Added line #L72 was not covered by tests

fig = make_api_requests_plot(api_requests, email_selected, end_time, start_time)
st.plotly_chart(fig, theme="streamlit")

Check warning on line 75 in src/users.py

View check run for this annotation

Codecov / codecov/patch

src/users.py#L74-L75

Added lines #L74 - L75 were not covered by tests


13 changes: 13 additions & 0 deletions tests/plots/test_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from plots.users import make_api_requests_plot
import pandas as pd

def test_make_api_requests_plot():

# create fake data
api_requests = pd.DataFrame(
{
"created_utc": ["2021-01-01", "2021-01-02", "2021-01-03"],
"url": ["https://api.solarforecastarbiter.org/", "https://api.solarforecastarbiter.org/", "https://api.solarforecastarbiter.org/"],
}
)
_ = make_api_requests_plot(api_requests, "", "", "")

0 comments on commit 0e543f4

Please sign in to comment.