Skip to content

Commit

Permalink
Run list of dates
Browse files Browse the repository at this point in the history
  • Loading branch information
joekakone committed Apr 6, 2024
1 parent cb6db28 commit 39b153d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 79 deletions.
2 changes: 1 addition & 1 deletion db_analytics_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

from .utils import Client

__version__ = "0.1.4.11"
__version__ = "0.1.4.12"
116 changes: 52 additions & 64 deletions db_analytics_tools/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,46 @@ def __init__(self, client):
self.client = client

@staticmethod
def generate_date_range(start_date, stop_date=None, freq='d', reverse=False):
def generate_date_range(start_date, stop_date=None, freq=None, dates=None, reverse=False, streamlit=False):
"""
Generate a range of dates.
:param start_date: The start date for the range.
:param stop_date: The stop date for the range.
:param freq: The frequency of the dates ('d' for daily, 'm' for monthly).
:param dates: A list of dates
:param reverse: If True, the date range is generated in reverse order (from stop_date to start_date).
:param streamlit: If True, use Streamlit for progress updates.01
:return: A list of formatted date strings.
"""
if stop_date is None:
a = start_date and (start_date or stop_date) and freq and dates is None # Date Range
b = start_date is None and stop_date is None and freq is None and dates # Specific list of dates
assert (a and not b) or (not a and b)

if dates:
dates_ranges = dates
# Reverse
if reverse: # Recent to Old
dates_ranges.sort(reverse=True)

print(f'Date Range : From {dates_ranges[0]} to {dates_ranges[-1]}')
print(f'Iterations : {len(dates_ranges)}')

if streamlit:
import streamlit as st
st.markdown(f"<p>Date Range : From {dates_ranges[0]} to {dates_ranges[-1]}</p>", unsafe_allow_html=True)
st.markdown(f"<p>Iterations : {len(dates_ranges)}</p>", unsafe_allow_html=True)

return dates_ranges

if start_date and stop_date is None:
print(f'Date : {start_date}')
print(f'Iterations : 1')
print('Iterations : 1')

if streamlit:
import streamlit as st
st.markdown(f"<p>Date : {start_date}</p>", unsafe_allow_html=True)
st.markdown(f"<p>Iterations : 1</p>", unsafe_allow_html=True)

return [start_date]

Expand All @@ -68,23 +95,30 @@ def generate_date_range(start_date, stop_date=None, freq='d', reverse=False):
print(f'Date Range : From {dates_ranges[0]} to {dates_ranges[-1]}')
print(f'Iterations : {len(dates_ranges)}')

if streamlit:
import streamlit as st
st.markdown(f"<p>Date Range : From {dates_ranges[0]} to {dates_ranges[-1]}</p>", unsafe_allow_html=True)
st.markdown(f"<p>Iterations : {len(dates_ranges)}</p>", unsafe_allow_html=True)

return dates_ranges

def run(self, function, start_date, stop_date=None, dates=[], freq='d', reverse=False, streamlit=False):
def run(self, function, start_date=None, stop_date=None, freq=None, dates=None, reverse=False, streamlit=False):
"""
Run a specified SQL function for a range of dates.
:param function: The SQL function to run for each date.
:param start_date: The start date for the range.
:param stop_date: The stop date for the range.
:param dates: A list of dates
:param freq: The frequency of the dates ('d' for daily, 'm' for monthly).
:param dates: A list of dates
:param reverse: If True, the date range is generated in reverse order (from stop_date to start_date).
:param streamlit: If True, use Streamlit for progress updates.
"""
print(f'Function : {function}')

# Generate Dates Range
dates_ranges = self.generate_date_range(start_date, stop_date, freq, reverse)
dates_ranges = self.generate_date_range(start_date, stop_date, freq, dates, reverse, streamlit)

print(f'Function : {function}')

