Skip to content
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e155871
Review- Removing unused imports
Ihebdhouibi Sep 15, 2024
8e43594
Review- Adding windows timeout mechanism
Ihebdhouibi Sep 15, 2024
04bb477
Review- removing obselete code
Ihebdhouibi Sep 15, 2024
694330d
Review- Encapsulate global variable in a config class
Ihebdhouibi Sep 15, 2024
48a7ecf
Review- fixing typo in ScraperConfig class
Ihebdhouibi Sep 15, 2024
9d03605
Review- fixing typo in timeout cnstr
Ihebdhouibi Sep 15, 2024
31fdb56
Review- Fixing search_download call
Ihebdhouibi Sep 15, 2024
34e6cad
Review- missing parentheses
Ihebdhouibi Sep 15, 2024
173afd2
Review- fixing typo
Ihebdhouibi Sep 15, 2024
2c863d7
Review- search_download call fix
Ihebdhouibi Sep 15, 2024
d948deb
Review- Establishing DB connection using context manager
Ihebdhouibi Sep 15, 2024
704250c
Review- Removing commented lines
Ihebdhouibi Sep 15, 2024
b58e70d
Review- Apply code reusebility to store alerts functions
Ihebdhouibi Sep 15, 2024
f7e3782
Review- Apply code reusebility to retrieving alerts functions
Ihebdhouibi Sep 15, 2024
042b951
Review- Add exception handling
Ihebdhouibi Sep 15, 2024
6289aa9
Review- Add remove_camera function
Ihebdhouibi Sep 15, 2024
5a17c59
Review- Fixing typo
Ihebdhouibi Sep 15, 2024
d628218
Review- Add exception handling
Ihebdhouibi Sep 15, 2024
caebad0
Review- Fixing typo and naming in functions
Ihebdhouibi Sep 15, 2024
8517949
Review- Updating create_azure_container function
Ihebdhouibi Sep 15, 2024
4075fc5
Review- Remove commented lines
Ihebdhouibi Sep 15, 2024
f7893a4
Review- Updating upload_blob function
Ihebdhouibi Sep 15, 2024
244dcb2
Review- removing debbuging and unecessary lines
Ihebdhouibi Sep 15, 2024
aae9185
Review- Removing unecessary prints
Ihebdhouibi Sep 16, 2024
f1e5c12
Review- Removing commented code
Ihebdhouibi Sep 16, 2024
39616c3
Review- Documenting functions
Ihebdhouibi Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 61 additions & 141 deletions DataBase/dataBase.py
Original file line number Diff line number Diff line change
@@ -1,167 +1,87 @@
import psycopg2
from configparser import ConfigParser
from contextlib import contextmanager

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context manager in python provides a better way to allocate and release resources


def add_user(username, password):
cursor, conn = connect()

query = "insert into users (username, password) values (%s, %s)"

cursor.execute(query, (username, password))
conn.commit()

conn.close()

def connect():
@contextmanager
def get_db_connection():

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

establishing DB connection manually using connect function is error-prone, instead opt for a context manager to ensure safely opening and closing connection

try:
conn = psycopg2.connect(host="127.0.0.1",
user="postgres",
database="visionalarm",
password="root",
port="5432")
cursor = conn.cursor()
# print connexion settings
print("Connexion settings : ", conn.get_dsn_parameters())
except (Exception, psycopg2.Error) as error:
print("Error while trying to connect to PostgreSQL ", error)

return cursor, conn
yield cursor, conn
except Exception as error:
print(f"Error while connecting to PostgreSQL: {error}")
finally:
conn.close()

def add_user(username, password):
with get_db_connection() as (cursor, conn):
query = "insert into users (username, password) values (%s, %s)"
cursor.execute(query, (username, password))
conn.commit()

def add_camera(address, nom):

cursor, conn = connect()

query = "select * from cameras"
cursor.execute(query)

result = cursor.fetchall()
number_cam = 0
for rows in result:
number_cam += 1
with get_db_connection() as (cursor, conn):
query = "select * from cameras"
cursor.execute(query)

print("Number cameras = ", number_cam)
result = cursor.fetchall()
number_cam = 0
for rows in result:
number_cam += 1

if number_cam < 4:
query = "insert into cameras (address, nom) values (%s, %s)"
cursor.execute(query, (address, nom))
conn.commit()
else:
print("Maximum number of cameras added already")
print("Number cameras = ", number_cam)

