integrates with postgres

This commit is contained in:
Mikayla Dobson
2023-01-09 22:11:41 -06:00
parent 8adbf35928
commit 5be4abe79c
4 changed files with 57 additions and 31 deletions

View File

@@ -26,14 +26,14 @@ class Config:
self.output_formatting() self.output_formatting()
with open("./config.json", "w") as outfile: with open("./config.json", "w") as outfile:
json.dump(self.__dict__, outfile) json.dump(self.__dict__, outfile)
return self return self.__dict__
else: else:
self.receive_data_path() self.receive_data_path()
self.set_pg_config() self.set_pg_config()
self.output_formatting() self.output_formatting()
with open("./config.json", "w") as outfile: with open("./config.json", "w") as outfile:
json.dump(self.__dict__, outfile) json.dump(self.__dict__, outfile)
return self return self.__dict__
# if a config file already exists, offer to use it instead # if a config file already exists, offer to use it instead
def handle_prev_config(self): def handle_prev_config(self):
@@ -80,15 +80,16 @@ class Config:
try: try:
dsn = f"dbname={dbname} user={user} password={password}" dsn = f"dbname={dbname} user={user} password={password}"
if host: if host:
dsn = dsn + host dsn = dsn + " host=" + host
if port: if port:
dsn = dsn + port dsn = dsn + + " port=" + port
conn = psycopg2.connect(f"dbname={dbname} user={user} password={password}") conn = psycopg2.connect(f"dbname={dbname} user={user} password={password}")
conn.close() conn.close()
print("Connection successful!") print("Connection successful!")
self.pg_dsn = dsn self.pg_dsn = dsn
except psycopg2.Error as e: except psycopg2.Error as e:
print("There was an error connecting: ") print("There was an error connecting: ")

View File

@@ -1,4 +1,5 @@
import json, os, shutil import json, os, shutil
import psycopg2
from pgutil import PGUTIL from pgutil import PGUTIL
from config import Config from config import Config
@@ -9,6 +10,7 @@ def format_result(app_config: Config, json_path):
data_path = app_config['data_path'] data_path = app_config['data_path']
# if pg_config is not None, run the postgres prediction[0] of this code # if pg_config is not None, run the postgres prediction[0] of this code
if app_config['pg_dsn']:
pgutil = PGUTIL(app_config, json_path) pgutil = PGUTIL(app_config, json_path)
# if this is True, run the prediction[0] "for line in contents:" below # if this is True, run the prediction[0] "for line in contents:" below
@@ -39,12 +41,21 @@ def format_result(app_config: Config, json_path):
match_strength = 'weak' match_strength = 'weak'
# container for data to ship to pg # container for data to ship to pg
pgutil.insert_data({ if pgutil:
conn = psycopg2.connect(app_config['pg_dsn'])
cur = conn.cursor()
pgutil.insert_data(cur, {
"filename": img_path, "filename": img_path,
"topprediction": guess_label, "topprediction": guess_label,
"matchaccuracy": match_accuracy "matchaccuracy": match_accuracy
}) })
conn.commit()
cur.close()
conn.close()
print("Database insertions successful!")
# assign value for match strength # assign value for match strength
if float(match_accuracy) > 0.9: if float(match_accuracy) > 0.9:
match_strength = 'strong' match_strength = 'strong'

View File

@@ -62,7 +62,7 @@ for file in files:
json_path = "./predictions/predictions" + cur_time + ".json" json_path = "./predictions/predictions" + cur_time + ".json"
print("Writing analysis results to " + json_path + "\n\n") print("\nWriting analysis results to " + json_path + "\n\n")
# convert object to JSON and write to JSON file # convert object to JSON and write to JSON file
with open(json_path, "w") as outfile: with open(json_path, "w") as outfile:

View File

@@ -5,38 +5,35 @@ class PGUTIL:
def __init__(self, config_file: Config = None, json_path: str = None): def __init__(self, config_file: Config = None, json_path: str = None):
if config_file is None: if config_file is None:
load_config = Config() load_config = Config()
config_file = load_config.run() self.config_file = load_config.run()
pg_dsn = config_file['pg_dsn'] self.pg_dsn = config_file['pg_dsn']
if pg_dsn is None:
raise Exception("Insufficient data to establish PostgreSQL connection.")
if json_path: if json_path:
self.json_path = json_path self.json_path = json_path
self.config_file = config_file if config_file['pg_dsn'] is None:
raise Exception("Insufficient data to establish PostgreSQL connection.")
def create_tables(self): def create_tables(self):
pg_dsn = self.config_file['pg_dsn']
print("Connecting to PostgreSQL...") print("Connecting to PostgreSQL...")
# establish connection and create cursor # establish connection and create cursor
conn = psycopg2.connect(pg_dsn) conn = psycopg2.connect(self.__dict__['pg_dsn'])
cur = conn.cursor() cur = conn.cursor()
# create base tables # create base tables
create_label_table = """ create_label_table = """
CREATE TABLE IF NOT EXISTS label ( CREATE TABLE IF NOT EXISTS label (
id INT PRIMARY KEY, id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name varchar name varchar UNIQUE
); );
""" """
create_photo_table = """ create_photo_table = """
CREATE TABLE IF NOT EXISTS photo ( CREATE TABLE IF NOT EXISTS photo (
id INT PRIMARY KEY, id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
path varchar, filename varchar,
label varchar, guesslabel varchar,
matchstrength decimal, matchstrength decimal,
labelid INT REFERENCES label(id) labelid INT REFERENCES label(id)
); );
@@ -60,12 +57,29 @@ class PGUTIL:
print("Connection closed.") print("Connection closed.")
def insert_data(self, analysis): def insert_data(self, cur, analysis):
# if self.json_path is None: self.create_tables()
# return
# establish pg connection insert_label = """
conn = psycopg2.connect(self['config_file']['dsn']) INSERT INTO label (name) VALUES (%s)
cur = conn.cursor() ON CONFLICT (name) DO NOTHING;
"""
filename = analysis['filename']
guesslabel = analysis['topprediction']
matchstrength = analysis['matchaccuracy']
insert_photo = """
INSERT INTO photo (filename, guesslabel, matchstrength, labelid) VALUES (
%s, %s, %s, (
SELECT id FROM label
WHERE name = %s
)
);
"""
try:
cur.execute(insert_label, [guesslabel])
cur.execute(insert_photo, [filename, guesslabel, matchstrength, guesslabel])
except psycopg2.Error:
raise Exception("Problem inserting data for photo " + analysis['filename'])