From b2270790da65b2c383ec3a8f75ca9cfe991ed281 Mon Sep 17 00:00:00 2001 From: nick-fournier Date: Sun, 19 Nov 2023 05:09:01 +0000 Subject: [PATCH] Added optional postgres functionality to db added psycopg2 to installs working postgres implementation --- .gitignore | 3 + config.py | 15 +- db_conn.py | 254 +++++++++++++++++++++++++++++++++ speed-cam.py | 147 +++++-------------- speed-install.sh | 2 + sql-make-graph-count-totals.py | 57 ++------ sql-make-graph-speed-ave.py | 60 ++------ 7 files changed, 336 insertions(+), 202 deletions(-) create mode 100644 db_conn.py diff --git a/.gitignore b/.gitignore index 11bd50b..26c05f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# .env file +.env + # Windows image file caches Thumbs.db ehthumbs.db diff --git a/config.py b/config.py index 0b44fd3..a17540b 100644 --- a/config.py +++ b/config.py @@ -8,6 +8,11 @@ # auto configured so you may not need to use a plugin # to change resolution. ####################################### +import os +from dotenv import load_dotenv + +# Load sensitive variables from .env +load_dotenv() # Calibration Settings # -------------------- @@ -34,7 +39,7 @@ # Camera Settings # --------------- -CAMERA = "pilibcam" # valid values usbcam, rtspcam, pilibcam, pilegcam +CAMERA = "usbcam" # valid values usbcam, rtspcam, pilibcam, pilegcam CAM_LOCATION = "Front Window" USBCAM_SRC = 0 # Device number of USB connection usually 0, 1, 2, Etc RTSPCAM_SRC = "rtsp://user:password@IP:554/path" # Set per IP Cam Docs and config see example below @@ -153,9 +158,15 @@ # Sqlite3 Settings # ---------------- DB_DIR = "data" -DB_NAME = "speed_cam.db" +DB_NAME = "speed_cam" # "speed_cam.db" DB_TABLE = "speed" +DB_TYPE = "postgres" # sqlite3 or postgres +DB_HOST = '192.168.1.159' +DB_USER = os.getenv('PG_USER') # Should set these in the .env +DB_PASSWORD = os.getenv('PG_PASSWORD') +DB_PORT = 5432 + # matplotlib graph image settings # ------------------------------- GRAPH_PATH = 'media/graphs' # Directory path for storing graph images diff --git a/db_conn.py b/db_conn.py new file mode 100644 index 0000000..2c929dc --- /dev/null +++ b/db_conn.py @@ -0,0 +1,254 @@ +import os +import psycopg2 +import sqlite3 +import logging +from config import DB_DIR +from config import DB_NAME +from config import DB_TABLE +from config import DB_TYPE +from config import DB_USER +from config import DB_PASSWORD +from config import DB_HOST +from config import DB_PORT + +#---------------------------------------------------------------------------------------- + +# Get information about this script including name, launch path, etc. +# This allows script to be renamed or relocated to another directory +mypath = os.path.abspath(__file__) # Find the full path of this python script +# get the path location only (excluding script name) +baseDir = mypath[0 : mypath.rfind("/") + 1] +# Do a quick check to see if the sqlite database directory path exists +DB_DIR_PATH = os.path.join(baseDir, DB_DIR) +if not os.path.exists(DB_DIR_PATH): # Check if database directory exists + os.makedirs(DB_DIR_PATH) # make directory if Not Found + +DB_CONN = {} +if DB_TYPE == "sqlite3": + DB_CONN['path'] = os.path.join(DB_DIR_PATH, DB_NAME) # Create path to db file + +elif DB_TYPE == 'postgres': + DB_CONN = { + 'dbname': DB_NAME, + 'user': DB_USER, + 'password': DB_PASSWORD, + 'port': DB_PORT, + 'host': DB_HOST + } + +else: + print(f'Invalid DB_TYPE {DB_TYPE}') + raise NameError + +def db_connect(db_conn_dict): + if DB_TYPE == "sqlite3": + db_file = db_conn_dict.get('path') + conn = sqlite3.connect(db_file, timeout=1) + + elif DB_TYPE == "postgres": + conn = psycopg2.connect(**db_conn_dict) + conn.autocommit = True + + else: + logging.error("Failed: invalid DB_TYPE %s", DB_TYPE) + return None + + return conn + +# ------------------------------------------------------------------------------ +def db_exists(db_conn_dict): + """ + Determines if DB exists, creates it otherwise + """ + + # Determine if filename is in sqlite3 format + if DB_TYPE == 'sqlite3': + filename = db_conn_dict.get('path') + + if os.path.isfile(filename): + if os.path.getsize(filename) < 100: # SQLite database file header is 100 bytes + size = os.path.getsize(filename) + logging.error("%s %d is Less than 100 bytes", filename, size) + return False + with open(filename, "rb") as fd: + header = fd.read(100) + if header.startswith(b"SQLite format 3"): + logging.info("Success: File is sqlite3 Format %s", filename) + return True + else: + logging.error("Failed: File NOT sqlite3 Header Format %s", filename) + return False + else: + logging.warning("File Not Found %s", filename) + logging.info("Create sqlite3 database File %s", filename) + try: + conn = sqlite3.connect(filename) + except sqlite3.Error as e: + logging.error("Failed: Create Database %s.", filename) + logging.error("Error Msg: %s", e) + return False + conn.commit() + conn.close() + logging.info(f"Success: Created sqlite3 Database %s", filename) + return True + + # Determine if the database exists on postgres server, create it if not + elif DB_TYPE == 'postgres': + + # Connect to the database + dbname = db_conn_dict['dbname'] + server_conn = {k: v for k, v in db_conn_dict.items() if k != 'dbname'} + conn = psycopg2.connect(**server_conn) + conn.autocommit = True + + # Check if the database exists + cursor = conn.cursor() + cursor.execute(f"SELECT EXISTS (SELECT 1 FROM pg_database WHERE datname = '{dbname}')") + + # Get the result + exists = cursor.fetchone()[0] + + if exists: + conn.close() + return True + else: + # Create the database + cursor.execute(f"CREATE DATABASE {dbname}") + conn.close() + return True + +# ------------------------------------------------------------------------------ +def db_check(db_conn_dict): + """ + Check if db_file is a sqlite3 file and connect if possible + """ + if db_exists(db_conn_dict): + try: + conn = db_connect(db_conn_dict) + + except (sqlite3.Error, psycopg2.Error) as e: + logging.error("Failed: %s Connect to DB", DB_TYPE) + logging.error("Error Msg: %s", e) + return None + + else: + logging.error("Failed: Could not connect to DB") + return None + + conn.commit() + logging.info("Success: Connected to DB %s", DB_TYPE) + return conn + + +# ------------------------------------------------------------------------------ +def db_open(db_conn_dict): + """ + Insert speed data into database table + """ + try: + db_conn = db_connect(db_conn_dict) + cursor = db_conn.cursor() + + except (sqlite3.Error, psycopg2.Error) as e: + logging.error("Failed: Connect to DB %s", DB_TYPE) + logging.error("Error Msg: %s", e) + return None + + sql_cmd = """create table if not exists {} (idx text primary key, + log_timestamp text, + camera text, + ave_speed real, speed_units text, image_path text, + image_w integer, image_h integer, image_bigger integer, + direction text, plugin_name text, + cx integer, cy integer, + mw integer, mh integer, m_area integer, + x_left integer, x_right integer, + y_upper integer, y_lower integer, + max_speed_over integer, + min_area integer, track_counter integer, + cal_obj_px integer, cal_obj_mm integer, status text, cam_location text)""".format( + DB_TABLE + ) + try: + if DB_TYPE == 'sqlite3': + db_conn.execute(sql_cmd) + + elif DB_TYPE == 'postgres': + cursor.execute(sql_cmd) + + else: + logging.error("Failed: To Create Table %s on DB_TYPE %s", DB_TABLE, DB_TYPE) + return None + + except (sqlite3.Error, psycopg2.Error) as e: + logging.error("Failed: To Create Table %s on %s DB", DB_TABLE, DB_TYPE) + logging.error("Error Msg: %s", e) + return None + + else: + db_conn.commit() + return db_conn + +#---------------------------------------------------------------------------------------- +def get_timestamp_substr(total_by): + ''' + Convert hour, day or month string to required + values for changing the log_timestamp to + an appropriate substring value + ''' + total_by = total_by.upper() + if total_by == 'HOUR': + timestamp_subst = '2, 13' + elif total_by == 'DAY': + timestamp_subst = '2, 10' + elif total_by == 'MONTH': + timestamp_subst = '2, 7' + else: + logging.info("total_by variable must be string. Valid values are hour, day, month") + logging.warning("Defaulting to hour") + timestamp_subst = '2, 13' + return timestamp_subst + +#---------------------------------------------------------------------------------------- +def get_query_str(agg_type, total_by, days_prev, speed_over): + ''' Create Sqlite3 Query to Get Totals for specified days previous and speeds over + ''' + timestamp_subst = get_timestamp_substr(total_by) + + aggregation = { + 'count': 'COUNT(*) AS count_totals', + 'ave_speed': 'ROUND(CAST(AVG(ave_speed) AS NUMERIC), 1) AS speed_ave' + } + + qstring = { + 'postgres': ''' + SELECT + SUBSTRING(log_timestamp, %s) AS log_date, + %s + FROM + %s + WHERE + ave_speed >= %s AND + TO_DATE(SUBSTRING(log_timestamp FROM 2 FOR 11), 'YYYY-MM-DD') >= CURRENT_DATE - INTERVAL '%i days' AND + TO_DATE(SUBSTRING(log_timestamp FROM 2 FOR 11), 'YYYY-MM-DD') <= CURRENT_DATE + INTERVAL '1 day' + GROUP BY + log_date; + ''', + + 'sqlite3': ''' + select + substr(log_timestamp, %s) log_date, + %s + from %s + where + ave_speed >= %s and + substr(log_timestamp, 2, 11) >= DATE('now', '-%i days') and + substr(log_timestamp, 2, 11) <= DATE('now', '+1 day') + group by + log_date + ''' + } + + sql_query_by_count = (qstring[DB_TYPE] % (timestamp_subst, aggregation[agg_type], DB_TABLE, speed_over, int(days_prev))) + + return sql_query_by_count diff --git a/speed-cam.py b/speed-cam.py index 040dfbf..2562614 100644 --- a/speed-cam.py +++ b/speed-cam.py @@ -54,7 +54,10 @@ import shutil import logging import sqlite3 +import psycopg2 import numpy as np +from db_conn import db_check, db_open +from db_conn import DB_DIR_PATH, DB_CONN # import the main strmcam launch module try: @@ -162,6 +165,7 @@ "CV_WINDOW_BIGGER": 1.0, "BLUR_SIZE": 10, "THRESHOLD_SENSITIVITY": 20, + "DB_TYPE": "sqlite3", # sqlite3 or postgres "DB_DIR": "data", "DB_NAME": "speed_cam.db", "DB_TABLE": "speed", @@ -272,12 +276,6 @@ datefmt="%Y-%m-%d %H:%M:%S", ) -# Do a quick check to see if the sqlite database directory path exists -DB_DIR_PATH = os.path.join(baseDir, DB_DIR) -if not os.path.exists(DB_DIR_PATH): # Check if database directory exists - os.makedirs(DB_DIR_PATH) # make directory if Not Found -DB_PATH = os.path.join(DB_DIR_PATH, DB_NAME) # Create path to db file - try: # Check to see if opencv is installed import cv2 except ImportError: @@ -525,7 +523,12 @@ def show_settings(): " LOG_TO_FILE_ON=%s LOG_FILE_PATH=%s" % (LOG_TO_FILE_ON, LOG_FILE_PATH) ) - print(" SQLITE3 DB_PATH=%s DB_TABLE=%s" % (DB_PATH, DB_TABLE)) + + if DB_TYPE == 'sqlite3': + print(" SQLITE3 DB_PATH=%s DB_TABLE=%s" % (DB_DIR_PATH, DB_TABLE)) + elif DB_TYPE == 'postgres': + print(" POSTGRES DB_HOST=%s DB_NAME=%s DB_TABLE=%s" % (DB_HOST, DB_NAME, DB_TABLE)) + print( "Speed Trigger ... Log only if MO_MAX_SPEED_OVER > %i %s" % (MO_MAX_SPEED_OVER, speed_units) @@ -1041,97 +1044,6 @@ def log_to_csv(csv_file_path, data_to_append): return -# ------------------------------------------------------------------------------ -def is_SQLite3(filename): - """ - Determine if filename is in sqlite3 format - """ - if os.path.isfile(filename): - if os.path.getsize(filename) < 100: # SQLite database file header is 100 bytes - size = os.path.getsize(filename) - logging.error("%s %d is Less than 100 bytes", filename, size) - return False - with open(filename, "rb") as fd: - header = fd.read(100) - if header.startswith(b"SQLite format 3"): - logging.info("Success: File is sqlite3 Format %s", filename) - return True - else: - logging.error("Failed: File NOT sqlite3 Header Format %s", filename) - return False - else: - logging.warning("File Not Found %s", filename) - logging.info("Create sqlite3 database File %s", filename) - try: - conn = sqlite3.connect(filename) - except sqlite3.Error as e: - logging.error("Failed: Create Database %s.", filename) - logging.error("Error Msg: %s", e) - return False - conn.commit() - conn.close() - logging.info("Success: Created sqlite3 Database %s", filename) - return True - - -# ------------------------------------------------------------------------------ -def db_check(db_file): - """ - Check if db_file is a sqlite3 file and connect if possible - """ - if is_SQLite3(db_file): - try: - conn = sqlite3.connect(db_file, timeout=1) - except sqlite3.Error as e: - logging.error("Failed: sqlite3 Connect to DB %s", db_file) - logging.error("Error Msg: %s", e) - return None - else: - logging.error("Failed: sqlite3 Not DB Format %s", db_file) - return None - conn.commit() - logging.info("Success: sqlite3 Connected to DB %s", db_file) - return conn - - -# ------------------------------------------------------------------------------ -def db_open(db_file): - """ - Insert speed data into database table - """ - try: - db_conn = sqlite3.connect(db_file) - cursor = db_conn.cursor() - except sqlite3.Error as e: - logging.error("Failed: sqlite3 Connect to DB %s", db_file) - logging.error("Error Msg: %s", e) - return None - sql_cmd = """create table if not exists {} (idx text primary key, - log_timestamp text, - camera text, - ave_speed real, speed_units text, image_path text, - image_w integer, image_h integer, image_bigger integer, - direction text, plugin_name text, - cx integer, cy integer, - mw integer, mh integer, m_area integer, - x_left integer, x_right integer, - y_upper integer, y_lower integer, - max_speed_over integer, - min_area integer, track_counter integer, - cal_obj_px integer, cal_obj_mm integer, status text, cam_location text)""".format( - DB_TABLE - ) - try: - db_conn.execute(sql_cmd) - except sqlite3.Error as e: - logging.error("Failed: To Create Table %s on sqlite3 DB %s", DB_TABLE, db_file) - logging.error("Error Msg: %s", e) - return None - else: - db_conn.commit() - return db_conn - - # ------------------------------------------------------------------------------ def get_motion_contours(grayimage1): """ @@ -1280,22 +1192,29 @@ def speed_camera(): speed_path = IM_DIR_PATH # check and open sqlite3 db - db_conn = db_check(DB_PATH) + db_conn = db_check(DB_CONN) if db_conn is not None: - db_conn = db_open(DB_PATH) + db_conn = db_open(DB_CONN) if db_conn is None: - logging.error("Failed: Connect to sqlite3 DB %s", DB_PATH) + logging.error("Failed: Connect to DB %s", DB_TYPE) else: - logging.info("sqlite3 DB is Open %s", DB_PATH) + logging.info("%s DB is Open", DB_TYPE) db_cur = db_conn.cursor() # Set cursor position # insert status column into speed table. Can be used for # alpr (automatic license plate reader) processing to indicate # images to be processed eg null field entry. try: - db_conn.execute("alter table speed add status text") - db_conn.execute("alter table speed add cam_location text") - except sqlite3.OperationalError: + if DB_TYPE == 'sqlite3': + db_conn.execute("alter table speed add status text") + db_conn.execute("alter table speed add cam_location text") + + if DB_TYPE == 'postgres': + db_cur.execute("alter table speed add status text") + db_cur.execute("alter table speed add cam_location text") + + except (sqlite3.OperationalError, psycopg2.errors.DuplicateColumn): pass + db_conn.close() speed_notify() @@ -1709,20 +1628,28 @@ def speed_camera(): sql_cmd = """insert into {} values {}""".format( DB_TABLE, speed_data ) - db_conn = db_check(DB_PATH) - db_conn.execute(sql_cmd) + db_conn = db_check(DB_CONN) + db_cur = db_conn.cursor() + + if DB_TYPE == 'sqlite3': + db_conn.execute(sql_cmd) + if DB_TYPE == 'postgres': + db_cur.execute(sql_cmd) + db_conn.commit() db_conn.close() - except sqlite3.Error as e: - logging.error("sqlite3 DB %s", DB_PATH) + + except (sqlite3.Error, psycopg2.Error) as e: + logging.error("%s DB", DB_TYPE) logging.error( "Failed: To INSERT Speed Data into TABLE %s", DB_TABLE, ) logging.error("Err Msg: %s", e) + else: logging.info( - " SQL - Inserted Data Row into %s", DB_PATH + " SQL - Inserted Data Row into %s", DB_TYPE ) # Format and Save Data to CSV Log File diff --git a/speed-install.sh b/speed-install.sh index df08c6c..df63f50 100644 --- a/speed-install.sh +++ b/speed-install.sh @@ -170,6 +170,8 @@ sudo apt-get install -yq sqlite3 sudo apt-get install -yq python-matplotlib sudo apt-get install -yq python3-matplotlib sudo apt-get install -yq python3-numpy +sudo apt-get install -yq python3-psycopg2 +sudo apt-get install -yq python3-dotenv # sudo apt-get install -yq gnuplot # if [ $? -ne 0 ]; then diff --git a/sql-make-graph-count-totals.py b/sql-make-graph-count-totals.py index a4eba18..da65bb8 100644 --- a/sql-make-graph-count-totals.py +++ b/sql-make-graph-count-totals.py @@ -39,12 +39,15 @@ # Import Variable constants from config.py from config import DB_DIR from config import DB_NAME -from config import DB_TABLE from config import MO_SPEED_MPH_ON from config import GRAPH_PATH from config import GRAPH_ADD_DATE_TO_FILENAME # Prefix graph image filename with datetime for uniqueness. from config import GRAPH_RUN_TIMER_HOURS from config import GRAPH_RUN_LIST +from config import DB_TYPE +from db_conn import DB_CONN +from db_conn import db_connect, get_query_str + if not os.path.exists(GRAPH_PATH): # Check if grpahs directory exists os.makedirs(GRAPH_PATH) # make directory if Not Found @@ -118,26 +121,6 @@ def is_int(var): return False return True -#---------------------------------------------------------------------------------------- -def get_timestamp_substr(total_by): - ''' - Convert hour, day or month string to required - values for changing the log_timestamp to an appropriate - substring value. - ''' - total_by = total_by.upper() - if total_by == 'HOUR': - timestamp_subst = '2, 13' - elif total_by == 'DAY': - timestamp_subst = '2, 10' - elif total_by == 'MONTH': - timestamp_subst = '2, 7' - else: - logging.info("total_by variable must be string. Valid values are hour, day, month") - logging.warning("Defaulting to hour") - timestamp_subst = '2, 13' - return timestamp_subst - #---------------------------------------------------------------------------------------- def get_speed_units_str(): ''' @@ -148,26 +131,6 @@ def get_speed_units_str(): speed_unit = 'mph' return speed_unit -#---------------------------------------------------------------------------------------- -def get_query_str(total_by, days_prev, speed_over): - ''' Create Sqlite3 Query to Get Totals for specified days previous and speeds over - ''' - timestamp_subst = get_timestamp_substr(total_by) - sql_query_by_count = (''' - select - substr(log_timestamp, %s) log_date, - count(*) count_totals - from %s - where - ave_speed >= %s and - substr(log_timestamp, 2, 11) >= DATE('now', '-%i days') and - substr(log_timestamp, 2, 11) <= DATE('now', '+1 day') - group by - log_date - ''' % (timestamp_subst, DB_TABLE, speed_over, int(days_prev))) - - return sql_query_by_count - #---------------------------------------------------------------------------------------- def make_graph_image(total_by, days_prev, speed_over): ''' Extract Data from sql db and generate matplotlib graph showing totals for specified @@ -183,7 +146,7 @@ def make_graph_image(total_by, days_prev, speed_over): speed_units = get_speed_units_str() total_by = total_by.upper() db_path = os.path.join(DB_DIR, DB_NAME) - count_sql_query = get_query_str(total_by, days_prev, speed_over) + count_sql_query = get_query_str('count', total_by, days_prev, speed_over) right_now = dt.datetime.now() now = ("%02d-%02d-%02d-%02d:%02d" % (right_now.year, right_now.month, @@ -207,9 +170,14 @@ def make_graph_image(total_by, days_prev, speed_over): if DEBUG: logging.info("Running: %s", graph_title) logging.info("Connect to Database %s", db_path) - connection = sqlite3.connect(db_path) - connection.row_factory = sqlite3.Row + # connection = sqlite3.connect(db_path) + connection = db_connect(DB_CONN) + + if DB_TYPE == 'sqlite3': + connection.row_factory = sqlite3.Row + cursor = connection.cursor() + if DEBUG: logging.info('Executing Query \n %s', count_sql_query) cursor.execute(count_sql_query) @@ -255,6 +223,7 @@ def graph_from_list(): total_graphs = len(GRAPH_RUN_LIST) logging.info("--- Start Generating %i Graph Images from config.py GRAPH_RUN_LIST Variable", total_graphs) start_time = time.time() # Start timer for processing duration + for graph_data in GRAPH_RUN_LIST: run_cntr +=1 total_by = graph_data[0] diff --git a/sql-make-graph-speed-ave.py b/sql-make-graph-speed-ave.py index b4c5f51..cfa2bb8 100644 --- a/sql-make-graph-speed-ave.py +++ b/sql-make-graph-speed-ave.py @@ -9,11 +9,12 @@ prog_ver = '13_02' DEBUG = False print('Loading ver %s DEBUG= %s ... ' % (prog_ver, DEBUG)) -import sqlite3 import os import time +import sqlite3 import datetime as dt import sys + try: import matplotlib except ImportError: @@ -39,12 +40,14 @@ # Import Variable constants from config.py from config import DB_DIR from config import DB_NAME -from config import DB_TABLE from config import MO_SPEED_MPH_ON from config import GRAPH_PATH from config import GRAPH_ADD_DATE_TO_FILENAME # Prefix graph image filename with datetime for uniqueness. from config import GRAPH_RUN_TIMER_HOURS from config import GRAPH_RUN_LIST +from config import DB_TYPE +from db_conn import DB_CONN +from db_conn import db_connect, get_query_str if not os.path.exists(GRAPH_PATH): # Check if grpahs directory exists os.makedirs(GRAPH_PATH) # make directory if Not Found @@ -108,6 +111,7 @@ days_prev = args.days total_by = args.totals + #---------------------------------------------------------------------------------------- def is_int(var): ''' Check if variable string can successfully be converted to an integer. @@ -118,26 +122,6 @@ def is_int(var): return False return True -#---------------------------------------------------------------------------------------- -def get_timestamp_substr(total_by): - ''' - Convert hour, day or month string to required - values for changing the log_timestamp to - an appropriate substring value - ''' - total_by = total_by.upper() - if total_by == 'HOUR': - timestamp_subst = '2, 13' - elif total_by == 'DAY': - timestamp_subst = '2, 10' - elif total_by == 'MONTH': - timestamp_subst = '2, 7' - else: - logging.info("total_by variable must be string. Valid values are hour, day, month") - logging.warning("Defaulting to hour") - timestamp_subst = '2, 13' - return timestamp_subst - #---------------------------------------------------------------------------------------- def get_speed_units_str(): ''' @@ -148,27 +132,6 @@ def get_speed_units_str(): speed_unit = 'mph' return speed_unit -#---------------------------------------------------------------------------------------- -def get_query_str(total_by, days_prev, speed_over): - ''' Create Sqlite3 Query to Get Speed Averages for - specified days previous and speeds over - ''' - timestamp_subst = get_timestamp_substr(total_by) - sql_query_speed_ave = (''' - select - substr(log_timestamp, %s) log_date, - round(avg(ave_speed), 1) speed_ave - from %s - where - ave_speed >= %s and - substr(log_timestamp, 2, 11) >= DATE('now', '-%i days') and - substr(log_timestamp, 2, 11) <= DATE('now', '+1 day') - group by - log_date - ''' % (timestamp_subst, DB_TABLE, speed_over, int(days_prev))) - - return sql_query_speed_ave - #---------------------------------------------------------------------------------------- def make_graph_image(total_by, days_prev, speed_over): ''' Extract Data from sql db and generate matplotlib graph showing totals for specified @@ -184,7 +147,7 @@ def make_graph_image(total_by, days_prev, speed_over): speed_units = get_speed_units_str() total_by = total_by.upper() db_path = os.path.join(DB_DIR, DB_NAME) - count_sql_query = get_query_str(total_by, days_prev, speed_over) + count_sql_query = get_query_str('ave_speed', total_by, days_prev, speed_over) right_now = dt.datetime.now() now = ("%02d-%02d-%02d-%02d:%02d" % (right_now.year, right_now.month, @@ -208,9 +171,14 @@ def make_graph_image(total_by, days_prev, speed_over): if DEBUG: logging.info("Running: %s", graph_title) logging.info("Connect to Database %s", db_path) - connection = sqlite3.connect(db_path) - connection.row_factory = sqlite3.Row + + connection = db_connect(DB_CONN) + # connection = sqlite3.connect(db_path) + if DB_TYPE == 'sqlite3': + connection.row_factory = sqlite3.Row + cursor = connection.cursor() + if DEBUG: logging.info('Executing Query \n %s', count_sql_query) cursor.execute(count_sql_query)