From fceb9dc7d15b3d6a921c2dd94e4f28b30750c8c4 Mon Sep 17 00:00:00 2001 From: ChickenSellerRED Date: Sat, 11 Nov 2023 23:07:26 -0800 Subject: [PATCH 1/3] add default config.yaml --- .gitignore | 5 ++--- conf/config.yaml | 32 ++++++++++++++++++++++++++++++++ script/nav.py | 7 +++++-- 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 conf/config.yaml diff --git a/.gitignore b/.gitignore index a4406d3..cf89b2e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,10 +7,9 @@ user.db .vscode/ .idea/ -# Don't state conf.py -conf/ + +conf/temp tools/ -config.yaml # Don't stages dataset *.csv diff --git a/conf/config.yaml b/conf/config.yaml new file mode 100644 index 0000000..b3a45fe --- /dev/null +++ b/conf/config.yaml @@ -0,0 +1,32 @@ +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 + +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 diff --git a/script/nav.py b/script/nav.py index 0e27067..0cfecee 100644 --- a/script/nav.py +++ b/script/nav.py @@ -1,6 +1,8 @@ import streamlit as st import streamlit as st +import webbrowser + def createNav(): # Using object notation @@ -22,6 +24,8 @@ def createNav(): 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): @@ -34,5 +38,4 @@ def createNav(): st.session_state["page"] = "result" st.experimental_rerun() if(tutorial): - st.session_state["page"] = "tutorial" - st.experimental_rerun() \ No newline at end of file + webbrowser.open_new_tab('https://chickensellerred.github.io/') \ No newline at end of file From e3c948505afee4c30460cd25c39cf135410676cf Mon Sep 17 00:00:00 2001 From: ChickenSellerRED Date: Mon, 13 Nov 2023 12:24:13 -0800 Subject: [PATCH 2/3] upload file from tutorial page --- script/nav.py | 4 +++- viz.py | 42 ++++++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/script/nav.py b/script/nav.py index 0cfecee..2d803a8 100644 --- a/script/nav.py +++ b/script/nav.py @@ -38,4 +38,6 @@ def createNav(): st.session_state["page"] = "result" st.experimental_rerun() if(tutorial): - webbrowser.open_new_tab('https://chickensellerred.github.io/') \ No newline at end of file + # webbrowser.open_new_tab('https://chickensellerred.github.io/') + st.session_state["page"] = "tutorial" + st.experimental_rerun() \ No newline at end of file diff --git a/viz.py b/viz.py index 52e6638..2345268 100644 --- a/viz.py +++ b/viz.py @@ -1125,26 +1125,28 @@ def login_page(): conn.close() 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!") From a4f8b5380e6aa352bb174e489858c843df3ca3f8 Mon Sep 17 00:00:00 2001 From: Jize Date: Mon, 27 Nov 2023 14:10:53 -0800 Subject: [PATCH 3/3] query history --- conf/config.yaml | 16 +++++++++----- conf/tem/config.yaml | 47 +++++++++++++++++++++++++++++++++++++++++ init_user.py | 7 ++++-- script/nav.py | 5 +++++ script/query_history.py | 9 ++++++++ script/w4h_db_utils.py | 30 ++++++++++++++++++++++++-- viz.py | 29 +++++++++++++++++++++++-- 7 files changed, 132 insertions(+), 11 deletions(-) create mode 100644 conf/tem/config.yaml create mode 100644 script/query_history.py diff --git a/conf/config.yaml b/conf/config.yaml index b3a45fe..408a93b 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -1,12 +1,18 @@ database_number: 1 database1: - nickname: 'local db' +# 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: '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 + 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: diff --git a/conf/tem/config.yaml b/conf/tem/config.yaml new file mode 100644 index 0000000..70dab6a --- /dev/null +++ b/conf/tem/config.yaml @@ -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 diff --git a/init_user.py b/init_user.py index 10f8830..b8b32b9 100644 --- a/init_user.py +++ b/init_user.py @@ -1,6 +1,7 @@ import sqlite3 import hashlib import os +import pickle # create a ramdom salt salt = os.urandom(16) @@ -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() \ No newline at end of file diff --git a/script/nav.py b/script/nav.py index 2d803a8..c3c2a71 100644 --- a/script/nav.py +++ b/script/nav.py @@ -18,6 +18,7 @@ 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") @@ -37,6 +38,10 @@ 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" diff --git a/script/query_history.py b/script/query_history.py new file mode 100644 index 0000000..0f6aa9c --- /dev/null +++ b/script/query_history.py @@ -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 \ No newline at end of file diff --git a/script/w4h_db_utils.py b/script/w4h_db_utils.py index 6ed4d89..8b2c3be 100644 --- a/script/w4h_db_utils.py +++ b/script/w4h_db_utils.py @@ -1,6 +1,8 @@ import datetime import os import sqlite3 +import json +import pickle from loguru import logger import pandas as pd @@ -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,)) @@ -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() \ No newline at end of file + 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]) \ No newline at end of file diff --git a/viz.py b/viz.py index 2345268..a6e8ee5 100644 --- a/viz.py +++ b/viz.py @@ -1,5 +1,6 @@ import hashlib import traceback +import pickle import numpy as np import pandas as pd @@ -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() @@ -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') @@ -1124,6 +1129,24 @@ 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(): st.markdown('Build your config file from here: ') st.markdown('[Tutorial](https://chickensellerred.github.io/)') @@ -1191,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__':