Add tests and fix issues
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
from os import getenv
|
||||
|
||||
import streamlit as st
|
||||
from sqlalchemy.sql import text
|
||||
from streamlit.connections import BaseConnection
|
||||
|
||||
connection: BaseConnection = st.connection("sqlite")
|
||||
def connection() -> BaseConnection:
|
||||
_connection = st.connection("sql", url=getenv('DATABASE_URL'))
|
||||
with _connection.session as configured_session:
|
||||
configured_session.execute(text('PRAGMA foreign_keys=ON'))
|
||||
return _connection
|
||||
|
||||
|
||||
with connection.session as configure_session:
|
||||
configure_session.execute(text('PRAGMA foreign_keys=ON'))
|
||||
@@ -8,7 +8,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
def create_counter(title:str, counter_type:CounterType, counter_color) -> None:
|
||||
logger.info("Adding counter %s", counter_type)
|
||||
with connection.session as session:
|
||||
with connection().session as session:
|
||||
try:
|
||||
query = text('INSERT INTO counters (name, type, color) VALUES (:title, :type, :color)')
|
||||
session.execute(query, {'title': title, 'type': counter_type, 'color': counter_color})
|
||||
@@ -19,14 +19,14 @@ def create_counter(title:str, counter_type:CounterType, counter_color) -> None:
|
||||
|
||||
def get_counters():
|
||||
try:
|
||||
return connection.query('SELECT id, name, type, color FROM counters', ttl=0)
|
||||
return connection().query('SELECT id, name, type, color FROM counters', ttl=0)
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return st.dataframe()
|
||||
|
||||
def increment_counter(counter_id:int) -> None:
|
||||
logger.info("Incrementing counter %s", counter_id)
|
||||
with connection.session as session:
|
||||
with connection().session as session:
|
||||
try:
|
||||
query = text('INSERT INTO entries (counter_id) VALUES (:id)')
|
||||
session.execute(query, {'id': counter_id})
|
||||
@@ -35,10 +35,9 @@ def increment_counter(counter_id:int) -> None:
|
||||
logger.error(e)
|
||||
session.rollback()
|
||||
|
||||
|
||||
def remove_counter(counter_id:int) -> None:
|
||||
logger.info("Removing counter %s", counter_id)
|
||||
with connection.session as session:
|
||||
with connection().session as session:
|
||||
try:
|
||||
query = text('DELETE FROM counters WHERE id = :id')
|
||||
session.execute(query, {'id': counter_id})
|
||||
@@ -47,10 +46,9 @@ def remove_counter(counter_id:int) -> None:
|
||||
logger.error(e)
|
||||
session.rollback()
|
||||
|
||||
|
||||
def get_counter(counter_id:int):
|
||||
try:
|
||||
return connection.query('SELECT * FROM counters WHERE id = :id', params={'id': counter_id}, ttl=0).iloc[0]
|
||||
return connection().query('SELECT * FROM counters WHERE id = :id', params={'id': counter_id}, ttl=0).iloc[0]
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return None
|
||||
@@ -58,7 +56,7 @@ def get_counter(counter_id:int):
|
||||
|
||||
def get_colors(palette_id:int):
|
||||
try:
|
||||
return connection.query('''SELECT color1,color2,color3,color4,color5 FROM color_palettes WHERE id = :id''', params={'id': palette_id})
|
||||
return connection().query('''SELECT color1,color2,color3,color4,color5 FROM color_palettes WHERE id = :id''', params={'id': palette_id})
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
return None
|
||||
|
||||
@@ -5,7 +5,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
def get_all_daily_analytics(end_date:str = 'now'):
|
||||
try:
|
||||
return connection.query('''
|
||||
return connection().query('''
|
||||
WITH RECURSIVE timeseries(d) AS (
|
||||
VALUES(date(:end_date))
|
||||
UNION ALL
|
||||
@@ -39,7 +39,7 @@ def get_all_daily_analytics(end_date:str = 'now'):
|
||||
|
||||
def get_daily_analytics(counter_id:int, end_date:str = 'now'):
|
||||
try:
|
||||
return connection.query('''
|
||||
return connection().query('''
|
||||
WITH RECURSIVE timeseries(d) AS (
|
||||
VALUES(date(:end_date))
|
||||
UNION ALL
|
||||
|
||||
@@ -5,7 +5,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
def get_all_monthly_analytics(end_date:str = 'now'):
|
||||
try:
|
||||
return connection.query('''
|
||||
return connection().query('''
|
||||
WITH RECURSIVE timeseries(d) AS (
|
||||
VALUES(date(:end_date,'start of year'))
|
||||
UNION ALL
|
||||
@@ -45,7 +45,7 @@ def get_all_monthly_analytics(end_date:str = 'now'):
|
||||
|
||||
def get_monthly_analytics(counter_id:int, end_date:str = 'now'):
|
||||
try:
|
||||
return connection.query('''
|
||||
return connection().query('''
|
||||
WITH RECURSIVE timeseries(d) AS (
|
||||
VALUES( date(:end_date, 'start of year'))
|
||||
UNION ALL
|
||||
|
||||
@@ -5,7 +5,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
def get_all_weekly_analytics(end_date:str = 'now'):
|
||||
try:
|
||||
return connection.query('''
|
||||
return connection().query('''
|
||||
WITH RECURSIVE timeseries(d) AS (
|
||||
VALUES(date(:end_date, 'weekday 0'))
|
||||
UNION ALL
|
||||
@@ -42,7 +42,7 @@ def get_all_weekly_analytics(end_date:str = 'now'):
|
||||
|
||||
def get_weekly_analytics(counter_id:int, end_date:str = 'now'):
|
||||
try:
|
||||
return connection.query('''
|
||||
return connection().query('''
|
||||
WITH RECURSIVE timeseries(d) AS (
|
||||
VALUES(date(:end_date, 'weekday 0'))
|
||||
UNION ALL
|
||||
|
||||
@@ -5,7 +5,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
def get_all_yearly_analytics(end_date:str = 'now'):
|
||||
try:
|
||||
return connection.query('''
|
||||
return connection().query('''
|
||||
WITH RECURSIVE timeseries(d) AS (
|
||||
VALUES(date(:end_date,'start of year', '-4 years'))
|
||||
UNION ALL
|
||||
@@ -42,7 +42,7 @@ def get_all_yearly_analytics(end_date:str = 'now'):
|
||||
|
||||
def get_yearly_analytics(counter_id:int, end_date:str = 'now'):
|
||||
try:
|
||||
return connection.query('''
|
||||
return connection().query('''
|
||||
WITH RECURSIVE timeseries(d) AS (
|
||||
VALUES( date(:end_date, 'start of year', '-4 years'))
|
||||
UNION ALL
|
||||
|
||||
Reference in New Issue
Block a user