Skip to content

Commit

Permalink
query history
Browse files Browse the repository at this point in the history
  • Loading branch information
ChickenSellerRED committed Nov 27, 2023
1 parent be8c6e7 commit 29f4fd5
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 6 deletions.
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()
5 changes: 5 additions & 0 deletions script/nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"
Expand Down
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])
29 changes: 27 additions & 2 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,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/)')
Expand Down Expand Up @@ -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__':
Expand Down

0 comments on commit 29f4fd5

Please sign in to comment.