1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166import psycopg2
import json
import traceback
from data.access import connection
from utils.watch import logger
from psycopg2.pool import SimpleConnectionPool
from datetime import datetime, timezone
# Set use_pooling to True to enable connection pooling
use_pooling = True
# Connection pool
pool = None
if use_pooling:
conn_params = connection().get_connection_params()
pool = SimpleConnectionPool(
minconn=1,
maxconn=10,
**conn_params
)
def connection_pooling():
return pool.getconn()
def release_pooling(conn):
pool.putconn(conn)
# Singular Updates
def execute_update(query, params=None, fetchone=True):
# logger.debug(f'๐๏ธ ๐ง Executing query: {query}')
# logger.debug(f'๐๏ธ ๐ง Query parameters: {params}... ')
# Connect to the database
conn = connection()
conn.open()
logger.debug(f'๐๏ธ ๐ง Database connection opened')
# Create a cursor
cur = conn.conn.cursor()
try:
# Execute the query
cur.execute(query, params)
conn.conn.commit()
logger.debug(f'๐๏ธ ๐ง Query executed and committed')
# Fetch the results if requested
result = None
if fetchone:
result = cur.fetchone() or () # return an empty tuple if None is returned
else:
result = cur.fetchall() or [] # return an empty list if None is returned
logger.debug(f'๐๏ธ ๐ง Fetched results: {result}')
except Exception as e:
# logger.error(f'๐๏ธ ๐ง Error executing update query: {e}')
result = None
# Close the cursor and connection
cur.close()
conn.close()
logger.debug(f'๐๏ธ ๐ง Cursor and connection closed')
return result
# # # # # # # # # #
# Bulk Updates
def execute_bulk_update(query, params_list):
# Connect to the database
if use_pooling:
conn = connection_pooling()
else:
conn = connection()
conn.open()
# Create a cursor
cur = conn.cursor()
try:
# Execute the query
with conn:
cur.executemany(query, params_list)
logger.debug("๐๏ธโ๏ธ๐ข Query executed and committed")
except Exception as e:
logger.error(f"๐๏ธโ๏ธ Error executing bulk insert query: {e}\n{traceback.format_exc()}")
# Close the cursor and connection
cur.close()
if use_pooling:
release_pooling(conn)
else:
conn.close()
#########################################################
## Queries
def tech_check_failure(url_id):
logger.info(f'๐๏ธ ๐ง Logging Tech Check Failure')
query = """
UPDATE targets.urls
SET active_scan_tech = False,
scanned_at_tech = %s
WHERE id = %s
"""
try:
execute_update(query, (datetime.now(timezone.utc), url_id))
logger.debug(f'๐๏ธ ๐ง Marked Failure: {url_id}')
return True
except Exception as e: # Add 'Exception as e' to capture the exception details
logger.error(f'๐๏ธ ๐ง Failed to Mark Failure: {url_id} - Error: {e}') # Display the error message
return False
def mark_url_axe_scanned(url_id):
query = """
UPDATE targets.urls
SET scanned_at_axe = %s
WHERE id = %s;
"""
try:
execute_update(query, (datetime.now(timezone.utc), url_id))
logger.debug(f'๐๏ธ ๐ง Marked {url_id} as Scanned')
return True
except Exception as e: # Add 'Exception as e' to capture the exception details
logger.error(f'๐๏ธ ๐ง Failed to mark as Scanned: {url_id} - Error: {e}') # Display the error message
return False
def tech_mark_url(url_id):
query = """
UPDATE targets.urls
SET scanned_at_tech = %s
WHERE id = %s;
"""
try:
execute_update(query, (datetime.now(timezone.utc), url_id))
logger.debug(f'๐๏ธ ๐ง Marked {url_id} as Teched')
return True
except Exception as e: # Add 'Exception as e' to capture the exception details
logger.error(f'๐๏ธ ๐ง Failed to mark as Teched: {url_id} - Error: {e}') # Display the error message
return False
def update_status_codes(url_status_list):
query = """
UPDATE targets.urls
SET uppies_code = %s, uppies_at = %s
WHERE id = %s;
"""
now = datetime.now(timezone.utc)
params_list = [(status_code, now, url_id) for url_id, status_code in url_status_list]
try:
execute_bulk_update(query, params_list)
for url_id, status_code in url_status_list:
logger.debug(f'๐๏ธ๐ง Updated status code for URL ID {url_id} to {status_code}')
except Exception as e:
logger.error(f'๐๏ธ๐ง Failed to update status codes in bulk - Error: {e}')