Skip to content

Commit

Permalink
fixed?
Browse files Browse the repository at this point in the history
  • Loading branch information
eccentricOrange committed May 14, 2022
1 parent 2ca77a2 commit 626ccd2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 42 deletions.
12 changes: 6 additions & 6 deletions npbc_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,11 +663,11 @@ def getlogs(args: ArgNamespace) -> None:
# attempt to get the logs from the database
try:
data = npbc_core.get_logged_data(
log_id = args.logid,
paper_id=args.paperid,
month=args.month,
year=args.year,
timestamp= datetime.strptime(args.timestamp, r'%d/%m/%Y %I:%M:%S %p') if args.timestamp else None
query_log_id = args.logid,
query_paper_id=args.paperid,
query_month=args.month,
query_year=args.year,
query_timestamp= datetime.strptime(args.timestamp, r'%d/%m/%Y %I:%M:%S %p') if args.timestamp else None
)

# if there is a database error, print an error message
Expand All @@ -683,7 +683,7 @@ def getlogs(args: ArgNamespace) -> None:
# print column headers
print(' | '.join(
f"{Fore.YELLOW}{header}{Style.RESET_ALL}"
for header in ['log_id', 'paper_id', 'month', 'year', 'timestamp', 'date', 'cost']
for header in ['log_id', 'paper_id', 'month', 'year', 'timestamp', 'date/cost']
))

# print the data
Expand Down
71 changes: 41 additions & 30 deletions npbc_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,71 +692,82 @@ def get_undelivered_strings(


def get_logged_data(
paper_id: int | None = None,
log_id: int | None = None,
month: int | None = None,
year: int | None = None,
timestamp: date_type | None = None
):
query_paper_id: int | None = None,
query_log_id: int | None = None,
query_month: int | None = None,
query_year: int | None = None,
query_timestamp: date_type | None = None
) -> Generator[tuple[int, int, int, int, str, str | float], None, None]:
"""get logged data
- the user may specify as parameters many as they want
- available parameters: paper_id, log_id, month, year, timestamp
- returns: a list of tuples containing the following fields:
log_id, paper_id, month, year, timestamp, date, cost."""
- yields: tuples containing the following fields:
log_id, paper_id, month, year, timestamp, date | cost."""

global DATABASE_PATH

# initialize parameters for the WHERE clause of the SQL query
data = []
parameters = []
values = ()

# check each parameter and add it to the WHERE clause if it is given
if paper_id:
if query_paper_id:
parameters.append("paper_id")
values += (paper_id,)
values += (query_paper_id,)

if log_id:
if query_log_id:
parameters.append("log_id")
values += (log_id,)
values += (query_log_id,)

if month:
if query_month:
parameters.append("month")
values += (month,)
values += (query_month,)

if year:
if query_year:
parameters.append("year")
values += (year,)
values += (query_year,)

if timestamp:
if query_timestamp:
parameters.append("timestamp")
values += (timestamp.strftime(r'%d/%m/%Y %I:%M:%S %p'),)
values += (query_timestamp.strftime(r'%d/%m/%Y %I:%M:%S %p'),)

# generate the SQL query
columns_only_query = """
SELECT logs.log_id, logs.paper_id, logs.month, logs.year, logs.timestamp, undelivered_dates_logs.date_not_delivered, cost_logs.cost
logs_base_query = """
SELECT log_id, paper_id, timestamp, month, year
FROM logs
INNER JOIN undelivered_dates_logs ON logs.log_id = undelivered_dates_logs.log_id
INNER JOIN cost_logs ON logs.log_id = cost_logs.log_id"""
ORDER BY log_id, paper_id
"""

if parameters:
conditions = ' AND '.join(
f"logs.{parameter} = ?"
f"{parameter} = ?"
for parameter in parameters
)

final_query = f"{columns_only_query} WHERE {conditions};"
logs_query = f"{logs_base_query} WHERE {conditions};"

else:
final_query = f"{columns_only_query};"
logs_query = f"{logs_base_query};"

dates_query = "SELECT log_id, date_not_delivered FROM undelivered_dates_logs;"
costs_query = "SELECT log_id, cost FROM cost_logs;"

# execute the query
with connect(DATABASE_PATH) as connection:
data = connection.execute(final_query, values).fetchall()
logs = {
log_id: [paper_id, month, year, timestamp]
for log_id, paper_id, timestamp, month, year in connection.execute(logs_query, values).fetchall()
}

connection.close()
dates = connection.execute(dates_query).fetchall()
costs = connection.execute(costs_query).fetchall()

return data
for log_id, date in dates:
yield tuple(logs[log_id] + [date])

for log_id, cost in costs:
yield tuple(logs[log_id] + [float(cost)])

connection.close()



Expand Down
16 changes: 10 additions & 6 deletions test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@


def setup_db(database_path: Path, schema_path: Path, test_sql: Path):
DATABASE_PATH.unlink(missing_ok=True)
database_path.unlink(missing_ok=True)


with connect(database_path) as connection:
connection.executescript(schema_path.read_text())
Expand Down Expand Up @@ -281,11 +282,14 @@ def test_save_results():
setup_db(DATABASE_PATH, SCHEMA_PATH, TEST_SQL)

known_data = [
(1, 1, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-01', 105.0),
(1, 1, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-02', 105.0),
(2, 2, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-03', 51.0),
(2, 2, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-01', 51.0),
(2, 2, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-05', 51.0)
(1, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-01'),
(1, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-02'),
(2, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-01'),
(2, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-05'),
(2, 1, 2020, '04/01/2022 01:05:42 AM', '2020-01-03'),
(1, 1, 2020, '04/01/2022 01:05:42 AM', 105.0),
(2, 1, 2020, '04/01/2022 01:05:42 AM', 51.0),
(3, 1, 2020, '04/01/2022 01:05:42 AM', 647.0)
]

npbc_core.save_results(
Expand Down

0 comments on commit 626ccd2

Please sign in to comment.