Skip to content

Commit

Permalink
Merge pull request #5 from USC-InfoLab/jize
Browse files Browse the repository at this point in the history
add default config.yaml, fix log out button bug
  • Loading branch information
Arashhs authored Nov 28, 2023
2 parents 5865386 + a4f8b53 commit 179aadb
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 29 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ user.db
.vscode/
.idea/

# Don't state conf.py
conf/

conf/temp
tools/
config.yaml

# Don't stages dataset
*.csv
Expand Down
38 changes: 38 additions & 0 deletions conf/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
database_number: 1

database1:
# nickname: 'local db'
# dbms: 'postgresql'
# host: 'db' # Replace with your DB host
# port: 5432 # Replace with your DB port
# user: 'admin' # Replace with your DB username
# password: 'admin'# Replace with your DB password
nickname: 'gcp'
dbms: 'postgresql'
host: '35.235.95.37' # Replace with your DB host
port: 5439 # Replace with your DB port
user: 'jize' # Replace with your DB username
password: 'jz@uscdash23' # Replace with your DB password

mapping:
columns:
user_id: 'user_id'
timestamp: 'timestamp'
value: 'value'
tables:
user_table:
name: 'geomts_users'
columns:
user_id: String(50)
device: String(50)
location: String(50)
time_series:
- heart_rates
- calories
- mets
- distances
- steps
- sleep
- weight
geo:
- locations
47 changes: 47 additions & 0 deletions conf/tem/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#database:
# dbms: 'postgresql'
# host: '35.235.95.37' # Replace with your DB host
# port: 5439 # Replace with your DB port
# user: 'jize' # Replace with your DB username
# password: 'jz@uscdash23' # Replace with your DB password

database_number: 2

database1:
nickname: 'local db' # will shows in the selectbox
dbms: 'postgresql'
host: 'db' # Replace with your DB host
port: 5432 # Replace with your DB port
user: 'admin' # Replace with your DB username
password: 'admin'

database2:
nickname: 'gcp'
dbms: 'postgresql'
host: '35.235.95.37' # Replace with your DB host
port: 5439 # Replace with your DB port
user: 'jize' # Replace with your DB username
password: 'jz@uscdash23' # Replace with your DB password

mapping:
columns:
user_id: 'user_id'
timestamp: 'timestamp'
value: 'value'
tables:
user_table:
name: 'geomts_users'
columns:
user_id: String(50)
device: String(50)
location: String(50)
time_series:
- heart_rates
- calories
- mets
- distances
- steps
- sleep
- weight
geo:
- locations
7 changes: 5 additions & 2 deletions init_user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sqlite3
import hashlib
import os
import pickle

# create a ramdom salt
salt = os.urandom(16)
Expand All @@ -15,13 +16,15 @@
#store password
conn = sqlite3.connect('user.db')
cursor = conn.cursor()
query_history = []
cursor.execute('''drop table if exists users''')
cursor.execute('''create table if not exists users (
username text primary key,
password BLOB,
salt BLOB,
current_db text)''')
current_db text,
query_history BLOB)''')
cursor.execute('''insert into users
values("admin",?,?,"")''',(encode_pass,salt,))
values("admin",?,?,"",?)''',(encode_pass,salt,pickle.dumps(query_history),))
conn.commit()
conn.close()
10 changes: 10 additions & 0 deletions script/nav.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import streamlit as st

import streamlit as st
import webbrowser


def createNav():
# Using object notation
Expand All @@ -16,12 +18,15 @@ def createNav():

inputPage = st.sidebar.button("Input Page",use_container_width=True,type="secondary")
resultPage = st.sidebar.button("Result Page",use_container_width=True,type="secondary")
queryHistory = st.sidebar.button("Query History",use_container_width=True,type="secondary")

st.sidebar.divider()
st.sidebar.caption("Tutorial")
tutorial = st.sidebar.button("How to Start",use_container_width=True,type="secondary")

if (loginPage):
if(isLogin):
st.session_state["login-state"] = False
st.session_state["page"] = "login"
st.experimental_rerun()
if (importPage):
Expand All @@ -33,6 +38,11 @@ def createNav():
if(resultPage):
st.session_state["page"] = "result"
st.experimental_rerun()
if(queryHistory):
st.session_state["page"] = "query_history"
st.experimental_rerun()

if(tutorial):
# webbrowser.open_new_tab('https://chickensellerred.github.io/')
st.session_state["page"] = "tutorial"
st.experimental_rerun()
9 changes: 9 additions & 0 deletions script/query_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class query_history:
def __init__(self):
self.data = None

def set(self, data):
self.data = data

def get(self):
return self.data
30 changes: 28 additions & 2 deletions script/w4h_db_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import datetime
import os
import sqlite3
import json
import pickle

