Skip to content

Commit

Permalink
update query history
Browse files Browse the repository at this point in the history
  • Loading branch information
ChickenSellerRED committed Dec 11, 2023
1 parent a4f8b53 commit 5336d8c
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 5 additions & 11 deletions conf/config.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
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'
nickname: 'local db' # will shows in the selectbox
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
host: 'db' # Replace with your DB host
port: 5432 # Replace with your DB port
user: 'admin' # Replace with your DB username
password: 'admin'

mapping:
columns:
Expand Down
22 changes: 16 additions & 6 deletions script/query_history.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@

class query_history:
def __init__(self):
self.data = None
def __init__(self,session):
self.data = {}
key_list = list(session.keys())
for key in key_list:
self.data[key] = session.get(key)

def set(self, key,value):
self.data[key] = value

def set(self, data):
self.data = data
def get(self,key):
return self.data[key]

def get(self):
return self.data
def setSession(self,session):
key_list = list(self.data.keys())
for key in key_list:
session[key] = self.data.get(key)
return session
14 changes: 14 additions & 0 deletions script/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pickle

a = [1]
print('a: ',a)

saved = pickle.dumps(a)
print('saved a:',saved)
b = pickle.loads(saved)
print('b: ',b)
b.append(2)
savedLoadArray = pickle.dumps(b)
print('saved b: ',savedLoadArray)
c = pickle.loads(savedLoadArray)
print('c: ',c)
19 changes: 9 additions & 10 deletions script/w4h_db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,27 +238,26 @@ def updateCurrentDbByUsername(username,currentDb):
cursor.execute('''update users set current_db = ? where username = ?''',(currentDb,username,))
conn.commit()

def saveSessionByUsername(session):
def saveQueryHistoryByUsername(queryHistory):
with sqlite3.connect('user.db') as conn:
cursor = conn.cursor()
cursor.execute('''select query_history from users where username = ?''',(session.get('login-username'),))
cursor.execute('''select query_history from users where username = ?''',(queryHistory.data.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)
historys = pickle.loads(result[0])
historys.append(queryHistory)
serialized_object = pickle.dumps(historys)

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

def getSessionByUsername(username):
def getQueryHistoryByUsername(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])
query_history = pickle.loads(result[0])
return query_history
41 changes: 26 additions & 15 deletions viz.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import hashlib
import traceback
import pickle
Expand All @@ -19,6 +20,8 @@
from script.import_hub_main import import_page
import geopandas as gpd
from shapely import wkb

from script.query_history import query_history
from script.utils import get_db_engine

import os
Expand Down Expand Up @@ -603,15 +606,16 @@ def input_page(garmin_df):
session['subjects_df'] = subjects_df
session['control_df'] = control_df

session_copy = session
saveSessionByUsername(session_copy)
q = query_history(session)
print('q:qqqq: ',q)
getQueryHistoryByUsername(q.data['login-username'])
saveQueryHistoryByUsername(q)

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

st.experimental_rerun()


# Define the results page
def results_page():
# Get the session state
Expand Down Expand Up @@ -1131,20 +1135,27 @@ def login_page():

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')
username = session.get('login-username')
query_history = getQueryHistoryByUsername(username)

st.write(f"Total {len(query_history)} queries")
for i, query in enumerate(query_history):
keys_list = list(query.data.keys())
button_label = f"{query.get('selected_users')} : {query.get('start_date')} ~ {query.get('end_date')}"
with st.expander(button_label, expanded=False):

if st.button('query again', key=f'query again {i}'):
query.setSession(session)
session['page'] = "results"
st.experimental_rerun()
for key in keys_list:
if(key.startswith('df_') or key.endswith('_df')):
continue
st.markdown(f"<font color='gray' size='2'>{key} : {query.data.get(key)}</font>",
unsafe_allow_html=True)

# st.write(f"{key} : {query.data.get(key)}")


def tutorial_page():
Expand Down

0 comments on commit 5336d8c

Please sign in to comment.