Skip to content

Commit

Permalink
a little less caching
Browse files Browse the repository at this point in the history
  • Loading branch information
sborms committed Nov 15, 2023
1 parent 9a87c2e commit 189844f
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 59 deletions.
Binary file removed database/futsalfriend-template.db
Binary file not shown.
File renamed without changes.
4 changes: 2 additions & 2 deletions scraper/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def ymdhms():


def write_current_date_to_file(dir):
"""Writes away current date as DD-MM-YYYY in a text file."""
"""Writes away current date as YYYY-MM-DD in a text file."""
with open(dir, "w") as f:
f.write(datetime.now().strftime("%d-%m-%Y"))
f.write(datetime.now().strftime("%Y-%m-%d"))


def chunks(lst, n):
Expand Down
6 changes: 2 additions & 4 deletions webapp/01_Home.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import streamlit as st
from utils import add_socials_to_sidebar
from utils import LAST_UPDATED, add_socials_to_sidebar

st.set_page_config(page_title="Futsal Friend", page_icon="⚽", layout="wide")

Expand All @@ -10,9 +10,7 @@
add_socials_to_sidebar()

with st.sidebar:
with open("webapp/last_updated.txt", "r") as f:
last_updated = f.read()
st.markdown(f"**Last updated**: {last_updated}")
st.markdown(f"**Last updated**: {LAST_UPDATED}")

st.markdown(
"""# ⚽ Futsal Friend <span style=color:#030080><font size=4>Beta</font></span>""",
Expand Down
2 changes: 1 addition & 1 deletion webapp/last_updated.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14-11-2023
2023-11-14
21 changes: 11 additions & 10 deletions webapp/pages/02_🏆_Friendly_Finder.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from datetime import datetime, timedelta

import queries
import streamlit as st
import utils

st.set_page_config(page_title="Friendly Finder", page_icon="🏆", layout="wide")


conn = utils.connect_to_sqlite_db()
df_teams = queries.query_teams(conn)
import queries
df_teams = queries.query_teams()

##################
########## UI ##
Expand All @@ -34,16 +32,20 @@
level = col4.selectbox("Level", levels.keys(), index=2)
horizon = col5.number_input("When (< days)?", value=14, min_value=3, max_value=30)

today = datetime.today().strftime("%Y-%m-%d")
max_date = (datetime.today() + timedelta(days=horizon)).strftime("%Y-%m-%d")
today = utils.LAST_UPDATED
max_date = (
datetime.strptime(today, "%Y-%m-%d").date() + timedelta(days=horizon)
).strftime("%Y-%m-%d")

# show output header
st.markdown("#### Potential play partners 🥰")

with st.spinner("Finding teams..."):
# query tables for specified parameters
df_levels = conn.query(f"select team from levels where level = {levels[level]};")
df_n_games = queries.query_nbr_next_games(conn, dates=[today, max_date])
df_levels = queries.CONNECTION.query(
f"select team from levels where level = {levels[level]};"
)
df_n_games = queries.query_nbr_next_games(dates=[today, max_date])

# filter teams based on remaining parameters
df_out = utils.filter_teams(df_teams, city, address, km)
Expand All @@ -65,8 +67,7 @@
)

# display table
# st.markdown("Reach out by going to the respective team page!")
st.write(
f"_The last column shows the amount of scheduled games until {max_date}._"
f"_The last column shows the amount of scheduled games between {today} and {max_date}._"
)
st.markdown(df_out.to_html(escape=False, index=False), unsafe_allow_html=True)
5 changes: 2 additions & 3 deletions webapp/pages/03_👫_Team_Finder.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import queries
import streamlit as st
import utils

st.set_page_config(page_title="Team Finder", page_icon="👫", layout="wide")

conn = utils.connect_to_sqlite_db()
df_teams = queries.query_teams(conn)
import queries
df_teams = queries.query_teams()

##################
########## UI ##
Expand Down
9 changes: 3 additions & 6 deletions webapp/pages/04_😏_Vanity_Stats.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import plotly.express as px
import queries
import streamlit as st
import utils

st.set_page_config(page_title="Vanity Stats", page_icon="😏", layout="wide")