conn.close()
if number_cam < 4:
query = "insert into cameras (address, nom) values (%s, %s)"
cursor.execute(query, (address, nom))
conn.commit()
else:
print("Maximum number of cameras added already")

def remove_camera(id):
pass


def storeFireAlertData(alertTime, videoLink, AlertClass):

cursor, conn = connect()
# Connection achieved
# Storing alert data
query = "insert into fire_alerts (alert_time, video_link, class) values ( %s, %s, %s)"

cursor.execute(query, (alertTime, videoLink, AlertClass))
conn.commit()

# Close connection
conn.close()


def storeFallAlertData(alertTime, videoLink, AlertClass):
# Try connection
cursor, conn = connect()
# Connection achieved
# Storing alert data
try:
query = "insert into fall_alerts (alert_time, video_link, class) values (%s, %s, %s)"

cursor.execute(query, (alertTime, videoLink, AlertClass))
conn.commit()
except Exception as e:
print("There is an issue inserting alert information into fall_alerts")
# close connection
conn.close()


def storeRobberyAlertData(alertTime, videoLink, AlertClass):

cursor, conn = connect()
# Connection achieved
# Storing alert data
query = "insert into robbery_alerts (alert_time, video_link, class) values ( %s, %s, %s)"

cursor.execute(query, (alertTime, videoLink, AlertClass))
conn.commit()

# Close connection
conn.close()


def retrieve_fire_alerts():

cursor, conn = connect()

query = "select * from fire_alerts"
cursor.execute(query)
print("Fire alerts : \n ------------------------------------ \n")
fire_alerts = cursor.fetchall()
for row in fire_alerts:
print(f"ID : {row[0]} | alert time : {row[1]} | video link : {row[2]} | class : {row[3]}")

conn.close()


def retrieve_fall_alerts():

cursor, conn = connect()
query = "select * from fall_alerts"
cursor.execute(query)
print("Fall alerts : \n ------------------------------------ \n")
fall_alerts = cursor.fetchall()
for row in fall_alerts:
print(f"ID : {row[0]} | alert time : {row[1]} | video link : {row[2]} | class : {row[3]}")

conn.close()


def retrieve_robbery_alerts():

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leveraging code_reusebility is important here as a centralized function with an additional parameter to decide which table to handle is a better approach


cursor, conn = connect()

query = "select * from robbery_alerts"
cursor.execute(query)
print("Robbery alerts : \n ------------------------------------ \n")
robbery_alerts = cursor.fetchall()
for row in robbery_alerts:
print(f"ID : {row[0]} | alert time : {row[1]} | video link : {row[2]} | class : {row[3]}")

conn.close()

def store_alert_data(alert_time, video_link, alert_class, alert_type):

query = f"INSERT INTO {alert_type}_alerts (alert_time, video_link, class) VALUES (%s, %s, %s)"

with get_db_connection() as (cursor, conn):
try:
cursor.execute(query, (alert_time, video_link, alert_class))
conn.commit()
except Exception as e:
print(f"Error inserting alert into {alert_type}_alerts: {e}")

Comment on lines 52 to 56

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always ensure handling exception in case of an error in the given SQL query

def retrieve_alerts(alert_type):

query=f"SELECT * FROM {alert_type}_alerts"

with get_db_connection() as (cursor, conn):
cursor.execute(query)
alerts = cursor.fetchall()
print(f"{alert_type.capitalize()} alerts: \n ----------------------------------------------- \n")

for row in alerts:
print(f"ID: {row[0]} | Alert Time: {row[1]} | Video Link; {row[2]} | Class: {row[3]}")

def retrieve_all_alerts():

cursor, conn = connect()

retrieve_fire_alerts()
retrieve_fall_alerts()
retrieve_robbery_alerts()
conn.close()

# storeFireAlertData("{20:20:20}", "{link}", True)
# storeMouvementAlertData("{20:20:20}", "{link}", True)

retrieve_alerts("fire")
retrieve_alerts("fall")
retrieve_alerts("robbery")

def retrieve_users():
cursor, conn = connect()

query = "select * from users"
cursor.execute(query)

result = cursor.fetchall()
for row in result:
print(f"id {row[0]} | username : {row[1]} | password : {row[2]}")

conn.close()

# add_user("user", "user")
# Establishing Connection to DB
with get_db_connection() as (cursor, conn):
query = "select * from users"
cursor.execute(query)

# retrieve_users()
result = cursor.fetchall()
for row in result:
print(f"id {row[0]} | username : {row[1]} | password : {row[2]}")
Loading