# Send query to the server
for date in dates_ranges:
Expand All @@ -110,77 +144,28 @@ def run(self, function, start_date, stop_date=None, dates=[], freq='d', reverse=
st.markdown(f"<span style='font-weight: bold;'>Execution time: {duration}</span>",
unsafe_allow_html=True)

def run_dates(self, functions, dates, reverse=False, streamlit=False):
"""
Run multiple specified SQL functions for a range of dates.
:param functions: A list of SQL functions to run for each date.
:param dates: A list of dates
:param reverse: If True, the date range is generated in reverse order (from stop_date to start_date).
:param streamlit: If True, use Streamlit for progress updates.
"""
print(f'Functions : {functions}')

# Compute MAX Length of functions (Adjust display)
max_fun = max(len(function) for function in functions)

# Generate Dates Range
dates_ranges = dates
if reverse:
dates_ranges = list(reversed(dates_ranges))

##
print(f'Iterations : {len(dates_ranges)}')

# Send query to the server
for date in dates_ranges:
# Show date separator line
print("*" * (NBCHAR + max_fun))
for function in functions:
print(f"[Running Date: {date}] [Function: {function.ljust(max_fun, '.')}] ", end="", flush=True)
if streamlit:
import streamlit as st
st.markdown(
f"<span style='font-weight: bold;'>[Running Date: {date}] [Function: {function}] </span>",
unsafe_allow_html=True)

query = f"select {function}('{date}'::date);"
duration = datetime.datetime.now()

try:
self.client.execute(query)
except Exception as e:
raise Exception("Something went wrong!")
# finally:
# self.client.close()

duration = datetime.datetime.now() - duration
print(f"Execution time: {duration}")
if streamlit:
st.markdown(f"<span style='font-weight: bold;'>Execution time: {duration}</span>",
unsafe_allow_html=True)

# Show final date separator line
print("*" * (NBCHAR + max_fun))

def run_multiple(self, functions, start_date, stop_date=None, dates=[], freq='d', reverse=False, streamlit=False):
def run_multiple(self, functions, start_date=None, stop_date=None, freq=None, dates=None, reverse=False, streamlit=False):
"""
Run multiple specified SQL functions for a range of dates.
:param functions: A list of SQL functions to run for each date.
:param start_date: The start date for the range.
:param stop_date: The stop date for the range.
:param freq: The frequency of the dates ('d' for daily, 'm' for monthly).
:param dates: A list of dates
:param reverse: If True, the date range is generated in reverse order (from stop_date to start_date).
:param streamlit: If True, use Streamlit for progress updates.
"""
# Generate Dates Range
dates_ranges = self.generate_date_range(start_date, stop_date, freq, dates, reverse, streamlit)

print(f'Functions : {functions}')

# Compute MAX Length of functions (Adjust display)
max_fun = max(len(function) for function in functions)

# Generate Dates Range
dates_ranges = self.generate_date_range(start_date, stop_date, freq, reverse)
dates_ranges = self.generate_date_range(start_date, stop_date, freq, dates, reverse)

# Send query to the server
for date in dates_ranges:
Expand Down Expand Up @@ -214,7 +199,7 @@ def run_multiple(self, functions, start_date, stop_date=None, dates=[], freq='d'
print("*" * (NBCHAR + max_fun))


def create_etl(host, port, database, username, password, engine):
def create_etl(host, port, database, username, password, engine, keep_connection):
"""
Create an ETL (Extract, Transform, Load) instance with the specified database connection parameters.
Expand All @@ -224,13 +209,16 @@ def create_etl(host, port, database, username, password, engine):
:param username: The username for authenticating the database connection.
:param password: The password for authenticating the database connection.
:param engine: The database engine to use, currently supports 'postgres' and 'sqlserver'.
:param keep_connection: If True, the connection will be maintained until explicitly closed. If False, the connection
will be opened and closed for each database operation (default is False).
:return: An ETL instance for performing data extraction, transformation, and loading.
"""
client = Client(host=host,
port=port,
database=database,
username=username,
password=password,
engine=engine)
engine=engine,
keep_connection=keep_connection)
etl = ETL(client)
return etl
12 changes: 6 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
psycopg2-binary==2.9.7
pyodbc==4.0.39
pandas==2.0.3
SQLAlchemy==2.0.20
streamlit==1.26.0
matplotlib
psycopg2-binary>=2.9.9
pyodbc>=5.1.0
pandas>=2.2.1
SQLAlchemy>=2.0.29
streamlit>=1.32.2
matplotlib>=3.8.3
16 changes: 8 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="db_analytics_tools",
version="0.1.4.11",
version="0.1.4.12",
url="https://josephkonka.com/#projects",
download_url="https://github.com/joekakone/db-analytics-tools",
project_urls={
Expand All @@ -23,13 +23,13 @@
long_description=long_description,
long_description_content_type="text/markdown",
install_requires=[
"psycopg2-binary",
"pyodbc",
"pandas",
"SQLAlchemy",
"streamlit",
"matplotlib"
"psycopg2-binary>=2.9.9",
"pyodbc>=5.1.0",
"pandas>=2.2.1",
"SQLAlchemy>=2.0.29",
"streamlit>=1.32.2",
"matplotlibmatplotlib>=3.8.3"
],
python_requires=">=3.6",
packages=find_packages()
)
)

0 comments on commit 39b153d

Please sign in to comment.