def filter_stats(df_stats_agg, df_filt, min_w):
df_stats = df_stats_agg.query(f"Games >= {min_w}").merge(
df_filt,
Expand All @@ -24,9 +21,9 @@ def filter_players(df, dict_filters):
return df


conn = utils.connect_to_sqlite_db()
df_players = queries.query_players(conn)
df_stats_agg = queries.query_stats_agg(conn)
import queries
df_players = queries.query_players()
df_stats_agg = queries.query_stats_agg()

##################
########## UI ##
Expand Down
17 changes: 7 additions & 10 deletions webapp/pages/05_📣_Coachbot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import time

import queries
import streamlit as st
import utils
from langchain.chains import ConversationChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferWindowMemory
Expand All @@ -11,8 +9,9 @@

st.set_page_config(page_title="Coachbot", page_icon="📣", layout="wide")

import queries

@st.cache_resource(show_spinner=False, ttl=utils.TTL)
@st.cache_resource(show_spinner=False, ttl=1800)
def load_chain(input_openai_api_key, context=""):
"""Configures a conversational chain for answering user questions."""
# load OpenAI's language model
Expand Down Expand Up @@ -68,8 +67,6 @@ def lets_chat():
st.session_state["lets_chat"] = True


conn = utils.connect_to_sqlite_db()

##################
########## UI ##
##################
Expand All @@ -88,7 +85,7 @@ def lets_chat():

# ask for team first
if not st.session_state["lets_chat"]:
teams_all = queries.query_list_teams(conn)["team"].tolist()
teams_all = queries.query_list_teams()["team"].tolist()

col1, _, _ = st.columns(3)
team = col1.selectbox(
Expand Down Expand Up @@ -144,14 +141,14 @@ def lets_chat():
# get relevant information to add as context to prompt
team = st.session_state["team"]

df_schedule = queries.query_schedule(conn, team)
df_schedule = queries.query_schedule(team=team)

df_stats_players = queries.query_stats_players(conn, team=team)
df_stats_players = queries.query_stats_players(team=team)

oponnent_1 = [who for who in df_schedule.loc[0][1:].tolist() if who != team][0]
df_stats_players_oponnent_1 = queries.query_stats_players(conn, team=oponnent_1)
df_stats_players_oponnent_1 = queries.query_stats_players(team=oponnent_1)

df_standings = queries.query_standings(conn, team=team)
df_standings = queries.query_standings(team=team)

dict_info = {
"Competition standings": df_standings,
Expand Down
37 changes: 20 additions & 17 deletions webapp/queries.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import streamlit as st
from utils import TTL

TTL = 0 # cache time to live in seconds

def query_nbr_next_games(_conn, dates):
CONNECTION = st.connection("futsalfriend_db", type="sql", ttl=0)


def query_nbr_next_games(dates):
q = f"""
with
horizon_set as
Expand All @@ -27,13 +30,13 @@ def query_nbr_next_games(_conn, dates):
group by team;
"""

df = _conn.query(q)
df = CONNECTION.query(q)

return df


@st.cache_data(show_spinner=False, ttl=TTL)
def query_teams(_conn):
def query_teams():
q = """
select
t.area,
Expand Down Expand Up @@ -66,13 +69,13 @@ def query_teams(_conn):
order by t.team;
"""

df = _conn.query(q)
df = CONNECTION.query(q)

return df


@st.cache_data(show_spinner=False, ttl=TTL)
def query_players(_conn):
def query_players():
q = """
select distinct
t.area as Area,
Expand All @@ -87,13 +90,13 @@ def query_players(_conn):
order by t.area, t.region, t.competition, c.team;
"""

df = _conn.query(q)
df = CONNECTION.query(q)

return df


@st.cache_data(show_spinner=False, ttl=TTL)
def query_stats_agg(_conn):
def query_stats_agg():
q = """
select distinct
c.name as Name,
Expand All @@ -108,18 +111,18 @@ def query_stats_agg(_conn):
group by c.name, c.team;
"""

df = _conn.query(q)
df = CONNECTION.query(q)

return df


@st.cache_data(show_spinner=False, ttl=TTL)
def query_list_teams(_conn):
return _conn.query("select distinct team from teams;")
def query_list_teams():
return CONNECTION.query("select distinct team from teams;")


@st.cache_data(show_spinner=False, ttl=TTL)
def query_schedule(_conn, team):
def query_schedule(team):
q = f"""
select
date,
Expand All @@ -129,13 +132,13 @@ def query_schedule(_conn, team):
where (goals1 is NULL) and (team1 = '{team}' or team2 = '{team}');
"""

df = _conn.query(q)
df = CONNECTION.query(q)

return df


@st.cache_data(show_spinner=False, ttl=TTL)
def query_stats_players(_conn, team):
def query_stats_players(team):
q = f"""
select
team,
Expand All @@ -147,13 +150,13 @@ def query_stats_players(_conn, team):
where team = '{team}';
"""

df = _conn.query(q)
df = CONNECTION.query(q)

return df


@st.cache_data(show_spinner=False, ttl=TTL)
def query_standings(_conn, team):
def query_standings(team):
q = f"""
select
positie as position,
Expand All @@ -172,6 +175,6 @@ def query_standings(_conn, team):
on s.region = t.region and s.competition = t.competition;
"""

df = _conn.query(q)
df = CONNECTION.query(q)

return df
10 changes: 4 additions & 6 deletions webapp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
from geopy.extra.rate_limiter import RateLimiter
from geopy.geocoders import Nominatim

TTL = 900 # cache time to live in seconds

with open("webapp/last_updated.txt", "r") as f:
LAST_UPDATED = f.read()


geolocator = RateLimiter(
Nominatim(user_agent="address_finder_futsalfriend_app").geocode,
min_delay_seconds=1,
)


def connect_to_sqlite_db():
"""Returns SQLite connection."""
return st.connection("futsalfriend_db", type="sql", ttl=TTL)


def get_coordinates(address, city, country="Belgium"):
"""Gets coordinates from user input address as (latitude, longitude)."""
location = geolocator(f"{address}, {city}, {country}")
Expand Down

0 comments on commit 189844f

Please sign in to comment.