from loguru import logger
import pandas as pd
Expand Down Expand Up @@ -224,7 +226,6 @@ def populate_subject_table(df: pd.DataFrame, db_name: str, config_path='conf/con
engine.dispose()

def getCurrentDbByUsername(username):
print(os.getcwd())
with sqlite3.connect('user.db') as conn:
cursor = conn.cursor()
cursor.execute('''select current_db from users where username = ?''',(username,))
Expand All @@ -235,4 +236,29 @@ def updateCurrentDbByUsername(username,currentDb):
with sqlite3.connect('user.db') as conn:
cursor = conn.cursor()
cursor.execute('''update users set current_db = ? where username = ?''',(currentDb,username,))
conn.commit()
conn.commit()

def saveSessionByUsername(session):
with sqlite3.connect('user.db') as conn:
cursor = conn.cursor()
cursor.execute('''select query_history from users where username = ?''',(session.get('login-username'),))
result = cursor.fetchone()
conn.commit()
query_history = pickle.loads(result[0])
print("history:",query_history[0].get('selected_users'))
query_history.append(session)
serialized_object = pickle.dumps(query_history)

with sqlite3.connect('user.db') as conn:
cursor = conn.cursor()
cursor.execute('''UPDATE users SET query_history = ? WHERE username = ?''', (serialized_object,session['login-username'],))
conn.commit()

def getSessionByUsername(username):
with sqlite3.connect('user.db') as conn:
cursor = conn.cursor()
cursor.execute('''select query_history from users where username = ?''',(username,))
result = cursor.fetchone()
conn.commit()

return pickle.loads(result[0])
71 changes: 49 additions & 22 deletions viz.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import hashlib
import traceback
import pickle

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -601,9 +602,13 @@ def input_page(garmin_df):
# Store the filtered dataframe in session state
session['subjects_df'] = subjects_df
session['control_df'] = control_df


session_copy = session
saveSessionByUsername(session_copy)

# Go to the results page
session['page'] = "results"

st.experimental_rerun()


Expand All @@ -615,7 +620,7 @@ def results_page():
st.error("Please use the inputs page first.")
return


print('result page!')
subjects_df = session.get('subjects_df')
subject_ids = subjects_df.subj_id.tolist()
control_df = session.get('control_df')
Expand Down Expand Up @@ -1124,27 +1129,47 @@ def login_page():
st.error("something wrong in the server")
conn.close()

def query_history_page():
session = st.session_state
username = session.get('login-username')
query_history = getSessionByUsername(username)
# print("query history:",query_history)
for i, item in enumerate(query_history):
if(i == 1):
break
button_label = f"{item.get('selected_users')[0]} : from {item.get('start_date')} to {item.get('end_date')}"
if st.button(button_label):
session = item;
session['page'] = "results"
st.experimental_rerun()
# st.write(f"Clicked on {button_label}, corresponding array item: {item}")
st.markdown('Query History')



def tutorial_page():
page = st.selectbox("Select a tutorial", ["Setting up", "How to start"])

if page == "Setting up":
with open('markdown/setting_up.md', 'r', encoding='utf-8') as markdown_file:
markdown_text = markdown_file.read()
elif page == "How to start":
with open('markdown/how_to_start.md', 'r', encoding='utf-8') as markdown_file:
markdown_text = markdown_file.read()
st.markdown(markdown_text, unsafe_allow_html=True)
if page == "Setting up":
config_file = st.file_uploader("Upload config file", type=['yaml', 'example','txt'])
update_config = st.button("Update config")
if config_file is not None and update_config:
conf_dir = 'conf'
if not os.path.exists(conf_dir):
os.makedirs(conf_dir)
with open(f'{conf_dir}/config.yaml', 'w') as f:
# write content as string data into the file
f.write(config_file.getvalue().decode("utf-8"))
st.success("Update success!")
st.markdown('Build your config file from here: ')
st.markdown('[Tutorial](https://chickensellerred.github.io/)')
st.markdown('Then upload here: ')
#
# if page == "Setting up":
# with open('markdown/setting_up.md', 'r', encoding='utf-8') as markdown_file:
# markdown_text = markdown_file.read()
# elif page == "How to start":
# with open('markdown/how_to_start.md', 'r', encoding='utf-8') as markdown_file:
# markdown_text = markdown_file.read()
# st.markdown(markdown_text, unsafe_allow_html=True)
# if page == "Setting up":
config_file = st.file_uploader("Upload config file", type=['yaml', 'example','txt'])
update_config = st.button("Update config")
if config_file is not None and update_config:
conf_dir = 'conf'
if not os.path.exists(conf_dir):
os.makedirs(conf_dir)
with open(f'{conf_dir}/config.yaml', 'w') as f:
# write content as string data into the file
f.write(config_file.getvalue().decode("utf-8"))
st.success("Update success!")



Expand Down Expand Up @@ -1189,6 +1214,8 @@ def main():
import_page()
elif session.get("page") == "results":
results_page()
elif session.get("page") == "query_history":
query_history_page()


if __name__ == '__main__':
Expand Down

0 comments on commit 179aadb

Please sign in to comment.